soc/intel/broadwell: Consolidate SPD handling

Mainboards do not need to know about `pei_data` to tell northbridge code
where to find the SPD data. As done on Haswell, add the `mb_get_spd_map`
function and the `struct spd_info` type to retrieve SPD information from
mainboard code without having to use `pei_data` in said mainboard code.

Unlike Haswell MRC, Broadwell MRC uses all positions of the `spd_data`
array, not just the first. The placeholder SPD address for memory-down
seems to be different as well. Adapt the existing code to handle these
variations. Once complete, the abstraction layer for both MRC binaries
will have the same API.

Change-Id: I92a05003a319c354675368cae8e34980bd2f9e10
Signed-off-by: Angel Pons <th3fanbus@gmail.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/55811
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Matt DeVillier <matt.devillier@gmail.com>
diff --git a/src/mainboard/google/auron/spd.c b/src/mainboard/google/auron/spd.c
index 79a51f8..0bb7dcb 100644
--- a/src/mainboard/google/auron/spd.c
+++ b/src/mainboard/google/auron/spd.c
@@ -1,21 +1,11 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 
 #include <mainboard/google/auron/variant.h>
-#include <soc/pei_data.h>
 #include <soc/pei_wrapper.h>
-#include <string.h>
-#include <types.h>
 
-/* Copy SPD data for on-board memory */
-void mainboard_fill_spd_data(struct pei_data *pei_data)
+void mb_get_spd_map(struct spd_info *spdi)
 {
-	const unsigned int spd_index = variant_get_spd_index();
-
-	fill_spd_for_index(pei_data->spd_data[0][0], spd_index);
-	pei_data->spd_addresses[0] = SPD_MEMORY_DOWN;
-
-	if (variant_is_dual_channel(spd_index)) {
-		memcpy(pei_data->spd_data[1][0], pei_data->spd_data[0][0], SPD_LEN);
-		pei_data->spd_addresses[2] = SPD_MEMORY_DOWN;
-	}
+	spdi->spd_index = variant_get_spd_index();
+	spdi->addresses[0] = SPD_MEMORY_DOWN;
+	spdi->addresses[2] = variant_is_dual_channel(spdi->spd_index) ? SPD_MEMORY_DOWN : 0;
 }
diff --git a/src/mainboard/google/auron/variants/buddy/spd/spd.c b/src/mainboard/google/auron/variants/buddy/spd/spd.c
index 5281d82..020bb8b 100644
--- a/src/mainboard/google/auron/variants/buddy/spd/spd.c
+++ b/src/mainboard/google/auron/variants/buddy/spd/spd.c
@@ -1,12 +1,9 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 
-#include <mainboard/google/auron/variant.h>
-#include <soc/pei_data.h>
 #include <soc/pei_wrapper.h>
 
-/* Copy SPD data for on-board memory */
-void mainboard_fill_spd_data(struct pei_data *pei_data)
+void mb_get_spd_map(struct spd_info *spdi)
 {
-	pei_data->spd_addresses[0] = 0xa0;
-	pei_data->spd_addresses[2] = 0xa4;
+	spdi->addresses[0] = 0x50;
+	spdi->addresses[2] = 0x52;
 }
diff --git a/src/mainboard/google/jecht/spd/spd.c b/src/mainboard/google/jecht/spd/spd.c
index 177e59a..020bb8b 100644
--- a/src/mainboard/google/jecht/spd/spd.c
+++ b/src/mainboard/google/jecht/spd/spd.c
@@ -1,11 +1,9 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 
-#include <soc/pei_data.h>
 #include <soc/pei_wrapper.h>
 
-/* Copy SPD data for on-board memory */
-void mainboard_fill_spd_data(struct pei_data *pei_data)
+void mb_get_spd_map(struct spd_info *spdi)
 {
-	pei_data->spd_addresses[0] = 0xa0;
-	pei_data->spd_addresses[2] = 0xa4;
+	spdi->addresses[0] = 0x50;
+	spdi->addresses[2] = 0x52;
 }
diff --git a/src/mainboard/intel/wtm2/pei_data.c b/src/mainboard/intel/wtm2/pei_data.c
index b0e8123..6c7ebd6 100644
--- a/src/mainboard/intel/wtm2/pei_data.c
+++ b/src/mainboard/intel/wtm2/pei_data.c
@@ -3,11 +3,10 @@
 #include <soc/pei_data.h>
 #include <soc/pei_wrapper.h>
 
