blob: ee20ba793a4da37cc534ab71f5959b1683c88c3d [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>
34
35/**
36 * \brief Convenience definitions for TCK values
37 *
38 * Different values for tCK, representing standard DDR3 frequencies.
39 * These values are in 1/256 ns units.
40 * @{
41 */
Patrick Rudolph9f3f9152016-01-26 20:02:14 +010042#define TCK_1333MHZ 192
43#define TCK_1200MHZ 212
Patrick Rudolph577aad62016-06-14 18:48:17 +020044#define TCK_1100MHZ 232
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050045#define TCK_1066MHZ 240
Patrick Rudolph577aad62016-06-14 18:48:17 +020046#define TCK_1000MHZ 256
Patrick Rudolph98b5f902016-02-06 13:23:31 +010047#define TCK_933MHZ 274
Patrick Rudolph577aad62016-06-14 18:48:17 +020048#define TCK_900MHZ 284
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050049#define TCK_800MHZ 320
Patrick Rudolph577aad62016-06-14 18:48:17 +020050#define TCK_700MHZ 365
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050051#define TCK_666MHZ 384
52#define TCK_533MHZ 480
53#define TCK_400MHZ 640
54#define TCK_333MHZ 768
55#define TCK_266MHZ 960
56#define TCK_200MHZ 1280
57/** @} */
58
59/**
60 * \brief Convenience macro for enabling printk with CONFIG_DEBUG_RAM_SETUP
61 *
62 * Use this macro instead of printk(); for verbose RAM initialization messages.
63 * When CONFIG_DEBUG_RAM_SETUP is not selected, these messages are automatically
64 * disabled.
65 * @{
66 */
Martin Rothc4e49f62015-07-11 13:42:54 -060067#if IS_ENABLED(CONFIG_DEBUG_RAM_SETUP)
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050068#define printram(x, ...) printk(BIOS_DEBUG, x, ##__VA_ARGS__)
69#else
70#define printram(x, ...)
71#endif
72/** @} */
73
74/*
75 * Module type (byte 3, bits 3:0) of SPD
Martin Roth0cb07e32013-07-09 21:46:01 -060076 * This definition is specific to DDR3. DDR2 SPDs have a different structure.
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050077 */
78enum spd_dimm_type {
79 SPD_DIMM_TYPE_UNDEFINED = 0x00,
80 SPD_DIMM_TYPE_RDIMM = 0x01,
81 SPD_DIMM_TYPE_UDIMM = 0x02,
82 SPD_DIMM_TYPE_SO_DIMM = 0x03,
83 SPD_DIMM_TYPE_MICRO_DIMM = 0x04,
84 SPD_DIMM_TYPE_MINI_RDIMM = 0x05,
85 SPD_DIMM_TYPE_MINI_UDIMM = 0x06,
86 SPD_DIMM_TYPE_MINI_CDIMM = 0x07,
87 SPD_DIMM_TYPE_72B_SO_UDIMM = 0x08,
88 SPD_DIMM_TYPE_72B_SO_RDIMM = 0x09,
89 SPD_DIMM_TYPE_72B_SO_CDIMM = 0x0a,
90 SPD_DIMM_TYPE_LRDIMM = 0x0b,
Elyes HAOUASaa8e7e72016-06-19 12:38:47 +020091 SPD_DIMM_TYPE_16B_SO_DIMM = 0x0c,
92 SPD_DIMM_TYPE_32B_SO_DIMM = 0x0d,
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -050093 /* Masks to bits 3:0 to give the dimm type */
94 SPD_DIMM_TYPE_MASK = 0x0f,
95};
96
97/**
98 * \brief DIMM flags
99 *
100 * Characteristic flags for the DIMM, as presented by the SPD
101 */
102typedef union dimm_flags_st {
103 /* The whole point of the union/struct construct is to allow us to clear
104 * all the bits with one line: flags.raw = 0.
105 * We do not care how these bits are ordered */
106 struct {
107 /* Indicates if rank 1 of DIMM uses a mirrored pin mapping. See:
108 * Annex K: Serial Presence Detect (SPD) for DDR3 SDRAM */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800109 unsigned int pins_mirrored:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500110 /* Module can work at 1.50V - All DIMMS must be 1.5V operable */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800111 unsigned int operable_1_50V:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500112 /* Module can work at 1.35V */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800113 unsigned int operable_1_35V:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500114 /* Module can work at 1.20V */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800115 unsigned int operable_1_25V:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500116 /* Has an 8-bit bus extension, meaning the DIMM supports ECC */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800117 unsigned int is_ecc:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500118 /* DLL-Off Mode Support */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800119 unsigned int dll_off_mode:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500120 /* Indicates a drive strength of RZQ/6 (40 Ohm) is supported */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800121 unsigned int rzq6_supported:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500122 /* Indicates a drive strength of RZQ/7 (35 Ohm) is supported */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800123 unsigned int rzq7_supported:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500124 /* Partial Array Self Refresh */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800125 unsigned int pasr:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500126 /* On-die Thermal Sensor Readout */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800127 unsigned int odts:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500128 /* Auto Self Refresh */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800129 unsigned int asr:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500130 /* Extended temperature range supported */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800131 unsigned int ext_temp_range:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500132 /* Operating at extended temperature requires 2X refresh rate */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800133 unsigned int ext_temp_refresh:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500134 /* Thermal sensor incorporated */
Lee Leahy0ca2a062017-03-06 18:01:04 -0800135 unsigned int therm_sensor:1;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500136 };
Lee Leahy0ca2a062017-03-06 18:01:04 -0800137 unsigned int raw;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500138} dimm_flags_t;
139
140/**
141 * \brief DIMM characteristics
142 *
143 * The characteristics of each DIMM, as presented by the SPD
144 */
145typedef struct dimm_attr_st {
146 enum spd_memory_type dram_type;
Vladimir Serbinenko0e675f72014-12-07 13:56:48 +0100147 enum spd_dimm_type dimm_type;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500148 u16 cas_supported;
149 /* Flags extracted from SPD */
150 dimm_flags_t flags;
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200151 /* SDRAM width */
152 u8 width;
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500153 /* Number of ranks */
154 u8 ranks;
155 /* Number or row address bits */
156 u8 row_bits;
157 /* Number or column address bits */
158 u8 col_bits;
159 /* Size of module in MiB */
160 u32 size_mb;
161 /* Latencies are in units of 1/256 ns */
162 u32 tCK;
163 u32 tAA;
164 u32 tWR;
165 u32 tRCD;
166 u32 tRRD;
167 u32 tRP;
168 u32 tRAS;
169 u32 tRC;
170 u32 tRFC;
171 u32 tWTR;
172 u32 tRTP;
173 u32 tFAW;
Vladimir Serbinenko7686a562014-05-18 11:05:56 +0200174
175 u8 reference_card;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100176 /* XMP: Module voltage in mV */
177 u16 voltage;
178 /* XMP: max DIMMs per channel supported (1-4) */
179 u8 dimms_per_channel;
Patrick Rudolph07691592016-02-29 18:21:00 +0100180 /* Manufacturer ID */
181 u16 manufacturer_id;
182 /* ASCII part number - NULL terminated */
183 u8 part_number[17];
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500184} dimm_attr;
185
186/** Result of the SPD decoding process */
187enum spd_status {
188 SPD_STATUS_OK = 0,
189 SPD_STATUS_INVALID,
190 SPD_STATUS_CRC_ERROR,
191 SPD_STATUS_INVALID_FIELD,
192};
193
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100194enum ddr3_xmp_profile {
195 DDR3_XMP_PROFILE_1 = 0,
196 DDR3_XMP_PROFILE_2 = 1,
197};
198
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500199typedef u8 spd_raw_data[256];
200
Alexandru Gagniuc4c37e582013-12-17 13:08:01 -0500201u16 spd_ddr3_calc_crc(u8 *spd, int len);
Kyösti Mälkki7dc4b842016-11-18 18:41:17 +0200202u16 spd_ddr3_calc_unique_crc(u8 *spd, int len);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500203int spd_decode_ddr3(dimm_attr * dimm, spd_raw_data spd_data);
204int dimm_is_registered(enum spd_dimm_type type);
205void dram_print_spd_ddr3(const dimm_attr * dimm);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100206int spd_xmp_decode_ddr3(dimm_attr * dimm,
Lee Leahy708fc272017-03-07 12:18:53 -0800207 spd_raw_data spd,
208 enum ddr3_xmp_profile profile);
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500209
210/**
211 * \brief Read double word from specified address
212 *
213 * Should be useful when doing an MRS to the DIMM
214 */
Stefan Reinauer1e2500e2015-06-19 14:59:06 -0700215static inline u32 volatile_read(volatile uintptr_t addr)
Alexandru Gagniucf97ff3f2013-05-21 14:43:45 -0500216{
217 volatile u32 result;
218 result = *(volatile u32 *)addr;
219 return result;
220}
221
Alexandru Gagniuc78706fd2013-06-03 13:58:10 -0500222/**
223 * \brief Representation of an MRS command
224 *
225 * This represents an MRS command as seen by the DIMM. This is not a memory
226 * address that can be read to generate an MRS command. The mapping of CPU
227 * to memory pins is hardware-dependent.
228 * \n
229 * The idea is to generalize the MRS code, and only need a hardware-specific
230 * function to map the MRS bits to CPU address bits. An MRS command can be
231 * sent like:
232 * @code{.c}
233 * u32 addr;
234 * mrs_cmd_t mrs;
235 * chipset_enable_mrs_command_mode();
236 * mrs = ddr3_get_mr2(rtt_wr, srt, asr, cwl)
237 * if (rank_has_mirrorred_pins)
238 * mrs = ddr3_mrs_mirror_pins(mrs);
239 * addr = chipset_specific_get_mrs_addr(mrs);
240 * volatile_read(addr);
241 * @endcode
242 *
243 * The MRS representation has the following structure:
244 * - cmd[15:0] = Address pins MA[15:0]
245 * - cmd[18:16] = Bank address BA[2:0]
246 */
247typedef u32 mrs_cmd_t;
248
249enum ddr3_mr0_precharge {
250 DDR3_MR0_PRECHARGE_SLOW = 0,
251 DDR3_MR0_PRECHARGE_FAST = 1,
252};
253enum ddr3_mr0_mode {
254 DDR3_MR0_MODE_NORMAL = 0,
255 DDR3_MR0_MODE_TEST = 1,
256};
257enum ddr3_mr0_dll_reset {
258 DDR3_MR0_DLL_RESET_NO = 0,
259 DDR3_MR0_DLL_RESET_YES = 1,
260};
261enum ddr3_mr0_burst_type {
262 DDR3_MR0_BURST_TYPE_SEQUENTIAL = 0,
263 DDR3_MR0_BURST_TYPE_INTERLEAVED = 1,
264};
265enum ddr3_mr0_burst_length {
266 DDR3_MR0_BURST_LENGTH_8 = 0,
267 DDR3_MR0_BURST_LENGTH_CHOP = 1,
268 DDR3_MR0_BURST_LENGTH_4 = 2,
269};
270mrs_cmd_t ddr3_get_mr0(enum ddr3_mr0_precharge precharge_pd,
271 u8 write_recovery,
272 enum ddr3_mr0_dll_reset dll_reset,
273 enum ddr3_mr0_mode mode,
274 u8 cas,
275 enum ddr3_mr0_burst_type interleaved_burst,
276 enum ddr3_mr0_burst_length burst_length);
277
278enum ddr3_mr1_qoff {
279 DDR3_MR1_QOFF_ENABLE = 0,
280 DDR3_MR1_QOFF_DISABLE = 1,
281};
282enum ddr3_mr1_tqds {
283 DDR3_MR1_TQDS_DISABLE = 0,
284 DDR3_MR1_TQDS_ENABLE = 1,
285};
286enum ddr3_mr1_write_leveling {
287 DDR3_MR1_WRLVL_DISABLE = 0,
288 DDR3_MR1_WRLVL_ENABLE = 1,
289};
290enum ddr3_mr1_rtt_nom {
291 DDR3_MR1_RTT_NOM_OFF = 0,
292 DDR3_MR1_RTT_NOM_RZQ4 = 1,
293 DDR3_MR1_RTT_NOM_RZQ2 = 2,
294 DDR3_MR1_RTT_NOM_RZQ6 = 3,
295 DDR3_MR1_RTT_NOM_RZQ12 = 4,
296 DDR3_MR1_RTT_NOM_RZQ8 = 5,
297};
298enum ddr3_mr1_additive_latency {
299 DDR3_MR1_AL_DISABLE = 0,
300 DDR3_MR1_AL_CL_MINUS_1 = 1,
301 DDR3_MR1_AL_CL_MINUS_2 = 2,
302};
303enum ddr3_mr1_ods {
304 DDR3_MR1_ODS_RZQ6 = 0,
305 DDR3_MR1_ODS_RZQ7 = 1,
306};
307enum ddr3_mr1_dll {
308 DDR3_MR1_DLL_ENABLE = 0,
309 DDR3_MR1_DLL_DISABLE = 1,
310};
311
312mrs_cmd_t ddr3_get_mr1(enum ddr3_mr1_qoff qoff,
313 enum ddr3_mr1_tqds tqds,
314 enum ddr3_mr1_rtt_nom rtt_nom,
315 enum ddr3_mr1_write_leveling write_leveling,
316 enum ddr3_mr1_ods output_drive_strenght,
317 enum ddr3_mr1_additive_latency additive_latency,
318 enum ddr3_mr1_dll dll_disable);
319
320enum ddr3_mr2_rttwr {
321 DDR3_MR2_RTTWR_OFF = 0,
322 DDR3_MR2_RTTWR_RZQ4 = 1,
323 DDR3_MR2_RTTWR_RZQ2 = 2,
324};
325enum ddr3_mr2_srt_range {
326 DDR3_MR2_SRT_NORMAL = 0,
327 DDR3_MR2_SRT_EXTENDED = 1,
328};
329enum ddr3_mr2_asr {
330 DDR3_MR2_ASR_MANUAL = 0,
331 DDR3_MR2_ASR_AUTO = 1,
332};
333
334mrs_cmd_t ddr3_get_mr2(enum ddr3_mr2_rttwr rtt_wr,
335 enum ddr3_mr2_srt_range extended_temp,
336 enum ddr3_mr2_asr self_refresh, u8 cas_cwl);
337
338mrs_cmd_t ddr3_get_mr3(char dataflow_from_mpr);
339mrs_cmd_t ddr3_mrs_mirror_pins(mrs_cmd_t cmd);
340
Martin Rothfd277d82016-01-11 12:47:30 -0700341#endif /* DEVICE_DRAM_DDR3L_H */