blob: 07560955885a1350e766ca8635f4b54ae08f6cfe [file] [log] [blame]
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 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
Elyes HAOUASaa8e7e72016-06-19 12:38:47 +020017/*
18 * JEDEC Standard No. 21-C
19 * Annex K: Serial Presence Detect (SPD) for DDR3 SDRAM Modules 2014
20 * http://www.jedec.org/sites/default/files/docs/4_01_02_11R24.pdf
21 */
22
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050023#ifndef DEVICE_DRAM_DDR3L_H
24#define DEVICE_DRAM_DDR3L_H
25
26/**
27 * @file ddr3.h
28 *
29 * \brief Utilities for decoding DDR3 SPDs
30 */
31
32#include <stdint.h>
33#include <spd.h>
Arthur Heymansfc31e442018-02-12 15:12:34 +010034#include <device/dram/common.h>
35
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050036
37/**
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -050038 * Convenience definitions for SPD offsets
39 *
40 * @{
41 */
42#define SPD_DIMM_MOD_ID1 117
43#define SPD_DIMM_MOD_ID2 118
44#define SPD_DIMM_SERIAL_NUM 122
45#define SPD_DIMM_SERIAL_LEN 4
46#define SPD_DIMM_PART_NUM 128
47#define SPD_DIMM_PART_LEN 18
48/** @} */
49
50/**
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050051 * \brief Convenience macro for enabling printk with CONFIG_DEBUG_RAM_SETUP
52 *
53 * Use this macro instead of printk(); for verbose RAM initialization messages.
54 * When CONFIG_DEBUG_RAM_SETUP is not selected, these messages are automatically
55 * disabled.
56 * @{
57 */
Martin Rothc4e49f62015-07-11 13:42:54 -060058#if IS_ENABLED(CONFIG_DEBUG_RAM_SETUP)
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050059#define printram(x, ...) printk(BIOS_DEBUG, x, ##__VA_ARGS__)
60#else
61#define printram(x, ...)
62#endif
63/** @} */
64
65/*
66 * Module type (byte 3, bits 3:0) of SPD
Martin Roth0cb07e32013-07-09 21:46:01 -060067 * This definition is specific to DDR3. DDR2 SPDs have a different structure.
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050068 */
69enum spd_dimm_type {
70 SPD_DIMM_TYPE_UNDEFINED = 0x00,
71 SPD_DIMM_TYPE_RDIMM = 0x01,
72 SPD_DIMM_TYPE_UDIMM = 0x02,
73 SPD_DIMM_TYPE_SO_DIMM = 0x03,
74 SPD_DIMM_TYPE_MICRO_DIMM = 0x04,
75 SPD_DIMM_TYPE_MINI_RDIMM = 0x05,
76 SPD_DIMM_TYPE_MINI_UDIMM = 0x06,
77 SPD_DIMM_TYPE_MINI_CDIMM = 0x07,
78 SPD_DIMM_TYPE_72B_SO_UDIMM = 0x08,
79 SPD_DIMM_TYPE_72B_SO_RDIMM = 0x09,
80 SPD_DIMM_TYPE_72B_SO_CDIMM = 0x0a,
81 SPD_DIMM_TYPE_LRDIMM = 0x0b,
Elyes HAOUASaa8e7e72016-06-19 12:38:47 +020082 SPD_DIMM_TYPE_16B_SO_DIMM = 0x0c,
83 SPD_DIMM_TYPE_32B_SO_DIMM = 0x0d,
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050084 /* Masks to bits 3:0 to give the dimm type */
85 SPD_DIMM_TYPE_MASK = 0x0f,
86};
87
88/**
89 * \brief DIMM flags
90 *
91 * Characteristic flags for the DIMM, as presented by the SPD
92 */
93typedef union dimm_flags_st {
94 /* The whole point of the union/struct construct is to allow us to clear
95 * all the bits with one line: flags.raw = 0.
96 * We do not care how these bits are ordered */
97 struct {
98 /* Indicates if rank 1 of DIMM uses a mirrored pin mapping. See:
99 * Annex K: Serial Presence Detect (SPD) for DDR3 SDRAM */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800100 unsigned int pins_mirrored:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500101 /* Module can work at 1.50V - All DIMMS must be 1.5V operable */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800102 unsigned int operable_1_50V:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500103 /* Module can work at 1.35V */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800104 unsigned int operable_1_35V:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500105 /* Module can work at 1.20V */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800106 unsigned int operable_1_25V:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500107 /* Has an 8-bit bus extension, meaning the DIMM supports ECC */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800108 unsigned int is_ecc:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500109 /* DLL-Off Mode Support */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800110 unsigned int dll_off_mode:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500111 /* Indicates a drive strength of RZQ/6 (40 Ohm) is supported */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800112 unsigned int rzq6_supported:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500113 /* Indicates a drive strength of RZQ/7 (35 Ohm) is supported */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800114 unsigned int rzq7_supported:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500115 /* Partial Array Self Refresh */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800116 unsigned int pasr:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500117 /* On-die Thermal Sensor Readout */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800118 unsigned int odts:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500119 /* Auto Self Refresh */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800120 unsigned int asr:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500121 /* Extended temperature range supported */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800122 unsigned int ext_temp_range:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500123 /* Operating at extended temperature requires 2X refresh rate */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800124 unsigned int ext_temp_refresh:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500125 /* Thermal sensor incorporated */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800126 unsigned int therm_sensor:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500127 };
Lee Leahy0ca2a062017-03-06 18:01:04 -0800128 unsigned int raw;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500129} dimm_flags_t;
130
131/**
132 * \brief DIMM characteristics
133 *
134 * The characteristics of each DIMM, as presented by the SPD
135 */
136typedef struct dimm_attr_st {
137 enum spd_memory_type dram_type;
Vladimir Serbinenko0e675f72014-12-07 13:56:48 +0100138 enum spd_dimm_type dimm_type;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500139 u16 cas_supported;
140 /* Flags extracted from SPD */
141 dimm_flags_t flags;
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200142 /* SDRAM width */
143 u8 width;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500144 /* Number of ranks */
145 u8 ranks;
146 /* Number or row address bits */
147 u8 row_bits;
148 /* Number or column address bits */
149 u8 col_bits;
150 /* Size of module in MiB */
151 u32 size_mb;
152 /* Latencies are in units of 1/256 ns */
153 u32 tCK;
154 u32 tAA;
155 u32 tWR;
156 u32 tRCD;
157 u32 tRRD;
158 u32 tRP;
159 u32 tRAS;
160 u32 tRC;
161 u32 tRFC;
162 u32 tWTR;
163 u32 tRTP;
164 u32 tFAW;
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200165
166 u8 reference_card;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100167 /* XMP: Module voltage in mV */
168 u16 voltage;
169 /* XMP: max DIMMs per channel supported (1-4) */
170 u8 dimms_per_channel;
Patrick Rudolph07691592016-02-29 18:21:00 +0100171 /* Manufacturer ID */
172 u16 manufacturer_id;
173 /* ASCII part number - NULL terminated */
174 u8 part_number[17];
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500175} dimm_attr;
176
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100177enum ddr3_xmp_profile {
178 DDR3_XMP_PROFILE_1 = 0,
179 DDR3_XMP_PROFILE_2 = 1,
180};
181
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500182typedef u8 spd_raw_data[256];
183
Alexandru Gagniuc4c37e582013-12-17 13:08:01 -0500184u16 spd_ddr3_calc_crc(u8 *spd, int len);
Kyösti Mälkki7dc4b842016-11-18 18:41:17 +0200185u16 spd_ddr3_calc_unique_crc(u8 *spd, int len);
Lee Leahy6d71a432017-03-07 15:24:16 -0800186int spd_decode_ddr3(dimm_attr *dimm, spd_raw_data spd_data);
Patrick Rudolph6e53ae62017-01-31 19:43:17 +0100187int spd_dimm_is_registered_ddr3(enum spd_dimm_type type);
Lee Leahy6d71a432017-03-07 15:24:16 -0800188void dram_print_spd_ddr3(const dimm_attr *dimm);
189int spd_xmp_decode_ddr3(dimm_attr *dimm,
Lee Leahy708fc272017-03-07 12:18:53 -0800190 spd_raw_data spd,
191 enum ddr3_xmp_profile profile);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500192
193/**
194 * \brief Read double word from specified address
195 *
196 * Should be useful when doing an MRS to the DIMM
197 */
Stefan Reinauer1e2500e2015-06-19 14:59:06 -0700198static inline u32 volatile_read(volatile uintptr_t addr)
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500199{
200 volatile u32 result;
201 result = *(volatile u32 *)addr;
202 return result;
203}
204
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500205/**
206 * \brief Representation of an MRS command
207 *
208 * This represents an MRS command as seen by the DIMM. This is not a memory
209 * address that can be read to generate an MRS command. The mapping of CPU
210 * to memory pins is hardware-dependent.
211 * \n
212 * The idea is to generalize the MRS code, and only need a hardware-specific
213 * function to map the MRS bits to CPU address bits. An MRS command can be
214 * sent like:
215 * @code{.c}
216 * u32 addr;
217 * mrs_cmd_t mrs;
218 * chipset_enable_mrs_command_mode();
219 * mrs = ddr3_get_mr2(rtt_wr, srt, asr, cwl)
220 * if (rank_has_mirrorred_pins)
221 * mrs = ddr3_mrs_mirror_pins(mrs);
222 * addr = chipset_specific_get_mrs_addr(mrs);
223 * volatile_read(addr);
224 * @endcode
225 *
226 * The MRS representation has the following structure:
227 * - cmd[15:0] = Address pins MA[15:0]
228 * - cmd[18:16] = Bank address BA[2:0]
229 */
230typedef u32 mrs_cmd_t;
231
232enum ddr3_mr0_precharge {
233 DDR3_MR0_PRECHARGE_SLOW = 0,
234 DDR3_MR0_PRECHARGE_FAST = 1,
235};
236enum ddr3_mr0_mode {
237 DDR3_MR0_MODE_NORMAL = 0,
238 DDR3_MR0_MODE_TEST = 1,
239};
240enum ddr3_mr0_dll_reset {
241 DDR3_MR0_DLL_RESET_NO = 0,
242 DDR3_MR0_DLL_RESET_YES = 1,
243};
244enum ddr3_mr0_burst_type {
245 DDR3_MR0_BURST_TYPE_SEQUENTIAL = 0,
246 DDR3_MR0_BURST_TYPE_INTERLEAVED = 1,
247};
248enum ddr3_mr0_burst_length {
249 DDR3_MR0_BURST_LENGTH_8 = 0,
250 DDR3_MR0_BURST_LENGTH_CHOP = 1,
251 DDR3_MR0_BURST_LENGTH_4 = 2,
252};
253mrs_cmd_t ddr3_get_mr0(enum ddr3_mr0_precharge precharge_pd,
254 u8 write_recovery,
255 enum ddr3_mr0_dll_reset dll_reset,
256 enum ddr3_mr0_mode mode,
257 u8 cas,
258 enum ddr3_mr0_burst_type interleaved_burst,
259 enum ddr3_mr0_burst_length burst_length);
260
261enum ddr3_mr1_qoff {
262 DDR3_MR1_QOFF_ENABLE = 0,
263 DDR3_MR1_QOFF_DISABLE = 1,
264};
265enum ddr3_mr1_tqds {
266 DDR3_MR1_TQDS_DISABLE = 0,
267 DDR3_MR1_TQDS_ENABLE = 1,
268};
269enum ddr3_mr1_write_leveling {
270 DDR3_MR1_WRLVL_DISABLE = 0,
271 DDR3_MR1_WRLVL_ENABLE = 1,
272};
273enum ddr3_mr1_rtt_nom {
274 DDR3_MR1_RTT_NOM_OFF = 0,
275 DDR3_MR1_RTT_NOM_RZQ4 = 1,
276 DDR3_MR1_RTT_NOM_RZQ2 = 2,
277 DDR3_MR1_RTT_NOM_RZQ6 = 3,
278 DDR3_MR1_RTT_NOM_RZQ12 = 4,
279 DDR3_MR1_RTT_NOM_RZQ8 = 5,
280};
281enum ddr3_mr1_additive_latency {
282 DDR3_MR1_AL_DISABLE = 0,
283 DDR3_MR1_AL_CL_MINUS_1 = 1,
284 DDR3_MR1_AL_CL_MINUS_2 = 2,
285};
286enum ddr3_mr1_ods {
287 DDR3_MR1_ODS_RZQ6 = 0,
288 DDR3_MR1_ODS_RZQ7 = 1,
289};
290enum ddr3_mr1_dll {
291 DDR3_MR1_DLL_ENABLE = 0,
292 DDR3_MR1_DLL_DISABLE = 1,
293};
294
295mrs_cmd_t ddr3_get_mr1(enum ddr3_mr1_qoff qoff,
296 enum ddr3_mr1_tqds tqds,
297 enum ddr3_mr1_rtt_nom rtt_nom,
298 enum ddr3_mr1_write_leveling write_leveling,
299 enum ddr3_mr1_ods output_drive_strenght,
300 enum ddr3_mr1_additive_latency additive_latency,
301 enum ddr3_mr1_dll dll_disable);
302
303enum ddr3_mr2_rttwr {
304 DDR3_MR2_RTTWR_OFF = 0,
305 DDR3_MR2_RTTWR_RZQ4 = 1,
306 DDR3_MR2_RTTWR_RZQ2 = 2,
307};
308enum ddr3_mr2_srt_range {
309 DDR3_MR2_SRT_NORMAL = 0,
310 DDR3_MR2_SRT_EXTENDED = 1,
311};
312enum ddr3_mr2_asr {
313 DDR3_MR2_ASR_MANUAL = 0,
314 DDR3_MR2_ASR_AUTO = 1,
315};
316
317mrs_cmd_t ddr3_get_mr2(enum ddr3_mr2_rttwr rtt_wr,
318 enum ddr3_mr2_srt_range extended_temp,
319 enum ddr3_mr2_asr self_refresh, u8 cas_cwl);
320
321mrs_cmd_t ddr3_get_mr3(char dataflow_from_mpr);
322mrs_cmd_t ddr3_mrs_mirror_pins(mrs_cmd_t cmd);
323
Martin Rothfd277d82016-01-11 12:47:30 -0700324#endif /* DEVICE_DRAM_DDR3L_H */