Debian Codegraph @

src/soc/intel/common/block/smbus/smbuslib.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <console/console.h>
4 #include <device/dram/ddr3.h>
5 #include <device/dram/ddr4.h>
6 #include <spd.h>
7 #include <spd_bin.h>
8 #include <device/smbus_def.h>
9 #include <device/smbus_host.h>
10 #include "smbuslib.h"
11
12 static void spd_read(u8 *spd, u8 addr)
13 {
14 u16 i;
15 u8 step = 1;
16
17 if (CONFIG(SPD_READ_BY_WORD))
18 step = sizeof(uint16_t);
19
20 for (i = 0; i < SPD_SIZE_MAX_DDR3; i += step) {
21 if (CONFIG(SPD_READ_BY_WORD))
22 ((u16*)spd)[i / sizeof(uint16_t)] =
23 spd_read_word(addr, i);
24 else
25 spd[i] = spd_read_byte(addr, i);
26 }
27 }
28
29 static void switch_page(u8 spd_addr, u8 new_page)
30 {
31 /*
32 * By default,an SPD5 hub accepts 1 byte addressing pointing
33 * to the first 128 bytes of memory. MR11[2:0] selects the page
34 * pointer to address the entire 1024 bytes of non-volatile memory.
35 */
36 smbus_write_byte(spd_addr, SPD5_MEMREG_REG(SPD5_MR11), new_page);
37 }
38
39 /*
40 * Read the SPD data over the SMBus, at the specified SPD address,
41 * starting at the specified starting offset and read the given amount of data.
42 */
43 static void smbus_read_spd5(u8 *spd, u8 spd_addr, u16 size)
44 {
45 u8 page = ~0;
46 u32 max_page_size = MAX_SPD_PAGE_SIZE_SPD5;
47
48 if (size > MAX_SPD_SIZE) {
49 printk(BIOS_ERR, "Maximum SPD size reached\n");
50 return;
51 }
52 for (int i = 0; i < size; i++) {
53 u8 next_page = (u8) (i / max_page_size);
54 if (next_page != page) {
55 switch_page(spd_addr, next_page);
56 page = next_page;
57 }
58 unsigned int byte_addr = SPD_HUB_MEMREG(i % max_page_size);
59 spd[i] = smbus_read_byte(spd_addr, byte_addr);
60 }
61 }
62
63 /* Read SPD5 MR0 and check if SPD Byte 0 matches the SPD5 HUB MR0 identifier.*/
64 static int is_spd5_hub(u8 spd_addr)
65 {
66 u8 spd_hub_byte;
67
68 spd_hub_byte = smbus_read_byte(spd_addr, SPD5_MEMREG_REG(SPD5_MR0));
69 return spd_hub_byte == SPD5_MR0_SPD5_HUB_DEV;
70 }
71
72 /*
73 * Reset the SPD page back to page 0 on an SPD5 Hub device at the
74 * input SPD SMbus address.
75 */
76 static void reset_page_spd5(u8 spd_addr)
77 {
78 /* Set SPD5 MR11[2:0] = 0 (Page 0) */
79 switch_page(spd_addr, 0);
80 }
81
82 /* return -1 if SMBus errors otherwise return 0 */
83 static int get_spd(u8 *spd, u8 addr)
84 {
85 if (CONFIG_DIMM_SPD_SIZE > SPD_SIZE_MAX_DDR3) {
86 /* Restore to page 0 before reading */
87 spd_write_byte(SPD_PAGE_0, 0, 0);
88 }
89
90 /* If address is not 0, it will return CB_ERR(-1) if no dimm */
91 if (spd_read_byte(addr, 0) < 0) {
92 printk(BIOS_INFO, "No memory dimm at address %02X\n",
93 addr << 1);
94 return -1;
95 }
96
97 if (CONFIG(DRAM_SUPPORT_DDR5) && is_spd5_hub(addr)) {
98 smbus_read_spd5(spd, addr, CONFIG_DIMM_SPD_SIZE);
99
100 /* Reset the page for the next loop iteration */
101 reset_page_spd5(addr);
102 } else {
103 /* IMC doesn't support i2c eeprom read. */
104 if (CONFIG(SOC_INTEL_COMMON_BLOCK_IMC) ||
105 i2c_eeprom_read(addr, 0, SPD_SIZE_MAX_DDR3, spd) < 0) {
106 printk(BIOS_INFO, "do_i2c_eeprom_read failed, using fallback\n");
107 spd_read(spd, addr);
108 }
109
110 /* Check if module is DDR4, DDR4 spd is 512 byte. */
111 if (spd[SPD_MEMORY_TYPE] == SPD_MEMORY_TYPE_DDR4_SDRAM &&
112 CONFIG_DIMM_SPD_SIZE > SPD_SIZE_MAX_DDR3) {
113 /* Switch to page 1 */
114 spd_write_byte(SPD_PAGE_1, 0, 0);
115
116 /* IMC doesn't support i2c eeprom read. */
117 if (CONFIG(SOC_INTEL_COMMON_BLOCK_IMC) ||
118 i2c_eeprom_read(addr, 0, SPD_SIZE_MAX_DDR3, spd + SPD_SIZE_MAX_DDR3) < 0) {
119 printk(BIOS_INFO, "do_i2c_eeprom_read failed, using fallback\n");
120 spd_read(spd + SPD_SIZE_MAX_DDR3, addr);
121 }
122 /* Restore to page 0 */
123 spd_write_byte(SPD_PAGE_0, 0, 0);
124 }
125 }
126
127 return 0;
128 }
129
130 static u8 spd_data[CONFIG_DIMM_MAX * CONFIG_DIMM_SPD_SIZE];
131
132 void get_spd_smbus(struct spd_block *blk)
133 {
134 u8 i;
135 for (i = 0 ; i < CONFIG_DIMM_MAX; i++) {
136 /**
137 * Slave address 0 is also available for IMC based SPD SMBus.
138 */
139 if (!CONFIG(SOC_INTEL_COMMON_BLOCK_IMC) && blk->addr_map[i] == 0) {
140 blk->spd_array[i] = NULL;
141 continue;
142 }
143
144 if (get_spd(&spd_data[i * CONFIG_DIMM_SPD_SIZE], blk->addr_map[i]) == 0)
145 blk->spd_array[i] = &spd_data[i * CONFIG_DIMM_SPD_SIZE];
146 else
147 blk->spd_array[i] = NULL;
148 }
149
150 blk->len = CONFIG_DIMM_SPD_SIZE;
151 }
152
153 /*
154 * get_spd_sn returns the SODIMM serial number. It only supports DDR3 and DDR4.
155 * return CB_SUCCESS, sn is the serial number and sn=0xffffffff if the dimm is not present.
156 * return CB_ERR, if dram_type is not supported or addr is a zero.
157 */
158 enum cb_err get_spd_sn(u8 addr, u32 *sn)
159 {
160 u8 i;
161 u8 dram_type;
162 int smbus_ret;
163
164 /* addr is not a zero. */
165 if (addr == 0x0)
166 return CB_ERR;
167
168 if (CONFIG_DIMM_SPD_SIZE > SPD_SIZE_MAX_DDR3) {
169 /* Restore to page 0 before reading */
170 spd_write_byte(SPD_PAGE_0, 0, 0);
171 }
172
173 /* If dimm is not present, set sn to 0xff. */
174 smbus_ret = spd_read_byte(addr, SPD_MEMORY_TYPE);
175 if (smbus_ret < 0) {
176 printk(BIOS_INFO, "No memory dimm at address %02X\n", addr << 1);
177 *sn = 0xffffffff;
178 return CB_SUCCESS;
179 }
180
181 dram_type = smbus_ret & 0xff;
182
183 /* Check if module is DDR4, DDR4 spd is 512 byte. */
184 if (dram_type == SPD_MEMORY_TYPE_DDR4_SDRAM && CONFIG_DIMM_SPD_SIZE > SPD_SIZE_MAX_DDR3) {
185 /* Switch to page 1 */
186 spd_write_byte(SPD_PAGE_1, 0, 0);
187
188 for (i = 0; i < SPD_SN_LEN; i++)
189 *((u8 *)sn + i) = spd_read_byte(addr,
190 i + DDR4_SPD_SN_OFF);
191
192 /* Restore to page 0 */
193 spd_write_byte(SPD_PAGE_0, 0, 0);
194 } else if (dram_type == SPD_MEMORY_TYPE_SDRAM_DDR3) {
195 for (i = 0; i < SPD_SN_LEN; i++)
196 *((u8 *)sn + i) = spd_read_byte(addr,
197 i + SPD_DDR3_SERIAL_NUM);
198 } else {
199 printk(BIOS_ERR, "Unsupported dram_type\n");
200 return CB_ERR;
201 }
202
203 return CB_SUCCESS;
204 }