-void mainboard_fill_spd_data(struct pei_data *pei_data)
+void mb_get_spd_map(struct spd_info *spdi)
 {
-	/* One installed DIMM per channel */
-	pei_data->spd_addresses[0] = 0xa2;
-	pei_data->spd_addresses[2] = 0xa2;
+	spdi->addresses[0] = 0x51;
+	spdi->addresses[2] = 0x51;
 }
 
 void mainboard_fill_pei_data(struct pei_data *pei_data)
diff --git a/src/mainboard/purism/librem_bdw/variants/librem13v1/pei_data.c b/src/mainboard/purism/librem_bdw/variants/librem13v1/pei_data.c
index 04fb9ea..022e424 100644
--- a/src/mainboard/purism/librem_bdw/variants/librem13v1/pei_data.c
+++ b/src/mainboard/purism/librem_bdw/variants/librem13v1/pei_data.c
@@ -3,10 +3,9 @@
 #include <soc/pei_data.h>
 #include <soc/pei_wrapper.h>
 
-void mainboard_fill_spd_data(struct pei_data *pei_data)
+void mb_get_spd_map(struct spd_info *spdi)
 {
-	/* One DIMM slot */
-	pei_data->spd_addresses[0] = 0xa0;
+	spdi->addresses[0] = 0x50;
 }
 
 void mainboard_fill_pei_data(struct pei_data *pei_data)
diff --git a/src/mainboard/purism/librem_bdw/variants/librem15v2/pei_data.c b/src/mainboard/purism/librem_bdw/variants/librem15v2/pei_data.c
index 209cf30..7eea654 100644
--- a/src/mainboard/purism/librem_bdw/variants/librem15v2/pei_data.c
+++ b/src/mainboard/purism/librem_bdw/variants/librem15v2/pei_data.c
@@ -3,11 +3,10 @@
 #include <soc/pei_data.h>
 #include <soc/pei_wrapper.h>
 
-void mainboard_fill_spd_data(struct pei_data *pei_data)
+void mb_get_spd_map(struct spd_info *spdi)
 {
-	/* One DIMM slot */
-	pei_data->spd_addresses[0] = 0xa0;
-	pei_data->spd_addresses[2] = 0xa4;
+	spdi->addresses[0] = 0x50;
+	spdi->addresses[2] = 0x52;
 }
 
 void mainboard_fill_pei_data(struct pei_data *pei_data)
diff --git a/src/soc/intel/broadwell/include/soc/pei_wrapper.h b/src/soc/intel/broadwell/include/soc/pei_wrapper.h
index bd12a2b0..80222be 100644
--- a/src/soc/intel/broadwell/include/soc/pei_wrapper.h
+++ b/src/soc/intel/broadwell/include/soc/pei_wrapper.h
@@ -28,12 +28,17 @@
 
 #define SPD_MEMORY_DOWN	0xff
 
-#define SPD_LEN		256
+struct spd_info {
+	uint8_t addresses[4];
+	unsigned int spd_index;
+};
+
+/* Mainboard callback to fill in the SPD addresses */
+void mb_get_spd_map(struct spd_info *spdi);
 
 void broadwell_fill_pei_data(struct pei_data *pei_data);
 void mainboard_fill_pei_data(struct pei_data *pei_data);
-void mainboard_fill_spd_data(struct pei_data *pei_data);
 
-void fill_spd_for_index(uint8_t spd[], unsigned int spd_index);
+void copy_spd(struct pei_data *pei_data, struct spd_info *spdi);
 
 #endif
diff --git a/src/soc/intel/broadwell/raminit.c b/src/soc/intel/broadwell/raminit.c
index 70f3804..686c782 100644
--- a/src/soc/intel/broadwell/raminit.c
+++ b/src/soc/intel/broadwell/raminit.c
@@ -183,9 +183,9 @@
  * 2 = disable dimm 1 on channel
  * 3 = disable dimm 0+1 on channel
  */
