blob: 834dc83db6499e7d8dab5f7df183c8c6bd7f5482 [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>
Patrick Rudolph07691592016-02-29 18:21:00 +010026#include <string.h>
Patrick Rudolph24efe732018-08-19 11:06:06 +020027#include <memory_info.h>
28#include <cbmem.h>
29#include <smbios.h>
Elyes HAOUASbd1683d2019-05-15 21:05:37 +020030#include <types.h>
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050031
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -050032/*==============================================================================
33 * = DDR3 SPD decoding helpers
34 *----------------------------------------------------------------------------*/
35
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050036/**
37 * \brief Checks if the DIMM is Registered based on byte[3] of the SPD
38 *
39 * Tells if the DIMM type is registered or not.
40 *
41 * @param type DIMM type. This is byte[3] of the SPD.
42 */
Patrick Rudolph6e53ae62017-01-31 19:43:17 +010043int spd_dimm_is_registered_ddr3(enum spd_dimm_type type)
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050044{
45 if ((type == SPD_DIMM_TYPE_RDIMM)
46 | (type == SPD_DIMM_TYPE_MINI_RDIMM)
47 | (type == SPD_DIMM_TYPE_72B_SO_RDIMM))
48 return 1;
49
50 return 0;
51}
52
Arthur Heymans97b337b2018-01-22 01:26:53 +010053u16 ddr3_crc16(const u8 *ptr, int n_crc)
Kyösti Mälkki7dc4b842016-11-18 18:41:17 +020054{
55 int i;
56 u16 crc = 0;
57
58 while (--n_crc >= 0) {
Kyösti Mälkki378d79e2016-11-21 02:39:59 +020059 crc = crc ^ ((int)*ptr++ << 8);
Kyösti Mälkki7dc4b842016-11-18 18:41:17 +020060 for (i = 0; i < 8; ++i)
61 if (crc & 0x8000) {
62 crc = (crc << 1) ^ 0x1021;
63 } else {
64 crc = crc << 1;
65 }
66 }
67
68 return crc;
69}
70
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050071/**
Alexandru Gagniuc4c37e582013-12-17 13:08:01 -050072 * \brief Calculate the CRC of a DDR3 SPD
73 *
74 * @param spd pointer to raw SPD data
75 * @param len length of data in SPD
76 *
77 * @return the CRC of the SPD data, or 0 when spd data is truncated.
78 */
79u16 spd_ddr3_calc_crc(u8 *spd, int len)
80{
Kyösti Mälkki7dc4b842016-11-18 18:41:17 +020081 int n_crc;
Alexandru Gagniuc4c37e582013-12-17 13:08:01 -050082
83 /* Find the number of bytes covered by CRC */
84 if (spd[0] & 0x80) {
85 n_crc = 117;
86 } else {
87 n_crc = 126;
88 }
89
90 if (len < n_crc)
91 /* Not enough bytes available to get the CRC */
92 return 0;
93
Arthur Heymans97b337b2018-01-22 01:26:53 +010094 return ddr3_crc16(spd, n_crc);
Kyösti Mälkki7dc4b842016-11-18 18:41:17 +020095}
96
97/**
98 * \brief Calculate the CRC of a DDR3 SPD unique identifier
99 *
100 * @param spd pointer to raw SPD data
101 * @param len length of data in SPD
102 *
103 * @return the CRC of SPD data bytes 117..127, or 0 when spd data is truncated.
104 */
105u16 spd_ddr3_calc_unique_crc(u8 *spd, int len)
106{
107 if (len < (117 + 11))
108 /* Not enough bytes available to get the CRC */
109 return 0;
110
Arthur Heymans97b337b2018-01-22 01:26:53 +0100111 return ddr3_crc16(&spd[117], 11);
Alexandru Gagniuc4c37e582013-12-17 13:08:01 -0500112}
113
114/**
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500115 * \brief Decode the raw SPD data
116 *
117 * Decodes a raw SPD data from a DDR3 DIMM, and organizes it into a
118 * @ref dimm_attr structure. The SPD data must first be read in a contiguous
119 * array, and passed to this function.
120 *
Martin Roth63373ed2013-07-08 16:24:19 -0600121 * @param dimm pointer to @ref dimm_attr structure where the decoded data is to
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200122 * be stored
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500123 * @param spd array of raw data previously read from the SPD.
124 *
125 * @return @ref spd_status enumerator
126 * SPD_STATUS_OK -- decoding was successful
127 * SPD_STATUS_INVALID -- invalid SPD or not a DDR3 SPD
128 * SPD_STATUS_CRC_ERROR -- CRC did not verify
129 * SPD_STATUS_INVALID_FIELD -- A field with an invalid value was
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200130 * detected.
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500131 */
132int spd_decode_ddr3(dimm_attr * dimm, spd_raw_data spd)
133{
Alexandru Gagniuc4c37e582013-12-17 13:08:01 -0500134 int ret;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500135 u16 crc, spd_crc;
Nicola Corna76f8dbc2016-11-16 08:57:15 +0100136 u8 capacity_shift, bus_width;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500137 u8 reg8;
138 u32 mtb; /* medium time base */
Nicola Corna76f8dbc2016-11-16 08:57:15 +0100139 u32 ftb; /* fine time base */
Elyes HAOUAS05c04552019-04-23 22:15:57 +0200140 unsigned int val;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500141
142 ret = SPD_STATUS_OK;
143
144 /* Don't assume we memset 0 dimm struct. Clear all our flags */
145 dimm->flags.raw = 0;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100146 dimm->dimms_per_channel = 3;
147
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500148 /* Make sure that the SPD dump is indeed from a DDR3 module */
149 if (spd[2] != SPD_MEMORY_TYPE_SDRAM_DDR3) {
150 printram("Not a DDR3 SPD!\n");
151 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
152 return SPD_STATUS_INVALID;
153 }
154 dimm->dram_type = SPD_MEMORY_TYPE_SDRAM_DDR3;
Vladimir Serbinenko0e675f72014-12-07 13:56:48 +0100155 dimm->dimm_type = spd[3] & 0xf;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500156
Patrick Rudolph8c639352015-06-22 19:32:53 +0200157 crc = spd_ddr3_calc_crc(spd, sizeof(spd_raw_data));
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500158 /* Compare with the CRC in the SPD */
159 spd_crc = (spd[127] << 8) + spd[126];
160 /* Verify the CRC is correct */
161 if (crc != spd_crc) {
Patrick Rudolph78c6e3e2015-06-22 19:46:34 +0200162 printram("ERROR: SPD CRC failed!!!\n");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500163 ret = SPD_STATUS_CRC_ERROR;
164 };
165
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100166 printram(" Revision : %x\n", spd[1]);
167 printram(" Type : %x\n", spd[2]);
168 printram(" Key : %x\n", spd[3]);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500169
170 reg8 = spd[4];
171 /* Number of memory banks */
172 val = (reg8 >> 4) & 0x07;
173 if (val > 0x03) {
174 printram(" Invalid number of memory banks\n");
175 ret = SPD_STATUS_INVALID_FIELD;
176 }
Elyes HAOUAS05c04552019-04-23 22:15:57 +0200177 printram(" Banks : %u\n", 1 << (val + 3));
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500178 /* SDRAM capacity */
179 capacity_shift = reg8 & 0x0f;
180 if (capacity_shift > 0x06) {
181 printram(" Invalid module capacity\n");
182 ret = SPD_STATUS_INVALID_FIELD;
183 }
184 if (capacity_shift < 0x02) {
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100185 printram(" Capacity : %u Mb\n", 256 << capacity_shift);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500186 } else {
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100187 printram(" Capacity : %u Gb\n", 1 << (capacity_shift - 2));
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500188 }
189
190 reg8 = spd[5];
191 /* Row address bits */
192 val = (reg8 >> 3) & 0x07;
193 if (val > 0x04) {
194 printram(" Invalid row address bits\n");
195 ret = SPD_STATUS_INVALID_FIELD;
196 }
197 dimm->row_bits = val + 12;
198 /* Column address bits */
199 val = reg8 & 0x07;
200 if (val > 0x03) {
201 printram(" Invalid column address bits\n");
202 ret = SPD_STATUS_INVALID_FIELD;
203 }
204 dimm->col_bits = val + 9;
205
206 /* Module nominal voltage */
207 reg8 = spd[6];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100208 printram(" Supported voltages :");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500209 if (reg8 & (1 << 2)) {
210 dimm->flags.operable_1_25V = 1;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100211 dimm->voltage = 1250;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500212 printram(" 1.25V");
213 }
214 if (reg8 & (1 << 1)) {
215 dimm->flags.operable_1_35V = 1;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100216 dimm->voltage = 1300;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500217 printram(" 1.35V");
218 }
219 if (!(reg8 & (1 << 0))) {
220 dimm->flags.operable_1_50V = 1;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100221 dimm->voltage = 1500;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500222 printram(" 1.5V");
223 }
224 printram("\n");
225
226 /* Module organization */
227 reg8 = spd[7];
228 /* Number of ranks */
229 val = (reg8 >> 3) & 0x07;
230 if (val > 3) {
231 printram(" Invalid number of ranks\n");
232 ret = SPD_STATUS_INVALID_FIELD;
233 }
234 dimm->ranks = val + 1;
235 /* SDRAM device width */
236 val = (reg8 & 0x07);
237 if (val > 3) {
238 printram(" Invalid SDRAM width\n");
239 ret = SPD_STATUS_INVALID_FIELD;
240 }
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200241 dimm->width = (4 << val);
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100242 printram(" SDRAM width : %u\n", dimm->width);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500243
244 /* Memory bus width */
245 reg8 = spd[8];
246 /* Bus extension */
247 val = (reg8 >> 3) & 0x03;
248 if (val > 1) {
249 printram(" Invalid bus extension\n");
250 ret = SPD_STATUS_INVALID_FIELD;
251 }
252 dimm->flags.is_ecc = val ? 1 : 0;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100253 printram(" Bus extension : %u bits\n", val ? 8 : 0);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500254 /* Bus width */
255 val = reg8 & 0x07;
256 if (val > 3) {
257 printram(" Invalid bus width\n");
258 ret = SPD_STATUS_INVALID_FIELD;
259 }
260 bus_width = 8 << val;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100261 printram(" Bus width : %u\n", bus_width);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500262
263 /* We have all the info we need to compute the dimm size */
264 /* Capacity is 256Mbit multiplied by the power of 2 specified in
265 * capacity_shift
266 * The rest is the JEDEC formula */
267 dimm->size_mb = ((1 << (capacity_shift + (25 - 20))) * bus_width
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200268 * dimm->ranks) / dimm->width;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500269
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500270 /* Medium Timebase =
271 * Medium Timebase (MTB) Dividend /
272 * Medium Timebase (MTB) Divisor */
273 mtb = (((u32) spd[10]) << 8) / spd[11];
274
275 /* SDRAM Minimum Cycle Time (tCKmin) */
276 dimm->tCK = spd[12] * mtb;
277 /* CAS Latencies Supported */
278 dimm->cas_supported = (spd[15] << 8) + spd[14];
279 /* Minimum CAS Latency Time (tAAmin) */
280 dimm->tAA = spd[16] * mtb;
281 /* Minimum Write Recovery Time (tWRmin) */
282 dimm->tWR = spd[17] * mtb;
283 /* Minimum RAS# to CAS# Delay Time (tRCDmin) */
284 dimm->tRCD = spd[18] * mtb;
285 /* Minimum Row Active to Row Active Delay Time (tRRDmin) */
286 dimm->tRRD = spd[19] * mtb;
287 /* Minimum Row Precharge Delay Time (tRPmin) */
288 dimm->tRP = spd[20] * mtb;
289 /* Minimum Active to Precharge Delay Time (tRASmin) */
290 dimm->tRAS = (((spd[21] & 0x0f) << 8) + spd[22]) * mtb;
291 /* Minimum Active to Active/Refresh Delay Time (tRCmin) */
292 dimm->tRC = (((spd[21] & 0xf0) << 4) + spd[23]) * mtb;
293 /* Minimum Refresh Recovery Delay Time (tRFCmin) */
294 dimm->tRFC = ((spd[25] << 8) + spd[24]) * mtb;
295 /* Minimum Internal Write to Read Command Delay Time (tWTRmin) */
296 dimm->tWTR = spd[26] * mtb;
297 /* Minimum Internal Read to Precharge Command Delay Time (tRTPmin) */
298 dimm->tRTP = spd[27] * mtb;
299 /* Minimum Four Activate Window Delay Time (tFAWmin) */
300 dimm->tFAW = (((spd[28] & 0x0f) << 8) + spd[29]) * mtb;
Dan Elkouby0c024202018-04-13 18:45:02 +0300301 /* Minimum CAS Write Latency Time (tCWLmin)
302 * - not present in standard SPD */
303 dimm->tCWL = 0;
304 /* System CMD Rate Mode - not present in standard SPD */
305 dimm->tCMD = 0;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500306
Nicola Corna76f8dbc2016-11-16 08:57:15 +0100307 printram(" FTB timings :");
308 /* FTB is introduced in SPD revision 1.1 */
309 if (spd[1] >= 0x11 && spd[9] & 0x0f) {
310 printram(" yes\n");
311
312 /* Fine timebase (1/256 ps) =
313 * Fine Timebase (FTB) Dividend /
314 * Fine Timebase (FTB) Divisor */
315 ftb = (((u16) spd[9] & 0xf0) << 4) / (spd[9] & 0x0f);
316
317 /* SPD recommends to round up the MTB part and use a negative
318 * FTB, so a negative rounding should be always safe */
319
320 /* SDRAM Minimum Cycle Time (tCKmin) correction */
321 dimm->tCK += (s32)((s8) spd[34] * ftb - 500) / 1000;
322 /* Minimum CAS Latency Time (tAAmin) correction */
323 dimm->tAA += (s32)((s8) spd[35] * ftb - 500) / 1000;
324 /* Minimum RAS# to CAS# Delay Time (tRCDmin) correction */
325 dimm->tRCD += (s32)((s8) spd[36] * ftb - 500) / 1000;
326 /* Minimum Row Precharge Delay Time (tRPmin) correction */
327 dimm->tRP += (s32)((s8) spd[37] * ftb - 500) / 1000;
328 /* Minimum Active to Active/Refresh Delay Time (tRCmin) corr. */
329 dimm->tRC += (s32)((s8) spd[38] * ftb - 500) / 1000;
330 }
331 else {
332 printram(" no\n");
333 }
334
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500335 /* SDRAM Optional Features */
336 reg8 = spd[30];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100337 printram(" Optional features :");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500338 if (reg8 & 0x80) {
339 dimm->flags.dll_off_mode = 1;
340 printram(" DLL-Off_mode");
341 }
342 if (reg8 & 0x02) {
343 dimm->flags.rzq7_supported = 1;
344 printram(" RZQ/7");
345 }
346 if (reg8 & 0x01) {
347 dimm->flags.rzq6_supported = 1;
348 printram(" RZQ/6");
349 }
350 printram("\n");
351
352 /* SDRAM Thermal and Refresh Options */
353 reg8 = spd[31];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100354 printram(" Thermal features :");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500355 if (reg8 & 0x80) {
356 dimm->flags.pasr = 1;
357 printram(" PASR");
358 }
359 if (reg8 & 0x08) {
360 dimm->flags.odts = 1;
361 printram(" ODTS");
362 }
363 if (reg8 & 0x04) {
364 dimm->flags.asr = 1;
365 printram(" ASR");
366 }
367 if (reg8 & 0x02) {
368 dimm->flags.ext_temp_range = 1;
369 printram(" ext_temp_refresh");
370 }
371 if (reg8 & 0x01) {
372 dimm->flags.ext_temp_refresh = 1;
373 printram(" ext_temp_range");
374 }
375 printram("\n");
376
377 /* Module Thermal Sensor */
378 reg8 = spd[32];
379 if (reg8 & 0x80)
380 dimm->flags.therm_sensor = 1;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100381 printram(" Thermal sensor : %s\n",
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500382 dimm->flags.therm_sensor ? "yes" : "no");
383
384 /* SDRAM Device Type */
Jacob Garber93064ff2019-06-24 13:02:27 -0600385 printram(" Standard SDRAM : %s\n", (spd[33] & 0x80) ? "no" : "yes");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500386
387 if (spd[63] & 0x01) {
388 dimm->flags.pins_mirrored = 1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500389 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100390 printram(" Rank1 Address bits : %s\n",
391 (spd[63] & 0x01) ? "mirrored" : "normal");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500392
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200393 dimm->reference_card = spd[62] & 0x1f;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100394 printram(" DIMM Reference card: %c\n", 'A' + dimm->reference_card);
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200395
Patrick Rudolph07691592016-02-29 18:21:00 +0100396 dimm->manufacturer_id = (spd[118] << 8) | spd[117];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100397 printram(" Manufacturer ID : %x\n", dimm->manufacturer_id);
Patrick Rudolph07691592016-02-29 18:21:00 +0100398
399 dimm->part_number[16] = 0;
400 memcpy(dimm->part_number, &spd[128], 16);
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100401 printram(" Part number : %s\n", dimm->part_number);
Patrick Rudolph07691592016-02-29 18:21:00 +0100402
Patrick Rudolph15e64692018-08-17 15:24:56 +0200403 memcpy(dimm->serial, &spd[SPD_DIMM_SERIAL_NUM], SPD_DIMM_SERIAL_LEN);
404
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500405 return ret;
406}
407
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100408/**
409 * \brief Decode the raw SPD XMP data
410 *
411 * Decodes a raw SPD XMP data from a DDR3 DIMM, and organizes it into a
412 * @ref dimm_attr structure. The SPD data must first be read in a contiguous
413 * array, and passed to this function.
414 *
415 * @param dimm pointer to @ref dimm_attr structure where the decoded data is to
416 * be stored
417 * @param spd array of raw data previously read from the SPD.
418 *
419 * @param profile select one of the profiles to load
420 *
421 * @return @ref spd_status enumerator
422 * SPD_STATUS_OK -- decoding was successful
423 * SPD_STATUS_INVALID -- invalid SPD or not a DDR3 SPD
424 * SPD_STATUS_CRC_ERROR -- CRC did not verify
425 * SPD_STATUS_INVALID_FIELD -- A field with an invalid value was
426 * detected.
427 */
428int spd_xmp_decode_ddr3(dimm_attr *dimm,
429 spd_raw_data spd,
430 enum ddr3_xmp_profile profile)
431{
432 int ret;
433 u32 mtb; /* medium time base */
434 u8 *xmp; /* pointer to XMP profile data */
435
436 /* need a valid SPD */
437 ret = spd_decode_ddr3(dimm, spd);
438 if (ret != SPD_STATUS_OK)
439 return ret;
440
441 /* search for magic header */
442 if (spd[176] != 0x0C || spd[177] != 0x4A) {
443 printram("Not a DDR3 XMP profile!\n");
444 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
445 return SPD_STATUS_INVALID;
446 }
447
448 if (profile == DDR3_XMP_PROFILE_1) {
449 if (!(spd[178] & 1)) {
450 printram("Selected XMP profile disabled!\n");
451 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
452 return SPD_STATUS_INVALID;
453 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100454
455 printram(" XMP Profile : 1\n");
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100456 xmp = &spd[185];
457
458 /* Medium Timebase =
459 * Medium Timebase (MTB) Dividend /
460 * Medium Timebase (MTB) Divisor */
461 mtb = (((u32) spd[180]) << 8) / spd[181];
462
463 dimm->dimms_per_channel = ((spd[178] >> 2) & 0x3) + 1;
464 } else {
465 if (!(spd[178] & 2)) {
466 printram("Selected XMP profile disabled!\n");
467 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
468 return SPD_STATUS_INVALID;
469 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100470 printram(" XMP Profile : 2\n");
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100471 xmp = &spd[220];
472
473 /* Medium Timebase =
474 * Medium Timebase (MTB) Dividend /
475 * Medium Timebase (MTB) Divisor */
476 mtb = (((u32) spd[182]) << 8) / spd[183];
477
478 dimm->dimms_per_channel = ((spd[178] >> 4) & 0x3) + 1;
479 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100480
481 printram(" Max DIMMs/channel : %u\n",
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100482 dimm->dimms_per_channel);
483
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100484 printram(" XMP Revision : %u.%u\n", spd[179] >> 4, spd[179] & 0xf);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100485
486 /* calculate voltage in mV */
487 dimm->voltage = (xmp[0] & 1) * 50;
488 dimm->voltage += ((xmp[0] >> 1) & 0xf) * 100;
489 dimm->voltage += ((xmp[0] >> 5) & 0x3) * 1000;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100490
491 printram(" Requested voltage : %u mV\n", dimm->voltage);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100492
493 /* SDRAM Minimum Cycle Time (tCKmin) */
494 dimm->tCK = xmp[1] * mtb;
495 /* CAS Latencies Supported */
Dan Elkouby0c024202018-04-13 18:45:02 +0300496 dimm->cas_supported = ((xmp[4] << 8) + xmp[3]) & 0x7fff;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100497 /* Minimum CAS Latency Time (tAAmin) */
498 dimm->tAA = xmp[2] * mtb;
499 /* Minimum Write Recovery Time (tWRmin) */
500 dimm->tWR = xmp[8] * mtb;
501 /* Minimum RAS# to CAS# Delay Time (tRCDmin) */
502 dimm->tRCD = xmp[7] * mtb;
503 /* Minimum Row Active to Row Active Delay Time (tRRDmin) */
504 dimm->tRRD = xmp[17] * mtb;
505 /* Minimum Row Precharge Delay Time (tRPmin) */
506 dimm->tRP = xmp[6] * mtb;
507 /* Minimum Active to Precharge Delay Time (tRASmin) */
508 dimm->tRAS = (((xmp[9] & 0x0f) << 8) + xmp[10]) * mtb;
509 /* Minimum Active to Active/Refresh Delay Time (tRCmin) */
510 dimm->tRC = (((xmp[9] & 0xf0) << 4) + xmp[11]) * mtb;
511 /* Minimum Refresh Recovery Delay Time (tRFCmin) */
512 dimm->tRFC = ((xmp[15] << 8) + xmp[14]) * mtb;
513 /* Minimum Internal Write to Read Command Delay Time (tWTRmin) */
514 dimm->tWTR = xmp[20] * mtb;
515 /* Minimum Internal Read to Precharge Command Delay Time (tRTPmin) */
516 dimm->tRTP = xmp[16] * mtb;
517 /* Minimum Four Activate Window Delay Time (tFAWmin) */
518 dimm->tFAW = (((xmp[18] & 0x0f) << 8) + xmp[19]) * mtb;
Dan Elkouby0c024202018-04-13 18:45:02 +0300519 /* Minimum CAS Write Latency Time (tCWLmin) */
520 dimm->tCWL = xmp[5] * mtb;
521 /* System CMD Rate Mode */
522 dimm->tCMD = xmp[23] * mtb;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100523
524 return ret;
525}
526
Patrick Rudolph24efe732018-08-19 11:06:06 +0200527
528/**
529 * Fill cbmem with information for SMBIOS type 17.
530 *
531 * @param channel Corresponding channel of provided @info
532 * @param slot Corresponding slot of provided @info
533 * @param selected_freq The actual frequency the DRAM is running on
534 * @param info DIMM parameters read from SPD
535 *
536 * @return CB_SUCCESS if DIMM info was written
537 */
538enum cb_err spd_add_smbios17(const u8 channel, const u8 slot,
539 const u16 selected_freq,
540 const dimm_attr *info)
541{
542 struct memory_info *mem_info;
543 struct dimm_info *dimm;
544
545 /*
546 * Allocate CBMEM area for DIMM information used to populate SMBIOS
547 * table 17
548 */
549 mem_info = cbmem_find(CBMEM_ID_MEMINFO);
550 if (!mem_info) {
551 mem_info = cbmem_add(CBMEM_ID_MEMINFO, sizeof(*mem_info));
552
553 printk(BIOS_DEBUG, "CBMEM entry for DIMM info: 0x%p\n",
554 mem_info);
555 if (!mem_info)
556 return CB_ERR;
557
558 memset(mem_info, 0, sizeof(*mem_info));
559 }
560
Nico Huberbb0ab9e2018-09-13 10:49:54 +0200561 if (mem_info->dimm_cnt >= ARRAY_SIZE(mem_info->dimm)) {
562 printk(BIOS_WARNING, "BUG: Too many DIMM infos for %s.\n",
563 __func__);
564 return CB_ERR;
565 }
566
Patrick Rudolph24efe732018-08-19 11:06:06 +0200567 dimm = &mem_info->dimm[mem_info->dimm_cnt];
568 if (info->size_mb) {
569 dimm->ddr_type = MEMORY_TYPE_DDR3;
570 dimm->ddr_frequency = selected_freq;
571 dimm->dimm_size = info->size_mb;
572 dimm->channel_num = channel;
573 dimm->rank_per_dimm = info->ranks;
574 dimm->dimm_num = slot;
575 memcpy(dimm->module_part_number, info->part_number, 16);
576 dimm->mod_id = info->manufacturer_id;
577
578 switch (info->dimm_type) {
579 case SPD_DIMM_TYPE_SO_DIMM:
580 dimm->mod_type = SPD_SODIMM;
581 break;
582 case SPD_DIMM_TYPE_72B_SO_CDIMM:
583 dimm->mod_type = SPD_72B_SO_CDIMM;
584 break;
585 case SPD_DIMM_TYPE_72B_SO_RDIMM:
586 dimm->mod_type = SPD_72B_SO_RDIMM;
587 break;
588 case SPD_DIMM_TYPE_UDIMM:
589 dimm->mod_type = SPD_UDIMM;
590 break;
591 case SPD_DIMM_TYPE_RDIMM:
592 dimm->mod_type = SPD_RDIMM;
593 break;
594 case SPD_DIMM_TYPE_UNDEFINED:
595 default:
596 dimm->mod_type = SPD_UNDEFINED;
597 break;
598 }
599
600 dimm->bus_width = MEMORY_BUS_WIDTH_64; // non-ECC only
601 memcpy(dimm->serial, info->serial,
602 MIN(sizeof(dimm->serial), sizeof(info->serial)));
603 mem_info->dimm_cnt++;
604 }
605
606 return CB_SUCCESS;
607}
608
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500609/*
610 * The information printed below has a more informational character, and is not
611 * necessarily tied in to RAM init debugging. Hence, we stop using printram(),
612 * and use the standard printk()'s below.
613 */
614
615static void print_ns(const char *msg, u32 val)
616{
617 u32 mant, fp;
618 mant = val / 256;
619 fp = (val % 256) * 1000 / 256;
620
621 printk(BIOS_INFO, "%s%3u.%.3u ns\n", msg, mant, fp);
622}
623
624/**
625* \brief Print the info in DIMM
626*
627* Print info about the DIMM. Useful to use when CONFIG_DEBUG_RAM_SETUP is
628* selected, or for a purely informative output.
629*
Martin Roth63373ed2013-07-08 16:24:19 -0600630* @param dimm pointer to already decoded @ref dimm_attr structure
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500631*/
632void dram_print_spd_ddr3(const dimm_attr * dimm)
633{
634 u16 val16;
635 int i;
636
637 printk(BIOS_INFO, " Row addr bits : %u\n", dimm->row_bits);
638 printk(BIOS_INFO, " Column addr bits : %u\n", dimm->col_bits);
639 printk(BIOS_INFO, " Number of ranks : %u\n", dimm->ranks);
640 printk(BIOS_INFO, " DIMM Capacity : %u MB\n", dimm->size_mb);
641
642 /* CAS Latencies Supported */
643 val16 = dimm->cas_supported;
644 printk(BIOS_INFO, " CAS latencies :");
645 i = 0;
646 do {
647 if (val16 & 1)
648 printk(BIOS_INFO, " %u", i + 4);
649 i++;
650 val16 >>= 1;
651 } while (val16);
652 printk(BIOS_INFO, "\n");
653
654 print_ns(" tCKmin : ", dimm->tCK);
655 print_ns(" tAAmin : ", dimm->tAA);
656 print_ns(" tWRmin : ", dimm->tWR);
657 print_ns(" tRCDmin : ", dimm->tRCD);
658 print_ns(" tRRDmin : ", dimm->tRRD);
659 print_ns(" tRPmin : ", dimm->tRP);
660 print_ns(" tRASmin : ", dimm->tRAS);
661 print_ns(" tRCmin : ", dimm->tRC);
662 print_ns(" tRFCmin : ", dimm->tRFC);
663 print_ns(" tWTRmin : ", dimm->tWTR);
664 print_ns(" tRTPmin : ", dimm->tRTP);
665 print_ns(" tFAWmin : ", dimm->tFAW);
Dan Elkouby0c024202018-04-13 18:45:02 +0300666 /* Those values are only relevant if an XMP profile sets them */
667 if (dimm->tCWL)
668 print_ns(" tCWLmin : ", dimm->tCWL);
669 if (dimm->tCMD)
670 printk(BIOS_INFO, " tCMDmin : %3u\n",
671 DIV_ROUND_UP(dimm->tCMD, 256));
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500672}
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500673
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500674/*==============================================================================
675 *= DDR3 MRS helpers
676 *----------------------------------------------------------------------------*/
677
678/*
679 * MRS command structure:
680 * cmd[15:0] = Address pins MA[15:0]
681 * cmd[18:16] = Bank address BA[2:0]
682 */
683
684/* Map tWR value to a bitmask of the MR0 cycle */
685static u16 ddr3_twr_to_mr0_map(u8 twr)
686{
687 if ((twr >= 5) && (twr <= 8))
688 return (twr - 4) << 9;
689
690 /*
691 * From 8T onwards, we can only use even values. Round up if we are
692 * given an odd value.
693 */
694 if ((twr >= 9) && (twr <= 14))
695 return ((twr + 1) >> 1) << 9;
696
697 /* tWR == 16T is [000] */
698 return 0;
699}
700
701/* Map the CAS latency to a bitmask for the MR0 cycle */
702static u16 ddr3_cas_to_mr0_map(u8 cas)
703{
704 u16 mask = 0;
705 /* A[6:4] are bits [2:0] of (CAS - 4) */
706 mask = ((cas - 4) & 0x07) << 4;
707
708 /* A2 is the MSB of (CAS - 4) */
709 if ((cas - 4) & (1 << 3))
710 mask |= (1 << 2);
711
712 return mask;
713}
714
715/**
716 * \brief Get command address for a DDR3 MR0 command
717 *
718 * The DDR3 specification only covers odd write_recovery up to 7T. If an odd
719 * write_recovery greater than 7 is specified, it will be rounded up. If a tWR
720 * greater than 8 is specified, it is recommended to explicitly round it up or
721 * down before calling this function.
722 *
723 * write_recovery and cas are given in clock cycles. For example, a CAS of 7T
724 * should be given as 7.
725 *
Martin Roth98b698c2015-01-06 21:02:52 -0700726 * @param precharge_pd
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500727 * @param write_recovery Write recovery latency, tWR in clock cycles.
Martin Roth98b698c2015-01-06 21:02:52 -0700728 * @param dll_reset
729 * @param mode
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500730 * @param cas CAS latency in clock cycles.
Martin Roth98b698c2015-01-06 21:02:52 -0700731 * @param burst_type
732 * @param burst_length
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500733 */
734mrs_cmd_t ddr3_get_mr0(enum ddr3_mr0_precharge precharge_pd,
735 u8 write_recovery,
736 enum ddr3_mr0_dll_reset dll_reset,
737 enum ddr3_mr0_mode mode,
738 u8 cas,
739 enum ddr3_mr0_burst_type burst_type,
740 enum ddr3_mr0_burst_length burst_length)
741{
742 mrs_cmd_t cmd = 0 << 16;
743
744 if (precharge_pd == DDR3_MR0_PRECHARGE_FAST)
745 cmd |= (1 << 12);
746
747 cmd |= ddr3_twr_to_mr0_map(write_recovery);
748
749 if (dll_reset == DDR3_MR0_DLL_RESET_YES)
750 cmd |= (1 << 8);
751
752 if (mode == DDR3_MR0_MODE_TEST)
753 cmd |= (1 << 7);
754
755 cmd |= ddr3_cas_to_mr0_map(cas);
756
757 if (burst_type == DDR3_MR0_BURST_TYPE_INTERLEAVED)
758 cmd |= (1 << 3);
759
760 cmd |= (burst_length & 0x03) << 0;
761
762 return cmd;
763}
764
765static u16 ddr3_rtt_nom_to_mr1_map(enum ddr3_mr1_rtt_nom rtt_nom)
766{
767 u16 mask = 0;
768 /* A9 <-> rtt_nom[2] */
769 if (rtt_nom & (1 << 2))
770 mask |= (1 << 9);
771 /* A6 <-> rtt_nom[1] */
772 if (rtt_nom & (1 << 1))
773 mask |= (1 << 6);
774 /* A2 <-> rtt_nom[0] */
775 if (rtt_nom & (1 << 0))
776 mask |= (1 << 2);
777
778 return mask;
779}
780
781static u16 ddr3_ods_to_mr1_map(enum ddr3_mr1_ods ods)
782{
783 u16 mask = 0;
784 /* A5 <-> ods[1] */
785 if (ods & (1 << 1))
786 mask |= (1 << 5);
787 /* A1 <-> ods[0] */
788 if (ods & (1 << 0))
789 mask |= (1 << 1);
790
791 return mask;
792}
793
794/**
795 * \brief Get command address for a DDR3 MR1 command
796 */
797mrs_cmd_t ddr3_get_mr1(enum ddr3_mr1_qoff qoff,
798 enum ddr3_mr1_tqds tqds,
799 enum ddr3_mr1_rtt_nom rtt_nom,
800 enum ddr3_mr1_write_leveling write_leveling,
801 enum ddr3_mr1_ods ods,
802 enum ddr3_mr1_additive_latency additive_latency,
803 enum ddr3_mr1_dll dll_disable)
804{
805 mrs_cmd_t cmd = 1 << 16;
806
807 if (qoff == DDR3_MR1_QOFF_DISABLE)
808 cmd |= (1 << 12);
809
810 if (tqds == DDR3_MR1_TQDS_ENABLE)
811 cmd |= (1 << 11);
812
813 cmd |= ddr3_rtt_nom_to_mr1_map(rtt_nom);
814
815 if (write_leveling == DDR3_MR1_WRLVL_ENABLE)
816 cmd |= (1 << 7);
817
818 cmd |= ddr3_ods_to_mr1_map(ods);
819
820 cmd |= (additive_latency & 0x03) << 3;
821
822 if (dll_disable == DDR3_MR1_DLL_DISABLE)
823 cmd |= (1 << 0);
824
825 return cmd;
826}
827
828/**
829 * \brief Get command address for a DDR3 MR2 command
830 *
831 * cas_cwl is given in clock cycles. For example, a cas_cwl of 7T should be
832 * given as 7.
833 *
Martin Roth98b698c2015-01-06 21:02:52 -0700834 * @param rtt_wr
835 * @param extended_temp
836 * @param self_refresh
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500837 * @param cas_cwl CAS write latency in clock cycles.
838 */
Martin Roth98b698c2015-01-06 21:02:52 -0700839
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500840mrs_cmd_t ddr3_get_mr2(enum ddr3_mr2_rttwr rtt_wr,
841 enum ddr3_mr2_srt_range extended_temp,
842 enum ddr3_mr2_asr self_refresh, u8 cas_cwl)
843{
844 mrs_cmd_t cmd = 2 << 16;
845
846 cmd |= (rtt_wr & 0x03) << 9;
847
848 if (extended_temp == DDR3_MR2_SRT_EXTENDED)
849 cmd |= (1 << 7);
850
851 if (self_refresh == DDR3_MR2_ASR_AUTO)
852 cmd |= (1 << 6);
853
854 cmd |= ((cas_cwl - 5) & 0x07) << 3;
855
856 return cmd;
857}
858
859/**
860 * \brief Get command address for a DDR3 MR3 command
861 *
862 * @param dataflow_from_mpr Specify a non-zero value to put DRAM in read
863 * leveling mode. Zero for normal operation.
864 */
865mrs_cmd_t ddr3_get_mr3(char dataflow_from_mpr)
866{
867 mrs_cmd_t cmd = 3 << 16;
868
869 if (dataflow_from_mpr)
870 cmd |= (1 << 2);
871
872 return cmd;
873}
874
875/**
876 * \brief Mirror the address bits for this MRS command
877 *
878 * Swap the following bits in the MRS command:
879 * - MA3 <-> MA4
880 * - MA5 <-> MA6
881 * - MA7 <-> MA8
882 * - BA0 <-> BA1
883 */
884mrs_cmd_t ddr3_mrs_mirror_pins(mrs_cmd_t cmd)
885{
886 u32 downshift, upshift;
887 /* High bits= A4 | A6 | A8 | BA1 */
888 /* Low bits = A3 | A5 | A7 | BA0 */
889 u32 lowbits = (1 << 3) | (1 << 5) | (1 << 7) | (1 << 16);
890 downshift = (cmd & (lowbits << 1));
891 upshift = (cmd & lowbits);
892 cmd &= ~(lowbits | (lowbits << 1));
893 cmd |= (downshift >> 1) | (upshift << 1);
894 return cmd;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500895}