blob: 3272dac6d7202869ddfc2a417861c5b89b1b161f [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 */
385 reg8 = spd[33];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100386 printram(" Standard SDRAM : %s\n", (reg8 & 0x80) ? "no" : "yes");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500387
388 if (spd[63] & 0x01) {
389 dimm->flags.pins_mirrored = 1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500390 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100391 printram(" Rank1 Address bits : %s\n",
392 (spd[63] & 0x01) ? "mirrored" : "normal");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500393
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200394 dimm->reference_card = spd[62] & 0x1f;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100395 printram(" DIMM Reference card: %c\n", 'A' + dimm->reference_card);
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200396
Patrick Rudolph07691592016-02-29 18:21:00 +0100397 dimm->manufacturer_id = (spd[118] << 8) | spd[117];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100398 printram(" Manufacturer ID : %x\n", dimm->manufacturer_id);
Patrick Rudolph07691592016-02-29 18:21:00 +0100399
400 dimm->part_number[16] = 0;
401 memcpy(dimm->part_number, &spd[128], 16);
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100402 printram(" Part number : %s\n", dimm->part_number);
Patrick Rudolph07691592016-02-29 18:21:00 +0100403
Patrick Rudolph15e64692018-08-17 15:24:56 +0200404 memcpy(dimm->serial, &spd[SPD_DIMM_SERIAL_NUM], SPD_DIMM_SERIAL_LEN);
405
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500406 return ret;
407}
408
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100409/**
410 * \brief Decode the raw SPD XMP data
411 *
412 * Decodes a raw SPD XMP data from a DDR3 DIMM, and organizes it into a
413 * @ref dimm_attr structure. The SPD data must first be read in a contiguous
414 * array, and passed to this function.
415 *
416 * @param dimm pointer to @ref dimm_attr structure where the decoded data is to
417 * be stored
418 * @param spd array of raw data previously read from the SPD.
419 *
420 * @param profile select one of the profiles to load
421 *
422 * @return @ref spd_status enumerator
423 * SPD_STATUS_OK -- decoding was successful
424 * SPD_STATUS_INVALID -- invalid SPD or not a DDR3 SPD
425 * SPD_STATUS_CRC_ERROR -- CRC did not verify
426 * SPD_STATUS_INVALID_FIELD -- A field with an invalid value was
427 * detected.
428 */
429int spd_xmp_decode_ddr3(dimm_attr *dimm,
430 spd_raw_data spd,
431 enum ddr3_xmp_profile profile)
432{
433 int ret;
434 u32 mtb; /* medium time base */
435 u8 *xmp; /* pointer to XMP profile data */
436
437 /* need a valid SPD */
438 ret = spd_decode_ddr3(dimm, spd);
439 if (ret != SPD_STATUS_OK)
440 return ret;
441
442 /* search for magic header */
443 if (spd[176] != 0x0C || spd[177] != 0x4A) {
444 printram("Not a DDR3 XMP profile!\n");
445 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
446 return SPD_STATUS_INVALID;
447 }
448
449 if (profile == DDR3_XMP_PROFILE_1) {
450 if (!(spd[178] & 1)) {
451 printram("Selected XMP profile disabled!\n");
452 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
453 return SPD_STATUS_INVALID;
454 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100455
456 printram(" XMP Profile : 1\n");
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100457 xmp = &spd[185];
458
459 /* Medium Timebase =
460 * Medium Timebase (MTB) Dividend /
461 * Medium Timebase (MTB) Divisor */
462 mtb = (((u32) spd[180]) << 8) / spd[181];
463
464 dimm->dimms_per_channel = ((spd[178] >> 2) & 0x3) + 1;
465 } else {
466 if (!(spd[178] & 2)) {
467 printram("Selected XMP profile disabled!\n");
468 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
469 return SPD_STATUS_INVALID;
470 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100471 printram(" XMP Profile : 2\n");
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100472 xmp = &spd[220];
473
474 /* Medium Timebase =
475 * Medium Timebase (MTB) Dividend /
476 * Medium Timebase (MTB) Divisor */
477 mtb = (((u32) spd[182]) << 8) / spd[183];
478
479 dimm->dimms_per_channel = ((spd[178] >> 4) & 0x3) + 1;
480 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100481
482 printram(" Max DIMMs/channel : %u\n",
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100483 dimm->dimms_per_channel);
484
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100485 printram(" XMP Revision : %u.%u\n", spd[179] >> 4, spd[179] & 0xf);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100486
487 /* calculate voltage in mV */
488 dimm->voltage = (xmp[0] & 1) * 50;
489 dimm->voltage += ((xmp[0] >> 1) & 0xf) * 100;
490 dimm->voltage += ((xmp[0] >> 5) & 0x3) * 1000;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100491
492 printram(" Requested voltage : %u mV\n", dimm->voltage);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100493
494 /* SDRAM Minimum Cycle Time (tCKmin) */
495 dimm->tCK = xmp[1] * mtb;
496 /* CAS Latencies Supported */
Dan Elkouby0c024202018-04-13 18:45:02 +0300497 dimm->cas_supported = ((xmp[4] << 8) + xmp[3]) & 0x7fff;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100498 /* Minimum CAS Latency Time (tAAmin) */
499 dimm->tAA = xmp[2] * mtb;
500 /* Minimum Write Recovery Time (tWRmin) */
501 dimm->tWR = xmp[8] * mtb;
502 /* Minimum RAS# to CAS# Delay Time (tRCDmin) */
503 dimm->tRCD = xmp[7] * mtb;
504 /* Minimum Row Active to Row Active Delay Time (tRRDmin) */
505 dimm->tRRD = xmp[17] * mtb;
506 /* Minimum Row Precharge Delay Time (tRPmin) */
507 dimm->tRP = xmp[6] * mtb;
508 /* Minimum Active to Precharge Delay Time (tRASmin) */
509 dimm->tRAS = (((xmp[9] & 0x0f) << 8) + xmp[10]) * mtb;
510 /* Minimum Active to Active/Refresh Delay Time (tRCmin) */
511 dimm->tRC = (((xmp[9] & 0xf0) << 4) + xmp[11]) * mtb;
512 /* Minimum Refresh Recovery Delay Time (tRFCmin) */
513 dimm->tRFC = ((xmp[15] << 8) + xmp[14]) * mtb;
514 /* Minimum Internal Write to Read Command Delay Time (tWTRmin) */
515 dimm->tWTR = xmp[20] * mtb;
516 /* Minimum Internal Read to Precharge Command Delay Time (tRTPmin) */
517 dimm->tRTP = xmp[16] * mtb;
518 /* Minimum Four Activate Window Delay Time (tFAWmin) */
519 dimm->tFAW = (((xmp[18] & 0x0f) << 8) + xmp[19]) * mtb;
Dan Elkouby0c024202018-04-13 18:45:02 +0300520 /* Minimum CAS Write Latency Time (tCWLmin) */
521 dimm->tCWL = xmp[5] * mtb;
522 /* System CMD Rate Mode */
523 dimm->tCMD = xmp[23] * mtb;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100524
525 return ret;
526}
527
Patrick Rudolph24efe732018-08-19 11:06:06 +0200528
529/**
530 * Fill cbmem with information for SMBIOS type 17.
531 *
532 * @param channel Corresponding channel of provided @info
533 * @param slot Corresponding slot of provided @info
534 * @param selected_freq The actual frequency the DRAM is running on
535 * @param info DIMM parameters read from SPD
536 *
537 * @return CB_SUCCESS if DIMM info was written
538 */
539enum cb_err spd_add_smbios17(const u8 channel, const u8 slot,
540 const u16 selected_freq,
541 const dimm_attr *info)
542{
543 struct memory_info *mem_info;
544 struct dimm_info *dimm;
545
546 /*
547 * Allocate CBMEM area for DIMM information used to populate SMBIOS
548 * table 17
549 */
550 mem_info = cbmem_find(CBMEM_ID_MEMINFO);
551 if (!mem_info) {
552 mem_info = cbmem_add(CBMEM_ID_MEMINFO, sizeof(*mem_info));
553
554 printk(BIOS_DEBUG, "CBMEM entry for DIMM info: 0x%p\n",
555 mem_info);
556 if (!mem_info)
557 return CB_ERR;
558
559 memset(mem_info, 0, sizeof(*mem_info));
560 }
561
Nico Huberbb0ab9e2018-09-13 10:49:54 +0200562 if (mem_info->dimm_cnt >= ARRAY_SIZE(mem_info->dimm)) {
563 printk(BIOS_WARNING, "BUG: Too many DIMM infos for %s.\n",
564 __func__);
565 return CB_ERR;
566 }
567
Patrick Rudolph24efe732018-08-19 11:06:06 +0200568 dimm = &mem_info->dimm[mem_info->dimm_cnt];
569 if (info->size_mb) {
570 dimm->ddr_type = MEMORY_TYPE_DDR3;
571 dimm->ddr_frequency = selected_freq;
572 dimm->dimm_size = info->size_mb;
573 dimm->channel_num = channel;
574 dimm->rank_per_dimm = info->ranks;
575 dimm->dimm_num = slot;
576 memcpy(dimm->module_part_number, info->part_number, 16);
577 dimm->mod_id = info->manufacturer_id;
578
579 switch (info->dimm_type) {
580 case SPD_DIMM_TYPE_SO_DIMM:
581 dimm->mod_type = SPD_SODIMM;
582 break;
583 case SPD_DIMM_TYPE_72B_SO_CDIMM:
584 dimm->mod_type = SPD_72B_SO_CDIMM;
585 break;
586 case SPD_DIMM_TYPE_72B_SO_RDIMM:
587 dimm->mod_type = SPD_72B_SO_RDIMM;
588 break;
589 case SPD_DIMM_TYPE_UDIMM:
590 dimm->mod_type = SPD_UDIMM;
591 break;
592 case SPD_DIMM_TYPE_RDIMM:
593 dimm->mod_type = SPD_RDIMM;
594 break;
595 case SPD_DIMM_TYPE_UNDEFINED:
596 default:
597 dimm->mod_type = SPD_UNDEFINED;
598 break;
599 }
600
601 dimm->bus_width = MEMORY_BUS_WIDTH_64; // non-ECC only
602 memcpy(dimm->serial, info->serial,
603 MIN(sizeof(dimm->serial), sizeof(info->serial)));
604 mem_info->dimm_cnt++;
605 }
606
607 return CB_SUCCESS;
608}
609
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500610/*
611 * The information printed below has a more informational character, and is not
612 * necessarily tied in to RAM init debugging. Hence, we stop using printram(),
613 * and use the standard printk()'s below.
614 */
615
616static void print_ns(const char *msg, u32 val)
617{
618 u32 mant, fp;
619 mant = val / 256;
620 fp = (val % 256) * 1000 / 256;
621
622 printk(BIOS_INFO, "%s%3u.%.3u ns\n", msg, mant, fp);
623}
624
625/**
626* \brief Print the info in DIMM
627*
628* Print info about the DIMM. Useful to use when CONFIG_DEBUG_RAM_SETUP is
629* selected, or for a purely informative output.
630*
Martin Roth63373ed2013-07-08 16:24:19 -0600631* @param dimm pointer to already decoded @ref dimm_attr structure
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500632*/
633void dram_print_spd_ddr3(const dimm_attr * dimm)
634{
635 u16 val16;
636 int i;
637
638 printk(BIOS_INFO, " Row addr bits : %u\n", dimm->row_bits);
639 printk(BIOS_INFO, " Column addr bits : %u\n", dimm->col_bits);
640 printk(BIOS_INFO, " Number of ranks : %u\n", dimm->ranks);
641 printk(BIOS_INFO, " DIMM Capacity : %u MB\n", dimm->size_mb);
642
643 /* CAS Latencies Supported */
644 val16 = dimm->cas_supported;
645 printk(BIOS_INFO, " CAS latencies :");
646 i = 0;
647 do {
648 if (val16 & 1)
649 printk(BIOS_INFO, " %u", i + 4);
650 i++;
651 val16 >>= 1;
652 } while (val16);
653 printk(BIOS_INFO, "\n");
654
655 print_ns(" tCKmin : ", dimm->tCK);
656 print_ns(" tAAmin : ", dimm->tAA);
657 print_ns(" tWRmin : ", dimm->tWR);
658 print_ns(" tRCDmin : ", dimm->tRCD);
659 print_ns(" tRRDmin : ", dimm->tRRD);
660 print_ns(" tRPmin : ", dimm->tRP);
661 print_ns(" tRASmin : ", dimm->tRAS);
662 print_ns(" tRCmin : ", dimm->tRC);
663 print_ns(" tRFCmin : ", dimm->tRFC);
664 print_ns(" tWTRmin : ", dimm->tWTR);
665 print_ns(" tRTPmin : ", dimm->tRTP);
666 print_ns(" tFAWmin : ", dimm->tFAW);
Dan Elkouby0c024202018-04-13 18:45:02 +0300667 /* Those values are only relevant if an XMP profile sets them */
668 if (dimm->tCWL)
669 print_ns(" tCWLmin : ", dimm->tCWL);
670 if (dimm->tCMD)
671 printk(BIOS_INFO, " tCMDmin : %3u\n",
672 DIV_ROUND_UP(dimm->tCMD, 256));
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500673}
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500674
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500675/*==============================================================================
676 *= DDR3 MRS helpers
677 *----------------------------------------------------------------------------*/
678
679/*
680 * MRS command structure:
681 * cmd[15:0] = Address pins MA[15:0]
682 * cmd[18:16] = Bank address BA[2:0]
683 */
684
685/* Map tWR value to a bitmask of the MR0 cycle */
686static u16 ddr3_twr_to_mr0_map(u8 twr)
687{
688 if ((twr >= 5) && (twr <= 8))
689 return (twr - 4) << 9;
690
691 /*
692 * From 8T onwards, we can only use even values. Round up if we are
693 * given an odd value.
694 */
695 if ((twr >= 9) && (twr <= 14))
696 return ((twr + 1) >> 1) << 9;
697
698 /* tWR == 16T is [000] */
699 return 0;
700}
701
702/* Map the CAS latency to a bitmask for the MR0 cycle */
703static u16 ddr3_cas_to_mr0_map(u8 cas)
704{
705 u16 mask = 0;
706 /* A[6:4] are bits [2:0] of (CAS - 4) */
707 mask = ((cas - 4) & 0x07) << 4;
708
709 /* A2 is the MSB of (CAS - 4) */
710 if ((cas - 4) & (1 << 3))
711 mask |= (1 << 2);
712
713 return mask;
714}
715
716/**
717 * \brief Get command address for a DDR3 MR0 command
718 *
719 * The DDR3 specification only covers odd write_recovery up to 7T. If an odd
720 * write_recovery greater than 7 is specified, it will be rounded up. If a tWR
721 * greater than 8 is specified, it is recommended to explicitly round it up or
722 * down before calling this function.
723 *
724 * write_recovery and cas are given in clock cycles. For example, a CAS of 7T
725 * should be given as 7.
726 *
Martin Roth98b698c2015-01-06 21:02:52 -0700727 * @param precharge_pd
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500728 * @param write_recovery Write recovery latency, tWR in clock cycles.
Martin Roth98b698c2015-01-06 21:02:52 -0700729 * @param dll_reset
730 * @param mode
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500731 * @param cas CAS latency in clock cycles.
Martin Roth98b698c2015-01-06 21:02:52 -0700732 * @param burst_type
733 * @param burst_length
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500734 */
735mrs_cmd_t ddr3_get_mr0(enum ddr3_mr0_precharge precharge_pd,
736 u8 write_recovery,
737 enum ddr3_mr0_dll_reset dll_reset,
738 enum ddr3_mr0_mode mode,
739 u8 cas,
740 enum ddr3_mr0_burst_type burst_type,
741 enum ddr3_mr0_burst_length burst_length)
742{
743 mrs_cmd_t cmd = 0 << 16;
744
745 if (precharge_pd == DDR3_MR0_PRECHARGE_FAST)
746 cmd |= (1 << 12);
747
748 cmd |= ddr3_twr_to_mr0_map(write_recovery);
749
750 if (dll_reset == DDR3_MR0_DLL_RESET_YES)
751 cmd |= (1 << 8);
752
753 if (mode == DDR3_MR0_MODE_TEST)
754 cmd |= (1 << 7);
755
756 cmd |= ddr3_cas_to_mr0_map(cas);
757
758 if (burst_type == DDR3_MR0_BURST_TYPE_INTERLEAVED)
759 cmd |= (1 << 3);
760
761 cmd |= (burst_length & 0x03) << 0;
762
763 return cmd;
764}
765
766static u16 ddr3_rtt_nom_to_mr1_map(enum ddr3_mr1_rtt_nom rtt_nom)
767{
768 u16 mask = 0;
769 /* A9 <-> rtt_nom[2] */
770 if (rtt_nom & (1 << 2))
771 mask |= (1 << 9);
772 /* A6 <-> rtt_nom[1] */
773 if (rtt_nom & (1 << 1))
774 mask |= (1 << 6);
775 /* A2 <-> rtt_nom[0] */
776 if (rtt_nom & (1 << 0))
777 mask |= (1 << 2);
778
779 return mask;
780}
781
782static u16 ddr3_ods_to_mr1_map(enum ddr3_mr1_ods ods)
783{
784 u16 mask = 0;
785 /* A5 <-> ods[1] */
786 if (ods & (1 << 1))
787 mask |= (1 << 5);
788 /* A1 <-> ods[0] */
789 if (ods & (1 << 0))
790 mask |= (1 << 1);
791
792 return mask;
793}
794
795/**
796 * \brief Get command address for a DDR3 MR1 command
797 */
798mrs_cmd_t ddr3_get_mr1(enum ddr3_mr1_qoff qoff,
799 enum ddr3_mr1_tqds tqds,
800 enum ddr3_mr1_rtt_nom rtt_nom,
801 enum ddr3_mr1_write_leveling write_leveling,
802 enum ddr3_mr1_ods ods,
803 enum ddr3_mr1_additive_latency additive_latency,
804 enum ddr3_mr1_dll dll_disable)
805{
806 mrs_cmd_t cmd = 1 << 16;
807
808 if (qoff == DDR3_MR1_QOFF_DISABLE)
809 cmd |= (1 << 12);
810
811 if (tqds == DDR3_MR1_TQDS_ENABLE)
812 cmd |= (1 << 11);
813
814 cmd |= ddr3_rtt_nom_to_mr1_map(rtt_nom);
815
816 if (write_leveling == DDR3_MR1_WRLVL_ENABLE)
817 cmd |= (1 << 7);
818
819 cmd |= ddr3_ods_to_mr1_map(ods);
820
821 cmd |= (additive_latency & 0x03) << 3;
822
823 if (dll_disable == DDR3_MR1_DLL_DISABLE)
824 cmd |= (1 << 0);
825
826 return cmd;
827}
828
829/**
830 * \brief Get command address for a DDR3 MR2 command
831 *
832 * cas_cwl is given in clock cycles. For example, a cas_cwl of 7T should be
833 * given as 7.
834 *
Martin Roth98b698c2015-01-06 21:02:52 -0700835 * @param rtt_wr
836 * @param extended_temp
837 * @param self_refresh
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500838 * @param cas_cwl CAS write latency in clock cycles.
839 */
Martin Roth98b698c2015-01-06 21:02:52 -0700840
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500841mrs_cmd_t ddr3_get_mr2(enum ddr3_mr2_rttwr rtt_wr,
842 enum ddr3_mr2_srt_range extended_temp,
843 enum ddr3_mr2_asr self_refresh, u8 cas_cwl)
844{
845 mrs_cmd_t cmd = 2 << 16;
846
847 cmd |= (rtt_wr & 0x03) << 9;
848
849 if (extended_temp == DDR3_MR2_SRT_EXTENDED)
850 cmd |= (1 << 7);
851
852 if (self_refresh == DDR3_MR2_ASR_AUTO)
853 cmd |= (1 << 6);
854
855 cmd |= ((cas_cwl - 5) & 0x07) << 3;
856
857 return cmd;
858}
859
860/**
861 * \brief Get command address for a DDR3 MR3 command
862 *
863 * @param dataflow_from_mpr Specify a non-zero value to put DRAM in read
864 * leveling mode. Zero for normal operation.
865 */
866mrs_cmd_t ddr3_get_mr3(char dataflow_from_mpr)
867{
868 mrs_cmd_t cmd = 3 << 16;
869
870 if (dataflow_from_mpr)
871 cmd |= (1 << 2);
872
873 return cmd;
874}
875
876/**
877 * \brief Mirror the address bits for this MRS command
878 *
879 * Swap the following bits in the MRS command:
880 * - MA3 <-> MA4
881 * - MA5 <-> MA6
882 * - MA7 <-> MA8
883 * - BA0 <-> BA1
884 */
885mrs_cmd_t ddr3_mrs_mirror_pins(mrs_cmd_t cmd)
886{
887 u32 downshift, upshift;
888 /* High bits= A4 | A6 | A8 | BA1 */
889 /* Low bits = A3 | A5 | A7 | BA0 */
890 u32 lowbits = (1 << 3) | (1 << 5) | (1 << 7) | (1 << 16);
891 downshift = (cmd & (lowbits << 1));
892 upshift = (cmd & lowbits);
893 cmd &= ~(lowbits | (lowbits << 1));
894 cmd |= (downshift >> 1) | (upshift << 1);
895 return cmd;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500896}