blob: 4a900fab479d63fe05f9eaf0ff6c2eccb31c4a03 [file] [log] [blame]
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2011-2013 Alexandru Gagniuc <mr.nuke.me@gmail.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050015 */
16
17/**
Martin Roth98b698c2015-01-06 21:02:52 -070018 * @file ddr3.c
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050019 *
20 * \brief Utilities for decoding DDR3 SPDs
21 */
22
23#include <console/console.h>
24#include <device/device.h>
25#include <device/dram/ddr3.h>
Andrey Petrov3f85edb2019-08-01 14:18:06 -070026#include <device/dram/common.h>
Patrick Rudolph07691592016-02-29 18:21:00 +010027#include <string.h>
Patrick Rudolph24efe732018-08-19 11:06:06 +020028#include <memory_info.h>
29#include <cbmem.h>
30#include <smbios.h>
Elyes HAOUASbd1683d2019-05-15 21:05:37 +020031#include <types.h>
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050032
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -050033/*==============================================================================
34 * = DDR3 SPD decoding helpers
35 *----------------------------------------------------------------------------*/
36
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050037/**
38 * \brief Checks if the DIMM is Registered based on byte[3] of the SPD
39 *
40 * Tells if the DIMM type is registered or not.
41 *
42 * @param type DIMM type. This is byte[3] of the SPD.
43 */
Patrick Rudolph6e53ae62017-01-31 19:43:17 +010044int spd_dimm_is_registered_ddr3(enum spd_dimm_type type)
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050045{
46 if ((type == SPD_DIMM_TYPE_RDIMM)
47 | (type == SPD_DIMM_TYPE_MINI_RDIMM)
48 | (type == SPD_DIMM_TYPE_72B_SO_RDIMM))
49 return 1;
50
51 return 0;
52}
53
54/**
Alexandru Gagniuc4c37e582013-12-17 13:08:01 -050055 * \brief Calculate the CRC of a DDR3 SPD
56 *
57 * @param spd pointer to raw SPD data
58 * @param len length of data in SPD
59 *
60 * @return the CRC of the SPD data, or 0 when spd data is truncated.
61 */
62u16 spd_ddr3_calc_crc(u8 *spd, int len)
63{
Kyösti Mälkki7dc4b842016-11-18 18:41:17 +020064 int n_crc;
Alexandru Gagniuc4c37e582013-12-17 13:08:01 -050065
66 /* Find the number of bytes covered by CRC */
67 if (spd[0] & 0x80) {
68 n_crc = 117;
69 } else {
70 n_crc = 126;
71 }
72
73 if (len < n_crc)
74 /* Not enough bytes available to get the CRC */
75 return 0;
76
Andrey Petrov3f85edb2019-08-01 14:18:06 -070077 return ddr_crc16(spd, n_crc);
Kyösti Mälkki7dc4b842016-11-18 18:41:17 +020078}
79
80/**
81 * \brief Calculate the CRC of a DDR3 SPD unique identifier
82 *
83 * @param spd pointer to raw SPD data
84 * @param len length of data in SPD
85 *
86 * @return the CRC of SPD data bytes 117..127, or 0 when spd data is truncated.
87 */
88u16 spd_ddr3_calc_unique_crc(u8 *spd, int len)
89{
90 if (len < (117 + 11))
91 /* Not enough bytes available to get the CRC */
92 return 0;
93
Andrey Petrov3f85edb2019-08-01 14:18:06 -070094 return ddr_crc16(&spd[117], 11);
Alexandru Gagniuc4c37e582013-12-17 13:08:01 -050095}
96
97/**
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050098 * \brief Decode the raw SPD data
99 *
100 * Decodes a raw SPD data from a DDR3 DIMM, and organizes it into a
101 * @ref dimm_attr structure. The SPD data must first be read in a contiguous
102 * array, and passed to this function.
103 *
Martin Roth63373ed2013-07-08 16:24:19 -0600104 * @param dimm pointer to @ref dimm_attr structure where the decoded data is to
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200105 * be stored
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500106 * @param spd array of raw data previously read from the SPD.
107 *
108 * @return @ref spd_status enumerator
109 * SPD_STATUS_OK -- decoding was successful
110 * SPD_STATUS_INVALID -- invalid SPD or not a DDR3 SPD
111 * SPD_STATUS_CRC_ERROR -- CRC did not verify
112 * SPD_STATUS_INVALID_FIELD -- A field with an invalid value was
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200113 * detected.
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500114 */
115int spd_decode_ddr3(dimm_attr * dimm, spd_raw_data spd)
116{
Alexandru Gagniuc4c37e582013-12-17 13:08:01 -0500117 int ret;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500118 u16 crc, spd_crc;
Nicola Corna76f8dbc2016-11-16 08:57:15 +0100119 u8 capacity_shift, bus_width;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500120 u8 reg8;
121 u32 mtb; /* medium time base */
Nicola Corna76f8dbc2016-11-16 08:57:15 +0100122 u32 ftb; /* fine time base */
Elyes HAOUAS05c04552019-04-23 22:15:57 +0200123 unsigned int val;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500124
125 ret = SPD_STATUS_OK;
126
127 /* Don't assume we memset 0 dimm struct. Clear all our flags */
128 dimm->flags.raw = 0;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100129 dimm->dimms_per_channel = 3;
130
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500131 /* Make sure that the SPD dump is indeed from a DDR3 module */
132 if (spd[2] != SPD_MEMORY_TYPE_SDRAM_DDR3) {
133 printram("Not a DDR3 SPD!\n");
134 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
135 return SPD_STATUS_INVALID;
136 }
137 dimm->dram_type = SPD_MEMORY_TYPE_SDRAM_DDR3;
Vladimir Serbinenko0e675f72014-12-07 13:56:48 +0100138 dimm->dimm_type = spd[3] & 0xf;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500139
Patrick Rudolph8c639352015-06-22 19:32:53 +0200140 crc = spd_ddr3_calc_crc(spd, sizeof(spd_raw_data));
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500141 /* Compare with the CRC in the SPD */
142 spd_crc = (spd[127] << 8) + spd[126];
143 /* Verify the CRC is correct */
144 if (crc != spd_crc) {
Patrick Rudolph78c6e3e2015-06-22 19:46:34 +0200145 printram("ERROR: SPD CRC failed!!!\n");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500146 ret = SPD_STATUS_CRC_ERROR;
147 };
148
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100149 printram(" Revision : %x\n", spd[1]);
150 printram(" Type : %x\n", spd[2]);
151 printram(" Key : %x\n", spd[3]);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500152
153 reg8 = spd[4];
154 /* Number of memory banks */
155 val = (reg8 >> 4) & 0x07;
156 if (val > 0x03) {
157 printram(" Invalid number of memory banks\n");
158 ret = SPD_STATUS_INVALID_FIELD;
159 }
Elyes HAOUAS05c04552019-04-23 22:15:57 +0200160 printram(" Banks : %u\n", 1 << (val + 3));
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500161 /* SDRAM capacity */
162 capacity_shift = reg8 & 0x0f;
163 if (capacity_shift > 0x06) {
164 printram(" Invalid module capacity\n");
165 ret = SPD_STATUS_INVALID_FIELD;
166 }
167 if (capacity_shift < 0x02) {
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100168 printram(" Capacity : %u Mb\n", 256 << capacity_shift);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500169 } else {
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100170 printram(" Capacity : %u Gb\n", 1 << (capacity_shift - 2));
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500171 }
172
173 reg8 = spd[5];
174 /* Row address bits */
175 val = (reg8 >> 3) & 0x07;
176 if (val > 0x04) {
177 printram(" Invalid row address bits\n");
178 ret = SPD_STATUS_INVALID_FIELD;
179 }
180 dimm->row_bits = val + 12;
181 /* Column address bits */
182 val = reg8 & 0x07;
183 if (val > 0x03) {
184 printram(" Invalid column address bits\n");
185 ret = SPD_STATUS_INVALID_FIELD;
186 }
187 dimm->col_bits = val + 9;
188
189 /* Module nominal voltage */
190 reg8 = spd[6];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100191 printram(" Supported voltages :");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500192 if (reg8 & (1 << 2)) {
193 dimm->flags.operable_1_25V = 1;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100194 dimm->voltage = 1250;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500195 printram(" 1.25V");
196 }
197 if (reg8 & (1 << 1)) {
198 dimm->flags.operable_1_35V = 1;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100199 dimm->voltage = 1300;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500200 printram(" 1.35V");
201 }
202 if (!(reg8 & (1 << 0))) {
203 dimm->flags.operable_1_50V = 1;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100204 dimm->voltage = 1500;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500205 printram(" 1.5V");
206 }
207 printram("\n");
208
209 /* Module organization */
210 reg8 = spd[7];
211 /* Number of ranks */
212 val = (reg8 >> 3) & 0x07;
213 if (val > 3) {
214 printram(" Invalid number of ranks\n");
215 ret = SPD_STATUS_INVALID_FIELD;
216 }
217 dimm->ranks = val + 1;
218 /* SDRAM device width */
219 val = (reg8 & 0x07);
220 if (val > 3) {
221 printram(" Invalid SDRAM width\n");
222 ret = SPD_STATUS_INVALID_FIELD;
223 }
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200224 dimm->width = (4 << val);
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100225 printram(" SDRAM width : %u\n", dimm->width);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500226
227 /* Memory bus width */
228 reg8 = spd[8];
229 /* Bus extension */
230 val = (reg8 >> 3) & 0x03;
231 if (val > 1) {
232 printram(" Invalid bus extension\n");
233 ret = SPD_STATUS_INVALID_FIELD;
234 }
235 dimm->flags.is_ecc = val ? 1 : 0;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100236 printram(" Bus extension : %u bits\n", val ? 8 : 0);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500237 /* Bus width */
238 val = reg8 & 0x07;
239 if (val > 3) {
240 printram(" Invalid bus width\n");
241 ret = SPD_STATUS_INVALID_FIELD;
242 }
243 bus_width = 8 << val;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100244 printram(" Bus width : %u\n", bus_width);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500245
246 /* We have all the info we need to compute the dimm size */
247 /* Capacity is 256Mbit multiplied by the power of 2 specified in
248 * capacity_shift
249 * The rest is the JEDEC formula */
250 dimm->size_mb = ((1 << (capacity_shift + (25 - 20))) * bus_width
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200251 * dimm->ranks) / dimm->width;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500252
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500253 /* Medium Timebase =
254 * Medium Timebase (MTB) Dividend /
255 * Medium Timebase (MTB) Divisor */
256 mtb = (((u32) spd[10]) << 8) / spd[11];
257
258 /* SDRAM Minimum Cycle Time (tCKmin) */
259 dimm->tCK = spd[12] * mtb;
260 /* CAS Latencies Supported */
261 dimm->cas_supported = (spd[15] << 8) + spd[14];
262 /* Minimum CAS Latency Time (tAAmin) */
263 dimm->tAA = spd[16] * mtb;
264 /* Minimum Write Recovery Time (tWRmin) */
265 dimm->tWR = spd[17] * mtb;
266 /* Minimum RAS# to CAS# Delay Time (tRCDmin) */
267 dimm->tRCD = spd[18] * mtb;
268 /* Minimum Row Active to Row Active Delay Time (tRRDmin) */
269 dimm->tRRD = spd[19] * mtb;
270 /* Minimum Row Precharge Delay Time (tRPmin) */
271 dimm->tRP = spd[20] * mtb;
272 /* Minimum Active to Precharge Delay Time (tRASmin) */
273 dimm->tRAS = (((spd[21] & 0x0f) << 8) + spd[22]) * mtb;
274 /* Minimum Active to Active/Refresh Delay Time (tRCmin) */
275 dimm->tRC = (((spd[21] & 0xf0) << 4) + spd[23]) * mtb;
276 /* Minimum Refresh Recovery Delay Time (tRFCmin) */
277 dimm->tRFC = ((spd[25] << 8) + spd[24]) * mtb;
278 /* Minimum Internal Write to Read Command Delay Time (tWTRmin) */
279 dimm->tWTR = spd[26] * mtb;
280 /* Minimum Internal Read to Precharge Command Delay Time (tRTPmin) */
281 dimm->tRTP = spd[27] * mtb;
282 /* Minimum Four Activate Window Delay Time (tFAWmin) */
283 dimm->tFAW = (((spd[28] & 0x0f) << 8) + spd[29]) * mtb;
Dan Elkouby0c024202018-04-13 18:45:02 +0300284 /* Minimum CAS Write Latency Time (tCWLmin)
285 * - not present in standard SPD */
286 dimm->tCWL = 0;
287 /* System CMD Rate Mode - not present in standard SPD */
288 dimm->tCMD = 0;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500289
Nicola Corna76f8dbc2016-11-16 08:57:15 +0100290 printram(" FTB timings :");
291 /* FTB is introduced in SPD revision 1.1 */
292 if (spd[1] >= 0x11 && spd[9] & 0x0f) {
293 printram(" yes\n");
294
295 /* Fine timebase (1/256 ps) =
296 * Fine Timebase (FTB) Dividend /
297 * Fine Timebase (FTB) Divisor */
298 ftb = (((u16) spd[9] & 0xf0) << 4) / (spd[9] & 0x0f);
299
300 /* SPD recommends to round up the MTB part and use a negative
301 * FTB, so a negative rounding should be always safe */
302
303 /* SDRAM Minimum Cycle Time (tCKmin) correction */
304 dimm->tCK += (s32)((s8) spd[34] * ftb - 500) / 1000;
305 /* Minimum CAS Latency Time (tAAmin) correction */
306 dimm->tAA += (s32)((s8) spd[35] * ftb - 500) / 1000;
307 /* Minimum RAS# to CAS# Delay Time (tRCDmin) correction */
308 dimm->tRCD += (s32)((s8) spd[36] * ftb - 500) / 1000;
309 /* Minimum Row Precharge Delay Time (tRPmin) correction */
310 dimm->tRP += (s32)((s8) spd[37] * ftb - 500) / 1000;
311 /* Minimum Active to Active/Refresh Delay Time (tRCmin) corr. */
312 dimm->tRC += (s32)((s8) spd[38] * ftb - 500) / 1000;
313 }
314 else {
315 printram(" no\n");
316 }
317
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500318 /* SDRAM Optional Features */
319 reg8 = spd[30];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100320 printram(" Optional features :");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500321 if (reg8 & 0x80) {
322 dimm->flags.dll_off_mode = 1;
323 printram(" DLL-Off_mode");
324 }
325 if (reg8 & 0x02) {
326 dimm->flags.rzq7_supported = 1;
327 printram(" RZQ/7");
328 }
329 if (reg8 & 0x01) {
330 dimm->flags.rzq6_supported = 1;
331 printram(" RZQ/6");
332 }
333 printram("\n");
334
335 /* SDRAM Thermal and Refresh Options */
336 reg8 = spd[31];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100337 printram(" Thermal features :");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500338 if (reg8 & 0x80) {
339 dimm->flags.pasr = 1;
340 printram(" PASR");
341 }
342 if (reg8 & 0x08) {
343 dimm->flags.odts = 1;
344 printram(" ODTS");
345 }
346 if (reg8 & 0x04) {
347 dimm->flags.asr = 1;
348 printram(" ASR");
349 }
350 if (reg8 & 0x02) {
351 dimm->flags.ext_temp_range = 1;
352 printram(" ext_temp_refresh");
353 }
354 if (reg8 & 0x01) {
355 dimm->flags.ext_temp_refresh = 1;
356 printram(" ext_temp_range");
357 }
358 printram("\n");
359
360 /* Module Thermal Sensor */
361 reg8 = spd[32];
362 if (reg8 & 0x80)
363 dimm->flags.therm_sensor = 1;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100364 printram(" Thermal sensor : %s\n",
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500365 dimm->flags.therm_sensor ? "yes" : "no");
366
367 /* SDRAM Device Type */
Jacob Garber93064ff2019-06-24 13:02:27 -0600368 printram(" Standard SDRAM : %s\n", (spd[33] & 0x80) ? "no" : "yes");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500369
370 if (spd[63] & 0x01) {
371 dimm->flags.pins_mirrored = 1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500372 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100373 printram(" Rank1 Address bits : %s\n",
374 (spd[63] & 0x01) ? "mirrored" : "normal");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500375
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200376 dimm->reference_card = spd[62] & 0x1f;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100377 printram(" DIMM Reference card: %c\n", 'A' + dimm->reference_card);
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200378
Patrick Rudolph07691592016-02-29 18:21:00 +0100379 dimm->manufacturer_id = (spd[118] << 8) | spd[117];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100380 printram(" Manufacturer ID : %x\n", dimm->manufacturer_id);
Patrick Rudolph07691592016-02-29 18:21:00 +0100381
382 dimm->part_number[16] = 0;
383 memcpy(dimm->part_number, &spd[128], 16);
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100384 printram(" Part number : %s\n", dimm->part_number);
Patrick Rudolph07691592016-02-29 18:21:00 +0100385
Patrick Rudolph15e64692018-08-17 15:24:56 +0200386 memcpy(dimm->serial, &spd[SPD_DIMM_SERIAL_NUM], SPD_DIMM_SERIAL_LEN);
387
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500388 return ret;
389}
390
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100391/**
392 * \brief Decode the raw SPD XMP data
393 *
394 * Decodes a raw SPD XMP data from a DDR3 DIMM, and organizes it into a
395 * @ref dimm_attr structure. The SPD data must first be read in a contiguous
396 * array, and passed to this function.
397 *
398 * @param dimm pointer to @ref dimm_attr structure where the decoded data is to
399 * be stored
400 * @param spd array of raw data previously read from the SPD.
401 *
402 * @param profile select one of the profiles to load
403 *
404 * @return @ref spd_status enumerator
405 * SPD_STATUS_OK -- decoding was successful
406 * SPD_STATUS_INVALID -- invalid SPD or not a DDR3 SPD
407 * SPD_STATUS_CRC_ERROR -- CRC did not verify
408 * SPD_STATUS_INVALID_FIELD -- A field with an invalid value was
409 * detected.
410 */
411int spd_xmp_decode_ddr3(dimm_attr *dimm,
412 spd_raw_data spd,
413 enum ddr3_xmp_profile profile)
414{
415 int ret;
416 u32 mtb; /* medium time base */
417 u8 *xmp; /* pointer to XMP profile data */
418
419 /* need a valid SPD */
420 ret = spd_decode_ddr3(dimm, spd);
421 if (ret != SPD_STATUS_OK)
422 return ret;
423
424 /* search for magic header */
425 if (spd[176] != 0x0C || spd[177] != 0x4A) {
426 printram("Not a DDR3 XMP profile!\n");
427 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
428 return SPD_STATUS_INVALID;
429 }
430
431 if (profile == DDR3_XMP_PROFILE_1) {
432 if (!(spd[178] & 1)) {
433 printram("Selected XMP profile disabled!\n");
434 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
435 return SPD_STATUS_INVALID;
436 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100437
438 printram(" XMP Profile : 1\n");
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100439 xmp = &spd[185];
440
441 /* Medium Timebase =
442 * Medium Timebase (MTB) Dividend /
443 * Medium Timebase (MTB) Divisor */
444 mtb = (((u32) spd[180]) << 8) / spd[181];
445
446 dimm->dimms_per_channel = ((spd[178] >> 2) & 0x3) + 1;
447 } else {
448 if (!(spd[178] & 2)) {
449 printram("Selected XMP profile disabled!\n");
450 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
451 return SPD_STATUS_INVALID;
452 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100453 printram(" XMP Profile : 2\n");
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100454 xmp = &spd[220];
455
456 /* Medium Timebase =
457 * Medium Timebase (MTB) Dividend /
458 * Medium Timebase (MTB) Divisor */
459 mtb = (((u32) spd[182]) << 8) / spd[183];
460
461 dimm->dimms_per_channel = ((spd[178] >> 4) & 0x3) + 1;
462 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100463
464 printram(" Max DIMMs/channel : %u\n",
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100465 dimm->dimms_per_channel);
466
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100467 printram(" XMP Revision : %u.%u\n", spd[179] >> 4, spd[179] & 0xf);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100468
469 /* calculate voltage in mV */
470 dimm->voltage = (xmp[0] & 1) * 50;
471 dimm->voltage += ((xmp[0] >> 1) & 0xf) * 100;
472 dimm->voltage += ((xmp[0] >> 5) & 0x3) * 1000;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100473
474 printram(" Requested voltage : %u mV\n", dimm->voltage);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100475
476 /* SDRAM Minimum Cycle Time (tCKmin) */
477 dimm->tCK = xmp[1] * mtb;
478 /* CAS Latencies Supported */
Dan Elkouby0c024202018-04-13 18:45:02 +0300479 dimm->cas_supported = ((xmp[4] << 8) + xmp[3]) & 0x7fff;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100480 /* Minimum CAS Latency Time (tAAmin) */
481 dimm->tAA = xmp[2] * mtb;
482 /* Minimum Write Recovery Time (tWRmin) */
483 dimm->tWR = xmp[8] * mtb;
484 /* Minimum RAS# to CAS# Delay Time (tRCDmin) */
485 dimm->tRCD = xmp[7] * mtb;
486 /* Minimum Row Active to Row Active Delay Time (tRRDmin) */
487 dimm->tRRD = xmp[17] * mtb;
488 /* Minimum Row Precharge Delay Time (tRPmin) */
489 dimm->tRP = xmp[6] * mtb;
490 /* Minimum Active to Precharge Delay Time (tRASmin) */
491 dimm->tRAS = (((xmp[9] & 0x0f) << 8) + xmp[10]) * mtb;
492 /* Minimum Active to Active/Refresh Delay Time (tRCmin) */
493 dimm->tRC = (((xmp[9] & 0xf0) << 4) + xmp[11]) * mtb;
494 /* Minimum Refresh Recovery Delay Time (tRFCmin) */
495 dimm->tRFC = ((xmp[15] << 8) + xmp[14]) * mtb;
496 /* Minimum Internal Write to Read Command Delay Time (tWTRmin) */
497 dimm->tWTR = xmp[20] * mtb;
498 /* Minimum Internal Read to Precharge Command Delay Time (tRTPmin) */
499 dimm->tRTP = xmp[16] * mtb;
500 /* Minimum Four Activate Window Delay Time (tFAWmin) */
501 dimm->tFAW = (((xmp[18] & 0x0f) << 8) + xmp[19]) * mtb;
Dan Elkouby0c024202018-04-13 18:45:02 +0300502 /* Minimum CAS Write Latency Time (tCWLmin) */
503 dimm->tCWL = xmp[5] * mtb;
504 /* System CMD Rate Mode */
505 dimm->tCMD = xmp[23] * mtb;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100506
507 return ret;
508}
509
Patrick Rudolph24efe732018-08-19 11:06:06 +0200510
511/**
512 * Fill cbmem with information for SMBIOS type 17.
513 *
514 * @param channel Corresponding channel of provided @info
515 * @param slot Corresponding slot of provided @info
516 * @param selected_freq The actual frequency the DRAM is running on
517 * @param info DIMM parameters read from SPD
518 *
519 * @return CB_SUCCESS if DIMM info was written
520 */
521enum cb_err spd_add_smbios17(const u8 channel, const u8 slot,
522 const u16 selected_freq,
523 const dimm_attr *info)
524{
525 struct memory_info *mem_info;
526 struct dimm_info *dimm;
527
528 /*
529 * Allocate CBMEM area for DIMM information used to populate SMBIOS
530 * table 17
531 */
532 mem_info = cbmem_find(CBMEM_ID_MEMINFO);
533 if (!mem_info) {
534 mem_info = cbmem_add(CBMEM_ID_MEMINFO, sizeof(*mem_info));
535
536 printk(BIOS_DEBUG, "CBMEM entry for DIMM info: 0x%p\n",
537 mem_info);
538 if (!mem_info)
539 return CB_ERR;
540
541 memset(mem_info, 0, sizeof(*mem_info));
542 }
543
Nico Huberbb0ab9e2018-09-13 10:49:54 +0200544 if (mem_info->dimm_cnt >= ARRAY_SIZE(mem_info->dimm)) {
545 printk(BIOS_WARNING, "BUG: Too many DIMM infos for %s.\n",
546 __func__);
547 return CB_ERR;
548 }
549
Patrick Rudolph24efe732018-08-19 11:06:06 +0200550 dimm = &mem_info->dimm[mem_info->dimm_cnt];
551 if (info->size_mb) {
552 dimm->ddr_type = MEMORY_TYPE_DDR3;
553 dimm->ddr_frequency = selected_freq;
554 dimm->dimm_size = info->size_mb;
555 dimm->channel_num = channel;
556 dimm->rank_per_dimm = info->ranks;
557 dimm->dimm_num = slot;
558 memcpy(dimm->module_part_number, info->part_number, 16);
559 dimm->mod_id = info->manufacturer_id;
560
561 switch (info->dimm_type) {
562 case SPD_DIMM_TYPE_SO_DIMM:
563 dimm->mod_type = SPD_SODIMM;
564 break;
565 case SPD_DIMM_TYPE_72B_SO_CDIMM:
566 dimm->mod_type = SPD_72B_SO_CDIMM;
567 break;
568 case SPD_DIMM_TYPE_72B_SO_RDIMM:
569 dimm->mod_type = SPD_72B_SO_RDIMM;
570 break;
571 case SPD_DIMM_TYPE_UDIMM:
572 dimm->mod_type = SPD_UDIMM;
573 break;
574 case SPD_DIMM_TYPE_RDIMM:
575 dimm->mod_type = SPD_RDIMM;
576 break;
577 case SPD_DIMM_TYPE_UNDEFINED:
578 default:
579 dimm->mod_type = SPD_UNDEFINED;
580 break;
581 }
582
583 dimm->bus_width = MEMORY_BUS_WIDTH_64; // non-ECC only
584 memcpy(dimm->serial, info->serial,
585 MIN(sizeof(dimm->serial), sizeof(info->serial)));
586 mem_info->dimm_cnt++;
587 }
588
589 return CB_SUCCESS;
590}
591
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500592/*
593 * The information printed below has a more informational character, and is not
594 * necessarily tied in to RAM init debugging. Hence, we stop using printram(),
595 * and use the standard printk()'s below.
596 */
597
598static void print_ns(const char *msg, u32 val)
599{
600 u32 mant, fp;
601 mant = val / 256;
602 fp = (val % 256) * 1000 / 256;
603
604 printk(BIOS_INFO, "%s%3u.%.3u ns\n", msg, mant, fp);
605}
606
607/**
608* \brief Print the info in DIMM
609*
610* Print info about the DIMM. Useful to use when CONFIG_DEBUG_RAM_SETUP is
611* selected, or for a purely informative output.
612*
Martin Roth63373ed2013-07-08 16:24:19 -0600613* @param dimm pointer to already decoded @ref dimm_attr structure
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500614*/
615void dram_print_spd_ddr3(const dimm_attr * dimm)
616{
617 u16 val16;
618 int i;
619
620 printk(BIOS_INFO, " Row addr bits : %u\n", dimm->row_bits);
621 printk(BIOS_INFO, " Column addr bits : %u\n", dimm->col_bits);
622 printk(BIOS_INFO, " Number of ranks : %u\n", dimm->ranks);
623 printk(BIOS_INFO, " DIMM Capacity : %u MB\n", dimm->size_mb);
624
625 /* CAS Latencies Supported */
626 val16 = dimm->cas_supported;
627 printk(BIOS_INFO, " CAS latencies :");
628 i = 0;
629 do {
630 if (val16 & 1)
631 printk(BIOS_INFO, " %u", i + 4);
632 i++;
633 val16 >>= 1;
634 } while (val16);
635 printk(BIOS_INFO, "\n");
636
637 print_ns(" tCKmin : ", dimm->tCK);
638 print_ns(" tAAmin : ", dimm->tAA);
639 print_ns(" tWRmin : ", dimm->tWR);
640 print_ns(" tRCDmin : ", dimm->tRCD);
641 print_ns(" tRRDmin : ", dimm->tRRD);
642 print_ns(" tRPmin : ", dimm->tRP);
643 print_ns(" tRASmin : ", dimm->tRAS);
644 print_ns(" tRCmin : ", dimm->tRC);
645 print_ns(" tRFCmin : ", dimm->tRFC);
646 print_ns(" tWTRmin : ", dimm->tWTR);
647 print_ns(" tRTPmin : ", dimm->tRTP);
648 print_ns(" tFAWmin : ", dimm->tFAW);
Dan Elkouby0c024202018-04-13 18:45:02 +0300649 /* Those values are only relevant if an XMP profile sets them */
650 if (dimm->tCWL)
651 print_ns(" tCWLmin : ", dimm->tCWL);
652 if (dimm->tCMD)
653 printk(BIOS_INFO, " tCMDmin : %3u\n",
654 DIV_ROUND_UP(dimm->tCMD, 256));
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500655}
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500656
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500657/*==============================================================================
658 *= DDR3 MRS helpers
659 *----------------------------------------------------------------------------*/
660
661/*
662 * MRS command structure:
663 * cmd[15:0] = Address pins MA[15:0]
664 * cmd[18:16] = Bank address BA[2:0]
665 */
666
667/* Map tWR value to a bitmask of the MR0 cycle */
668static u16 ddr3_twr_to_mr0_map(u8 twr)
669{
670 if ((twr >= 5) && (twr <= 8))
671 return (twr - 4) << 9;
672
673 /*
674 * From 8T onwards, we can only use even values. Round up if we are
675 * given an odd value.
676 */
677 if ((twr >= 9) && (twr <= 14))
678 return ((twr + 1) >> 1) << 9;
679
680 /* tWR == 16T is [000] */
681 return 0;
682}
683
684/* Map the CAS latency to a bitmask for the MR0 cycle */
685static u16 ddr3_cas_to_mr0_map(u8 cas)
686{
687 u16 mask = 0;
688 /* A[6:4] are bits [2:0] of (CAS - 4) */
689 mask = ((cas - 4) & 0x07) << 4;
690
691 /* A2 is the MSB of (CAS - 4) */
692 if ((cas - 4) & (1 << 3))
693 mask |= (1 << 2);
694
695 return mask;
696}
697
698/**
699 * \brief Get command address for a DDR3 MR0 command
700 *
701 * The DDR3 specification only covers odd write_recovery up to 7T. If an odd
702 * write_recovery greater than 7 is specified, it will be rounded up. If a tWR
703 * greater than 8 is specified, it is recommended to explicitly round it up or
704 * down before calling this function.
705 *
706 * write_recovery and cas are given in clock cycles. For example, a CAS of 7T
707 * should be given as 7.
708 *
Martin Roth98b698c2015-01-06 21:02:52 -0700709 * @param precharge_pd
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500710 * @param write_recovery Write recovery latency, tWR in clock cycles.
Martin Roth98b698c2015-01-06 21:02:52 -0700711 * @param dll_reset
712 * @param mode
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500713 * @param cas CAS latency in clock cycles.
Martin Roth98b698c2015-01-06 21:02:52 -0700714 * @param burst_type
715 * @param burst_length
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500716 */
717mrs_cmd_t ddr3_get_mr0(enum ddr3_mr0_precharge precharge_pd,
718 u8 write_recovery,
719 enum ddr3_mr0_dll_reset dll_reset,
720 enum ddr3_mr0_mode mode,
721 u8 cas,
722 enum ddr3_mr0_burst_type burst_type,
723 enum ddr3_mr0_burst_length burst_length)
724{
725 mrs_cmd_t cmd = 0 << 16;
726
727 if (precharge_pd == DDR3_MR0_PRECHARGE_FAST)
728 cmd |= (1 << 12);
729
730 cmd |= ddr3_twr_to_mr0_map(write_recovery);
731
732 if (dll_reset == DDR3_MR0_DLL_RESET_YES)
733 cmd |= (1 << 8);
734
735 if (mode == DDR3_MR0_MODE_TEST)
736 cmd |= (1 << 7);
737
738 cmd |= ddr3_cas_to_mr0_map(cas);
739
740 if (burst_type == DDR3_MR0_BURST_TYPE_INTERLEAVED)
741 cmd |= (1 << 3);
742
743 cmd |= (burst_length & 0x03) << 0;
744
745 return cmd;
746}
747
748static u16 ddr3_rtt_nom_to_mr1_map(enum ddr3_mr1_rtt_nom rtt_nom)
749{
750 u16 mask = 0;
751 /* A9 <-> rtt_nom[2] */
752 if (rtt_nom & (1 << 2))
753 mask |= (1 << 9);
754 /* A6 <-> rtt_nom[1] */
755 if (rtt_nom & (1 << 1))
756 mask |= (1 << 6);
757 /* A2 <-> rtt_nom[0] */
758 if (rtt_nom & (1 << 0))
759 mask |= (1 << 2);
760
761 return mask;
762}
763
764static u16 ddr3_ods_to_mr1_map(enum ddr3_mr1_ods ods)
765{
766 u16 mask = 0;
767 /* A5 <-> ods[1] */
768 if (ods & (1 << 1))
769 mask |= (1 << 5);
770 /* A1 <-> ods[0] */
771 if (ods & (1 << 0))
772 mask |= (1 << 1);
773
774 return mask;
775}
776
777/**
778 * \brief Get command address for a DDR3 MR1 command
779 */
780mrs_cmd_t ddr3_get_mr1(enum ddr3_mr1_qoff qoff,
781 enum ddr3_mr1_tqds tqds,
782 enum ddr3_mr1_rtt_nom rtt_nom,
783 enum ddr3_mr1_write_leveling write_leveling,
784 enum ddr3_mr1_ods ods,
785 enum ddr3_mr1_additive_latency additive_latency,
786 enum ddr3_mr1_dll dll_disable)
787{
788 mrs_cmd_t cmd = 1 << 16;
789
790 if (qoff == DDR3_MR1_QOFF_DISABLE)
791 cmd |= (1 << 12);
792
793 if (tqds == DDR3_MR1_TQDS_ENABLE)
794 cmd |= (1 << 11);
795
796 cmd |= ddr3_rtt_nom_to_mr1_map(rtt_nom);
797
798 if (write_leveling == DDR3_MR1_WRLVL_ENABLE)
799 cmd |= (1 << 7);
800
801 cmd |= ddr3_ods_to_mr1_map(ods);
802
803 cmd |= (additive_latency & 0x03) << 3;
804
805 if (dll_disable == DDR3_MR1_DLL_DISABLE)
806 cmd |= (1 << 0);
807
808 return cmd;
809}
810
811/**
812 * \brief Get command address for a DDR3 MR2 command
813 *
814 * cas_cwl is given in clock cycles. For example, a cas_cwl of 7T should be
815 * given as 7.
816 *
Martin Roth98b698c2015-01-06 21:02:52 -0700817 * @param rtt_wr
818 * @param extended_temp
819 * @param self_refresh
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500820 * @param cas_cwl CAS write latency in clock cycles.
821 */
Martin Roth98b698c2015-01-06 21:02:52 -0700822
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500823mrs_cmd_t ddr3_get_mr2(enum ddr3_mr2_rttwr rtt_wr,
824 enum ddr3_mr2_srt_range extended_temp,
825 enum ddr3_mr2_asr self_refresh, u8 cas_cwl)
826{
827 mrs_cmd_t cmd = 2 << 16;
828
829 cmd |= (rtt_wr & 0x03) << 9;
830
831 if (extended_temp == DDR3_MR2_SRT_EXTENDED)
832 cmd |= (1 << 7);
833
834 if (self_refresh == DDR3_MR2_ASR_AUTO)
835 cmd |= (1 << 6);
836
837 cmd |= ((cas_cwl - 5) & 0x07) << 3;
838
839 return cmd;
840}
841
842/**
843 * \brief Get command address for a DDR3 MR3 command
844 *
845 * @param dataflow_from_mpr Specify a non-zero value to put DRAM in read
846 * leveling mode. Zero for normal operation.
847 */
848mrs_cmd_t ddr3_get_mr3(char dataflow_from_mpr)
849{
850 mrs_cmd_t cmd = 3 << 16;
851
852 if (dataflow_from_mpr)
853 cmd |= (1 << 2);
854
855 return cmd;
856}
857
858/**
859 * \brief Mirror the address bits for this MRS command
860 *
861 * Swap the following bits in the MRS command:
862 * - MA3 <-> MA4
863 * - MA5 <-> MA6
864 * - MA7 <-> MA8
865 * - BA0 <-> BA1
866 */
867mrs_cmd_t ddr3_mrs_mirror_pins(mrs_cmd_t cmd)
868{
869 u32 downshift, upshift;
870 /* High bits= A4 | A6 | A8 | BA1 */
871 /* Low bits = A3 | A5 | A7 | BA0 */
872 u32 lowbits = (1 << 3) | (1 << 5) | (1 << 7) | (1 << 16);
873 downshift = (cmd & (lowbits << 1));
874 upshift = (cmd & lowbits);
875 cmd &= ~(lowbits | (lowbits << 1));
876 cmd |= (downshift >> 1) | (upshift << 1);
877 return cmd;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500878}