blob: 6bfaabcd7666dee5ebe1f0d7b39cef2cfa7a91b5 [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>
26
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -050027/*==============================================================================
28 * = DDR3 SPD decoding helpers
29 *----------------------------------------------------------------------------*/
30
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050031/**
32 * \brief Checks if the DIMM is Registered based on byte[3] of the SPD
33 *
34 * Tells if the DIMM type is registered or not.
35 *
36 * @param type DIMM type. This is byte[3] of the SPD.
37 */
38int dimm_is_registered(enum spd_dimm_type type)
39{
40 if ((type == SPD_DIMM_TYPE_RDIMM)
41 | (type == SPD_DIMM_TYPE_MINI_RDIMM)
42 | (type == SPD_DIMM_TYPE_72B_SO_RDIMM))
43 return 1;
44
45 return 0;
46}
47
48/**
Alexandru Gagniuc4c37e582013-12-17 13:08:01 -050049 * \brief Calculate the CRC of a DDR3 SPD
50 *
51 * @param spd pointer to raw SPD data
52 * @param len length of data in SPD
53 *
54 * @return the CRC of the SPD data, or 0 when spd data is truncated.
55 */
56u16 spd_ddr3_calc_crc(u8 *spd, int len)
57{
58 int n_crc, i;
59 u8 *ptr;
60 u16 crc;
61
62 /* Find the number of bytes covered by CRC */
63 if (spd[0] & 0x80) {
64 n_crc = 117;
65 } else {
66 n_crc = 126;
67 }
68
69 if (len < n_crc)
70 /* Not enough bytes available to get the CRC */
71 return 0;
72
73 /* Compute the CRC */
74 crc = 0;
75 ptr = spd;
76 while (--n_crc >= 0) {
77 crc = crc ^ (int)*ptr++ << 8;
78 for (i = 0; i < 8; ++i)
79 if (crc & 0x8000) {
80 crc = crc << 1 ^ 0x1021;
81 } else {
82 crc = crc << 1;
83 }
84 }
85 return crc;
86}
87
88/**
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050089 * \brief Decode the raw SPD data
90 *
91 * Decodes a raw SPD data from a DDR3 DIMM, and organizes it into a
92 * @ref dimm_attr structure. The SPD data must first be read in a contiguous
93 * array, and passed to this function.
94 *
Martin Roth63373ed2013-07-08 16:24:19 -060095 * @param dimm pointer to @ref dimm_attr structure where the decoded data is to
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050096 * be stored
97 * @param spd array of raw data previously read from the SPD.
98 *
99 * @return @ref spd_status enumerator
100 * SPD_STATUS_OK -- decoding was successful
101 * SPD_STATUS_INVALID -- invalid SPD or not a DDR3 SPD
102 * SPD_STATUS_CRC_ERROR -- CRC did not verify
103 * SPD_STATUS_INVALID_FIELD -- A field with an invalid value was
104 * detected.
105 */
106int spd_decode_ddr3(dimm_attr * dimm, spd_raw_data spd)
107{
Alexandru Gagniuc4c37e582013-12-17 13:08:01 -0500108 int ret;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500109 u16 crc, spd_crc;
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200110 u8 ftb_divisor, ftb_dividend, capacity_shift, bus_width;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500111 u8 reg8;
112 u32 mtb; /* medium time base */
113 unsigned int val, param;
114
115 ret = SPD_STATUS_OK;
116
117 /* Don't assume we memset 0 dimm struct. Clear all our flags */
118 dimm->flags.raw = 0;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100119 dimm->dimms_per_channel = 3;
120
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500121 /* Make sure that the SPD dump is indeed from a DDR3 module */
122 if (spd[2] != SPD_MEMORY_TYPE_SDRAM_DDR3) {
123 printram("Not a DDR3 SPD!\n");
124 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
125 return SPD_STATUS_INVALID;
126 }
127 dimm->dram_type = SPD_MEMORY_TYPE_SDRAM_DDR3;
Vladimir Serbinenko0e675f72014-12-07 13:56:48 +0100128 dimm->dimm_type = spd[3] & 0xf;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500129
Patrick Rudolph8c639352015-06-22 19:32:53 +0200130 crc = spd_ddr3_calc_crc(spd, sizeof(spd_raw_data));
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500131 /* Compare with the CRC in the SPD */
132 spd_crc = (spd[127] << 8) + spd[126];
133 /* Verify the CRC is correct */
134 if (crc != spd_crc) {
Patrick Rudolph78c6e3e2015-06-22 19:46:34 +0200135 printram("ERROR: SPD CRC failed!!!\n");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500136 ret = SPD_STATUS_CRC_ERROR;
137 };
138
139 printram(" Revision: %x\n", spd[1]);
140 printram(" Type : %x\n", spd[2]);
141 printram(" Key : %x\n", spd[3]);
142
143 reg8 = spd[4];
144 /* Number of memory banks */
145 val = (reg8 >> 4) & 0x07;
146 if (val > 0x03) {
147 printram(" Invalid number of memory banks\n");
148 ret = SPD_STATUS_INVALID_FIELD;
149 }
150 param = 1 << (val + 3);
151 printram(" Banks : %u\n", param);
152 /* SDRAM capacity */
153 capacity_shift = reg8 & 0x0f;
154 if (capacity_shift > 0x06) {
155 printram(" Invalid module capacity\n");
156 ret = SPD_STATUS_INVALID_FIELD;
157 }
158 if (capacity_shift < 0x02) {
159 printram(" Capacity: %u Mb\n", 256 << capacity_shift);
160 } else {
161 printram(" Capacity: %u Gb\n", 1 << (capacity_shift - 2));
162 }
163
164 reg8 = spd[5];
165 /* Row address bits */
166 val = (reg8 >> 3) & 0x07;
167 if (val > 0x04) {
168 printram(" Invalid row address bits\n");
169 ret = SPD_STATUS_INVALID_FIELD;
170 }
171 dimm->row_bits = val + 12;
172 /* Column address bits */
173 val = reg8 & 0x07;
174 if (val > 0x03) {
175 printram(" Invalid column address bits\n");
176 ret = SPD_STATUS_INVALID_FIELD;
177 }
178 dimm->col_bits = val + 9;
179
180 /* Module nominal voltage */
181 reg8 = spd[6];
182 printram(" Supported voltages:");
183 if (reg8 & (1 << 2)) {
184 dimm->flags.operable_1_25V = 1;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100185 dimm->voltage = 1250;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500186 printram(" 1.25V");
187 }
188 if (reg8 & (1 << 1)) {
189 dimm->flags.operable_1_35V = 1;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100190 dimm->voltage = 1300;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500191 printram(" 1.35V");
192 }
193 if (!(reg8 & (1 << 0))) {
194 dimm->flags.operable_1_50V = 1;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100195 dimm->voltage = 1500;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500196 printram(" 1.5V");
197 }
198 printram("\n");
199
200 /* Module organization */
201 reg8 = spd[7];
202 /* Number of ranks */
203 val = (reg8 >> 3) & 0x07;
204 if (val > 3) {
205 printram(" Invalid number of ranks\n");
206 ret = SPD_STATUS_INVALID_FIELD;
207 }
208 dimm->ranks = val + 1;
209 /* SDRAM device width */
210 val = (reg8 & 0x07);
211 if (val > 3) {
212 printram(" Invalid SDRAM width\n");
213 ret = SPD_STATUS_INVALID_FIELD;
214 }
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200215 dimm->width = (4 << val);
216 printram(" SDRAM width : %u\n", dimm->width);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500217
218 /* Memory bus width */
219 reg8 = spd[8];
220 /* Bus extension */
221 val = (reg8 >> 3) & 0x03;
222 if (val > 1) {
223 printram(" Invalid bus extension\n");
224 ret = SPD_STATUS_INVALID_FIELD;
225 }
226 dimm->flags.is_ecc = val ? 1 : 0;
227 printram(" Bus extension : %u bits\n", val ? 8 : 0);
228 /* Bus width */
229 val = reg8 & 0x07;
230 if (val > 3) {
231 printram(" Invalid bus width\n");
232 ret = SPD_STATUS_INVALID_FIELD;
233 }
234 bus_width = 8 << val;
235 printram(" Bus width : %u\n", bus_width);
236
237 /* We have all the info we need to compute the dimm size */
238 /* Capacity is 256Mbit multiplied by the power of 2 specified in
239 * capacity_shift
240 * The rest is the JEDEC formula */
241 dimm->size_mb = ((1 << (capacity_shift + (25 - 20))) * bus_width
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200242 * dimm->ranks) / dimm->width;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500243
244 /* Fine Timebase (FTB) Dividend/Divisor */
245 /* Dividend */
246 ftb_dividend = (spd[9] >> 4) & 0x0f;
247 /* Divisor */
248 ftb_divisor = spd[9] & 0x0f;
249
250 /* Medium Timebase =
251 * Medium Timebase (MTB) Dividend /
252 * Medium Timebase (MTB) Divisor */
253 mtb = (((u32) spd[10]) << 8) / spd[11];
254
255 /* SDRAM Minimum Cycle Time (tCKmin) */
256 dimm->tCK = spd[12] * mtb;
257 /* CAS Latencies Supported */
258 dimm->cas_supported = (spd[15] << 8) + spd[14];
259 /* Minimum CAS Latency Time (tAAmin) */
260 dimm->tAA = spd[16] * mtb;
261 /* Minimum Write Recovery Time (tWRmin) */
262 dimm->tWR = spd[17] * mtb;
263 /* Minimum RAS# to CAS# Delay Time (tRCDmin) */
264 dimm->tRCD = spd[18] * mtb;
265 /* Minimum Row Active to Row Active Delay Time (tRRDmin) */
266 dimm->tRRD = spd[19] * mtb;
267 /* Minimum Row Precharge Delay Time (tRPmin) */
268 dimm->tRP = spd[20] * mtb;
269 /* Minimum Active to Precharge Delay Time (tRASmin) */
270 dimm->tRAS = (((spd[21] & 0x0f) << 8) + spd[22]) * mtb;
271 /* Minimum Active to Active/Refresh Delay Time (tRCmin) */
272 dimm->tRC = (((spd[21] & 0xf0) << 4) + spd[23]) * mtb;
273 /* Minimum Refresh Recovery Delay Time (tRFCmin) */
274 dimm->tRFC = ((spd[25] << 8) + spd[24]) * mtb;
275 /* Minimum Internal Write to Read Command Delay Time (tWTRmin) */
276 dimm->tWTR = spd[26] * mtb;
277 /* Minimum Internal Read to Precharge Command Delay Time (tRTPmin) */
278 dimm->tRTP = spd[27] * mtb;
279 /* Minimum Four Activate Window Delay Time (tFAWmin) */
280 dimm->tFAW = (((spd[28] & 0x0f) << 8) + spd[29]) * mtb;
281
282 /* SDRAM Optional Features */
283 reg8 = spd[30];
284 printram(" Optional features :");
285 if (reg8 & 0x80) {
286 dimm->flags.dll_off_mode = 1;
287 printram(" DLL-Off_mode");
288 }
289 if (reg8 & 0x02) {
290 dimm->flags.rzq7_supported = 1;
291 printram(" RZQ/7");
292 }
293 if (reg8 & 0x01) {
294 dimm->flags.rzq6_supported = 1;
295 printram(" RZQ/6");
296 }
297 printram("\n");
298
299 /* SDRAM Thermal and Refresh Options */
300 reg8 = spd[31];
301 printram(" Thermal features :");
302 if (reg8 & 0x80) {
303 dimm->flags.pasr = 1;
304 printram(" PASR");
305 }
306 if (reg8 & 0x08) {
307 dimm->flags.odts = 1;
308 printram(" ODTS");
309 }
310 if (reg8 & 0x04) {
311 dimm->flags.asr = 1;
312 printram(" ASR");
313 }
314 if (reg8 & 0x02) {
315 dimm->flags.ext_temp_range = 1;
316 printram(" ext_temp_refresh");
317 }
318 if (reg8 & 0x01) {
319 dimm->flags.ext_temp_refresh = 1;
320 printram(" ext_temp_range");
321 }
322 printram("\n");
323
324 /* Module Thermal Sensor */
325 reg8 = spd[32];
326 if (reg8 & 0x80)
327 dimm->flags.therm_sensor = 1;
328 printram(" Thermal sensor : %s\n",
329 dimm->flags.therm_sensor ? "yes" : "no");
330
331 /* SDRAM Device Type */
332 reg8 = spd[33];
333 printram(" Standard SDRAM : %s\n", (reg8 & 0x80) ? "no" : "yes");
334
335 if (spd[63] & 0x01) {
336 dimm->flags.pins_mirrored = 1;
Martin Roth63373ed2013-07-08 16:24:19 -0600337 printram(" DIMM Rank1 Address bits mirrored!!!\n");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500338 }
339
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200340 dimm->reference_card = spd[62] & 0x1f;
341 printram(" DIMM Reference card %c\n", 'A' + dimm->reference_card);
342
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500343 return ret;
344}
345
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100346/**
347 * \brief Decode the raw SPD XMP data
348 *
349 * Decodes a raw SPD XMP data from a DDR3 DIMM, and organizes it into a
350 * @ref dimm_attr structure. The SPD data must first be read in a contiguous
351 * array, and passed to this function.
352 *
353 * @param dimm pointer to @ref dimm_attr structure where the decoded data is to
354 * be stored
355 * @param spd array of raw data previously read from the SPD.
356 *
357 * @param profile select one of the profiles to load
358 *
359 * @return @ref spd_status enumerator
360 * SPD_STATUS_OK -- decoding was successful
361 * SPD_STATUS_INVALID -- invalid SPD or not a DDR3 SPD
362 * SPD_STATUS_CRC_ERROR -- CRC did not verify
363 * SPD_STATUS_INVALID_FIELD -- A field with an invalid value was
364 * detected.
365 */
366int spd_xmp_decode_ddr3(dimm_attr *dimm,
367 spd_raw_data spd,
368 enum ddr3_xmp_profile profile)
369{
370 int ret;
371 u32 mtb; /* medium time base */
372 u8 *xmp; /* pointer to XMP profile data */
373
374 /* need a valid SPD */
375 ret = spd_decode_ddr3(dimm, spd);
376 if (ret != SPD_STATUS_OK)
377 return ret;
378
379 /* search for magic header */
380 if (spd[176] != 0x0C || spd[177] != 0x4A) {
381 printram("Not a DDR3 XMP profile!\n");
382 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
383 return SPD_STATUS_INVALID;
384 }
385
386 if (profile == DDR3_XMP_PROFILE_1) {
387 if (!(spd[178] & 1)) {
388 printram("Selected XMP profile disabled!\n");
389 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
390 return SPD_STATUS_INVALID;
391 }
392 printram(" XMP Profile 1\n");
393 xmp = &spd[185];
394
395 /* Medium Timebase =
396 * Medium Timebase (MTB) Dividend /
397 * Medium Timebase (MTB) Divisor */
398 mtb = (((u32) spd[180]) << 8) / spd[181];
399
400 dimm->dimms_per_channel = ((spd[178] >> 2) & 0x3) + 1;
401 } else {
402 if (!(spd[178] & 2)) {
403 printram("Selected XMP profile disabled!\n");
404 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
405 return SPD_STATUS_INVALID;
406 }
407 printram(" XMP Profile 2\n");
408 xmp = &spd[220];
409
410 /* Medium Timebase =
411 * Medium Timebase (MTB) Dividend /
412 * Medium Timebase (MTB) Divisor */
413 mtb = (((u32) spd[182]) << 8) / spd[183];
414
415 dimm->dimms_per_channel = ((spd[178] >> 4) & 0x3) + 1;
416 }
417 printram(" Max DIMMs per channel: %u\n",
418 dimm->dimms_per_channel);
419
420 printram(" XMP Revision: %u.%u\n", spd[179] >> 4, spd[179] & 0xf);
421
422 /* calculate voltage in mV */
423 dimm->voltage = (xmp[0] & 1) * 50;
424 dimm->voltage += ((xmp[0] >> 1) & 0xf) * 100;
425 dimm->voltage += ((xmp[0] >> 5) & 0x3) * 1000;
426 printram(" Requested voltage: %u mV\n", dimm->voltage);
427
428 /* SDRAM Minimum Cycle Time (tCKmin) */
429 dimm->tCK = xmp[1] * mtb;
430 /* CAS Latencies Supported */
431 dimm->cas_supported = (xmp[9] << 8) + xmp[8];
432 /* Minimum CAS Latency Time (tAAmin) */
433 dimm->tAA = xmp[2] * mtb;
434 /* Minimum Write Recovery Time (tWRmin) */
435 dimm->tWR = xmp[8] * mtb;
436 /* Minimum RAS# to CAS# Delay Time (tRCDmin) */
437 dimm->tRCD = xmp[7] * mtb;
438 /* Minimum Row Active to Row Active Delay Time (tRRDmin) */
439 dimm->tRRD = xmp[17] * mtb;
440 /* Minimum Row Precharge Delay Time (tRPmin) */
441 dimm->tRP = xmp[6] * mtb;
442 /* Minimum Active to Precharge Delay Time (tRASmin) */
443 dimm->tRAS = (((xmp[9] & 0x0f) << 8) + xmp[10]) * mtb;
444 /* Minimum Active to Active/Refresh Delay Time (tRCmin) */
445 dimm->tRC = (((xmp[9] & 0xf0) << 4) + xmp[11]) * mtb;
446 /* Minimum Refresh Recovery Delay Time (tRFCmin) */
447 dimm->tRFC = ((xmp[15] << 8) + xmp[14]) * mtb;
448 /* Minimum Internal Write to Read Command Delay Time (tWTRmin) */
449 dimm->tWTR = xmp[20] * mtb;
450 /* Minimum Internal Read to Precharge Command Delay Time (tRTPmin) */
451 dimm->tRTP = xmp[16] * mtb;
452 /* Minimum Four Activate Window Delay Time (tFAWmin) */
453 dimm->tFAW = (((xmp[18] & 0x0f) << 8) + xmp[19]) * mtb;
454
455 return ret;
456}
457
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500458/*
459 * The information printed below has a more informational character, and is not
460 * necessarily tied in to RAM init debugging. Hence, we stop using printram(),
461 * and use the standard printk()'s below.
462 */
463
464static void print_ns(const char *msg, u32 val)
465{
466 u32 mant, fp;
467 mant = val / 256;
468 fp = (val % 256) * 1000 / 256;
469
470 printk(BIOS_INFO, "%s%3u.%.3u ns\n", msg, mant, fp);
471}
472
473/**
474* \brief Print the info in DIMM
475*
476* Print info about the DIMM. Useful to use when CONFIG_DEBUG_RAM_SETUP is
477* selected, or for a purely informative output.
478*
Martin Roth63373ed2013-07-08 16:24:19 -0600479* @param dimm pointer to already decoded @ref dimm_attr structure
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500480*/
481void dram_print_spd_ddr3(const dimm_attr * dimm)
482{
483 u16 val16;
484 int i;
485
486 printk(BIOS_INFO, " Row addr bits : %u\n", dimm->row_bits);
487 printk(BIOS_INFO, " Column addr bits : %u\n", dimm->col_bits);
488 printk(BIOS_INFO, " Number of ranks : %u\n", dimm->ranks);
489 printk(BIOS_INFO, " DIMM Capacity : %u MB\n", dimm->size_mb);
490
491 /* CAS Latencies Supported */
492 val16 = dimm->cas_supported;
493 printk(BIOS_INFO, " CAS latencies :");
494 i = 0;
495 do {
496 if (val16 & 1)
497 printk(BIOS_INFO, " %u", i + 4);
498 i++;
499 val16 >>= 1;
500 } while (val16);
501 printk(BIOS_INFO, "\n");
502
503 print_ns(" tCKmin : ", dimm->tCK);
504 print_ns(" tAAmin : ", dimm->tAA);
505 print_ns(" tWRmin : ", dimm->tWR);
506 print_ns(" tRCDmin : ", dimm->tRCD);
507 print_ns(" tRRDmin : ", dimm->tRRD);
508 print_ns(" tRPmin : ", dimm->tRP);
509 print_ns(" tRASmin : ", dimm->tRAS);
510 print_ns(" tRCmin : ", dimm->tRC);
511 print_ns(" tRFCmin : ", dimm->tRFC);
512 print_ns(" tWTRmin : ", dimm->tWTR);
513 print_ns(" tRTPmin : ", dimm->tRTP);
514 print_ns(" tFAWmin : ", dimm->tFAW);
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500515}
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500516
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500517/*==============================================================================
518 *= DDR3 MRS helpers
519 *----------------------------------------------------------------------------*/
520
521/*
522 * MRS command structure:
523 * cmd[15:0] = Address pins MA[15:0]
524 * cmd[18:16] = Bank address BA[2:0]
525 */
526
527/* Map tWR value to a bitmask of the MR0 cycle */
528static u16 ddr3_twr_to_mr0_map(u8 twr)
529{
530 if ((twr >= 5) && (twr <= 8))
531 return (twr - 4) << 9;
532
533 /*
534 * From 8T onwards, we can only use even values. Round up if we are
535 * given an odd value.
536 */
537 if ((twr >= 9) && (twr <= 14))
538 return ((twr + 1) >> 1) << 9;
539
540 /* tWR == 16T is [000] */
541 return 0;
542}
543
544/* Map the CAS latency to a bitmask for the MR0 cycle */
545static u16 ddr3_cas_to_mr0_map(u8 cas)
546{
547 u16 mask = 0;
548 /* A[6:4] are bits [2:0] of (CAS - 4) */
549 mask = ((cas - 4) & 0x07) << 4;
550
551 /* A2 is the MSB of (CAS - 4) */
552 if ((cas - 4) & (1 << 3))
553 mask |= (1 << 2);
554
555 return mask;
556}
557
558/**
559 * \brief Get command address for a DDR3 MR0 command
560 *
561 * The DDR3 specification only covers odd write_recovery up to 7T. If an odd
562 * write_recovery greater than 7 is specified, it will be rounded up. If a tWR
563 * greater than 8 is specified, it is recommended to explicitly round it up or
564 * down before calling this function.
565 *
566 * write_recovery and cas are given in clock cycles. For example, a CAS of 7T
567 * should be given as 7.
568 *
Martin Roth98b698c2015-01-06 21:02:52 -0700569 * @param precharge_pd
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500570 * @param write_recovery Write recovery latency, tWR in clock cycles.
Martin Roth98b698c2015-01-06 21:02:52 -0700571 * @param dll_reset
572 * @param mode
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500573 * @param cas CAS latency in clock cycles.
Martin Roth98b698c2015-01-06 21:02:52 -0700574 * @param burst_type
575 * @param burst_length
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500576 */
577mrs_cmd_t ddr3_get_mr0(enum ddr3_mr0_precharge precharge_pd,
578 u8 write_recovery,
579 enum ddr3_mr0_dll_reset dll_reset,
580 enum ddr3_mr0_mode mode,
581 u8 cas,
582 enum ddr3_mr0_burst_type burst_type,
583 enum ddr3_mr0_burst_length burst_length)
584{
585 mrs_cmd_t cmd = 0 << 16;
586
587 if (precharge_pd == DDR3_MR0_PRECHARGE_FAST)
588 cmd |= (1 << 12);
589
590 cmd |= ddr3_twr_to_mr0_map(write_recovery);
591
592 if (dll_reset == DDR3_MR0_DLL_RESET_YES)
593 cmd |= (1 << 8);
594
595 if (mode == DDR3_MR0_MODE_TEST)
596 cmd |= (1 << 7);
597
598 cmd |= ddr3_cas_to_mr0_map(cas);
599
600 if (burst_type == DDR3_MR0_BURST_TYPE_INTERLEAVED)
601 cmd |= (1 << 3);
602
603 cmd |= (burst_length & 0x03) << 0;
604
605 return cmd;
606}
607
608static u16 ddr3_rtt_nom_to_mr1_map(enum ddr3_mr1_rtt_nom rtt_nom)
609{
610 u16 mask = 0;
611 /* A9 <-> rtt_nom[2] */
612 if (rtt_nom & (1 << 2))
613 mask |= (1 << 9);
614 /* A6 <-> rtt_nom[1] */
615 if (rtt_nom & (1 << 1))
616 mask |= (1 << 6);
617 /* A2 <-> rtt_nom[0] */
618 if (rtt_nom & (1 << 0))
619 mask |= (1 << 2);
620
621 return mask;
622}
623
624static u16 ddr3_ods_to_mr1_map(enum ddr3_mr1_ods ods)
625{
626 u16 mask = 0;
627 /* A5 <-> ods[1] */
628 if (ods & (1 << 1))
629 mask |= (1 << 5);
630 /* A1 <-> ods[0] */
631 if (ods & (1 << 0))
632 mask |= (1 << 1);
633
634 return mask;
635}
636
637/**
638 * \brief Get command address for a DDR3 MR1 command
639 */
640mrs_cmd_t ddr3_get_mr1(enum ddr3_mr1_qoff qoff,
641 enum ddr3_mr1_tqds tqds,
642 enum ddr3_mr1_rtt_nom rtt_nom,
643 enum ddr3_mr1_write_leveling write_leveling,
644 enum ddr3_mr1_ods ods,
645 enum ddr3_mr1_additive_latency additive_latency,
646 enum ddr3_mr1_dll dll_disable)
647{
648 mrs_cmd_t cmd = 1 << 16;
649
650 if (qoff == DDR3_MR1_QOFF_DISABLE)
651 cmd |= (1 << 12);
652
653 if (tqds == DDR3_MR1_TQDS_ENABLE)
654 cmd |= (1 << 11);
655
656 cmd |= ddr3_rtt_nom_to_mr1_map(rtt_nom);
657
658 if (write_leveling == DDR3_MR1_WRLVL_ENABLE)
659 cmd |= (1 << 7);
660
661 cmd |= ddr3_ods_to_mr1_map(ods);
662
663 cmd |= (additive_latency & 0x03) << 3;
664
665 if (dll_disable == DDR3_MR1_DLL_DISABLE)
666 cmd |= (1 << 0);
667
668 return cmd;
669}
670
671/**
672 * \brief Get command address for a DDR3 MR2 command
673 *
674 * cas_cwl is given in clock cycles. For example, a cas_cwl of 7T should be
675 * given as 7.
676 *
Martin Roth98b698c2015-01-06 21:02:52 -0700677 * @param rtt_wr
678 * @param extended_temp
679 * @param self_refresh
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500680 * @param cas_cwl CAS write latency in clock cycles.
681 */
Martin Roth98b698c2015-01-06 21:02:52 -0700682
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500683mrs_cmd_t ddr3_get_mr2(enum ddr3_mr2_rttwr rtt_wr,
684 enum ddr3_mr2_srt_range extended_temp,
685 enum ddr3_mr2_asr self_refresh, u8 cas_cwl)
686{
687 mrs_cmd_t cmd = 2 << 16;
688
689 cmd |= (rtt_wr & 0x03) << 9;
690
691 if (extended_temp == DDR3_MR2_SRT_EXTENDED)
692 cmd |= (1 << 7);
693
694 if (self_refresh == DDR3_MR2_ASR_AUTO)
695 cmd |= (1 << 6);
696
697 cmd |= ((cas_cwl - 5) & 0x07) << 3;
698
699 return cmd;
700}
701
702/**
703 * \brief Get command address for a DDR3 MR3 command
704 *
705 * @param dataflow_from_mpr Specify a non-zero value to put DRAM in read
706 * leveling mode. Zero for normal operation.
707 */
708mrs_cmd_t ddr3_get_mr3(char dataflow_from_mpr)
709{
710 mrs_cmd_t cmd = 3 << 16;
711
712 if (dataflow_from_mpr)
713 cmd |= (1 << 2);
714
715 return cmd;
716}
717
718/**
719 * \brief Mirror the address bits for this MRS command
720 *
721 * Swap the following bits in the MRS command:
722 * - MA3 <-> MA4
723 * - MA5 <-> MA6
724 * - MA7 <-> MA8
725 * - BA0 <-> BA1
726 */
727mrs_cmd_t ddr3_mrs_mirror_pins(mrs_cmd_t cmd)
728{
729 u32 downshift, upshift;
730 /* High bits= A4 | A6 | A8 | BA1 */
731 /* Low bits = A3 | A5 | A7 | BA0 */
732 u32 lowbits = (1 << 3) | (1 << 5) | (1 << 7) | (1 << 16);
733 downshift = (cmd & (lowbits << 1));
734 upshift = (cmd & lowbits);
735 cmd &= ~(lowbits | (lowbits << 1));
736 cmd |= (downshift >> 1) | (upshift << 1);
737 return cmd;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500738}