blob: 9b4f490630ff14d1a72ca371986adf73e676adf3 [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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/**
21 * @file ddr3_util.h
22 *
23 * \brief Utilities for decoding DDR3 SPDs
24 */
25
26#include <console/console.h>
27#include <device/device.h>
28#include <device/dram/ddr3.h>
29
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -050030/*==============================================================================
31 * = DDR3 SPD decoding helpers
32 *----------------------------------------------------------------------------*/
33
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050034/**
35 * \brief Checks if the DIMM is Registered based on byte[3] of the SPD
36 *
37 * Tells if the DIMM type is registered or not.
38 *
39 * @param type DIMM type. This is byte[3] of the SPD.
40 */
41int dimm_is_registered(enum spd_dimm_type type)
42{
43 if ((type == SPD_DIMM_TYPE_RDIMM)
44 | (type == SPD_DIMM_TYPE_MINI_RDIMM)
45 | (type == SPD_DIMM_TYPE_72B_SO_RDIMM))
46 return 1;
47
48 return 0;
49}
50
51/**
Alexandru Gagniuc4c37e582013-12-17 13:08:01 -050052 * \brief Calculate the CRC of a DDR3 SPD
53 *
54 * @param spd pointer to raw SPD data
55 * @param len length of data in SPD
56 *
57 * @return the CRC of the SPD data, or 0 when spd data is truncated.
58 */
59u16 spd_ddr3_calc_crc(u8 *spd, int len)
60{
61 int n_crc, i;
62 u8 *ptr;
63 u16 crc;
64
65 /* Find the number of bytes covered by CRC */
66 if (spd[0] & 0x80) {
67 n_crc = 117;
68 } else {
69 n_crc = 126;
70 }
71
72 if (len < n_crc)
73 /* Not enough bytes available to get the CRC */
74 return 0;
75
76 /* Compute the CRC */
77 crc = 0;
78 ptr = spd;
79 while (--n_crc >= 0) {
80 crc = crc ^ (int)*ptr++ << 8;
81 for (i = 0; i < 8; ++i)
82 if (crc & 0x8000) {
83 crc = crc << 1 ^ 0x1021;
84 } else {
85 crc = crc << 1;
86 }
87 }
88 return crc;
89}
90
91/**
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050092 * \brief Decode the raw SPD data
93 *
94 * Decodes a raw SPD data from a DDR3 DIMM, and organizes it into a
95 * @ref dimm_attr structure. The SPD data must first be read in a contiguous
96 * array, and passed to this function.
97 *
Martin Roth63373ed2013-07-08 16:24:19 -060098 * @param dimm pointer to @ref dimm_attr structure where the decoded data is to
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050099 * be stored
100 * @param spd array of raw data previously read from the SPD.
101 *
102 * @return @ref spd_status enumerator
103 * SPD_STATUS_OK -- decoding was successful
104 * SPD_STATUS_INVALID -- invalid SPD or not a DDR3 SPD
105 * SPD_STATUS_CRC_ERROR -- CRC did not verify
106 * SPD_STATUS_INVALID_FIELD -- A field with an invalid value was
107 * detected.
108 */
109int spd_decode_ddr3(dimm_attr * dimm, spd_raw_data spd)
110{
Alexandru Gagniuc4c37e582013-12-17 13:08:01 -0500111 int ret;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500112 u16 crc, spd_crc;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500113 u8 ftb_divisor, ftb_dividend, capacity_shift, bus_width, sdram_width;
114 u8 reg8;
115 u32 mtb; /* medium time base */
116 unsigned int val, param;
117
118 ret = SPD_STATUS_OK;
119
120 /* Don't assume we memset 0 dimm struct. Clear all our flags */
121 dimm->flags.raw = 0;
122 /* Make sure that the SPD dump is indeed from a DDR3 module */
123 if (spd[2] != SPD_MEMORY_TYPE_SDRAM_DDR3) {
124 printram("Not a DDR3 SPD!\n");
125 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
126 return SPD_STATUS_INVALID;
127 }
128 dimm->dram_type = SPD_MEMORY_TYPE_SDRAM_DDR3;
129
Alexandru Gagniuc4c37e582013-12-17 13:08:01 -0500130 crc = spd_ddr3_calc_crc(spd, sizeof(spd));
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) {
135 printram("ERROR: SPD CRC failed!!!");
136 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;
185 printram(" 1.25V");
186 }
187 if (reg8 & (1 << 1)) {
188 dimm->flags.operable_1_35V = 1;
189 printram(" 1.35V");
190 }
191 if (!(reg8 & (1 << 0))) {
192 dimm->flags.operable_1_50V = 1;
193 printram(" 1.5V");
194 }
195 printram("\n");
196
197 /* Module organization */
198 reg8 = spd[7];
199 /* Number of ranks */
200 val = (reg8 >> 3) & 0x07;
201 if (val > 3) {
202 printram(" Invalid number of ranks\n");
203 ret = SPD_STATUS_INVALID_FIELD;
204 }
205 dimm->ranks = val + 1;
206 /* SDRAM device width */
207 val = (reg8 & 0x07);
208 if (val > 3) {
209 printram(" Invalid SDRAM width\n");
210 ret = SPD_STATUS_INVALID_FIELD;
211 }
212 sdram_width = (4 << val);
213 printram(" SDRAM width : %u\n", sdram_width);
214
215 /* Memory bus width */
216 reg8 = spd[8];
217 /* Bus extension */
218 val = (reg8 >> 3) & 0x03;
219 if (val > 1) {
220 printram(" Invalid bus extension\n");
221 ret = SPD_STATUS_INVALID_FIELD;
222 }
223 dimm->flags.is_ecc = val ? 1 : 0;
224 printram(" Bus extension : %u bits\n", val ? 8 : 0);
225 /* Bus width */
226 val = reg8 & 0x07;
227 if (val > 3) {
228 printram(" Invalid bus width\n");
229 ret = SPD_STATUS_INVALID_FIELD;
230 }
231 bus_width = 8 << val;
232 printram(" Bus width : %u\n", bus_width);
233
234 /* We have all the info we need to compute the dimm size */
235 /* Capacity is 256Mbit multiplied by the power of 2 specified in
236 * capacity_shift
237 * The rest is the JEDEC formula */
238 dimm->size_mb = ((1 << (capacity_shift + (25 - 20))) * bus_width
239 * dimm->ranks) / sdram_width;
240
241 /* Fine Timebase (FTB) Dividend/Divisor */
242 /* Dividend */
243 ftb_dividend = (spd[9] >> 4) & 0x0f;
244 /* Divisor */
245 ftb_divisor = spd[9] & 0x0f;
246
247 /* Medium Timebase =
248 * Medium Timebase (MTB) Dividend /
249 * Medium Timebase (MTB) Divisor */
250 mtb = (((u32) spd[10]) << 8) / spd[11];
251
252 /* SDRAM Minimum Cycle Time (tCKmin) */
253 dimm->tCK = spd[12] * mtb;
254 /* CAS Latencies Supported */
255 dimm->cas_supported = (spd[15] << 8) + spd[14];
256 /* Minimum CAS Latency Time (tAAmin) */
257 dimm->tAA = spd[16] * mtb;
258 /* Minimum Write Recovery Time (tWRmin) */
259 dimm->tWR = spd[17] * mtb;
260 /* Minimum RAS# to CAS# Delay Time (tRCDmin) */
261 dimm->tRCD = spd[18] * mtb;
262 /* Minimum Row Active to Row Active Delay Time (tRRDmin) */
263 dimm->tRRD = spd[19] * mtb;
264 /* Minimum Row Precharge Delay Time (tRPmin) */
265 dimm->tRP = spd[20] * mtb;
266 /* Minimum Active to Precharge Delay Time (tRASmin) */
267 dimm->tRAS = (((spd[21] & 0x0f) << 8) + spd[22]) * mtb;
268 /* Minimum Active to Active/Refresh Delay Time (tRCmin) */
269 dimm->tRC = (((spd[21] & 0xf0) << 4) + spd[23]) * mtb;
270 /* Minimum Refresh Recovery Delay Time (tRFCmin) */
271 dimm->tRFC = ((spd[25] << 8) + spd[24]) * mtb;
272 /* Minimum Internal Write to Read Command Delay Time (tWTRmin) */
273 dimm->tWTR = spd[26] * mtb;
274 /* Minimum Internal Read to Precharge Command Delay Time (tRTPmin) */
275 dimm->tRTP = spd[27] * mtb;
276 /* Minimum Four Activate Window Delay Time (tFAWmin) */
277 dimm->tFAW = (((spd[28] & 0x0f) << 8) + spd[29]) * mtb;
278
279 /* SDRAM Optional Features */
280 reg8 = spd[30];
281 printram(" Optional features :");
282 if (reg8 & 0x80) {
283 dimm->flags.dll_off_mode = 1;
284 printram(" DLL-Off_mode");
285 }
286 if (reg8 & 0x02) {
287 dimm->flags.rzq7_supported = 1;
288 printram(" RZQ/7");
289 }
290 if (reg8 & 0x01) {
291 dimm->flags.rzq6_supported = 1;
292 printram(" RZQ/6");
293 }
294 printram("\n");
295
296 /* SDRAM Thermal and Refresh Options */
297 reg8 = spd[31];
298 printram(" Thermal features :");
299 if (reg8 & 0x80) {
300 dimm->flags.pasr = 1;
301 printram(" PASR");
302 }
303 if (reg8 & 0x08) {
304 dimm->flags.odts = 1;
305 printram(" ODTS");
306 }
307 if (reg8 & 0x04) {
308 dimm->flags.asr = 1;
309 printram(" ASR");
310 }
311 if (reg8 & 0x02) {
312 dimm->flags.ext_temp_range = 1;
313 printram(" ext_temp_refresh");
314 }
315 if (reg8 & 0x01) {
316 dimm->flags.ext_temp_refresh = 1;
317 printram(" ext_temp_range");
318 }
319 printram("\n");
320
321 /* Module Thermal Sensor */
322 reg8 = spd[32];
323 if (reg8 & 0x80)
324 dimm->flags.therm_sensor = 1;
325 printram(" Thermal sensor : %s\n",
326 dimm->flags.therm_sensor ? "yes" : "no");
327
328 /* SDRAM Device Type */
329 reg8 = spd[33];
330 printram(" Standard SDRAM : %s\n", (reg8 & 0x80) ? "no" : "yes");
331
332 if (spd[63] & 0x01) {
333 dimm->flags.pins_mirrored = 1;
Martin Roth63373ed2013-07-08 16:24:19 -0600334 printram(" DIMM Rank1 Address bits mirrored!!!\n");
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500335 }
336
337 return ret;
338}
339
340/*
341 * The information printed below has a more informational character, and is not
342 * necessarily tied in to RAM init debugging. Hence, we stop using printram(),
343 * and use the standard printk()'s below.
344 */
345
346static void print_ns(const char *msg, u32 val)
347{
348 u32 mant, fp;
349 mant = val / 256;
350 fp = (val % 256) * 1000 / 256;
351
352 printk(BIOS_INFO, "%s%3u.%.3u ns\n", msg, mant, fp);
353}
354
355/**
356* \brief Print the info in DIMM
357*
358* Print info about the DIMM. Useful to use when CONFIG_DEBUG_RAM_SETUP is
359* selected, or for a purely informative output.
360*
Martin Roth63373ed2013-07-08 16:24:19 -0600361* @param dimm pointer to already decoded @ref dimm_attr structure
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500362*/
363void dram_print_spd_ddr3(const dimm_attr * dimm)
364{
365 u16 val16;
366 int i;
367
368 printk(BIOS_INFO, " Row addr bits : %u\n", dimm->row_bits);
369 printk(BIOS_INFO, " Column addr bits : %u\n", dimm->col_bits);
370 printk(BIOS_INFO, " Number of ranks : %u\n", dimm->ranks);
371 printk(BIOS_INFO, " DIMM Capacity : %u MB\n", dimm->size_mb);
372
373 /* CAS Latencies Supported */
374 val16 = dimm->cas_supported;
375 printk(BIOS_INFO, " CAS latencies :");
376 i = 0;
377 do {
378 if (val16 & 1)
379 printk(BIOS_INFO, " %u", i + 4);
380 i++;
381 val16 >>= 1;
382 } while (val16);
383 printk(BIOS_INFO, "\n");
384
385 print_ns(" tCKmin : ", dimm->tCK);
386 print_ns(" tAAmin : ", dimm->tAA);
387 print_ns(" tWRmin : ", dimm->tWR);
388 print_ns(" tRCDmin : ", dimm->tRCD);
389 print_ns(" tRRDmin : ", dimm->tRRD);
390 print_ns(" tRPmin : ", dimm->tRP);
391 print_ns(" tRASmin : ", dimm->tRAS);
392 print_ns(" tRCmin : ", dimm->tRC);
393 print_ns(" tRFCmin : ", dimm->tRFC);
394 print_ns(" tWTRmin : ", dimm->tWTR);
395 print_ns(" tRTPmin : ", dimm->tRTP);
396 print_ns(" tFAWmin : ", dimm->tFAW);
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500397}
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500398
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500399/*==============================================================================
400 *= DDR3 MRS helpers
401 *----------------------------------------------------------------------------*/
402
403/*
404 * MRS command structure:
405 * cmd[15:0] = Address pins MA[15:0]
406 * cmd[18:16] = Bank address BA[2:0]
407 */
408
409/* Map tWR value to a bitmask of the MR0 cycle */
410static u16 ddr3_twr_to_mr0_map(u8 twr)
411{
412 if ((twr >= 5) && (twr <= 8))
413 return (twr - 4) << 9;
414
415 /*
416 * From 8T onwards, we can only use even values. Round up if we are
417 * given an odd value.
418 */
419 if ((twr >= 9) && (twr <= 14))
420 return ((twr + 1) >> 1) << 9;
421
422 /* tWR == 16T is [000] */
423 return 0;
424}
425
426/* Map the CAS latency to a bitmask for the MR0 cycle */
427static u16 ddr3_cas_to_mr0_map(u8 cas)
428{
429 u16 mask = 0;
430 /* A[6:4] are bits [2:0] of (CAS - 4) */
431 mask = ((cas - 4) & 0x07) << 4;
432
433 /* A2 is the MSB of (CAS - 4) */
434 if ((cas - 4) & (1 << 3))
435 mask |= (1 << 2);
436
437 return mask;
438}
439
440/**
441 * \brief Get command address for a DDR3 MR0 command
442 *
443 * The DDR3 specification only covers odd write_recovery up to 7T. If an odd
444 * write_recovery greater than 7 is specified, it will be rounded up. If a tWR
445 * greater than 8 is specified, it is recommended to explicitly round it up or
446 * down before calling this function.
447 *
448 * write_recovery and cas are given in clock cycles. For example, a CAS of 7T
449 * should be given as 7.
450 *
451 * @param write_recovery Write recovery latency, tWR in clock cycles.
452 * @param cas CAS latency in clock cycles.
453 */
454mrs_cmd_t ddr3_get_mr0(enum ddr3_mr0_precharge precharge_pd,
455 u8 write_recovery,
456 enum ddr3_mr0_dll_reset dll_reset,
457 enum ddr3_mr0_mode mode,
458 u8 cas,
459 enum ddr3_mr0_burst_type burst_type,
460 enum ddr3_mr0_burst_length burst_length)
461{
462 mrs_cmd_t cmd = 0 << 16;
463
464 if (precharge_pd == DDR3_MR0_PRECHARGE_FAST)
465 cmd |= (1 << 12);
466
467 cmd |= ddr3_twr_to_mr0_map(write_recovery);
468
469 if (dll_reset == DDR3_MR0_DLL_RESET_YES)
470 cmd |= (1 << 8);
471
472 if (mode == DDR3_MR0_MODE_TEST)
473 cmd |= (1 << 7);
474
475 cmd |= ddr3_cas_to_mr0_map(cas);
476
477 if (burst_type == DDR3_MR0_BURST_TYPE_INTERLEAVED)
478 cmd |= (1 << 3);
479
480 cmd |= (burst_length & 0x03) << 0;
481
482 return cmd;
483}
484
485static u16 ddr3_rtt_nom_to_mr1_map(enum ddr3_mr1_rtt_nom rtt_nom)
486{
487 u16 mask = 0;
488 /* A9 <-> rtt_nom[2] */
489 if (rtt_nom & (1 << 2))
490 mask |= (1 << 9);
491 /* A6 <-> rtt_nom[1] */
492 if (rtt_nom & (1 << 1))
493 mask |= (1 << 6);
494 /* A2 <-> rtt_nom[0] */
495 if (rtt_nom & (1 << 0))
496 mask |= (1 << 2);
497
498 return mask;
499}
500
501static u16 ddr3_ods_to_mr1_map(enum ddr3_mr1_ods ods)
502{
503 u16 mask = 0;
504 /* A5 <-> ods[1] */
505 if (ods & (1 << 1))
506 mask |= (1 << 5);
507 /* A1 <-> ods[0] */
508 if (ods & (1 << 0))
509 mask |= (1 << 1);
510
511 return mask;
512}
513
514/**
515 * \brief Get command address for a DDR3 MR1 command
516 */
517mrs_cmd_t ddr3_get_mr1(enum ddr3_mr1_qoff qoff,
518 enum ddr3_mr1_tqds tqds,
519 enum ddr3_mr1_rtt_nom rtt_nom,
520 enum ddr3_mr1_write_leveling write_leveling,
521 enum ddr3_mr1_ods ods,
522 enum ddr3_mr1_additive_latency additive_latency,
523 enum ddr3_mr1_dll dll_disable)
524{
525 mrs_cmd_t cmd = 1 << 16;
526
527 if (qoff == DDR3_MR1_QOFF_DISABLE)
528 cmd |= (1 << 12);
529
530 if (tqds == DDR3_MR1_TQDS_ENABLE)
531 cmd |= (1 << 11);
532
533 cmd |= ddr3_rtt_nom_to_mr1_map(rtt_nom);
534
535 if (write_leveling == DDR3_MR1_WRLVL_ENABLE)
536 cmd |= (1 << 7);
537
538 cmd |= ddr3_ods_to_mr1_map(ods);
539
540 cmd |= (additive_latency & 0x03) << 3;
541
542 if (dll_disable == DDR3_MR1_DLL_DISABLE)
543 cmd |= (1 << 0);
544
545 return cmd;
546}
547
548/**
549 * \brief Get command address for a DDR3 MR2 command
550 *
551 * cas_cwl is given in clock cycles. For example, a cas_cwl of 7T should be
552 * given as 7.
553 *
554 * @param cas_cwl CAS write latency in clock cycles.
555 */
556mrs_cmd_t ddr3_get_mr2(enum ddr3_mr2_rttwr rtt_wr,
557 enum ddr3_mr2_srt_range extended_temp,
558 enum ddr3_mr2_asr self_refresh, u8 cas_cwl)
559{
560 mrs_cmd_t cmd = 2 << 16;
561
562 cmd |= (rtt_wr & 0x03) << 9;
563
564 if (extended_temp == DDR3_MR2_SRT_EXTENDED)
565 cmd |= (1 << 7);
566
567 if (self_refresh == DDR3_MR2_ASR_AUTO)
568 cmd |= (1 << 6);
569
570 cmd |= ((cas_cwl - 5) & 0x07) << 3;
571
572 return cmd;
573}
574
575/**
576 * \brief Get command address for a DDR3 MR3 command
577 *
578 * @param dataflow_from_mpr Specify a non-zero value to put DRAM in read
579 * leveling mode. Zero for normal operation.
580 */
581mrs_cmd_t ddr3_get_mr3(char dataflow_from_mpr)
582{
583 mrs_cmd_t cmd = 3 << 16;
584
585 if (dataflow_from_mpr)
586 cmd |= (1 << 2);
587
588 return cmd;
589}
590
591/**
592 * \brief Mirror the address bits for this MRS command
593 *
594 * Swap the following bits in the MRS command:
595 * - MA3 <-> MA4
596 * - MA5 <-> MA6
597 * - MA7 <-> MA8
598 * - BA0 <-> BA1
599 */
600mrs_cmd_t ddr3_mrs_mirror_pins(mrs_cmd_t cmd)
601{
602 u32 downshift, upshift;
603 /* High bits= A4 | A6 | A8 | BA1 */
604 /* Low bits = A3 | A5 | A7 | BA0 */
605 u32 lowbits = (1 << 3) | (1 << 5) | (1 << 7) | (1 << 16);
606 downshift = (cmd & (lowbits << 1));
607 upshift = (cmd & lowbits);
608 cmd &= ~(lowbits | (lowbits << 1));
609 cmd |= (downshift >> 1) | (upshift << 1);
610 return cmd;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500611}