drivers/wifi,soc/intel/adl: Move CnviDdrRfim property to drivers

Some non-SoC code might want to know whether or not the CNVi DDR RFIM
feature is enabled. Also note that future SoCs may also support this
feature. To make the CnviDdrRfim property generic, move it from
soc/intel/alderlake to drivers/wifi/generic instead.

Signed-off-by: Tim Wawrzynczak <twawrzynczak@chromium.org>
Change-Id: Idf9fba0a79d1f431269be5851b026ed966600160
Reviewed-on: https://review.coreboot.org/c/coreboot/+/61638
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Rizwan Qureshi <rizwan.qureshi@intel.com>
Reviewed-by: Varshit B Pandya <varshit.b.pandya@intel.com>
diff --git a/src/drivers/wifi/generic/Makefile.inc b/src/drivers/wifi/generic/Makefile.inc
index 5650d8a..91f22d1 100644
--- a/src/drivers/wifi/generic/Makefile.inc
+++ b/src/drivers/wifi/generic/Makefile.inc
@@ -1,6 +1,8 @@
 
 ifeq ($(CONFIG_DRIVERS_WIFI_GENERIC),y)
 
+romstage-y += generic.c
+
 ramstage-y += generic.c
 ramstage-$(CONFIG_GENERATE_SMBIOS_TABLES) += smbios.c
 ramstage-$(CONFIG_HAVE_ACPI_TABLES) += acpi.c
diff --git a/src/drivers/wifi/generic/chip.h b/src/drivers/wifi/generic/chip.h
index 907b260..56a4afc 100644
--- a/src/drivers/wifi/generic/chip.h
+++ b/src/drivers/wifi/generic/chip.h
@@ -12,6 +12,13 @@
 	/* When set to true, this will add a _DSD which contains a single
 	   property, `DmaProperty`, set to 1, under the ACPI Device. */
 	bool is_untrusted;
+
+	/*
+	 * Applicable for Intel chipsets that use CNVi WiFi only. Set this to 1
+	 * to enable CNVi DDR RFIM (radio frequency interference mitigation);
+	 * SoC code propagates this value the applicable FSP UPD.
+	 */
+	bool enable_cnvi_ddr_rfim;
 };
 
 #endif /* _GENERIC_WIFI_H_ */
diff --git a/src/drivers/wifi/generic/generic.c b/src/drivers/wifi/generic/generic.c
index de93741..a6936f1 100644
--- a/src/drivers/wifi/generic/generic.c
+++ b/src/drivers/wifi/generic/generic.c
@@ -7,6 +7,7 @@
 #include <elog.h>
 #include "chip.h"
 #include "wifi_private.h"
+#include "wifi.h"
 
 static void wifi_pci_dev_init(struct device *dev)
 {
@@ -40,17 +41,39 @@
 #endif
 };
 
