Debian Codegraph @

src/mainboard/google/cyan/spd/spd.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <cbfs.h>
4 #include <cbmem.h>
5 #include <console/console.h>
6 #include <device/dram/ddr3.h>
7 #include <gpio.h>
8 #include <lib.h>
9 #include <memory_info.h>
10 #include <smbios.h>
11 #include <spd.h>
12 #include <soc/romstage.h>
13 #include <string.h>
14 #include <spd_bin.h>
15 #include "spd_util.h"
16
17 __weak uint8_t get_ramid(void)
18 {
19 gpio_t spd_gpios[] = {
20 GP_SW_80, /* SATA_GP3, RAMID0 */
21 GP_SW_67, /* I2C3_SCL, RAMID1 */
22 GP_SE_02, /* MF_PLT_CLK1, RAMID2 */
23 GP_SW_64, /* I2C3_SDA, RAMID3 */
24 };
25
26 return gpio_base2_value(spd_gpios, ARRAY_SIZE(spd_gpios));
27 }
28
29 static void *get_spd_pointer(int *dual)
30 {
31 char *spd_file;
32 size_t spd_file_len;
33 int total_spds;
34 int ram_id = 0;
35 int spd_index = 0;
36
37 /* Find the SPD data in CBFS. */
38 spd_file = cbfs_map("spd.bin", &spd_file_len);
39 if (!spd_file)
40 die("SPD data not found.");
41
42 if (spd_file_len < SPD_SIZE_MAX_DDR3)
43 die("Missing SPD data.");
44 total_spds = spd_file_len / SPD_SIZE_MAX_DDR3;
45
46 ram_id = get_ramid();
47 printk(BIOS_DEBUG, "ram_id=%d, total_spds: %d\n", ram_id, total_spds);
48
49 spd_index = get_variant_spd_index(ram_id, dual);
50 if (spd_index >= total_spds) {
51 printk(BIOS_ERR, "SPD index > total SPDs\n");
52 return NULL;
53 }
54 /* Return the serial product data for the RAM */
55 return &spd_file[SPD_SIZE_MAX_DDR3 * spd_index];
56 }
57
58 /* Copy SPD data for on-board memory */
59 void spd_memory_init_params(MEMORY_INIT_UPD *memory_params)
60 {
61 void *spd_content;
62 int dual_channel = 0;
63
64 /*
65 * Both channels are always present in SPD data. Always use matched
66 * DIMMs so use the same SPD data for each DIMM.
67 */
68 spd_content = get_spd_pointer(&dual_channel);
69 if (CONFIG(DISPLAY_SPD_DATA) && spd_content != NULL) {
70 printk(BIOS_DEBUG, "SPD Data:\n");
71 hexdump(spd_content, SPD_SIZE_MAX_DDR3);
72 printk(BIOS_DEBUG, "\n");
73 }
74
75 /*
76 * Set SPD and memory configuration:
77 * Memory type: 0=DimmInstalled,
78 * 1=SolderDownMemory,
79 * 2=DimmDisabled
80 */
81 if (spd_content != NULL) {
82 memory_params->PcdMemChannel0Config = 1;
83 printk(BIOS_DEBUG, "Channel 0 DIMM soldered down\n");
84 if (dual_channel) {
85 printk(BIOS_DEBUG, "Channel 1 DIMM soldered down\n");
86 memory_params->PcdMemChannel1Config = 1;
87 } else {
88 printk(BIOS_DEBUG, "Channel 1 DIMM not installed\n");
89 memory_params->PcdMemChannel1Config = 2;
90 }
91 }
92
93 /* Update SPD data */
94 if (CONFIG(BOARD_GOOGLE_CYAN)) {
95 memory_params->PcdMemoryTypeEnable = MEM_DDR3;
96 memory_params->PcdMemorySpdPtr = (uintptr_t)spd_content;
97 } else {
98 memory_params->PcdMemoryTypeEnable = MEM_LPDDR3;
99 }
100 }
101
102 static void set_dimm_info(const uint8_t *spd, struct dimm_info *dimm)
103 {
104 const int spd_capmb[8] = { 1, 2, 4, 8, 16, 32, 64, 0 };
105 const int spd_ranks[8] = { 1, 2, 3, 4, -1, -1, -1, -1 };
106 const int spd_devw[8] = { 4, 8, 16, 32, -1, -1, -1, -1 };
107 const int spd_busw[8] = { 8, 16, 32, 64, -1, -1, -1, -1 };
108
109 int capmb = spd_capmb[spd[SPD_DENSITY_BANKS] & 7] * 256;
110 int ranks = spd_ranks[(spd[DDR3_ORGANIZATION] >> 3) & 7];
111 int devw = spd_devw[spd[DDR3_ORGANIZATION] & 7];
112 int busw = spd_busw[spd[DDR3_BUS_DEV_WIDTH] & 7];
113
114 void *hob_list_ptr;
115 EFI_HOB_GUID_TYPE *hob_ptr;
116 FSP_SMBIOS_MEMORY_INFO *memory_info_hob;
117 const EFI_GUID memory_info_hob_guid = FSP_SMBIOS_MEMORY_INFO_GUID;
118
119 /* Locate the memory info HOB, presence validated by raminit */
120 hob_list_ptr = fsp_get_hob_list();
121 hob_ptr = get_guid_hob(&memory_info_hob_guid, hob_list_ptr);
122 if (hob_ptr != NULL) {
123 memory_info_hob = (FSP_SMBIOS_MEMORY_INFO *)(hob_ptr + 1);
124 dimm->ddr_frequency = memory_info_hob->MemoryFrequencyInMHz;
125 } else {
126 printk(BIOS_ERR, "Can't get memory info hob pointer\n");
127 dimm->ddr_frequency = 0;
128 }
129
130 /* Parse the SPD data to determine the DIMM information */
131 if (CONFIG(BOARD_GOOGLE_CYAN)) {
132 dimm->ddr_type = MEMORY_TYPE_DDR3;
133 } else {
134 dimm->ddr_type = MEMORY_TYPE_LPDDR3;
135 }
136 dimm->dimm_size = capmb / 8 * busw / devw * ranks; /* MiB */
137 dimm->mod_type = spd[3] & 0xf;
138 strncpy((char *)&dimm->module_part_number[0], (char *)&spd[0x80],
139 SPD_DDR3_PART_LEN);
140 dimm->module_part_number[SPD_DDR3_PART_LEN] = 0;
141 dimm->mod_id = *(uint16_t *)&spd[0x94];
142
143 switch (busw) {
144 default:
145 case 8:
146 dimm->bus_width = MEMORY_BUS_WIDTH_8;
147 break;
148
149 case 16:
150 dimm->bus_width = MEMORY_BUS_WIDTH_16;
151 break;
152
153 case 32:
154 dimm->bus_width = MEMORY_BUS_WIDTH_32;
155 break;
156
157 case 64:
158 dimm->bus_width = MEMORY_BUS_WIDTH_64;
159 break;
160 }
161 }
162
163 void mainboard_save_dimm_info(struct romstage_params *params)
164 {
165 const void *spd_content;
166 int dual_channel;
167 struct dimm_info *dimm;
168 struct memory_info *mem_info;
169
170 spd_content = get_spd_pointer(&dual_channel);
171 if (spd_content == NULL)
172 return;
173
174 /*
175 * Allocate CBMEM area for DIMM information used to populate SMBIOS
176 * table 17
177 */
178 mem_info = cbmem_add(CBMEM_ID_MEMINFO, sizeof(*mem_info));
179 printk(BIOS_DEBUG, "CBMEM entry for DIMM info: %p\n", mem_info);
180 if (mem_info == NULL)
181 return;
182 memset(mem_info, 0, sizeof(*mem_info));
183
184 /* Describe the first channel memory */
185 dimm = &mem_info->dimm[0];
186 set_dimm_info(spd_content, dimm);
187 mem_info->dimm_cnt = 1;
188
189 /* Describe the second channel memory */
190 if (dual_channel) {
191 dimm = &mem_info->dimm[1];
192 set_dimm_info(spd_content, dimm);
193 dimm->channel_num = 1;
194 mem_info->dimm_cnt = 2;
195 }
196 }