blob: a084ca06e0e215c8e3d4c1b1eb5ddaeaf0c94b67 [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>
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050027
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -050028/*==============================================================================
29 * = DDR3 SPD decoding helpers
30 *----------------------------------------------------------------------------*/
31
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050032/**
33 * \brief Checks if the DIMM is Registered based on byte[3] of the SPD
34 *
35 * Tells if the DIMM type is registered or not.
36 *
37 * @param type DIMM type. This is byte[3] of the SPD.
38 */
Patrick Rudolph6e53ae62017-01-31 19:43:17 +010039int spd_dimm_is_registered_ddr3(enum spd_dimm_type type)
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050040{
41 if ((type == SPD_DIMM_TYPE_RDIMM)
42 | (type == SPD_DIMM_TYPE_MINI_RDIMM)
43 | (type == SPD_DIMM_TYPE_72B_SO_RDIMM))
44 return 1;
45
46 return 0;
47}
48
Arthur Heymans97b337b2018-01-22 01:26:53 +010049u16 ddr3_crc16(const u8 *ptr, int n_crc)
Kyösti Mälkki7dc4b842016-11-18 18:41:17 +020050{
51 int i;
52 u16 crc = 0;
53
54 while (--n_crc >= 0) {
Kyösti Mälkki378d79e2016-11-21 02:39:59 +020055 crc = crc ^ ((int)*ptr++ << 8);
Kyösti Mälkki7dc4b842016-11-18 18:41:17 +020056 for (i = 0; i < 8; ++i)
57 if (crc & 0x8000) {
58 crc = (crc << 1) ^ 0x1021;
59 } else {
60 crc = crc << 1;
61 }
62 }
63
64 return crc;
65}
66
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050067/**
Alexandru Gagniuc4c37e582013-12-17 13:08:01 -050068 * \brief Calculate the CRC of a DDR3 SPD
69 *
70 * @param spd pointer to raw SPD data
71 * @param len length of data in SPD
72 *
73 * @return the CRC of the SPD data, or 0 when spd data is truncated.
74 */
75u16 spd_ddr3_calc_crc(u8 *spd, int len)
76{
Kyösti Mälkki7dc4b842016-11-18 18:41:17 +020077 int n_crc;
Alexandru Gagniuc4c37e582013-12-17 13:08:01 -050078
79 /* Find the number of bytes covered by CRC */
80 if (spd[0] & 0x80) {
81 n_crc = 117;
82 } else {
83 n_crc = 126;
84 }
85
86 if (len < n_crc)
87 /* Not enough bytes available to get the CRC */
88 return 0;
89
Arthur Heymans97b337b2018-01-22 01:26:53 +010090 return ddr3_crc16(spd, n_crc);
Kyösti Mälkki7dc4b842016-11-18 18:41:17 +020091}
92
93/**
94 * \brief Calculate the CRC of a DDR3 SPD unique identifier
95 *
96 * @param spd pointer to raw SPD data
97 * @param len length of data in SPD
98 *
99 * @return the CRC of SPD data bytes 117..127, or 0 when spd data is truncated.
100 */
101u16 spd_ddr3_calc_unique_crc(u8 *spd, int len)
102{
103 if (len < (117 + 11))
104 /* Not enough bytes available to get the CRC */
105 return 0;
106
Arthur Heymans97b337b2018-01-22 01:26:53 +0100107 return ddr3_crc16(&spd[117], 11);
Alexandru Gagniuc4c37e582013-12-17 13:08:01 -0500108}
109
110/**
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500111 * \brief Decode the raw SPD data
112 *
113 * Decodes a raw SPD data from a DDR3 DIMM, and organizes it into a
114 * @ref dimm_attr structure. The SPD data must first be read in a contiguous
115 * array, and passed to this function.
116 *
Martin Roth63373ed2013-07-08 16:24:19 -0600117 * @param dimm pointer to @ref dimm_attr structure where the decoded data is to
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200118 * be stored
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500119 * @param spd array of raw data previously read from the SPD.
120 *
121 * @return @ref spd_status enumerator
122 * SPD_STATUS_OK -- decoding was successful
123 * SPD_STATUS_INVALID -- invalid SPD or not a DDR3 SPD
124 * SPD_STATUS_CRC_ERROR -- CRC did not verify
125 * SPD_STATUS_INVALID_FIELD -- A field with an invalid value was
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200126 * detected.
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500127 */
128int spd_decode_ddr3(dimm_attr * dimm, spd_raw_data spd)
129{
Alexandru Gagniuc4c37e582013-12-17 13:08:01 -0500130 int ret;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500131 u16 crc, spd_crc;
Nicola Corna76f8dbc2016-11-16 08:57:15 +0100132 u8 capacity_shift, bus_width;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500133 u8 reg8;
134 u32 mtb; /* medium time base */
Nicola Corna76f8dbc2016-11-16 08:57:15 +0100135 u32 ftb; /* fine time base */
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500136 unsigned int val, param;
137
138 ret = SPD_STATUS_OK;
139
140 /* Don't assume we memset 0 dimm struct. Clear all our flags */
141 dimm->flags.raw = 0;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100142 dimm->dimms_per_channel = 3;
143
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500144 /* Make sure that the SPD dump is indeed from a DDR3 module */
145 if (spd[2] != SPD_MEMORY_TYPE_SDRAM_DDR3) {
146 printram("Not a DDR3 SPD!\n");
147 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
148 return SPD_STATUS_INVALID;
149 }
150 dimm->dram_type = SPD_MEMORY_TYPE_SDRAM_DDR3;
Vladimir Serbinenko0e675f72014-12-07 13:56:48 +0100151 dimm->dimm_type = spd[3] & 0xf;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500152
Patrick Rudolph8c639352015-06-22 19:32:53 +0200153 crc = spd_ddr3_calc_crc(spd, sizeof(spd_raw_data));
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500154 /* Compare with the CRC in the SPD */
155 spd_crc = (spd[127] << 8) + spd[126];
156 /* Verify the CRC is correct */
157 if (crc != spd_crc) {
Patrick Rudolph78c6e3e2015-06-22 19:46:34 +0200158 printram("ERROR: SPD CRC failed!!!\n");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500159 ret = SPD_STATUS_CRC_ERROR;
160 };
161
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100162 printram(" Revision : %x\n", spd[1]);
163 printram(" Type : %x\n", spd[2]);
164 printram(" Key : %x\n", spd[3]);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500165
166 reg8 = spd[4];
167 /* Number of memory banks */
168 val = (reg8 >> 4) & 0x07;
169 if (val > 0x03) {
170 printram(" Invalid number of memory banks\n");
171 ret = SPD_STATUS_INVALID_FIELD;
172 }
173 param = 1 << (val + 3);
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100174 printram(" Banks : %u\n", param);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500175 /* SDRAM capacity */
176 capacity_shift = reg8 & 0x0f;
177 if (capacity_shift > 0x06) {
178 printram(" Invalid module capacity\n");
179 ret = SPD_STATUS_INVALID_FIELD;
180 }
181 if (capacity_shift < 0x02) {
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100182 printram(" Capacity : %u Mb\n", 256 << capacity_shift);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500183 } else {
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100184 printram(" Capacity : %u Gb\n", 1 << (capacity_shift - 2));
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500185 }
186
187 reg8 = spd[5];
188 /* Row address bits */
189 val = (reg8 >> 3) & 0x07;
190 if (val > 0x04) {
191 printram(" Invalid row address bits\n");
192 ret = SPD_STATUS_INVALID_FIELD;
193 }
194 dimm->row_bits = val + 12;
195 /* Column address bits */
196 val = reg8 & 0x07;
197 if (val > 0x03) {
198 printram(" Invalid column address bits\n");
199 ret = SPD_STATUS_INVALID_FIELD;
200 }
201 dimm->col_bits = val + 9;
202
203 /* Module nominal voltage */
204 reg8 = spd[6];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100205 printram(" Supported voltages :");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500206 if (reg8 & (1 << 2)) {
207 dimm->flags.operable_1_25V = 1;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100208 dimm->voltage = 1250;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500209 printram(" 1.25V");
210 }
211 if (reg8 & (1 << 1)) {
212 dimm->flags.operable_1_35V = 1;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100213 dimm->voltage = 1300;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500214 printram(" 1.35V");
215 }
216 if (!(reg8 & (1 << 0))) {
217 dimm->flags.operable_1_50V = 1;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100218 dimm->voltage = 1500;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500219 printram(" 1.5V");
220 }
221 printram("\n");
222
223 /* Module organization */
224 reg8 = spd[7];
225 /* Number of ranks */
226 val = (reg8 >> 3) & 0x07;
227 if (val > 3) {
228 printram(" Invalid number of ranks\n");
229 ret = SPD_STATUS_INVALID_FIELD;
230 }
231 dimm->ranks = val + 1;
232 /* SDRAM device width */
233 val = (reg8 & 0x07);
234 if (val > 3) {
235 printram(" Invalid SDRAM width\n");
236 ret = SPD_STATUS_INVALID_FIELD;
237 }
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200238 dimm->width = (4 << val);
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100239 printram(" SDRAM width : %u\n", dimm->width);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500240
241 /* Memory bus width */
242 reg8 = spd[8];
243 /* Bus extension */
244 val = (reg8 >> 3) & 0x03;
245 if (val > 1) {
246 printram(" Invalid bus extension\n");
247 ret = SPD_STATUS_INVALID_FIELD;
248 }
249 dimm->flags.is_ecc = val ? 1 : 0;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100250 printram(" Bus extension : %u bits\n", val ? 8 : 0);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500251 /* Bus width */
252 val = reg8 & 0x07;
253 if (val > 3) {
254 printram(" Invalid bus width\n");
255 ret = SPD_STATUS_INVALID_FIELD;
256 }
257 bus_width = 8 << val;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100258 printram(" Bus width : %u\n", bus_width);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500259
260 /* We have all the info we need to compute the dimm size */
261 /* Capacity is 256Mbit multiplied by the power of 2 specified in
262 * capacity_shift
263 * The rest is the JEDEC formula */
264 dimm->size_mb = ((1 << (capacity_shift + (25 - 20))) * bus_width
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200265 * dimm->ranks) / dimm->width;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500266
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500267 /* Medium Timebase =
268 * Medium Timebase (MTB) Dividend /
269 * Medium Timebase (MTB) Divisor */
270 mtb = (((u32) spd[10]) << 8) / spd[11];
271
272 /* SDRAM Minimum Cycle Time (tCKmin) */
273 dimm->tCK = spd[12] * mtb;
274 /* CAS Latencies Supported */
275 dimm->cas_supported = (spd[15] << 8) + spd[14];
276 /* Minimum CAS Latency Time (tAAmin) */
277 dimm->tAA = spd[16] * mtb;
278 /* Minimum Write Recovery Time (tWRmin) */
279 dimm->tWR = spd[17] * mtb;
280 /* Minimum RAS# to CAS# Delay Time (tRCDmin) */
281 dimm->tRCD = spd[18] * mtb;
282 /* Minimum Row Active to Row Active Delay Time (tRRDmin) */
283 dimm->tRRD = spd[19] * mtb;
284 /* Minimum Row Precharge Delay Time (tRPmin) */
285 dimm->tRP = spd[20] * mtb;
286 /* Minimum Active to Precharge Delay Time (tRASmin) */
287 dimm->tRAS = (((spd[21] & 0x0f) << 8) + spd[22]) * mtb;
288 /* Minimum Active to Active/Refresh Delay Time (tRCmin) */
289 dimm->tRC = (((spd[21] & 0xf0) << 4) + spd[23]) * mtb;
290 /* Minimum Refresh Recovery Delay Time (tRFCmin) */
291 dimm->tRFC = ((spd[25] << 8) + spd[24]) * mtb;
292 /* Minimum Internal Write to Read Command Delay Time (tWTRmin) */
293 dimm->tWTR = spd[26] * mtb;
294 /* Minimum Internal Read to Precharge Command Delay Time (tRTPmin) */
295 dimm->tRTP = spd[27] * mtb;
296 /* Minimum Four Activate Window Delay Time (tFAWmin) */
297 dimm->tFAW = (((spd[28] & 0x0f) << 8) + spd[29]) * mtb;
Dan Elkouby0c024202018-04-13 18:45:02 +0300298 /* Minimum CAS Write Latency Time (tCWLmin)
299 * - not present in standard SPD */
300 dimm->tCWL = 0;
301 /* System CMD Rate Mode - not present in standard SPD */
302 dimm->tCMD = 0;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500303
Nicola Corna76f8dbc2016-11-16 08:57:15 +0100304 printram(" FTB timings :");
305 /* FTB is introduced in SPD revision 1.1 */
306 if (spd[1] >= 0x11 && spd[9] & 0x0f) {
307 printram(" yes\n");
308
309 /* Fine timebase (1/256 ps) =
310 * Fine Timebase (FTB) Dividend /
311 * Fine Timebase (FTB) Divisor */
312 ftb = (((u16) spd[9] & 0xf0) << 4) / (spd[9] & 0x0f);
313
314 /* SPD recommends to round up the MTB part and use a negative
315 * FTB, so a negative rounding should be always safe */
316
317 /* SDRAM Minimum Cycle Time (tCKmin) correction */
318 dimm->tCK += (s32)((s8) spd[34] * ftb - 500) / 1000;
319 /* Minimum CAS Latency Time (tAAmin) correction */
320 dimm->tAA += (s32)((s8) spd[35] * ftb - 500) / 1000;
321 /* Minimum RAS# to CAS# Delay Time (tRCDmin) correction */
322 dimm->tRCD += (s32)((s8) spd[36] * ftb - 500) / 1000;
323 /* Minimum Row Precharge Delay Time (tRPmin) correction */
324 dimm->tRP += (s32)((s8) spd[37] * ftb - 500) / 1000;
325 /* Minimum Active to Active/Refresh Delay Time (tRCmin) corr. */
326 dimm->tRC += (s32)((s8) spd[38] * ftb - 500) / 1000;
327 }
328 else {
329 printram(" no\n");
330 }
331
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500332 /* SDRAM Optional Features */
333 reg8 = spd[30];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100334 printram(" Optional features :");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500335 if (reg8 & 0x80) {
336 dimm->flags.dll_off_mode = 1;
337 printram(" DLL-Off_mode");
338 }
339 if (reg8 & 0x02) {
340 dimm->flags.rzq7_supported = 1;
341 printram(" RZQ/7");
342 }
343 if (reg8 & 0x01) {
344 dimm->flags.rzq6_supported = 1;
345 printram(" RZQ/6");
346 }
347 printram("\n");
348
349 /* SDRAM Thermal and Refresh Options */
350 reg8 = spd[31];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100351 printram(" Thermal features :");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500352 if (reg8 & 0x80) {
353 dimm->flags.pasr = 1;
354 printram(" PASR");
355 }
356 if (reg8 & 0x08) {
357 dimm->flags.odts = 1;
358 printram(" ODTS");
359 }
360 if (reg8 & 0x04) {
361 dimm->flags.asr = 1;
362 printram(" ASR");
363 }
364 if (reg8 & 0x02) {
365 dimm->flags.ext_temp_range = 1;
366 printram(" ext_temp_refresh");
367 }
368 if (reg8 & 0x01) {
369 dimm->flags.ext_temp_refresh = 1;
370 printram(" ext_temp_range");
371 }
372 printram("\n");
373
374 /* Module Thermal Sensor */
375 reg8 = spd[32];
376 if (reg8 & 0x80)
377 dimm->flags.therm_sensor = 1;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100378 printram(" Thermal sensor : %s\n",
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500379 dimm->flags.therm_sensor ? "yes" : "no");
380
381 /* SDRAM Device Type */
382 reg8 = spd[33];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100383 printram(" Standard SDRAM : %s\n", (reg8 & 0x80) ? "no" : "yes");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500384
385 if (spd[63] & 0x01) {
386 dimm->flags.pins_mirrored = 1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500387 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100388 printram(" Rank1 Address bits : %s\n",
389 (spd[63] & 0x01) ? "mirrored" : "normal");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500390
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200391 dimm->reference_card = spd[62] & 0x1f;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100392 printram(" DIMM Reference card: %c\n", 'A' + dimm->reference_card);
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200393
Patrick Rudolph07691592016-02-29 18:21:00 +0100394 dimm->manufacturer_id = (spd[118] << 8) | spd[117];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100395 printram(" Manufacturer ID : %x\n", dimm->manufacturer_id);
Patrick Rudolph07691592016-02-29 18:21:00 +0100396
397 dimm->part_number[16] = 0;
398 memcpy(dimm->part_number, &spd[128], 16);
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100399 printram(" Part number : %s\n", dimm->part_number);
Patrick Rudolph07691592016-02-29 18:21:00 +0100400
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500401 return ret;
402}
403
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100404/**
405 * \brief Decode the raw SPD XMP data
406 *
407 * Decodes a raw SPD XMP data from a DDR3 DIMM, and organizes it into a
408 * @ref dimm_attr structure. The SPD data must first be read in a contiguous
409 * array, and passed to this function.
410 *
411 * @param dimm pointer to @ref dimm_attr structure where the decoded data is to
412 * be stored
413 * @param spd array of raw data previously read from the SPD.
414 *
415 * @param profile select one of the profiles to load
416 *
417 * @return @ref spd_status enumerator
418 * SPD_STATUS_OK -- decoding was successful
419 * SPD_STATUS_INVALID -- invalid SPD or not a DDR3 SPD
420 * SPD_STATUS_CRC_ERROR -- CRC did not verify
421 * SPD_STATUS_INVALID_FIELD -- A field with an invalid value was
422 * detected.
423 */
424int spd_xmp_decode_ddr3(dimm_attr *dimm,
425 spd_raw_data spd,
426 enum ddr3_xmp_profile profile)
427{
428 int ret;
429 u32 mtb; /* medium time base */
430 u8 *xmp; /* pointer to XMP profile data */
431
432 /* need a valid SPD */
433 ret = spd_decode_ddr3(dimm, spd);
434 if (ret != SPD_STATUS_OK)
435 return ret;
436
437 /* search for magic header */
438 if (spd[176] != 0x0C || spd[177] != 0x4A) {
439 printram("Not a DDR3 XMP profile!\n");
440 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
441 return SPD_STATUS_INVALID;
442 }
443
444 if (profile == DDR3_XMP_PROFILE_1) {
445 if (!(spd[178] & 1)) {
446 printram("Selected XMP profile disabled!\n");
447 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
448 return SPD_STATUS_INVALID;
449 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100450
451 printram(" XMP Profile : 1\n");
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100452 xmp = &spd[185];
453
454 /* Medium Timebase =
455 * Medium Timebase (MTB) Dividend /
456 * Medium Timebase (MTB) Divisor */
457 mtb = (((u32) spd[180]) << 8) / spd[181];
458
459 dimm->dimms_per_channel = ((spd[178] >> 2) & 0x3) + 1;
460 } else {
461 if (!(spd[178] & 2)) {
462 printram("Selected XMP profile disabled!\n");
463 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
464 return SPD_STATUS_INVALID;
465 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100466 printram(" XMP Profile : 2\n");
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100467 xmp = &spd[220];
468
469 /* Medium Timebase =
470 * Medium Timebase (MTB) Dividend /
471 * Medium Timebase (MTB) Divisor */
472 mtb = (((u32) spd[182]) << 8) / spd[183];
473
474 dimm->dimms_per_channel = ((spd[178] >> 4) & 0x3) + 1;
475 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100476
477 printram(" Max DIMMs/channel : %u\n",
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100478 dimm->dimms_per_channel);
479
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100480 printram(" XMP Revision : %u.%u\n", spd[179] >> 4, spd[179] & 0xf);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100481
482 /* calculate voltage in mV */
483 dimm->voltage = (xmp[0] & 1) * 50;
484 dimm->voltage += ((xmp[0] >> 1) & 0xf) * 100;
485 dimm->voltage += ((xmp[0] >> 5) & 0x3) * 1000;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100486
487 printram(" Requested voltage : %u mV\n", dimm->voltage);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100488
489 /* SDRAM Minimum Cycle Time (tCKmin) */
490 dimm->tCK = xmp[1] * mtb;
491 /* CAS Latencies Supported */
Dan Elkouby0c024202018-04-13 18:45:02 +0300492 dimm->cas_supported = ((xmp[4] << 8) + xmp[3]) & 0x7fff;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100493 /* Minimum CAS Latency Time (tAAmin) */
494 dimm->tAA = xmp[2] * mtb;
495 /* Minimum Write Recovery Time (tWRmin) */
496 dimm->tWR = xmp[8] * mtb;
497 /* Minimum RAS# to CAS# Delay Time (tRCDmin) */
498 dimm->tRCD = xmp[7] * mtb;
499 /* Minimum Row Active to Row Active Delay Time (tRRDmin) */
500 dimm->tRRD = xmp[17] * mtb;
501 /* Minimum Row Precharge Delay Time (tRPmin) */
502 dimm->tRP = xmp[6] * mtb;
503 /* Minimum Active to Precharge Delay Time (tRASmin) */
504 dimm->tRAS = (((xmp[9] & 0x0f) << 8) + xmp[10]) * mtb;
505 /* Minimum Active to Active/Refresh Delay Time (tRCmin) */
506 dimm->tRC = (((xmp[9] & 0xf0) << 4) + xmp[11]) * mtb;
507 /* Minimum Refresh Recovery Delay Time (tRFCmin) */
508 dimm->tRFC = ((xmp[15] << 8) + xmp[14]) * mtb;
509 /* Minimum Internal Write to Read Command Delay Time (tWTRmin) */
510 dimm->tWTR = xmp[20] * mtb;
511 /* Minimum Internal Read to Precharge Command Delay Time (tRTPmin) */
512 dimm->tRTP = xmp[16] * mtb;
513 /* Minimum Four Activate Window Delay Time (tFAWmin) */
514 dimm->tFAW = (((xmp[18] & 0x0f) << 8) + xmp[19]) * mtb;
Dan Elkouby0c024202018-04-13 18:45:02 +0300515 /* Minimum CAS Write Latency Time (tCWLmin) */
516 dimm->tCWL = xmp[5] * mtb;
517 /* System CMD Rate Mode */
518 dimm->tCMD = xmp[23] * mtb;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100519
520 return ret;
521}
522
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500523/*
524 * The information printed below has a more informational character, and is not
525 * necessarily tied in to RAM init debugging. Hence, we stop using printram(),
526 * and use the standard printk()'s below.
527 */
528
529static void print_ns(const char *msg, u32 val)
530{
531 u32 mant, fp;
532 mant = val / 256;
533 fp = (val % 256) * 1000 / 256;
534
535 printk(BIOS_INFO, "%s%3u.%.3u ns\n", msg, mant, fp);
536}
537
538/**
539* \brief Print the info in DIMM
540*
541* Print info about the DIMM. Useful to use when CONFIG_DEBUG_RAM_SETUP is
542* selected, or for a purely informative output.
543*
Martin Roth63373ed2013-07-08 16:24:19 -0600544* @param dimm pointer to already decoded @ref dimm_attr structure
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500545*/
546void dram_print_spd_ddr3(const dimm_attr * dimm)
547{
548 u16 val16;
549 int i;
550
551 printk(BIOS_INFO, " Row addr bits : %u\n", dimm->row_bits);
552 printk(BIOS_INFO, " Column addr bits : %u\n", dimm->col_bits);
553 printk(BIOS_INFO, " Number of ranks : %u\n", dimm->ranks);
554 printk(BIOS_INFO, " DIMM Capacity : %u MB\n", dimm->size_mb);
555
556 /* CAS Latencies Supported */
557 val16 = dimm->cas_supported;
558 printk(BIOS_INFO, " CAS latencies :");
559 i = 0;
560 do {
561 if (val16 & 1)
562 printk(BIOS_INFO, " %u", i + 4);
563 i++;
564 val16 >>= 1;
565 } while (val16);
566 printk(BIOS_INFO, "\n");
567
568 print_ns(" tCKmin : ", dimm->tCK);
569 print_ns(" tAAmin : ", dimm->tAA);
570 print_ns(" tWRmin : ", dimm->tWR);
571 print_ns(" tRCDmin : ", dimm->tRCD);
572 print_ns(" tRRDmin : ", dimm->tRRD);
573 print_ns(" tRPmin : ", dimm->tRP);
574 print_ns(" tRASmin : ", dimm->tRAS);
575 print_ns(" tRCmin : ", dimm->tRC);
576 print_ns(" tRFCmin : ", dimm->tRFC);
577 print_ns(" tWTRmin : ", dimm->tWTR);
578 print_ns(" tRTPmin : ", dimm->tRTP);
579 print_ns(" tFAWmin : ", dimm->tFAW);
Dan Elkouby0c024202018-04-13 18:45:02 +0300580 /* Those values are only relevant if an XMP profile sets them */
581 if (dimm->tCWL)
582 print_ns(" tCWLmin : ", dimm->tCWL);
583 if (dimm->tCMD)
584 printk(BIOS_INFO, " tCMDmin : %3u\n",
585 DIV_ROUND_UP(dimm->tCMD, 256));
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500586}
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500587
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500588/*==============================================================================
589 *= DDR3 MRS helpers
590 *----------------------------------------------------------------------------*/
591
592/*
593 * MRS command structure:
594 * cmd[15:0] = Address pins MA[15:0]
595 * cmd[18:16] = Bank address BA[2:0]
596 */
597
598/* Map tWR value to a bitmask of the MR0 cycle */
599static u16 ddr3_twr_to_mr0_map(u8 twr)
600{
601 if ((twr >= 5) && (twr <= 8))
602 return (twr - 4) << 9;
603
604 /*
605 * From 8T onwards, we can only use even values. Round up if we are
606 * given an odd value.
607 */
608 if ((twr >= 9) && (twr <= 14))
609 return ((twr + 1) >> 1) << 9;
610
611 /* tWR == 16T is [000] */
612 return 0;
613}
614
615/* Map the CAS latency to a bitmask for the MR0 cycle */
616static u16 ddr3_cas_to_mr0_map(u8 cas)
617{
618 u16 mask = 0;
619 /* A[6:4] are bits [2:0] of (CAS - 4) */
620 mask = ((cas - 4) & 0x07) << 4;
621
622 /* A2 is the MSB of (CAS - 4) */
623 if ((cas - 4) & (1 << 3))
624 mask |= (1 << 2);
625
626 return mask;
627}
628
629/**
630 * \brief Get command address for a DDR3 MR0 command
631 *
632 * The DDR3 specification only covers odd write_recovery up to 7T. If an odd
633 * write_recovery greater than 7 is specified, it will be rounded up. If a tWR
634 * greater than 8 is specified, it is recommended to explicitly round it up or
635 * down before calling this function.
636 *
637 * write_recovery and cas are given in clock cycles. For example, a CAS of 7T
638 * should be given as 7.
639 *
Martin Roth98b698c2015-01-06 21:02:52 -0700640 * @param precharge_pd
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500641 * @param write_recovery Write recovery latency, tWR in clock cycles.
Martin Roth98b698c2015-01-06 21:02:52 -0700642 * @param dll_reset
643 * @param mode
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500644 * @param cas CAS latency in clock cycles.
Martin Roth98b698c2015-01-06 21:02:52 -0700645 * @param burst_type
646 * @param burst_length
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500647 */
648mrs_cmd_t ddr3_get_mr0(enum ddr3_mr0_precharge precharge_pd,
649 u8 write_recovery,
650 enum ddr3_mr0_dll_reset dll_reset,
651 enum ddr3_mr0_mode mode,
652 u8 cas,
653 enum ddr3_mr0_burst_type burst_type,
654 enum ddr3_mr0_burst_length burst_length)
655{
656 mrs_cmd_t cmd = 0 << 16;
657
658 if (precharge_pd == DDR3_MR0_PRECHARGE_FAST)
659 cmd |= (1 << 12);
660
661 cmd |= ddr3_twr_to_mr0_map(write_recovery);
662
663 if (dll_reset == DDR3_MR0_DLL_RESET_YES)
664 cmd |= (1 << 8);
665
666 if (mode == DDR3_MR0_MODE_TEST)
667 cmd |= (1 << 7);
668
669 cmd |= ddr3_cas_to_mr0_map(cas);
670
671 if (burst_type == DDR3_MR0_BURST_TYPE_INTERLEAVED)
672 cmd |= (1 << 3);
673
674 cmd |= (burst_length & 0x03) << 0;
675
676 return cmd;
677}
678
679static u16 ddr3_rtt_nom_to_mr1_map(enum ddr3_mr1_rtt_nom rtt_nom)
680{
681 u16 mask = 0;
682 /* A9 <-> rtt_nom[2] */
683 if (rtt_nom & (1 << 2))
684 mask |= (1 << 9);
685 /* A6 <-> rtt_nom[1] */
686 if (rtt_nom & (1 << 1))
687 mask |= (1 << 6);
688 /* A2 <-> rtt_nom[0] */
689 if (rtt_nom & (1 << 0))
690 mask |= (1 << 2);
691
692 return mask;
693}
694
695static u16 ddr3_ods_to_mr1_map(enum ddr3_mr1_ods ods)
696{
697 u16 mask = 0;
698 /* A5 <-> ods[1] */
699 if (ods & (1 << 1))
700 mask |= (1 << 5);
701 /* A1 <-> ods[0] */
702 if (ods & (1 << 0))
703 mask |= (1 << 1);
704
705 return mask;
706}
707
708/**
709 * \brief Get command address for a DDR3 MR1 command
710 */
711mrs_cmd_t ddr3_get_mr1(enum ddr3_mr1_qoff qoff,
712 enum ddr3_mr1_tqds tqds,
713 enum ddr3_mr1_rtt_nom rtt_nom,
714 enum ddr3_mr1_write_leveling write_leveling,
715 enum ddr3_mr1_ods ods,
716 enum ddr3_mr1_additive_latency additive_latency,
717 enum ddr3_mr1_dll dll_disable)
718{
719 mrs_cmd_t cmd = 1 << 16;
720
721 if (qoff == DDR3_MR1_QOFF_DISABLE)
722 cmd |= (1 << 12);
723
724 if (tqds == DDR3_MR1_TQDS_ENABLE)
725 cmd |= (1 << 11);
726
727 cmd |= ddr3_rtt_nom_to_mr1_map(rtt_nom);
728
729 if (write_leveling == DDR3_MR1_WRLVL_ENABLE)
730 cmd |= (1 << 7);
731
732 cmd |= ddr3_ods_to_mr1_map(ods);
733
734 cmd |= (additive_latency & 0x03) << 3;
735
736 if (dll_disable == DDR3_MR1_DLL_DISABLE)
737 cmd |= (1 << 0);
738
739 return cmd;
740}
741
742/**
743 * \brief Get command address for a DDR3 MR2 command
744 *
745 * cas_cwl is given in clock cycles. For example, a cas_cwl of 7T should be
746 * given as 7.
747 *
Martin Roth98b698c2015-01-06 21:02:52 -0700748 * @param rtt_wr
749 * @param extended_temp
750 * @param self_refresh
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500751 * @param cas_cwl CAS write latency in clock cycles.
752 */
Martin Roth98b698c2015-01-06 21:02:52 -0700753
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500754mrs_cmd_t ddr3_get_mr2(enum ddr3_mr2_rttwr rtt_wr,
755 enum ddr3_mr2_srt_range extended_temp,
756 enum ddr3_mr2_asr self_refresh, u8 cas_cwl)
757{
758 mrs_cmd_t cmd = 2 << 16;
759
760 cmd |= (rtt_wr & 0x03) << 9;
761
762 if (extended_temp == DDR3_MR2_SRT_EXTENDED)
763 cmd |= (1 << 7);
764
765 if (self_refresh == DDR3_MR2_ASR_AUTO)
766 cmd |= (1 << 6);
767
768 cmd |= ((cas_cwl - 5) & 0x07) << 3;
769
770 return cmd;
771}
772
773/**
774 * \brief Get command address for a DDR3 MR3 command
775 *
776 * @param dataflow_from_mpr Specify a non-zero value to put DRAM in read
777 * leveling mode. Zero for normal operation.
778 */
779mrs_cmd_t ddr3_get_mr3(char dataflow_from_mpr)
780{
781 mrs_cmd_t cmd = 3 << 16;
782
783 if (dataflow_from_mpr)
784 cmd |= (1 << 2);
785
786 return cmd;
787}
788
789/**
790 * \brief Mirror the address bits for this MRS command
791 *
792 * Swap the following bits in the MRS command:
793 * - MA3 <-> MA4
794 * - MA5 <-> MA6
795 * - MA7 <-> MA8
796 * - BA0 <-> BA1
797 */
798mrs_cmd_t ddr3_mrs_mirror_pins(mrs_cmd_t cmd)
799{
800 u32 downshift, upshift;
801 /* High bits= A4 | A6 | A8 | BA1 */
802 /* Low bits = A3 | A5 | A7 | BA0 */
803 u32 lowbits = (1 << 3) | (1 << 5) | (1 << 7) | (1 << 16);
804 downshift = (cmd & (lowbits << 1));
805 upshift = (cmd & lowbits);
806 cmd &= ~(lowbits | (lowbits << 1));
807 cmd |= (downshift >> 1) | (upshift << 1);
808 return cmd;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500809}