+static bool is_cnvi(const struct device *dev)
+{
+#if !DEVTREE_EARLY
+	return dev && dev->ops == &wifi_cnvi_ops;
+#else
+	return dev && dev->path.type != DEVICE_PATH_PCI;
+#endif
+}
+
+bool wifi_generic_cnvi_ddr_rfim_enabled(const struct device *dev)
+{
+	const struct drivers_wifi_generic_config *config;
+
+	if (!dev || !is_cnvi(dev) || !dev->chip_info)
+		return false;
+
+	config = dev->chip_info;
+	return config->enable_cnvi_ddr_rfim;
+}
+
 static void wifi_generic_enable(struct device *dev)
 {
-	struct drivers_wifi_generic_config *config = dev ? dev->chip_info : NULL;
+	DEVTREE_CONST struct drivers_wifi_generic_config *config = dev ? dev->chip_info : NULL;
 
 	if (!config)
 		return;
 
-	if (dev->path.type == DEVICE_PATH_PCI)
-		dev->ops = &wifi_pcie_ops;
-	else
+#if !DEVTREE_EARLY
+	if (is_cnvi(dev))
 		dev->ops = &wifi_cnvi_ops;
+	else
+		dev->ops = &wifi_pcie_ops;
+#endif
 }
 
 struct chip_operations drivers_wifi_generic_ops = {
diff --git a/src/drivers/wifi/generic/wifi.h b/src/drivers/wifi/generic/wifi.h
new file mode 100644
index 0000000..54acfa4
--- /dev/null
+++ b/src/drivers/wifi/generic/wifi.h
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <device/device.h>
+
+#ifndef _WIFI_GENERIC_WIFI_H_
+#define _WIFI_GENERIC_WIFI_H_
+
+bool wifi_generic_cnvi_ddr_rfim_enabled(const struct device *dev);
+
+#endif /* _WIFI_GENERIC_WIFI_H_ */
diff --git a/src/mainboard/google/brya/variants/brya0/overridetree.cb b/src/mainboard/google/brya/variants/brya0/overridetree.cb
index 60d0469..f621b12 100644
--- a/src/mainboard/google/brya/variants/brya0/overridetree.cb
+++ b/src/mainboard/google/brya/variants/brya0/overridetree.cb
@@ -45,9 +45,6 @@
 
 	register "typec_aux_bias_pads[0]" = "{.pad_auxp_dc = GPP_E22, .pad_auxn_dc = GPP_E23}"
 
-	# Enable CNVi DDR RFIM
-	register "CnviDdrRfim" = "1"
-
 	# FIVR configurations for brya are disabled since the board doesn't have V1p05 and Vnn
 	# bypass rails implemented.
 	register "ext_fivr_settings" = "{
@@ -187,6 +184,7 @@
 		device ref cnvi_wifi on
 			chip drivers/wifi/generic
 				register "wake" = "GPE0_PME_B0"
+				register "enable_cnvi_ddr_rfim" = "true"
 				device generic 0 on end
 			end
 		end
diff --git a/src/soc/intel/alderlake/chip.h b/src/soc/intel/alderlake/chip.h
index 6766406..7a16902 100644
--- a/src/soc/intel/alderlake/chip.h
+++ b/src/soc/intel/alderlake/chip.h
@@ -559,11 +559,6 @@
 	 * 0: Fast/2, 1: Fast/4, 2: Fast/8, 3: Fast/16; see enum slew_rate for values
 	 */
 	uint8_t SlowSlewRate[NUM_VR_DOMAINS];
-
-	/* CNVi DDR RFIM Enable/Disable
-	 * Default 0. Setting this to 1 enable CNVi DDR RFIM.
-	 */
-	bool CnviDdrRfim;
 };
 
 typedef struct soc_intel_alderlake_config config_t;
diff --git a/src/soc/intel/alderlake/romstage/fsp_params.c b/src/soc/intel/alderlake/romstage/fsp_params.c
index e0f5eb7..3483abd 100644
--- a/src/soc/intel/alderlake/romstage/fsp_params.c
+++ b/src/soc/intel/alderlake/romstage/fsp_params.c
@@ -5,6 +5,7 @@
 #include <cpu/x86/msr.h>
 #include <cpu/intel/cpu_ids.h>
 #include <device/device.h>
+#include <drivers/wifi/generic/wifi.h>
 #include <fsp/util.h>
 #include <intelblocks/cpulib.h>
 #include <intelblocks/pcie_rp.h>
@@ -208,7 +209,13 @@
 	m_cfg->GpioOverride = 0x1;
 
 	/* CNVi DDR RFI Mitigation */
-	m_cfg->CnviDdrRfim = config->CnviDdrRfim;
+	const struct device_path path[] = {
+		{ .type = DEVICE_PATH_PCI, .pci.devfn = PCH_DEVFN_CNVI_WIFI },
+		{ .type = DEVICE_PATH_GENERIC, .generic.id = 0 } };
+	const struct device *dev = find_dev_nested_path(pci_root_bus(), path,
+							ARRAY_SIZE(path));
+	if (is_dev_enabled(dev))
+		m_cfg->CnviDdrRfim = wifi_generic_cnvi_ddr_rfim_enabled(dev);
 }
 
 static void fill_fspm_audio_params(FSP_M_CONFIG *m_cfg,