blob: b3bcd680feb54cd3e13f07c5587fcf1e4e19ddbc [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 */
39int dimm_is_registered(enum spd_dimm_type type)
40{
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) {
55 crc = (crc ^ (int)*ptr++) << 8;
56 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;
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200132 u8 ftb_divisor, ftb_dividend, capacity_shift, bus_width;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500133 u8 reg8;
134 u32 mtb; /* medium time base */
135 unsigned int val, param;
136
137 ret = SPD_STATUS_OK;
138
139 /* Don't assume we memset 0 dimm struct. Clear all our flags */
140 dimm->flags.raw = 0;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100141 dimm->dimms_per_channel = 3;
142
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500143 /* Make sure that the SPD dump is indeed from a DDR3 module */
144 if (spd[2] != SPD_MEMORY_TYPE_SDRAM_DDR3) {
145 printram("Not a DDR3 SPD!\n");
146 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
147 return SPD_STATUS_INVALID;
148 }
149 dimm->dram_type = SPD_MEMORY_TYPE_SDRAM_DDR3;
Vladimir Serbinenko0e675f72014-12-07 13:56:48 +0100150 dimm->dimm_type = spd[3] & 0xf;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500151
Patrick Rudolph8c639352015-06-22 19:32:53 +0200152 crc = spd_ddr3_calc_crc(spd, sizeof(spd_raw_data));
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500153 /* Compare with the CRC in the SPD */
154 spd_crc = (spd[127] << 8) + spd[126];
155 /* Verify the CRC is correct */
156 if (crc != spd_crc) {
Patrick Rudolph78c6e3e2015-06-22 19:46:34 +0200157 printram("ERROR: SPD CRC failed!!!\n");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500158 ret = SPD_STATUS_CRC_ERROR;
159 };
160
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100161 printram(" Revision : %x\n", spd[1]);
162 printram(" Type : %x\n", spd[2]);
163 printram(" Key : %x\n", spd[3]);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500164
165 reg8 = spd[4];
166 /* Number of memory banks */
167 val = (reg8 >> 4) & 0x07;
168 if (val > 0x03) {
169 printram(" Invalid number of memory banks\n");
170 ret = SPD_STATUS_INVALID_FIELD;
171 }
172 param = 1 << (val + 3);
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100173 printram(" Banks : %u\n", param);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500174 /* SDRAM capacity */
175 capacity_shift = reg8 & 0x0f;
176 if (capacity_shift > 0x06) {
177 printram(" Invalid module capacity\n");
178 ret = SPD_STATUS_INVALID_FIELD;
179 }
180 if (capacity_shift < 0x02) {
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100181 printram(" Capacity : %u Mb\n", 256 << capacity_shift);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500182 } else {
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100183 printram(" Capacity : %u Gb\n", 1 << (capacity_shift - 2));
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500184 }
185
186 reg8 = spd[5];
187 /* Row address bits */
188 val = (reg8 >> 3) & 0x07;
189 if (val > 0x04) {
190 printram(" Invalid row address bits\n");
191 ret = SPD_STATUS_INVALID_FIELD;
192 }
193 dimm->row_bits = val + 12;
194 /* Column address bits */
195 val = reg8 & 0x07;
196 if (val > 0x03) {
197 printram(" Invalid column address bits\n");
198 ret = SPD_STATUS_INVALID_FIELD;
199 }
200 dimm->col_bits = val + 9;
201
202 /* Module nominal voltage */
203 reg8 = spd[6];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100204 printram(" Supported voltages :");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500205 if (reg8 & (1 << 2)) {
206 dimm->flags.operable_1_25V = 1;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100207 dimm->voltage = 1250;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500208 printram(" 1.25V");
209 }
210 if (reg8 & (1 << 1)) {
211 dimm->flags.operable_1_35V = 1;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100212 dimm->voltage = 1300;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500213 printram(" 1.35V");
214 }
215 if (!(reg8 & (1 << 0))) {
216 dimm->flags.operable_1_50V = 1;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100217 dimm->voltage = 1500;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500218 printram(" 1.5V");
219 }
220 printram("\n");
221
222 /* Module organization */
223 reg8 = spd[7];
224 /* Number of ranks */
225 val = (reg8 >> 3) & 0x07;
226 if (val > 3) {
227 printram(" Invalid number of ranks\n");
228 ret = SPD_STATUS_INVALID_FIELD;
229 }
230 dimm->ranks = val + 1;
231 /* SDRAM device width */
232 val = (reg8 & 0x07);
233 if (val > 3) {
234 printram(" Invalid SDRAM width\n");
235 ret = SPD_STATUS_INVALID_FIELD;
236 }
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200237 dimm->width = (4 << val);
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100238 printram(" SDRAM width : %u\n", dimm->width);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500239
240 /* Memory bus width */
241 reg8 = spd[8];
242 /* Bus extension */
243 val = (reg8 >> 3) & 0x03;
244 if (val > 1) {
245 printram(" Invalid bus extension\n");
246 ret = SPD_STATUS_INVALID_FIELD;
247 }
248 dimm->flags.is_ecc = val ? 1 : 0;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100249 printram(" Bus extension : %u bits\n", val ? 8 : 0);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500250 /* Bus width */
251 val = reg8 & 0x07;
252 if (val > 3) {
253 printram(" Invalid bus width\n");
254 ret = SPD_STATUS_INVALID_FIELD;
255 }
256 bus_width = 8 << val;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100257 printram(" Bus width : %u\n", bus_width);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500258
259 /* We have all the info we need to compute the dimm size */
260 /* Capacity is 256Mbit multiplied by the power of 2 specified in
261 * capacity_shift
262 * The rest is the JEDEC formula */
263 dimm->size_mb = ((1 << (capacity_shift + (25 - 20))) * bus_width
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200264 * dimm->ranks) / dimm->width;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500265
266 /* Fine Timebase (FTB) Dividend/Divisor */
267 /* Dividend */
268 ftb_dividend = (spd[9] >> 4) & 0x0f;
269 /* Divisor */
270 ftb_divisor = spd[9] & 0x0f;
271
272 /* Medium Timebase =
273 * Medium Timebase (MTB) Dividend /
274 * Medium Timebase (MTB) Divisor */
275 mtb = (((u32) spd[10]) << 8) / spd[11];
276
277 /* SDRAM Minimum Cycle Time (tCKmin) */
278 dimm->tCK = spd[12] * mtb;
279 /* CAS Latencies Supported */
280 dimm->cas_supported = (spd[15] << 8) + spd[14];
281 /* Minimum CAS Latency Time (tAAmin) */
282 dimm->tAA = spd[16] * mtb;
283 /* Minimum Write Recovery Time (tWRmin) */
284 dimm->tWR = spd[17] * mtb;
285 /* Minimum RAS# to CAS# Delay Time (tRCDmin) */
286 dimm->tRCD = spd[18] * mtb;
287 /* Minimum Row Active to Row Active Delay Time (tRRDmin) */
288 dimm->tRRD = spd[19] * mtb;
289 /* Minimum Row Precharge Delay Time (tRPmin) */
290 dimm->tRP = spd[20] * mtb;
291 /* Minimum Active to Precharge Delay Time (tRASmin) */
292 dimm->tRAS = (((spd[21] & 0x0f) << 8) + spd[22]) * mtb;
293 /* Minimum Active to Active/Refresh Delay Time (tRCmin) */
294 dimm->tRC = (((spd[21] & 0xf0) << 4) + spd[23]) * mtb;
295 /* Minimum Refresh Recovery Delay Time (tRFCmin) */
296 dimm->tRFC = ((spd[25] << 8) + spd[24]) * mtb;
297 /* Minimum Internal Write to Read Command Delay Time (tWTRmin) */
298 dimm->tWTR = spd[26] * mtb;
299 /* Minimum Internal Read to Precharge Command Delay Time (tRTPmin) */
300 dimm->tRTP = spd[27] * mtb;
301 /* Minimum Four Activate Window Delay Time (tFAWmin) */
302 dimm->tFAW = (((spd[28] & 0x0f) << 8) + spd[29]) * mtb;
303
304 /* SDRAM Optional Features */
305 reg8 = spd[30];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100306 printram(" Optional features :");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500307 if (reg8 & 0x80) {
308 dimm->flags.dll_off_mode = 1;
309 printram(" DLL-Off_mode");
310 }
311 if (reg8 & 0x02) {
312 dimm->flags.rzq7_supported = 1;
313 printram(" RZQ/7");
314 }
315 if (reg8 & 0x01) {
316 dimm->flags.rzq6_supported = 1;
317 printram(" RZQ/6");
318 }
319 printram("\n");
320
321 /* SDRAM Thermal and Refresh Options */
322 reg8 = spd[31];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100323 printram(" Thermal features :");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500324 if (reg8 & 0x80) {
325 dimm->flags.pasr = 1;
326 printram(" PASR");
327 }
328 if (reg8 & 0x08) {
329 dimm->flags.odts = 1;
330 printram(" ODTS");
331 }
332 if (reg8 & 0x04) {
333 dimm->flags.asr = 1;
334 printram(" ASR");
335 }
336 if (reg8 & 0x02) {
337 dimm->flags.ext_temp_range = 1;
338 printram(" ext_temp_refresh");
339 }
340 if (reg8 & 0x01) {
341 dimm->flags.ext_temp_refresh = 1;
342 printram(" ext_temp_range");
343 }
344 printram("\n");
345
346 /* Module Thermal Sensor */
347 reg8 = spd[32];
348 if (reg8 & 0x80)
349 dimm->flags.therm_sensor = 1;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100350 printram(" Thermal sensor : %s\n",
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500351 dimm->flags.therm_sensor ? "yes" : "no");
352
353 /* SDRAM Device Type */
354 reg8 = spd[33];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100355 printram(" Standard SDRAM : %s\n", (reg8 & 0x80) ? "no" : "yes");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500356
357 if (spd[63] & 0x01) {
358 dimm->flags.pins_mirrored = 1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500359 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100360 printram(" Rank1 Address bits : %s\n",
361 (spd[63] & 0x01) ? "mirrored" : "normal");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500362
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200363 dimm->reference_card = spd[62] & 0x1f;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100364 printram(" DIMM Reference card: %c\n", 'A' + dimm->reference_card);
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200365
Patrick Rudolph07691592016-02-29 18:21:00 +0100366 dimm->manufacturer_id = (spd[118] << 8) | spd[117];
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100367 printram(" Manufacturer ID : %x\n", dimm->manufacturer_id);
Patrick Rudolph07691592016-02-29 18:21:00 +0100368
369 dimm->part_number[16] = 0;
370 memcpy(dimm->part_number, &spd[128], 16);
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100371 printram(" Part number : %s\n", dimm->part_number);
Patrick Rudolph07691592016-02-29 18:21:00 +0100372
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500373 return ret;
374}
375
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100376/**
377 * \brief Decode the raw SPD XMP data
378 *
379 * Decodes a raw SPD XMP data from a DDR3 DIMM, and organizes it into a
380 * @ref dimm_attr structure. The SPD data must first be read in a contiguous
381 * array, and passed to this function.
382 *
383 * @param dimm pointer to @ref dimm_attr structure where the decoded data is to
384 * be stored
385 * @param spd array of raw data previously read from the SPD.
386 *
387 * @param profile select one of the profiles to load
388 *
389 * @return @ref spd_status enumerator
390 * SPD_STATUS_OK -- decoding was successful
391 * SPD_STATUS_INVALID -- invalid SPD or not a DDR3 SPD
392 * SPD_STATUS_CRC_ERROR -- CRC did not verify
393 * SPD_STATUS_INVALID_FIELD -- A field with an invalid value was
394 * detected.
395 */
396int spd_xmp_decode_ddr3(dimm_attr *dimm,
397 spd_raw_data spd,
398 enum ddr3_xmp_profile profile)
399{
400 int ret;
401 u32 mtb; /* medium time base */
402 u8 *xmp; /* pointer to XMP profile data */
403
404 /* need a valid SPD */
405 ret = spd_decode_ddr3(dimm, spd);
406 if (ret != SPD_STATUS_OK)
407 return ret;
408
409 /* search for magic header */
410 if (spd[176] != 0x0C || spd[177] != 0x4A) {
411 printram("Not a DDR3 XMP profile!\n");
412 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
413 return SPD_STATUS_INVALID;
414 }
415
416 if (profile == DDR3_XMP_PROFILE_1) {
417 if (!(spd[178] & 1)) {
418 printram("Selected XMP profile disabled!\n");
419 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
420 return SPD_STATUS_INVALID;
421 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100422
423 printram(" XMP Profile : 1\n");
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100424 xmp = &spd[185];
425
426 /* Medium Timebase =
427 * Medium Timebase (MTB) Dividend /
428 * Medium Timebase (MTB) Divisor */
429 mtb = (((u32) spd[180]) << 8) / spd[181];
430
431 dimm->dimms_per_channel = ((spd[178] >> 2) & 0x3) + 1;
432 } else {
433 if (!(spd[178] & 2)) {
434 printram("Selected XMP profile disabled!\n");
435 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
436 return SPD_STATUS_INVALID;
437 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100438 printram(" XMP Profile : 2\n");
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100439 xmp = &spd[220];
440
441 /* Medium Timebase =
442 * Medium Timebase (MTB) Dividend /
443 * Medium Timebase (MTB) Divisor */
444 mtb = (((u32) spd[182]) << 8) / spd[183];
445
446 dimm->dimms_per_channel = ((spd[178] >> 4) & 0x3) + 1;
447 }
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100448
449 printram(" Max DIMMs/channel : %u\n",
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100450 dimm->dimms_per_channel);
451
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100452 printram(" XMP Revision : %u.%u\n", spd[179] >> 4, spd[179] & 0xf);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100453
454 /* calculate voltage in mV */
455 dimm->voltage = (xmp[0] & 1) * 50;
456 dimm->voltage += ((xmp[0] >> 1) & 0xf) * 100;
457 dimm->voltage += ((xmp[0] >> 5) & 0x3) * 1000;
Patrick Rudolph66a98ee2016-03-13 13:02:16 +0100458
459 printram(" Requested voltage : %u mV\n", dimm->voltage);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100460
461 /* SDRAM Minimum Cycle Time (tCKmin) */
462 dimm->tCK = xmp[1] * mtb;
463 /* CAS Latencies Supported */
464 dimm->cas_supported = (xmp[9] << 8) + xmp[8];
465 /* Minimum CAS Latency Time (tAAmin) */
466 dimm->tAA = xmp[2] * mtb;
467 /* Minimum Write Recovery Time (tWRmin) */
468 dimm->tWR = xmp[8] * mtb;
469 /* Minimum RAS# to CAS# Delay Time (tRCDmin) */
470 dimm->tRCD = xmp[7] * mtb;
471 /* Minimum Row Active to Row Active Delay Time (tRRDmin) */
472 dimm->tRRD = xmp[17] * mtb;
473 /* Minimum Row Precharge Delay Time (tRPmin) */
474 dimm->tRP = xmp[6] * mtb;
475 /* Minimum Active to Precharge Delay Time (tRASmin) */
476 dimm->tRAS = (((xmp[9] & 0x0f) << 8) + xmp[10]) * mtb;
477 /* Minimum Active to Active/Refresh Delay Time (tRCmin) */
478 dimm->tRC = (((xmp[9] & 0xf0) << 4) + xmp[11]) * mtb;
479 /* Minimum Refresh Recovery Delay Time (tRFCmin) */
480 dimm->tRFC = ((xmp[15] << 8) + xmp[14]) * mtb;
481 /* Minimum Internal Write to Read Command Delay Time (tWTRmin) */
482 dimm->tWTR = xmp[20] * mtb;
483 /* Minimum Internal Read to Precharge Command Delay Time (tRTPmin) */
484 dimm->tRTP = xmp[16] * mtb;
485 /* Minimum Four Activate Window Delay Time (tFAWmin) */
486 dimm->tFAW = (((xmp[18] & 0x0f) << 8) + xmp[19]) * mtb;
487
488 return ret;
489}
490
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500491/*
492 * The information printed below has a more informational character, and is not
493 * necessarily tied in to RAM init debugging. Hence, we stop using printram(),
494 * and use the standard printk()'s below.
495 */
496
497static void print_ns(const char *msg, u32 val)
498{
499 u32 mant, fp;
500 mant = val / 256;
501 fp = (val % 256) * 1000 / 256;
502
503 printk(BIOS_INFO, "%s%3u.%.3u ns\n", msg, mant, fp);
504}
505
506/**
507* \brief Print the info in DIMM
508*
509* Print info about the DIMM. Useful to use when CONFIG_DEBUG_RAM_SETUP is
510* selected, or for a purely informative output.
511*
Martin Roth63373ed2013-07-08 16:24:19 -0600512* @param dimm pointer to already decoded @ref dimm_attr structure
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500513*/
514void dram_print_spd_ddr3(const dimm_attr * dimm)
515{
516 u16 val16;
517 int i;
518
519 printk(BIOS_INFO, " Row addr bits : %u\n", dimm->row_bits);
520 printk(BIOS_INFO, " Column addr bits : %u\n", dimm->col_bits);
521 printk(BIOS_INFO, " Number of ranks : %u\n", dimm->ranks);
522 printk(BIOS_INFO, " DIMM Capacity : %u MB\n", dimm->size_mb);
523
524 /* CAS Latencies Supported */
525 val16 = dimm->cas_supported;
526 printk(BIOS_INFO, " CAS latencies :");
527 i = 0;
528 do {
529 if (val16 & 1)
530 printk(BIOS_INFO, " %u", i + 4);
531 i++;
532 val16 >>= 1;
533 } while (val16);
534 printk(BIOS_INFO, "\n");
535
536 print_ns(" tCKmin : ", dimm->tCK);
537 print_ns(" tAAmin : ", dimm->tAA);
538 print_ns(" tWRmin : ", dimm->tWR);
539 print_ns(" tRCDmin : ", dimm->tRCD);
540 print_ns(" tRRDmin : ", dimm->tRRD);
541 print_ns(" tRPmin : ", dimm->tRP);
542 print_ns(" tRASmin : ", dimm->tRAS);
543 print_ns(" tRCmin : ", dimm->tRC);
544 print_ns(" tRFCmin : ", dimm->tRFC);
545 print_ns(" tWTRmin : ", dimm->tWTR);
546 print_ns(" tRTPmin : ", dimm->tRTP);
547 print_ns(" tFAWmin : ", dimm->tFAW);
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500548}
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500549
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500550/*==============================================================================
551 *= DDR3 MRS helpers
552 *----------------------------------------------------------------------------*/
553
554/*
555 * MRS command structure:
556 * cmd[15:0] = Address pins MA[15:0]
557 * cmd[18:16] = Bank address BA[2:0]
558 */
559
560/* Map tWR value to a bitmask of the MR0 cycle */
561static u16 ddr3_twr_to_mr0_map(u8 twr)
562{
563 if ((twr >= 5) && (twr <= 8))
564 return (twr - 4) << 9;
565
566 /*
567 * From 8T onwards, we can only use even values. Round up if we are
568 * given an odd value.
569 */
570 if ((twr >= 9) && (twr <= 14))
571 return ((twr + 1) >> 1) << 9;
572
573 /* tWR == 16T is [000] */
574 return 0;
575}
576
577/* Map the CAS latency to a bitmask for the MR0 cycle */
578static u16 ddr3_cas_to_mr0_map(u8 cas)
579{
580 u16 mask = 0;
581 /* A[6:4] are bits [2:0] of (CAS - 4) */
582 mask = ((cas - 4) & 0x07) << 4;
583
584 /* A2 is the MSB of (CAS - 4) */
585 if ((cas - 4) & (1 << 3))
586 mask |= (1 << 2);
587
588 return mask;
589}
590
591/**
592 * \brief Get command address for a DDR3 MR0 command
593 *
594 * The DDR3 specification only covers odd write_recovery up to 7T. If an odd
595 * write_recovery greater than 7 is specified, it will be rounded up. If a tWR
596 * greater than 8 is specified, it is recommended to explicitly round it up or
597 * down before calling this function.
598 *
599 * write_recovery and cas are given in clock cycles. For example, a CAS of 7T
600 * should be given as 7.
601 *
Martin Roth98b698c2015-01-06 21:02:52 -0700602 * @param precharge_pd
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500603 * @param write_recovery Write recovery latency, tWR in clock cycles.
Martin Roth98b698c2015-01-06 21:02:52 -0700604 * @param dll_reset
605 * @param mode
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500606 * @param cas CAS latency in clock cycles.
Martin Roth98b698c2015-01-06 21:02:52 -0700607 * @param burst_type
608 * @param burst_length
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500609 */
610mrs_cmd_t ddr3_get_mr0(enum ddr3_mr0_precharge precharge_pd,
611 u8 write_recovery,
612 enum ddr3_mr0_dll_reset dll_reset,
613 enum ddr3_mr0_mode mode,
614 u8 cas,
615 enum ddr3_mr0_burst_type burst_type,
616 enum ddr3_mr0_burst_length burst_length)
617{
618 mrs_cmd_t cmd = 0 << 16;
619
620 if (precharge_pd == DDR3_MR0_PRECHARGE_FAST)
621 cmd |= (1 << 12);
622
623 cmd |= ddr3_twr_to_mr0_map(write_recovery);
624
625 if (dll_reset == DDR3_MR0_DLL_RESET_YES)
626 cmd |= (1 << 8);
627
628 if (mode == DDR3_MR0_MODE_TEST)
629 cmd |= (1 << 7);
630
631 cmd |= ddr3_cas_to_mr0_map(cas);
632
633 if (burst_type == DDR3_MR0_BURST_TYPE_INTERLEAVED)
634 cmd |= (1 << 3);
635
636 cmd |= (burst_length & 0x03) << 0;
637
638 return cmd;
639}
640
641static u16 ddr3_rtt_nom_to_mr1_map(enum ddr3_mr1_rtt_nom rtt_nom)
642{
643 u16 mask = 0;
644 /* A9 <-> rtt_nom[2] */
645 if (rtt_nom & (1 << 2))
646 mask |= (1 << 9);
647 /* A6 <-> rtt_nom[1] */
648 if (rtt_nom & (1 << 1))
649 mask |= (1 << 6);
650 /* A2 <-> rtt_nom[0] */
651 if (rtt_nom & (1 << 0))
652 mask |= (1 << 2);
653
654 return mask;
655}
656
657static u16 ddr3_ods_to_mr1_map(enum ddr3_mr1_ods ods)
658{
659 u16 mask = 0;
660 /* A5 <-> ods[1] */
661 if (ods & (1 << 1))
662 mask |= (1 << 5);
663 /* A1 <-> ods[0] */
664 if (ods & (1 << 0))
665 mask |= (1 << 1);
666
667 return mask;
668}
669
670/**
671 * \brief Get command address for a DDR3 MR1 command
672 */
673mrs_cmd_t ddr3_get_mr1(enum ddr3_mr1_qoff qoff,
674 enum ddr3_mr1_tqds tqds,
675 enum ddr3_mr1_rtt_nom rtt_nom,
676 enum ddr3_mr1_write_leveling write_leveling,
677 enum ddr3_mr1_ods ods,
678 enum ddr3_mr1_additive_latency additive_latency,
679 enum ddr3_mr1_dll dll_disable)
680{
681 mrs_cmd_t cmd = 1 << 16;
682
683 if (qoff == DDR3_MR1_QOFF_DISABLE)
684 cmd |= (1 << 12);
685
686 if (tqds == DDR3_MR1_TQDS_ENABLE)
687 cmd |= (1 << 11);
688
689 cmd |= ddr3_rtt_nom_to_mr1_map(rtt_nom);
690
691 if (write_leveling == DDR3_MR1_WRLVL_ENABLE)
692 cmd |= (1 << 7);
693
694 cmd |= ddr3_ods_to_mr1_map(ods);
695
696 cmd |= (additive_latency & 0x03) << 3;
697
698 if (dll_disable == DDR3_MR1_DLL_DISABLE)
699 cmd |= (1 << 0);
700
701 return cmd;
702}
703
704/**
705 * \brief Get command address for a DDR3 MR2 command
706 *
707 * cas_cwl is given in clock cycles. For example, a cas_cwl of 7T should be
708 * given as 7.
709 *
Martin Roth98b698c2015-01-06 21:02:52 -0700710 * @param rtt_wr
711 * @param extended_temp
712 * @param self_refresh
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500713 * @param cas_cwl CAS write latency in clock cycles.
714 */
Martin Roth98b698c2015-01-06 21:02:52 -0700715
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500716mrs_cmd_t ddr3_get_mr2(enum ddr3_mr2_rttwr rtt_wr,
717 enum ddr3_mr2_srt_range extended_temp,
718 enum ddr3_mr2_asr self_refresh, u8 cas_cwl)
719{
720 mrs_cmd_t cmd = 2 << 16;
721
722 cmd |= (rtt_wr & 0x03) << 9;
723
724 if (extended_temp == DDR3_MR2_SRT_EXTENDED)
725 cmd |= (1 << 7);
726
727 if (self_refresh == DDR3_MR2_ASR_AUTO)
728 cmd |= (1 << 6);
729
730 cmd |= ((cas_cwl - 5) & 0x07) << 3;
731
732 return cmd;
733}
734
735/**
736 * \brief Get command address for a DDR3 MR3 command
737 *
738 * @param dataflow_from_mpr Specify a non-zero value to put DRAM in read
739 * leveling mode. Zero for normal operation.
740 */
741mrs_cmd_t ddr3_get_mr3(char dataflow_from_mpr)
742{
743 mrs_cmd_t cmd = 3 << 16;
744
745 if (dataflow_from_mpr)
746 cmd |= (1 << 2);
747
748 return cmd;
749}
750
751/**
752 * \brief Mirror the address bits for this MRS command
753 *
754 * Swap the following bits in the MRS command:
755 * - MA3 <-> MA4
756 * - MA5 <-> MA6
757 * - MA7 <-> MA8
758 * - BA0 <-> BA1
759 */
760mrs_cmd_t ddr3_mrs_mirror_pins(mrs_cmd_t cmd)
761{
762 u32 downshift, upshift;
763 /* High bits= A4 | A6 | A8 | BA1 */
764 /* Low bits = A3 | A5 | A7 | BA0 */
765 u32 lowbits = (1 << 3) | (1 << 5) | (1 << 7) | (1 << 16);
766 downshift = (cmd & (lowbits << 1));
767 upshift = (cmd & lowbits);
768 cmd &= ~(lowbits | (lowbits << 1));
769 cmd |= (downshift >> 1) | (upshift << 1);
770 return cmd;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500771}