blob: 87aa3c536283daed38b87f22c2088ffd4486d610 [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
Kyösti Mälkki7dc4b842016-11-18 18:41:17 +020049static u16 crc16(const u8 *ptr, int n_crc)
50{
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
Kyösti Mälkki7dc4b842016-11-18 18:41:17 +020090 return crc16(spd, n_crc);
91}
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
107 return 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
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500118 * be stored
119 * @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
126 * detected.
127 */
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;
298
Nicola Corna76f8dbc2016-11-16 08:57:15 +0100299 printram(" FTB timings :");
300 /* FTB is introduced in SPD revision 1.1 */
301 if (spd[1] >= 0x11 && spd[9] & 0x0f) {
302 printram(" yes\n");
303
304 /* Fine timebase (1/256 ps) =
305 * Fine Timebase (FTB) Dividend /
306 * Fine Timebase (FTB) Divisor */
307 ftb = (((u16) spd[9] & 0xf0) << 4) / (spd[9] & 0x0f);
308
309 /* SPD recommends to round up the MTB part and use a negative
310 * FTB, so a negative rounding should be always safe */
311
312 /* SDRAM Minimum Cycle Time (tCKmin) correction */
313 dimm->tCK += (s32)((s8) spd[34] * ftb - 500) / 1000;
314 /* Minimum CAS Latency Time (tAAmin) correction */
315 dimm->tAA += (s32)((s8) spd[35] * ftb - 500) / 1000;
316 /* Minimum RAS# to CAS# Delay Time (tRCDmin) correction */
317 dimm->tRCD += (s32)((s8) spd[36] * ftb - 500) / 1000;
318 /* Minimum Row Precharge Delay Time (tRPmin) correction */
319 dimm->tRP += (s32)((s8) spd[37] * ftb - 500) / 1000;
320 /* Minimum Active to Active/Refresh Delay Time (tRCmin) corr. */
321 dimm->tRC += (s32)((s8) spd[38] * ftb - 500) / 1000;
322 }
323 else {
324 printram(" no\n");
325 }
326
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500327 /* SDRAM Optional Features */
328 reg8 = spd[30];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100329 printram(" Optional features :");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500330 if (reg8 & 0x80) {
331 dimm->flags.dll_off_mode = 1;
332 printram(" DLL-Off_mode");
333 }
334 if (reg8 & 0x02) {
335 dimm->flags.rzq7_supported = 1;
336 printram(" RZQ/7");
337 }
338 if (reg8 & 0x01) {
339 dimm->flags.rzq6_supported = 1;
340 printram(" RZQ/6");
341 }
342 printram("\n");
343
344 /* SDRAM Thermal and Refresh Options */
345 reg8 = spd[31];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100346 printram(" Thermal features :");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500347 if (reg8 & 0x80) {
348 dimm->flags.pasr = 1;
349 printram(" PASR");
350 }
351 if (reg8 & 0x08) {
352 dimm->flags.odts = 1;
353 printram(" ODTS");
354 }
355 if (reg8 & 0x04) {
356 dimm->flags.asr = 1;
357 printram(" ASR");
358 }
359 if (reg8 & 0x02) {
360 dimm->flags.ext_temp_range = 1;
361 printram(" ext_temp_refresh");
362 }
363 if (reg8 & 0x01) {
364 dimm->flags.ext_temp_refresh = 1;
365 printram(" ext_temp_range");
366 }
367 printram("\n");
368
369 /* Module Thermal Sensor */
370 reg8 = spd[32];
371 if (reg8 & 0x80)
372 dimm->flags.therm_sensor = 1;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100373 printram(" Thermal sensor : %s\n",
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500374 dimm->flags.therm_sensor ? "yes" : "no");
375
376 /* SDRAM Device Type */
377 reg8 = spd[33];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100378 printram(" Standard SDRAM : %s\n", (reg8 & 0x80) ? "no" : "yes");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500379
380 if (spd[63] & 0x01) {
381 dimm->flags.pins_mirrored = 1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500382 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100383 printram(" Rank1 Address bits : %s\n",
384 (spd[63] & 0x01) ? "mirrored" : "normal");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500385
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200386 dimm->reference_card = spd[62] & 0x1f;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100387 printram(" DIMM Reference card: %c\n", 'A' + dimm->reference_card);
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200388
Patrick Rudolph07691592016-02-29 18:21:00 +0100389 dimm->manufacturer_id = (spd[118] << 8) | spd[117];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100390 printram(" Manufacturer ID : %x\n", dimm->manufacturer_id);
Patrick Rudolph07691592016-02-29 18:21:00 +0100391
392 dimm->part_number[16] = 0;
393 memcpy(dimm->part_number, &spd[128], 16);
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100394 printram(" Part number : %s\n", dimm->part_number);
Patrick Rudolph07691592016-02-29 18:21:00 +0100395
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500396 return ret;
397}
398
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100399/**
400 * \brief Decode the raw SPD XMP data
401 *
402 * Decodes a raw SPD XMP data from a DDR3 DIMM, and organizes it into a
403 * @ref dimm_attr structure. The SPD data must first be read in a contiguous
404 * array, and passed to this function.
405 *
406 * @param dimm pointer to @ref dimm_attr structure where the decoded data is to
407 * be stored
408 * @param spd array of raw data previously read from the SPD.
409 *
410 * @param profile select one of the profiles to load
411 *
412 * @return @ref spd_status enumerator
413 * SPD_STATUS_OK -- decoding was successful
414 * SPD_STATUS_INVALID -- invalid SPD or not a DDR3 SPD
415 * SPD_STATUS_CRC_ERROR -- CRC did not verify
416 * SPD_STATUS_INVALID_FIELD -- A field with an invalid value was
417 * detected.
418 */
419int spd_xmp_decode_ddr3(dimm_attr *dimm,
420 spd_raw_data spd,
421 enum ddr3_xmp_profile profile)
422{
423 int ret;
424 u32 mtb; /* medium time base */
425 u8 *xmp; /* pointer to XMP profile data */
426
427 /* need a valid SPD */
428 ret = spd_decode_ddr3(dimm, spd);
429 if (ret != SPD_STATUS_OK)
430 return ret;
431
432 /* search for magic header */
433 if (spd[176] != 0x0C || spd[177] != 0x4A) {
434 printram("Not a DDR3 XMP profile!\n");
435 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
436 return SPD_STATUS_INVALID;
437 }
438
439 if (profile == DDR3_XMP_PROFILE_1) {
440 if (!(spd[178] & 1)) {
441 printram("Selected XMP profile disabled!\n");
442 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
443 return SPD_STATUS_INVALID;
444 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100445
446 printram(" XMP Profile : 1\n");
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100447 xmp = &spd[185];
448
449 /* Medium Timebase =
450 * Medium Timebase (MTB) Dividend /
451 * Medium Timebase (MTB) Divisor */
452 mtb = (((u32) spd[180]) << 8) / spd[181];
453
454 dimm->dimms_per_channel = ((spd[178] >> 2) & 0x3) + 1;
455 } else {
456 if (!(spd[178] & 2)) {
457 printram("Selected XMP profile disabled!\n");
458 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
459 return SPD_STATUS_INVALID;
460 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100461 printram(" XMP Profile : 2\n");
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100462 xmp = &spd[220];
463
464 /* Medium Timebase =
465 * Medium Timebase (MTB) Dividend /
466 * Medium Timebase (MTB) Divisor */
467 mtb = (((u32) spd[182]) << 8) / spd[183];
468
469 dimm->dimms_per_channel = ((spd[178] >> 4) & 0x3) + 1;
470 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100471
472 printram(" Max DIMMs/channel : %u\n",
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100473 dimm->dimms_per_channel);
474
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100475 printram(" XMP Revision : %u.%u\n", spd[179] >> 4, spd[179] & 0xf);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100476
477 /* calculate voltage in mV */
478 dimm->voltage = (xmp[0] & 1) * 50;
479 dimm->voltage += ((xmp[0] >> 1) & 0xf) * 100;
480 dimm->voltage += ((xmp[0] >> 5) & 0x3) * 1000;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100481
482 printram(" Requested voltage : %u mV\n", dimm->voltage);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100483
484 /* SDRAM Minimum Cycle Time (tCKmin) */
485 dimm->tCK = xmp[1] * mtb;
486 /* CAS Latencies Supported */
487 dimm->cas_supported = (xmp[9] << 8) + xmp[8];
488 /* Minimum CAS Latency Time (tAAmin) */
489 dimm->tAA = xmp[2] * mtb;
490 /* Minimum Write Recovery Time (tWRmin) */
491 dimm->tWR = xmp[8] * mtb;
492 /* Minimum RAS# to CAS# Delay Time (tRCDmin) */
493 dimm->tRCD = xmp[7] * mtb;
494 /* Minimum Row Active to Row Active Delay Time (tRRDmin) */
495 dimm->tRRD = xmp[17] * mtb;
496 /* Minimum Row Precharge Delay Time (tRPmin) */
497 dimm->tRP = xmp[6] * mtb;
498 /* Minimum Active to Precharge Delay Time (tRASmin) */
499 dimm->tRAS = (((xmp[9] & 0x0f) << 8) + xmp[10]) * mtb;
500 /* Minimum Active to Active/Refresh Delay Time (tRCmin) */
501 dimm->tRC = (((xmp[9] & 0xf0) << 4) + xmp[11]) * mtb;
502 /* Minimum Refresh Recovery Delay Time (tRFCmin) */
503 dimm->tRFC = ((xmp[15] << 8) + xmp[14]) * mtb;
504 /* Minimum Internal Write to Read Command Delay Time (tWTRmin) */
505 dimm->tWTR = xmp[20] * mtb;
506 /* Minimum Internal Read to Precharge Command Delay Time (tRTPmin) */
507 dimm->tRTP = xmp[16] * mtb;
508 /* Minimum Four Activate Window Delay Time (tFAWmin) */
509 dimm->tFAW = (((xmp[18] & 0x0f) << 8) + xmp[19]) * mtb;
510
511 return ret;
512}
513
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500514/*
515 * The information printed below has a more informational character, and is not
516 * necessarily tied in to RAM init debugging. Hence, we stop using printram(),
517 * and use the standard printk()'s below.
518 */
519
520static void print_ns(const char *msg, u32 val)
521{
522 u32 mant, fp;
523 mant = val / 256;
524 fp = (val % 256) * 1000 / 256;
525
526 printk(BIOS_INFO, "%s%3u.%.3u ns\n", msg, mant, fp);
527}
528
529/**
530* \brief Print the info in DIMM
531*
532* Print info about the DIMM. Useful to use when CONFIG_DEBUG_RAM_SETUP is
533* selected, or for a purely informative output.
534*
Martin Roth63373ed2013-07-08 16:24:19 -0600535* @param dimm pointer to already decoded @ref dimm_attr structure
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500536*/
537void dram_print_spd_ddr3(const dimm_attr * dimm)
538{
539 u16 val16;
540 int i;
541
542 printk(BIOS_INFO, " Row addr bits : %u\n", dimm->row_bits);
543 printk(BIOS_INFO, " Column addr bits : %u\n", dimm->col_bits);
544 printk(BIOS_INFO, " Number of ranks : %u\n", dimm->ranks);
545 printk(BIOS_INFO, " DIMM Capacity : %u MB\n", dimm->size_mb);
546
547 /* CAS Latencies Supported */
548 val16 = dimm->cas_supported;
549 printk(BIOS_INFO, " CAS latencies :");
550 i = 0;
551 do {
552 if (val16 & 1)
553 printk(BIOS_INFO, " %u", i + 4);
554 i++;
555 val16 >>= 1;
556 } while (val16);
557 printk(BIOS_INFO, "\n");
558
559 print_ns(" tCKmin : ", dimm->tCK);
560 print_ns(" tAAmin : ", dimm->tAA);
561 print_ns(" tWRmin : ", dimm->tWR);
562 print_ns(" tRCDmin : ", dimm->tRCD);
563 print_ns(" tRRDmin : ", dimm->tRRD);
564 print_ns(" tRPmin : ", dimm->tRP);
565 print_ns(" tRASmin : ", dimm->tRAS);
566 print_ns(" tRCmin : ", dimm->tRC);
567 print_ns(" tRFCmin : ", dimm->tRFC);
568 print_ns(" tWTRmin : ", dimm->tWTR);
569 print_ns(" tRTPmin : ", dimm->tRTP);
570 print_ns(" tFAWmin : ", dimm->tFAW);
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500571}
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500572
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500573/*==============================================================================
574 *= DDR3 MRS helpers
575 *----------------------------------------------------------------------------*/
576
577/*
578 * MRS command structure:
579 * cmd[15:0] = Address pins MA[15:0]
580 * cmd[18:16] = Bank address BA[2:0]
581 */
582
583/* Map tWR value to a bitmask of the MR0 cycle */
584static u16 ddr3_twr_to_mr0_map(u8 twr)
585{
586 if ((twr >= 5) && (twr <= 8))
587 return (twr - 4) << 9;
588
589 /*
590 * From 8T onwards, we can only use even values. Round up if we are
591 * given an odd value.
592 */
593 if ((twr >= 9) && (twr <= 14))
594 return ((twr + 1) >> 1) << 9;
595
596 /* tWR == 16T is [000] */
597 return 0;
598}
599
600/* Map the CAS latency to a bitmask for the MR0 cycle */
601static u16 ddr3_cas_to_mr0_map(u8 cas)
602{
603 u16 mask = 0;
604 /* A[6:4] are bits [2:0] of (CAS - 4) */
605 mask = ((cas - 4) & 0x07) << 4;
606
607 /* A2 is the MSB of (CAS - 4) */
608 if ((cas - 4) & (1 << 3))
609 mask |= (1 << 2);
610
611 return mask;
612}
613
614/**
615 * \brief Get command address for a DDR3 MR0 command
616 *
617 * The DDR3 specification only covers odd write_recovery up to 7T. If an odd
618 * write_recovery greater than 7 is specified, it will be rounded up. If a tWR
619 * greater than 8 is specified, it is recommended to explicitly round it up or
620 * down before calling this function.
621 *
622 * write_recovery and cas are given in clock cycles. For example, a CAS of 7T
623 * should be given as 7.
624 *
Martin Roth98b698c2015-01-06 21:02:52 -0700625 * @param precharge_pd
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500626 * @param write_recovery Write recovery latency, tWR in clock cycles.
Martin Roth98b698c2015-01-06 21:02:52 -0700627 * @param dll_reset
628 * @param mode
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500629 * @param cas CAS latency in clock cycles.
Martin Roth98b698c2015-01-06 21:02:52 -0700630 * @param burst_type
631 * @param burst_length
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500632 */
633mrs_cmd_t ddr3_get_mr0(enum ddr3_mr0_precharge precharge_pd,
634 u8 write_recovery,
635 enum ddr3_mr0_dll_reset dll_reset,
636 enum ddr3_mr0_mode mode,
637 u8 cas,
638 enum ddr3_mr0_burst_type burst_type,
639 enum ddr3_mr0_burst_length burst_length)
640{
641 mrs_cmd_t cmd = 0 << 16;
642
643 if (precharge_pd == DDR3_MR0_PRECHARGE_FAST)
644 cmd |= (1 << 12);
645
646 cmd |= ddr3_twr_to_mr0_map(write_recovery);
647
648 if (dll_reset == DDR3_MR0_DLL_RESET_YES)
649 cmd |= (1 << 8);
650
651 if (mode == DDR3_MR0_MODE_TEST)
652 cmd |= (1 << 7);
653
654 cmd |= ddr3_cas_to_mr0_map(cas);
655
656 if (burst_type == DDR3_MR0_BURST_TYPE_INTERLEAVED)
657 cmd |= (1 << 3);
658
659 cmd |= (burst_length & 0x03) << 0;
660
661 return cmd;
662}
663
664static u16 ddr3_rtt_nom_to_mr1_map(enum ddr3_mr1_rtt_nom rtt_nom)
665{
666 u16 mask = 0;
667 /* A9 <-> rtt_nom[2] */
668 if (rtt_nom & (1 << 2))
669 mask |= (1 << 9);
670 /* A6 <-> rtt_nom[1] */
671 if (rtt_nom & (1 << 1))
672 mask |= (1 << 6);
673 /* A2 <-> rtt_nom[0] */
674 if (rtt_nom & (1 << 0))
675 mask |= (1 << 2);
676
677 return mask;
678}
679
680static u16 ddr3_ods_to_mr1_map(enum ddr3_mr1_ods ods)
681{
682 u16 mask = 0;
683 /* A5 <-> ods[1] */
684 if (ods & (1 << 1))
685 mask |= (1 << 5);
686 /* A1 <-> ods[0] */
687 if (ods & (1 << 0))
688 mask |= (1 << 1);
689
690 return mask;
691}
692
693/**
694 * \brief Get command address for a DDR3 MR1 command
695 */
696mrs_cmd_t ddr3_get_mr1(enum ddr3_mr1_qoff qoff,
697 enum ddr3_mr1_tqds tqds,
698 enum ddr3_mr1_rtt_nom rtt_nom,
699 enum ddr3_mr1_write_leveling write_leveling,
700 enum ddr3_mr1_ods ods,
701 enum ddr3_mr1_additive_latency additive_latency,
702 enum ddr3_mr1_dll dll_disable)
703{
704 mrs_cmd_t cmd = 1 << 16;
705
706 if (qoff == DDR3_MR1_QOFF_DISABLE)
707 cmd |= (1 << 12);
708
709 if (tqds == DDR3_MR1_TQDS_ENABLE)
710 cmd |= (1 << 11);
711
712 cmd |= ddr3_rtt_nom_to_mr1_map(rtt_nom);
713
714 if (write_leveling == DDR3_MR1_WRLVL_ENABLE)
715 cmd |= (1 << 7);
716
717 cmd |= ddr3_ods_to_mr1_map(ods);
718
719 cmd |= (additive_latency & 0x03) << 3;
720
721 if (dll_disable == DDR3_MR1_DLL_DISABLE)
722 cmd |= (1 << 0);
723
724 return cmd;
725}
726
727/**
728 * \brief Get command address for a DDR3 MR2 command
729 *
730 * cas_cwl is given in clock cycles. For example, a cas_cwl of 7T should be
731 * given as 7.
732 *
Martin Roth98b698c2015-01-06 21:02:52 -0700733 * @param rtt_wr
734 * @param extended_temp
735 * @param self_refresh
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500736 * @param cas_cwl CAS write latency in clock cycles.
737 */
Martin Roth98b698c2015-01-06 21:02:52 -0700738
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500739mrs_cmd_t ddr3_get_mr2(enum ddr3_mr2_rttwr rtt_wr,
740 enum ddr3_mr2_srt_range extended_temp,
741 enum ddr3_mr2_asr self_refresh, u8 cas_cwl)
742{
743 mrs_cmd_t cmd = 2 << 16;
744
745 cmd |= (rtt_wr & 0x03) << 9;
746
747 if (extended_temp == DDR3_MR2_SRT_EXTENDED)
748 cmd |= (1 << 7);
749
750 if (self_refresh == DDR3_MR2_ASR_AUTO)
751 cmd |= (1 << 6);
752
753 cmd |= ((cas_cwl - 5) & 0x07) << 3;
754
755 return cmd;
756}
757
758/**
759 * \brief Get command address for a DDR3 MR3 command
760 *
761 * @param dataflow_from_mpr Specify a non-zero value to put DRAM in read
762 * leveling mode. Zero for normal operation.
763 */
764mrs_cmd_t ddr3_get_mr3(char dataflow_from_mpr)
765{
766 mrs_cmd_t cmd = 3 << 16;
767
768 if (dataflow_from_mpr)
769 cmd |= (1 << 2);
770
771 return cmd;
772}
773
774/**
775 * \brief Mirror the address bits for this MRS command
776 *
777 * Swap the following bits in the MRS command:
778 * - MA3 <-> MA4
779 * - MA5 <-> MA6
780 * - MA7 <-> MA8
781 * - BA0 <-> BA1
782 */
783mrs_cmd_t ddr3_mrs_mirror_pins(mrs_cmd_t cmd)
784{
785 u32 downshift, upshift;
786 /* High bits= A4 | A6 | A8 | BA1 */
787 /* Low bits = A3 | A5 | A7 | BA0 */
788 u32 lowbits = (1 << 3) | (1 << 5) | (1 << 7) | (1 << 16);
789 downshift = (cmd & (lowbits << 1));
790 upshift = (cmd & lowbits);
791 cmd &= ~(lowbits | (lowbits << 1));
792 cmd |= (downshift >> 1) | (upshift << 1);
793 return cmd;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500794}