blob: 863218c2d355815f854a05ad55104fb21d25baab [file] [log] [blame]
Angel Pons118a9c72020-04-02 23:48:34 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Naresh G Solanki335781a2016-10-26 19:43:14 +05302
Naresh G Solanki335781a2016-10-26 19:43:14 +05303#include <cbfs.h>
4#include <console/console.h>
Nick Vaccaro53b99a82020-10-01 00:37:38 -07005#include <memory_info.h>
Naresh G Solanki335781a2016-10-26 19:43:14 +05306#include <spd_bin.h>
7#include <string.h>
Patrick Georgi0e3c59e2017-01-28 15:59:25 +01008#include <device/dram/ddr3.h>
Naresh G Solanki335781a2016-10-26 19:43:14 +05309
Naresh G Solanki335781a2016-10-26 19:43:14 +053010void dump_spd_info(struct spd_block *blk)
11{
12 u8 i;
13
14 for (i = 0; i < CONFIG_DIMM_MAX; i++)
15 if (blk->spd_array[i] != NULL && blk->spd_array[i][0] != 0) {
Furquan Shaikha26f9da2017-06-08 13:28:59 -070016 printk(BIOS_DEBUG, "SPD @ 0x%02X\n", blk->addr_map[i]);
Naresh G Solanki335781a2016-10-26 19:43:14 +053017 print_spd_info(blk->spd_array[i]);
18 }
19}
20
Nick Vaccaro53b99a82020-10-01 00:37:38 -070021const char * __weak mainboard_get_dram_part_num(void)
22{
23 /* Default weak implementation, no need to override part number. */
24 return NULL;
25}
26
Eric Lai8fb7cd42020-03-07 13:55:33 +080027static bool use_ddr4_params(int dram_type)
Naresh G Solanki335781a2016-10-26 19:43:14 +053028{
Eric Lai8fb7cd42020-03-07 13:55:33 +080029 switch (dram_type) {
30 case SPD_DRAM_DDR3:
31 case SPD_DRAM_LPDDR3_INTEL:
32 return false;
Eric Laicb1e3862020-03-13 17:16:20 +080033 /* Below DDR type share the same attributes */
Eric Lai8fb7cd42020-03-07 13:55:33 +080034 case SPD_DRAM_LPDDR3_JEDEC:
35 case SPD_DRAM_DDR4:
36 case SPD_DRAM_LPDDR4:
Eric Laicb1e3862020-03-13 17:16:20 +080037 case SPD_DRAM_LPDDR4X:
Eric Lai8fb7cd42020-03-07 13:55:33 +080038 return true;
39 default:
40 printk(BIOS_ERR, "Defaulting to using DDR4 params. Please add dram_type check for %d to %s\n",
41 dram_type, __func__);
42 return true;
43 }
Eric Laiaa8d7722019-09-02 15:01:56 +080044}
Naresh G Solanki335781a2016-10-26 19:43:14 +053045
Eric Laiaa8d7722019-09-02 15:01:56 +080046static const char *spd_get_module_type_string(int dram_type)
47{
48 switch (dram_type) {
Naresh G Solanki335781a2016-10-26 19:43:14 +053049 case SPD_DRAM_DDR3:
Eric Laiaa8d7722019-09-02 15:01:56 +080050 return "DDR3";
51 case SPD_DRAM_LPDDR3_INTEL:
52 case SPD_DRAM_LPDDR3_JEDEC:
53 return "LPDDR3";
54 case SPD_DRAM_DDR4:
55 return "DDR4";
Eric Laid0ee8702020-03-06 21:18:30 +080056 case SPD_DRAM_LPDDR4:
57 return "LPDDR4";
Eric Laicb1e3862020-03-13 17:16:20 +080058 case SPD_DRAM_LPDDR4X:
59 return "LPDDR4X";
60 case SPD_DRAM_DDR5:
61 return "DDR5";
62 case SPD_DRAM_LPDDR5:
63 return "LPDDR5";
Eric Laiaa8d7722019-09-02 15:01:56 +080064 }
65 return "UNKNOWN";
66}
67
68static int spd_get_banks(const uint8_t spd[], int dram_type)
69{
70 static const int ddr3_banks[4] = { 8, 16, 32, 64 };
71 static const int ddr4_banks[10] = { 4, 8, -1, -1, 8, 16, -1, -1, 16, 32 };
72 int index = (spd[SPD_DENSITY_BANKS] >> 4) & 0xf;
Eric Lai4d5fd772020-03-13 17:21:59 +080073
74 if (use_ddr4_params(dram_type)) {
Eric Laiaa8d7722019-09-02 15:01:56 +080075 if (index >= ARRAY_SIZE(ddr4_banks))
76 return -1;
77 return ddr4_banks[index];
Eric Lai4d5fd772020-03-13 17:21:59 +080078 } else {
79 if (index >= ARRAY_SIZE(ddr3_banks))
80 return -1;
81 return ddr3_banks[index];
Eric Laiaa8d7722019-09-02 15:01:56 +080082 }
83}
84
85static int spd_get_capmb(const uint8_t spd[])
86{
Eric Laid0ee8702020-03-06 21:18:30 +080087 static const int spd_capmb[13] = { 1, 2, 4, 8, 16, 32, 64,
88 128, 48, 96, 12, 24, 72 };
Eric Laiaa8d7722019-09-02 15:01:56 +080089 int index = spd[SPD_DENSITY_BANKS] & 0xf;
90 if (index >= ARRAY_SIZE(spd_capmb))
91 return -1;
92 return spd_capmb[index] * 256;
93}
94
95static int spd_get_rows(const uint8_t spd[])
96{
97 static const int spd_rows[7] = { 12, 13, 14, 15, 16, 17, 18 };
98 int index = (spd[SPD_ADDRESSING] >> 3) & 7;
99 if (index >= ARRAY_SIZE(spd_rows))
100 return -1;
101 return spd_rows[index];
102}
103
104static int spd_get_cols(const uint8_t spd[])
105{
106 static const int spd_cols[4] = { 9, 10, 11, 12 };
107 int index = spd[SPD_ADDRESSING] & 7;
108 if (index >= ARRAY_SIZE(spd_cols))
109 return -1;
110 return spd_cols[index];
111}
112
113static int spd_get_ranks(const uint8_t spd[], int dram_type)
114{
115 static const int spd_ranks[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
Eric Lai8fb7cd42020-03-07 13:55:33 +0800116 int organ_offset = use_ddr4_params(dram_type) ? DDR4_ORGANIZATION
117 : DDR3_ORGANIZATION;
Eric Laiaa8d7722019-09-02 15:01:56 +0800118 int index = (spd[organ_offset] >> 3) & 7;
119 if (index >= ARRAY_SIZE(spd_ranks))
120 return -1;
121 return spd_ranks[index];
122}
123
124static int spd_get_devw(const uint8_t spd[], int dram_type)
125{
126 static const int spd_devw[4] = { 4, 8, 16, 32 };
Eric Lai8fb7cd42020-03-07 13:55:33 +0800127 int organ_offset = use_ddr4_params(dram_type) ? DDR4_ORGANIZATION
128 : DDR3_ORGANIZATION;
Eric Laiaa8d7722019-09-02 15:01:56 +0800129 int index = spd[organ_offset] & 7;
130 if (index >= ARRAY_SIZE(spd_devw))
131 return -1;
132 return spd_devw[index];
133}
134
135static int spd_get_busw(const uint8_t spd[], int dram_type)
136{
137 static const int spd_busw[4] = { 8, 16, 32, 64 };
Eric Lai8fb7cd42020-03-07 13:55:33 +0800138 int busw_offset = use_ddr4_params(dram_type) ? DDR4_BUS_DEV_WIDTH
139 : DDR3_BUS_DEV_WIDTH;
Eric Laiaa8d7722019-09-02 15:01:56 +0800140 int index = spd[busw_offset] & 7;
141 if (index >= ARRAY_SIZE(spd_busw))
142 return -1;
143 return spd_busw[index];
144}
145
Nick Vaccaro88d4e822020-09-16 17:08:00 -0700146static void spd_get_name(const uint8_t spd[], int type, const char **spd_name, size_t *len)
Eric Laiaa8d7722019-09-02 15:01:56 +0800147{
Nick Vaccaro88d4e822020-09-16 17:08:00 -0700148 *spd_name = mainboard_get_dram_part_num();
149 if (*spd_name != NULL) {
150 *len = strlen(*spd_name);
151 return;
152 }
153
154 switch (type) {
Eric Laiaa8d7722019-09-02 15:01:56 +0800155 case SPD_DRAM_DDR3:
Nick Vaccaro88d4e822020-09-16 17:08:00 -0700156 *spd_name = (const char *) &spd[DDR3_SPD_PART_OFF];
157 *len = DDR3_SPD_PART_LEN;
Naresh G Solanki335781a2016-10-26 19:43:14 +0530158 break;
159 case SPD_DRAM_LPDDR3_INTEL:
Nick Vaccaro88d4e822020-09-16 17:08:00 -0700160 *spd_name = (const char *) &spd[LPDDR3_SPD_PART_OFF];
161 *len = LPDDR3_SPD_PART_LEN;
Naresh G Solanki335781a2016-10-26 19:43:14 +0530162 break;
Nick Vaccaro88d4e822020-09-16 17:08:00 -0700163 /* LPDDR3, LPDDR4 and DDR4 have same part number offset and length */
Eric Lai8fb7cd42020-03-07 13:55:33 +0800164 case SPD_DRAM_LPDDR3_JEDEC:
Naresh G Solanki335781a2016-10-26 19:43:14 +0530165 case SPD_DRAM_DDR4:
Eric Laid0ee8702020-03-06 21:18:30 +0800166 case SPD_DRAM_LPDDR4:
Nick Vaccarodfcd7392020-09-30 16:37:01 -0700167 case SPD_DRAM_LPDDR4X:
Nick Vaccaro88d4e822020-09-16 17:08:00 -0700168 *spd_name = (const char *) &spd[DDR4_SPD_PART_OFF];
169 *len = DDR4_SPD_PART_LEN;
Naresh G Solanki335781a2016-10-26 19:43:14 +0530170 break;
171 default:
Nick Vaccaro88d4e822020-09-16 17:08:00 -0700172 *len = 0;
Naresh G Solanki335781a2016-10-26 19:43:14 +0530173 break;
174 }
Eric Laiaa8d7722019-09-02 15:01:56 +0800175}
176
177void print_spd_info(uint8_t spd[])
178{
Nick Vaccaro88d4e822020-09-16 17:08:00 -0700179 const char *nameptr = NULL;
180 size_t len;
Eric Laiaa8d7722019-09-02 15:01:56 +0800181 int type = spd[SPD_DRAM_TYPE];
182 int banks = spd_get_banks(spd, type);
183 int capmb = spd_get_capmb(spd);
184 int rows = spd_get_rows(spd);
185 int cols = spd_get_cols(spd);
186 int ranks = spd_get_ranks(spd, type);
187 int devw = spd_get_devw(spd, type);
188 int busw = spd_get_busw(spd, type);
189
190 /* Module type */
191 printk(BIOS_INFO, "SPD: module type is %s\n",
192 spd_get_module_type_string(type));
193 /* Module Part Number */
Nick Vaccaro88d4e822020-09-16 17:08:00 -0700194 spd_get_name(spd, type, &nameptr, &len);
195 if (nameptr)
196 printk(BIOS_INFO, "SPD: module part number is %.*s\n", (int) len, nameptr);
Naresh G Solanki335781a2016-10-26 19:43:14 +0530197
198 printk(BIOS_INFO,
199 "SPD: banks %d, ranks %d, rows %d, columns %d, density %d Mb\n",
200 banks, ranks, rows, cols, capmb);
201 printk(BIOS_INFO, "SPD: device width %d bits, bus width %d bits\n",
202 devw, busw);
203
204 if (capmb > 0 && busw > 0 && devw > 0 && ranks > 0) {
205 /* SIZE = DENSITY / 8 * BUS_WIDTH / SDRAM_WIDTH * RANKS */
206 printk(BIOS_INFO, "SPD: module size is %u MB (per channel)\n",
207 capmb / 8 * busw / devw * ranks);
208 }
209}
210
Naresh G Solanki335781a2016-10-26 19:43:14 +0530211int get_spd_cbfs_rdev(struct region_device *spd_rdev, u8 spd_index)
212{
213 struct cbfsf fh;
214
215 uint32_t cbfs_type = CBFS_TYPE_SPD;
216
Naresh G Solankib8a57362016-12-13 20:27:15 +0530217 if (cbfs_boot_locate(&fh, "spd.bin", &cbfs_type) < 0)
218 return -1;
Naresh G Solanki335781a2016-10-26 19:43:14 +0530219 cbfs_file_data(spd_rdev, &fh);
220 return rdev_chain(spd_rdev, spd_rdev, spd_index * CONFIG_DIMM_SPD_SIZE,
221 CONFIG_DIMM_SPD_SIZE);
222}
223
Patrick Georgi0e3c59e2017-01-28 15:59:25 +0100224#if CONFIG_DIMM_SPD_SIZE == 128
225int read_ddr3_spd_from_cbfs(u8 *buf, int idx)
226{
227 const int SPD_CRC_HI = 127;
228 const int SPD_CRC_LO = 126;
229
Julius Werner834b3ec2020-03-04 16:52:08 -0800230 char *spd_file;
Patrick Georgi0e3c59e2017-01-28 15:59:25 +0100231 size_t spd_file_len = 0;
232 size_t min_len = (idx + 1) * CONFIG_DIMM_SPD_SIZE;
233
Julius Werner834b3ec2020-03-04 16:52:08 -0800234 spd_file = cbfs_map("spd.bin", &spd_file_len);
Patrick Georgi0e3c59e2017-01-28 15:59:25 +0100235 if (!spd_file)
236 printk(BIOS_EMERG, "file [spd.bin] not found in CBFS");
237 if (spd_file_len < min_len)
238 printk(BIOS_EMERG, "Missing SPD data.");
239 if (!spd_file || spd_file_len < min_len)
240 return -1;
241
Lee Leahy73402172017-03-10 15:23:24 -0800242 memcpy(buf, spd_file + (idx * CONFIG_DIMM_SPD_SIZE),
243 CONFIG_DIMM_SPD_SIZE);
Julius Werner834b3ec2020-03-04 16:52:08 -0800244 cbfs_unmap(spd_file);
Patrick Georgi0e3c59e2017-01-28 15:59:25 +0100245
246 u16 crc = spd_ddr3_calc_crc(buf, CONFIG_DIMM_SPD_SIZE);
247
248 if (((buf[SPD_CRC_LO] == 0) && (buf[SPD_CRC_HI] == 0))
Lee Leahye20a3192017-03-09 16:21:34 -0800249 || (buf[SPD_CRC_LO] != (crc & 0xff))
250 || (buf[SPD_CRC_HI] != (crc >> 8))) {
Lee Leahy73402172017-03-10 15:23:24 -0800251 printk(BIOS_WARNING,
252 "SPD CRC %02x%02x is invalid, should be %04x\n",
Patrick Georgi0e3c59e2017-01-28 15:59:25 +0100253 buf[SPD_CRC_HI], buf[SPD_CRC_LO], crc);
254 buf[SPD_CRC_LO] = crc & 0xff;
255 buf[SPD_CRC_HI] = crc >> 8;
256 u16 i;
257 printk(BIOS_WARNING, "\nDisplay the SPD");
258 for (i = 0; i < CONFIG_DIMM_SPD_SIZE; i++) {
Lee Leahy45fde702017-03-08 18:02:24 -0800259 if ((i % 16) == 0x00)
Patrick Georgi0e3c59e2017-01-28 15:59:25 +0100260 printk(BIOS_WARNING, "\n%02x: ", i);
261 printk(BIOS_WARNING, "%02x ", buf[i]);
262 }
263 printk(BIOS_WARNING, "\n");
Lee Leahye20a3192017-03-09 16:21:34 -0800264 }
265 return 0;
Patrick Georgi0e3c59e2017-01-28 15:59:25 +0100266}
267#endif