-static int make_channel_disabled_mask(const struct pei_data *pd, int ch)
+static int make_channel_disabled_mask(const struct spd_info *spdi, int ch)
 {
-	return (!pd->spd_addresses[ch + ch] << 0) | (!pd->spd_addresses[ch + ch + 1] << 1);
+	return (!spdi->addresses[ch + ch] << 0) | (!spdi->addresses[ch + ch + 1] << 1);
 }
 
 void perform_raminit(const struct chipset_power_state *const power_state)
@@ -195,15 +195,22 @@
 	struct pei_data pei_data = { 0 };
 
 	mainboard_fill_pei_data(&pei_data);
-	mainboard_fill_spd_data(&pei_data);
+
+	/* Obtain the SPD addresses from mainboard code */
+	struct spd_info spdi = { 0 };
+	mb_get_spd_map(&spdi);
+
+	if (CONFIG(HAVE_SPD_IN_CBFS))
+		copy_spd(&pei_data, &spdi);
 
 	/* Calculate unimplemented DIMM slots for each channel */
-	pei_data.dimm_channel0_disabled = make_channel_disabled_mask(&pei_data, 0);
-	pei_data.dimm_channel1_disabled = make_channel_disabled_mask(&pei_data, 1);
+	pei_data.dimm_channel0_disabled = make_channel_disabled_mask(&spdi, 0);
+	pei_data.dimm_channel1_disabled = make_channel_disabled_mask(&spdi, 1);
 
-	for (size_t i = 0; i < ARRAY_SIZE(pei_data.spd_addresses); i++) {
-		const uint8_t addr = pei_data.spd_addresses[i];
-		pei_data.spd_addresses[i] = addr == SPD_MEMORY_DOWN ? 0 : addr;
+	/* MRC expects left-aligned SMBus addresses, and 0 for memory-down */
+	for (size_t i = 0; i < ARRAY_SIZE(spdi.addresses); i++) {
+		const uint8_t addr = spdi.addresses[i];
+		pei_data.spd_addresses[i] = addr == SPD_MEMORY_DOWN ? 0 : addr << 1;
 	}
 
 	post_code(0x32);
diff --git a/src/soc/intel/broadwell/spd.c b/src/soc/intel/broadwell/spd.c
index 97cac38..1af66f1 100644
--- a/src/soc/intel/broadwell/spd.c
+++ b/src/soc/intel/broadwell/spd.c
@@ -17,6 +17,8 @@
 #define SPD_PART_OFF		128
 #define SPD_PART_LEN		18
 
+#define SPD_LEN			256
+
 static void print_spd_info(uint8_t spd[])
 {
 	const int spd_banks[8] = {  8, 16, 32, 64, -1, -1, -1, -1 };
@@ -67,12 +69,12 @@
 	}
 }
 
-void fill_spd_for_index(uint8_t spd[], unsigned int spd_index)
+void copy_spd(struct pei_data *pei_data, struct spd_info *spdi)
 {
 	size_t spd_file_len;
 	uint8_t *spd_file = cbfs_map("spd.bin", &spd_file_len);
 
-	printk(BIOS_DEBUG, "SPD index %d\n", spd_index);
+	printk(BIOS_DEBUG, "SPD index %d\n", spdi->spd_index);
 
 	if (!spd_file)
 		die("SPD data not found.");
@@ -80,16 +82,21 @@
 	if (spd_file_len < SPD_LEN)
 		die("Missing SPD data.");
 
-	if (spd_file_len < ((spd_index + 1) * SPD_LEN)) {
+	if (spd_file_len < ((spdi->spd_index + 1) * SPD_LEN)) {
 		printk(BIOS_ERR, "SPD index override to 0 - old hardware?\n");
-		spd_index = 0;
+		spdi->spd_index = 0;
 	}
 
-	memcpy(spd, spd_file + (spd_index * SPD_LEN), SPD_LEN);
+	uint8_t *const spd = spd_file + (spdi->spd_index * SPD_LEN);
 
 	/* Make sure a valid SPD was found */
 	if (spd[0] == 0)
 		die("Invalid SPD data.");
 
 	print_spd_info(spd);
+
+	for (size_t i = 0; i < ARRAY_SIZE(spdi->addresses); i++) {
+		if (spdi->addresses[i] == SPD_MEMORY_DOWN)
+			memcpy(pei_data->spd_data[i / 2][i % 2], spd, SPD_LEN);
+	}
 }