Angel Pons | 118a9c7 | 2020-04-02 23:48:34 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Naresh G Solanki | 335781a | 2016-10-26 19:43:14 +0530 | [diff] [blame] | 2 | |
Naresh G Solanki | 335781a | 2016-10-26 19:43:14 +0530 | [diff] [blame] | 3 | #include <cbfs.h> |
| 4 | #include <console/console.h> |
Nick Vaccaro | 53b99a8 | 2020-10-01 00:37:38 -0700 | [diff] [blame] | 5 | #include <memory_info.h> |
Naresh G Solanki | 335781a | 2016-10-26 19:43:14 +0530 | [diff] [blame] | 6 | #include <spd_bin.h> |
| 7 | #include <string.h> |
Patrick Georgi | 0e3c59e | 2017-01-28 15:59:25 +0100 | [diff] [blame] | 8 | #include <device/dram/ddr3.h> |
Naresh G Solanki | 335781a | 2016-10-26 19:43:14 +0530 | [diff] [blame] | 9 | |
Naresh G Solanki | 335781a | 2016-10-26 19:43:14 +0530 | [diff] [blame] | 10 | void 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 Shaikh | a26f9da | 2017-06-08 13:28:59 -0700 | [diff] [blame] | 16 | printk(BIOS_DEBUG, "SPD @ 0x%02X\n", blk->addr_map[i]); |
Naresh G Solanki | 335781a | 2016-10-26 19:43:14 +0530 | [diff] [blame] | 17 | print_spd_info(blk->spd_array[i]); |
| 18 | } |
| 19 | } |
| 20 | |
Nick Vaccaro | 53b99a8 | 2020-10-01 00:37:38 -0700 | [diff] [blame] | 21 | const 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 Lai | 8fb7cd4 | 2020-03-07 13:55:33 +0800 | [diff] [blame] | 27 | static bool use_ddr4_params(int dram_type) |
Naresh G Solanki | 335781a | 2016-10-26 19:43:14 +0530 | [diff] [blame] | 28 | { |
Eric Lai | 8fb7cd4 | 2020-03-07 13:55:33 +0800 | [diff] [blame] | 29 | switch (dram_type) { |
| 30 | case SPD_DRAM_DDR3: |
| 31 | case SPD_DRAM_LPDDR3_INTEL: |
| 32 | return false; |
Eric Lai | cb1e386 | 2020-03-13 17:16:20 +0800 | [diff] [blame] | 33 | /* Below DDR type share the same attributes */ |
Eric Lai | 8fb7cd4 | 2020-03-07 13:55:33 +0800 | [diff] [blame] | 34 | case SPD_DRAM_LPDDR3_JEDEC: |
| 35 | case SPD_DRAM_DDR4: |
Subrata Banik | 02bec2b | 2021-02-15 21:42:38 +0530 | [diff] [blame] | 36 | case SPD_DRAM_DDR5: |
Scott Chao | ea99f0d | 2022-04-18 10:40:18 +0800 | [diff] [blame] | 37 | case SPD_DRAM_LPDDR5: |
Eric Lai | 8fb7cd4 | 2020-03-07 13:55:33 +0800 | [diff] [blame] | 38 | case SPD_DRAM_LPDDR4: |
Eric Lai | cb1e386 | 2020-03-13 17:16:20 +0800 | [diff] [blame] | 39 | case SPD_DRAM_LPDDR4X: |
Eric Lai | 8fb7cd4 | 2020-03-07 13:55:33 +0800 | [diff] [blame] | 40 | return true; |
| 41 | default: |
Eric Lai | 586be05 | 2022-04-22 13:44:18 +0800 | [diff] [blame] | 42 | printk(BIOS_NOTICE, "Defaulting to using DDR4 params. Please add dram_type check for %d to %s\n", |
Eric Lai | 8fb7cd4 | 2020-03-07 13:55:33 +0800 | [diff] [blame] | 43 | dram_type, __func__); |
| 44 | return true; |
| 45 | } |
Eric Lai | aa8d772 | 2019-09-02 15:01:56 +0800 | [diff] [blame] | 46 | } |
Naresh G Solanki | 335781a | 2016-10-26 19:43:14 +0530 | [diff] [blame] | 47 | |
Eric Lai | aa8d772 | 2019-09-02 15:01:56 +0800 | [diff] [blame] | 48 | static const char *spd_get_module_type_string(int dram_type) |
| 49 | { |
| 50 | switch (dram_type) { |
Naresh G Solanki | 335781a | 2016-10-26 19:43:14 +0530 | [diff] [blame] | 51 | case SPD_DRAM_DDR3: |
Eric Lai | aa8d772 | 2019-09-02 15:01:56 +0800 | [diff] [blame] | 52 | return "DDR3"; |
| 53 | case SPD_DRAM_LPDDR3_INTEL: |
| 54 | case SPD_DRAM_LPDDR3_JEDEC: |
| 55 | return "LPDDR3"; |
| 56 | case SPD_DRAM_DDR4: |
| 57 | return "DDR4"; |
Eric Lai | d0ee870 | 2020-03-06 21:18:30 +0800 | [diff] [blame] | 58 | case SPD_DRAM_LPDDR4: |
| 59 | return "LPDDR4"; |
Eric Lai | cb1e386 | 2020-03-13 17:16:20 +0800 | [diff] [blame] | 60 | case SPD_DRAM_LPDDR4X: |
| 61 | return "LPDDR4X"; |
| 62 | case SPD_DRAM_DDR5: |
| 63 | return "DDR5"; |
| 64 | case SPD_DRAM_LPDDR5: |
| 65 | return "LPDDR5"; |
Eric Lai | aa8d772 | 2019-09-02 15:01:56 +0800 | [diff] [blame] | 66 | } |
| 67 | return "UNKNOWN"; |
| 68 | } |
| 69 | |
| 70 | static int spd_get_banks(const uint8_t spd[], int dram_type) |
| 71 | { |
| 72 | static const int ddr3_banks[4] = { 8, 16, 32, 64 }; |
| 73 | static const int ddr4_banks[10] = { 4, 8, -1, -1, 8, 16, -1, -1, 16, 32 }; |
| 74 | int index = (spd[SPD_DENSITY_BANKS] >> 4) & 0xf; |
Eric Lai | 4d5fd77 | 2020-03-13 17:21:59 +0800 | [diff] [blame] | 75 | |
| 76 | if (use_ddr4_params(dram_type)) { |
Eric Lai | aa8d772 | 2019-09-02 15:01:56 +0800 | [diff] [blame] | 77 | if (index >= ARRAY_SIZE(ddr4_banks)) |
| 78 | return -1; |
| 79 | return ddr4_banks[index]; |
Eric Lai | 4d5fd77 | 2020-03-13 17:21:59 +0800 | [diff] [blame] | 80 | } else { |
| 81 | if (index >= ARRAY_SIZE(ddr3_banks)) |
| 82 | return -1; |
| 83 | return ddr3_banks[index]; |
Eric Lai | aa8d772 | 2019-09-02 15:01:56 +0800 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | static int spd_get_capmb(const uint8_t spd[]) |
| 88 | { |
Eric Lai | d0ee870 | 2020-03-06 21:18:30 +0800 | [diff] [blame] | 89 | static const int spd_capmb[13] = { 1, 2, 4, 8, 16, 32, 64, |
| 90 | 128, 48, 96, 12, 24, 72 }; |
Eric Lai | aa8d772 | 2019-09-02 15:01:56 +0800 | [diff] [blame] | 91 | int index = spd[SPD_DENSITY_BANKS] & 0xf; |
| 92 | if (index >= ARRAY_SIZE(spd_capmb)) |
| 93 | return -1; |
| 94 | return spd_capmb[index] * 256; |
| 95 | } |
| 96 | |
| 97 | static int spd_get_rows(const uint8_t spd[]) |
| 98 | { |
| 99 | static const int spd_rows[7] = { 12, 13, 14, 15, 16, 17, 18 }; |
| 100 | int index = (spd[SPD_ADDRESSING] >> 3) & 7; |
| 101 | if (index >= ARRAY_SIZE(spd_rows)) |
| 102 | return -1; |
| 103 | return spd_rows[index]; |
| 104 | } |
| 105 | |
| 106 | static int spd_get_cols(const uint8_t spd[]) |
| 107 | { |
| 108 | static const int spd_cols[4] = { 9, 10, 11, 12 }; |
| 109 | int index = spd[SPD_ADDRESSING] & 7; |
| 110 | if (index >= ARRAY_SIZE(spd_cols)) |
| 111 | return -1; |
| 112 | return spd_cols[index]; |
| 113 | } |
| 114 | |
| 115 | static int spd_get_ranks(const uint8_t spd[], int dram_type) |
| 116 | { |
| 117 | static const int spd_ranks[8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; |
Eric Lai | 8fb7cd4 | 2020-03-07 13:55:33 +0800 | [diff] [blame] | 118 | int organ_offset = use_ddr4_params(dram_type) ? DDR4_ORGANIZATION |
| 119 | : DDR3_ORGANIZATION; |
Eric Lai | aa8d772 | 2019-09-02 15:01:56 +0800 | [diff] [blame] | 120 | int index = (spd[organ_offset] >> 3) & 7; |
| 121 | if (index >= ARRAY_SIZE(spd_ranks)) |
| 122 | return -1; |
| 123 | return spd_ranks[index]; |
| 124 | } |
| 125 | |
| 126 | static int spd_get_devw(const uint8_t spd[], int dram_type) |
| 127 | { |
| 128 | static const int spd_devw[4] = { 4, 8, 16, 32 }; |
Eric Lai | 8fb7cd4 | 2020-03-07 13:55:33 +0800 | [diff] [blame] | 129 | int organ_offset = use_ddr4_params(dram_type) ? DDR4_ORGANIZATION |
| 130 | : DDR3_ORGANIZATION; |
Eric Lai | aa8d772 | 2019-09-02 15:01:56 +0800 | [diff] [blame] | 131 | int index = spd[organ_offset] & 7; |
| 132 | if (index >= ARRAY_SIZE(spd_devw)) |
| 133 | return -1; |
| 134 | return spd_devw[index]; |
| 135 | } |
| 136 | |
| 137 | static int spd_get_busw(const uint8_t spd[], int dram_type) |
| 138 | { |
| 139 | static const int spd_busw[4] = { 8, 16, 32, 64 }; |
Eric Lai | 8fb7cd4 | 2020-03-07 13:55:33 +0800 | [diff] [blame] | 140 | int busw_offset = use_ddr4_params(dram_type) ? DDR4_BUS_DEV_WIDTH |
| 141 | : DDR3_BUS_DEV_WIDTH; |
Eric Lai | aa8d772 | 2019-09-02 15:01:56 +0800 | [diff] [blame] | 142 | int index = spd[busw_offset] & 7; |
| 143 | if (index >= ARRAY_SIZE(spd_busw)) |
| 144 | return -1; |
| 145 | return spd_busw[index]; |
| 146 | } |
| 147 | |
Nick Vaccaro | 88d4e82 | 2020-09-16 17:08:00 -0700 | [diff] [blame] | 148 | static void spd_get_name(const uint8_t spd[], int type, const char **spd_name, size_t *len) |
Eric Lai | aa8d772 | 2019-09-02 15:01:56 +0800 | [diff] [blame] | 149 | { |
Nick Vaccaro | 88d4e82 | 2020-09-16 17:08:00 -0700 | [diff] [blame] | 150 | *spd_name = mainboard_get_dram_part_num(); |
| 151 | if (*spd_name != NULL) { |
| 152 | *len = strlen(*spd_name); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | switch (type) { |
Eric Lai | aa8d772 | 2019-09-02 15:01:56 +0800 | [diff] [blame] | 157 | case SPD_DRAM_DDR3: |
Nick Vaccaro | 88d4e82 | 2020-09-16 17:08:00 -0700 | [diff] [blame] | 158 | *spd_name = (const char *) &spd[DDR3_SPD_PART_OFF]; |
| 159 | *len = DDR3_SPD_PART_LEN; |
Naresh G Solanki | 335781a | 2016-10-26 19:43:14 +0530 | [diff] [blame] | 160 | break; |
| 161 | case SPD_DRAM_LPDDR3_INTEL: |
Nick Vaccaro | 88d4e82 | 2020-09-16 17:08:00 -0700 | [diff] [blame] | 162 | *spd_name = (const char *) &spd[LPDDR3_SPD_PART_OFF]; |
| 163 | *len = LPDDR3_SPD_PART_LEN; |
Naresh G Solanki | 335781a | 2016-10-26 19:43:14 +0530 | [diff] [blame] | 164 | break; |
Nick Vaccaro | 88d4e82 | 2020-09-16 17:08:00 -0700 | [diff] [blame] | 165 | /* LPDDR3, LPDDR4 and DDR4 have same part number offset and length */ |
Eric Lai | 8fb7cd4 | 2020-03-07 13:55:33 +0800 | [diff] [blame] | 166 | case SPD_DRAM_LPDDR3_JEDEC: |
Naresh G Solanki | 335781a | 2016-10-26 19:43:14 +0530 | [diff] [blame] | 167 | case SPD_DRAM_DDR4: |
Subrata Banik | 02bec2b | 2021-02-15 21:42:38 +0530 | [diff] [blame] | 168 | case SPD_DRAM_DDR5: |
Scott Chao | ea99f0d | 2022-04-18 10:40:18 +0800 | [diff] [blame] | 169 | case SPD_DRAM_LPDDR5: |
Eric Lai | d0ee870 | 2020-03-06 21:18:30 +0800 | [diff] [blame] | 170 | case SPD_DRAM_LPDDR4: |
Nick Vaccaro | dfcd739 | 2020-09-30 16:37:01 -0700 | [diff] [blame] | 171 | case SPD_DRAM_LPDDR4X: |
Werner Zeh | 9b565de | 2022-03-09 08:17:54 +0100 | [diff] [blame] | 172 | if (spd[DDR4_SPD_PART_OFF]) { |
| 173 | *spd_name = (const char *) &spd[DDR4_SPD_PART_OFF]; |
| 174 | *len = DDR4_SPD_PART_LEN; |
| 175 | } |
Naresh G Solanki | 335781a | 2016-10-26 19:43:14 +0530 | [diff] [blame] | 176 | break; |
| 177 | default: |
Nick Vaccaro | 88d4e82 | 2020-09-16 17:08:00 -0700 | [diff] [blame] | 178 | *len = 0; |
Naresh G Solanki | 335781a | 2016-10-26 19:43:14 +0530 | [diff] [blame] | 179 | break; |
| 180 | } |
Eric Lai | aa8d772 | 2019-09-02 15:01:56 +0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void print_spd_info(uint8_t spd[]) |
| 184 | { |
Nick Vaccaro | 88d4e82 | 2020-09-16 17:08:00 -0700 | [diff] [blame] | 185 | const char *nameptr = NULL; |
| 186 | size_t len; |
Eric Lai | aa8d772 | 2019-09-02 15:01:56 +0800 | [diff] [blame] | 187 | int type = spd[SPD_DRAM_TYPE]; |
| 188 | int banks = spd_get_banks(spd, type); |
| 189 | int capmb = spd_get_capmb(spd); |
| 190 | int rows = spd_get_rows(spd); |
| 191 | int cols = spd_get_cols(spd); |
| 192 | int ranks = spd_get_ranks(spd, type); |
| 193 | int devw = spd_get_devw(spd, type); |
| 194 | int busw = spd_get_busw(spd, type); |
| 195 | |
| 196 | /* Module type */ |
| 197 | printk(BIOS_INFO, "SPD: module type is %s\n", |
| 198 | spd_get_module_type_string(type)); |
| 199 | /* Module Part Number */ |
Nick Vaccaro | 88d4e82 | 2020-09-16 17:08:00 -0700 | [diff] [blame] | 200 | spd_get_name(spd, type, &nameptr, &len); |
| 201 | if (nameptr) |
| 202 | printk(BIOS_INFO, "SPD: module part number is %.*s\n", (int) len, nameptr); |
Naresh G Solanki | 335781a | 2016-10-26 19:43:14 +0530 | [diff] [blame] | 203 | |
| 204 | printk(BIOS_INFO, |
| 205 | "SPD: banks %d, ranks %d, rows %d, columns %d, density %d Mb\n", |
| 206 | banks, ranks, rows, cols, capmb); |
| 207 | printk(BIOS_INFO, "SPD: device width %d bits, bus width %d bits\n", |
| 208 | devw, busw); |
| 209 | |
| 210 | if (capmb > 0 && busw > 0 && devw > 0 && ranks > 0) { |
| 211 | /* SIZE = DENSITY / 8 * BUS_WIDTH / SDRAM_WIDTH * RANKS */ |
| 212 | printk(BIOS_INFO, "SPD: module size is %u MB (per channel)\n", |
| 213 | capmb / 8 * busw / devw * ranks); |
| 214 | } |
| 215 | } |
| 216 | |
Julius Werner | a9b44f4 | 2021-02-05 17:27:45 -0800 | [diff] [blame] | 217 | uintptr_t spd_cbfs_map(u8 spd_index) |
Naresh G Solanki | 335781a | 2016-10-26 19:43:14 +0530 | [diff] [blame] | 218 | { |
Julius Werner | a9b44f4 | 2021-02-05 17:27:45 -0800 | [diff] [blame] | 219 | enum cbfs_type cbfs_type = CBFS_TYPE_SPD; |
| 220 | size_t size; |
Naresh G Solanki | 335781a | 2016-10-26 19:43:14 +0530 | [diff] [blame] | 221 | |
Julius Werner | a9b44f4 | 2021-02-05 17:27:45 -0800 | [diff] [blame] | 222 | void *map = cbfs_type_map("spd.bin", &size, &cbfs_type); |
| 223 | if (!map || size < (spd_index + 1) * CONFIG_DIMM_SPD_SIZE) |
| 224 | return 0; |
Naresh G Solanki | 335781a | 2016-10-26 19:43:14 +0530 | [diff] [blame] | 225 | |
Julius Werner | a9b44f4 | 2021-02-05 17:27:45 -0800 | [diff] [blame] | 226 | return (uintptr_t)map + spd_index * CONFIG_DIMM_SPD_SIZE; |
Naresh G Solanki | 335781a | 2016-10-26 19:43:14 +0530 | [diff] [blame] | 227 | } |
| 228 | |
Patrick Georgi | 0e3c59e | 2017-01-28 15:59:25 +0100 | [diff] [blame] | 229 | #if CONFIG_DIMM_SPD_SIZE == 128 |
| 230 | int read_ddr3_spd_from_cbfs(u8 *buf, int idx) |
| 231 | { |
| 232 | const int SPD_CRC_HI = 127; |
| 233 | const int SPD_CRC_LO = 126; |
| 234 | |
Julius Werner | 834b3ec | 2020-03-04 16:52:08 -0800 | [diff] [blame] | 235 | char *spd_file; |
Patrick Georgi | 0e3c59e | 2017-01-28 15:59:25 +0100 | [diff] [blame] | 236 | size_t spd_file_len = 0; |
| 237 | size_t min_len = (idx + 1) * CONFIG_DIMM_SPD_SIZE; |
| 238 | |
Julius Werner | 834b3ec | 2020-03-04 16:52:08 -0800 | [diff] [blame] | 239 | spd_file = cbfs_map("spd.bin", &spd_file_len); |
Patrick Georgi | 0e3c59e | 2017-01-28 15:59:25 +0100 | [diff] [blame] | 240 | if (!spd_file) |
| 241 | printk(BIOS_EMERG, "file [spd.bin] not found in CBFS"); |
| 242 | if (spd_file_len < min_len) |
| 243 | printk(BIOS_EMERG, "Missing SPD data."); |
| 244 | if (!spd_file || spd_file_len < min_len) |
| 245 | return -1; |
| 246 | |
Lee Leahy | 7340217 | 2017-03-10 15:23:24 -0800 | [diff] [blame] | 247 | memcpy(buf, spd_file + (idx * CONFIG_DIMM_SPD_SIZE), |
| 248 | CONFIG_DIMM_SPD_SIZE); |
Julius Werner | 834b3ec | 2020-03-04 16:52:08 -0800 | [diff] [blame] | 249 | cbfs_unmap(spd_file); |
Patrick Georgi | 0e3c59e | 2017-01-28 15:59:25 +0100 | [diff] [blame] | 250 | |
| 251 | u16 crc = spd_ddr3_calc_crc(buf, CONFIG_DIMM_SPD_SIZE); |
| 252 | |
| 253 | if (((buf[SPD_CRC_LO] == 0) && (buf[SPD_CRC_HI] == 0)) |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 254 | || (buf[SPD_CRC_LO] != (crc & 0xff)) |
| 255 | || (buf[SPD_CRC_HI] != (crc >> 8))) { |
Lee Leahy | 7340217 | 2017-03-10 15:23:24 -0800 | [diff] [blame] | 256 | printk(BIOS_WARNING, |
| 257 | "SPD CRC %02x%02x is invalid, should be %04x\n", |
Patrick Georgi | 0e3c59e | 2017-01-28 15:59:25 +0100 | [diff] [blame] | 258 | buf[SPD_CRC_HI], buf[SPD_CRC_LO], crc); |
| 259 | buf[SPD_CRC_LO] = crc & 0xff; |
| 260 | buf[SPD_CRC_HI] = crc >> 8; |
| 261 | u16 i; |
| 262 | printk(BIOS_WARNING, "\nDisplay the SPD"); |
| 263 | for (i = 0; i < CONFIG_DIMM_SPD_SIZE; i++) { |
Lee Leahy | 45fde70 | 2017-03-08 18:02:24 -0800 | [diff] [blame] | 264 | if ((i % 16) == 0x00) |
Patrick Georgi | 0e3c59e | 2017-01-28 15:59:25 +0100 | [diff] [blame] | 265 | printk(BIOS_WARNING, "\n%02x: ", i); |
| 266 | printk(BIOS_WARNING, "%02x ", buf[i]); |
| 267 | } |
| 268 | printk(BIOS_WARNING, "\n"); |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 269 | } |
| 270 | return 0; |
Patrick Georgi | 0e3c59e | 2017-01-28 15:59:25 +0100 | [diff] [blame] | 271 | } |
| 272 | #endif |