1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <cbfs.h>
4 #include <console/console.h>
5 #include <device/dram/ddr3.h>
6 #include <gpio.h>
7 #include <soc/romstage.h>
8 #include <spd.h>
9 #include <string.h>
10
11 #include "../gpio.h"
12 #include "spd.h"
13
mainboard_print_spd_info(uint8_t spd[])14 static void mainboard_print_spd_info(uint8_t spd[])
15 {
16 const int spd_banks[8] = { 8, 16, 32, 64, -1, -1, -1, -1 };
17 const int spd_capmb[8] = { 1, 2, 4, 8, 16, 32, 64, 0 };
18 const int spd_rows[8] = { 12, 13, 14, 15, 16, -1, -1, -1 };
19 const int spd_cols[8] = { 9, 10, 11, 12, -1, -1, -1, -1 };
20 const int spd_ranks[8] = { 1, 2, 3, 4, -1, -1, -1, -1 };
21 const int spd_devw[8] = { 4, 8, 16, 32, -1, -1, -1, -1 };
22 const int spd_busw[8] = { 8, 16, 32, 64, -1, -1, -1, -1 };
23 char spd_name[SPD_DDR3_PART_LEN + 1] = { 0 };
24
25 int banks = spd_banks[(spd[SPD_DENSITY_BANKS] >> 4) & 7];
26 int capmb = spd_capmb[spd[SPD_DENSITY_BANKS] & 7] * 256;
27 int rows = spd_rows[(spd[SPD_ADDRESSING] >> 3) & 7];
28 int cols = spd_cols[spd[SPD_ADDRESSING] & 7];
29 int ranks = spd_ranks[(spd[SPD_ORGANIZATION] >> 3) & 7];
30 int devw = spd_devw[spd[SPD_ORGANIZATION] & 7];
31 int busw = spd_busw[spd[SPD_BUS_DEV_WIDTH] & 7];
32
33 /* Module type */
34 printk(BIOS_INFO, "SPD: module type is ");
35 switch (spd[SPD_MEMORY_TYPE]) {
36 case SPD_MEMORY_TYPE_SDRAM_DDR3:
37 printk(BIOS_INFO, "DDR3\n");
38 break;
39 case SPD_MEMORY_TYPE_LPDDR3_INTEL:
40 printk(BIOS_INFO, "LPDDR3\n");
41 break;
42 default:
43 printk(BIOS_INFO, "Unknown (%02x)\n", spd[SPD_MEMORY_TYPE]);
44 break;
45 }
46
47 /* Module Part Number */
48 memcpy(spd_name, &spd[SPD_DDR3_PART_NUM], SPD_DDR3_PART_LEN);
49 spd_name[SPD_DDR3_PART_LEN] = 0;
50 printk(BIOS_INFO, "SPD: module part is %s\n", spd_name);
51
52 printk(BIOS_INFO,
53 "SPD: banks %d, ranks %d, rows %d, columns %d, density %d Mb\n",
54 banks, ranks, rows, cols, capmb);
55 printk(BIOS_INFO, "SPD: device width %d bits, bus width %d bits\n",
56 devw, busw);
57
58 if (capmb > 0 && busw > 0 && devw > 0 && ranks > 0) {
59 /* SIZE = DENSITY / 8 * BUS_WIDTH / SDRAM_WIDTH * RANKS */
60 printk(BIOS_INFO, "SPD: module size is %u MB (per channel)\n",
61 capmb / 8 * busw / devw * ranks);
62 }
63 }
64
mainboard_get_spd_index(void)65 int mainboard_get_spd_index(void)
66 {
67 gpio_t spd_gpios[] = {
68 GPIO_MEM_CONFIG_0,
69 GPIO_MEM_CONFIG_1,
70 GPIO_MEM_CONFIG_2,
71 GPIO_MEM_CONFIG_3,
72 };
73 return gpio_base2_value(spd_gpios, ARRAY_SIZE(spd_gpios));
74 }
75
mainboard_get_spd_data(void)76 uintptr_t mainboard_get_spd_data(void)
77 {
78 char *spd_file;
79 size_t spd_file_len;
80 int spd_index;
81
82 spd_index = mainboard_get_spd_index();
83 printk(BIOS_INFO, "SPD index %d\n", spd_index);
84
85 /* Load SPD data from CBFS */
86 spd_file = cbfs_map("spd.bin", &spd_file_len);
87 if (!spd_file)
88 die("SPD data not found.");
89
90 /* make sure we have at least one SPD in the file. */
91 if (spd_file_len < SPD_SIZE_MAX_DDR3)
92 die("Missing SPD data.");
93
94 /* Make sure we did not overrun the buffer */
95 if (spd_file_len < ((spd_index + 1) * SPD_SIZE_MAX_DDR3)) {
96 printk(BIOS_ERR, "SPD index override to 1 - old hardware?\n");
97 spd_index = 1;
98 }
99
100 spd_index *= SPD_SIZE_MAX_DDR3;
101 mainboard_print_spd_info((uint8_t *)(spd_file + spd_index));
102
103 return (uintptr_t)(spd_file + spd_index);
104 }
105