Add support for some important configuration options and FSP memory init.
The memory init uses swizzle tables from the device tree.

Note that support for the FSP_S binary is not yet included.

Signed-off-by: Simon Glass <s...@chromium.org>
---

 arch/x86/Kconfig                         |  41 ++-
 arch/x86/include/asm/fsp2/fsp_api.h      |  38 +++
 arch/x86/include/asm/fsp2/fsp_internal.h |  16 ++
 arch/x86/lib/fsp2/Makefile               |   7 +
 arch/x86/lib/fsp2/fsp_dram.c             |  73 +++++
 arch/x86/lib/fsp2/fsp_meminit.c          | 344 +++++++++++++++++++++++
 arch/x86/lib/fsp2/fsp_support.c          | 109 +++++++
 7 files changed, 627 insertions(+), 1 deletion(-)
 create mode 100644 arch/x86/include/asm/fsp2/fsp_api.h
 create mode 100644 arch/x86/include/asm/fsp2/fsp_internal.h
 create mode 100644 arch/x86/lib/fsp2/Makefile
 create mode 100644 arch/x86/lib/fsp2/fsp_dram.c
 create mode 100644 arch/x86/lib/fsp2/fsp_meminit.c
 create mode 100644 arch/x86/lib/fsp2/fsp_support.c

diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 0de3f6ed7da..49b5f0f4ebd 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -331,7 +331,7 @@ config X86_RAMTEST
 
 config FLASH_DESCRIPTOR_FILE
        string "Flash descriptor binary filename"
-       depends on HAVE_INTEL_ME
+       depends on HAVE_INTEL_ME || FSP_VERSION2
        default "descriptor.bin"
        help
          The filename of the file to use as flash descriptor in the
@@ -416,6 +416,45 @@ config FSP_ADDR
          The default base address of 0xfffc0000 indicates that the binary must
          be located at offset 0xc0000 from the beginning of a 1MB flash device.
 
+if FSP_VERSION2
+
+config FSP_FILE_T
+       string "Firmware-Support-Package binary filename (Temp RAM)"
+       default "fsp_t.bin"
+       help
+         The filename of the file to use for the temporary-RAM init phase from
+         the Firmware-Support-Package binary. Put this in the board directory.
+         It is used to set up an initial area of RAM which can be used for the
+         stack and other purposes, while bringing up the main system DRAM.
+
+config FSP_ADDR_T
+       hex "Firmware-Support-Package binary location (Temp RAM)"
+       default 0xffff8000
+       help
+         FSP is not Position-Independent Code (PIC) and FSP components have to
+         be rebased if placed at a location which is different from the
+         perferred base address specified during the FSP build. Use Intel's
+         Binary Configuration Tool (BCT) to do the rebase.
+
+config FSP_FILE_M
+       string "Firmware-Support-Package binary filename (Memory Init)"
+       default "fsp_m.bin"
+       help
+         The filename of the file to use for the RAM init phase from the
+         Firmware   Support Package binary. Put this in the board directory.
+         It is used to set up an initial area of RAM which can be used for the
+         stack and other purposes, while bringing up the main system DRAM.
+
+config IFWI_INPUT_FILE
+       string "Filename containing FIT (Firmware Interface Table) with IFWI"
+       default "fitimage.bin"
+       help
+         The IFWI is obtained by running a tool on this file to extract the
+         IFWI. Put this in the board directory. The IFWI contains U-Boot TPL,
+         microcode and other internal items.
+
+endif
+
 config FSP_TEMP_RAM_ADDR
        hex
        depends on FSP_VERSION1
diff --git a/arch/x86/include/asm/fsp2/fsp_api.h 
b/arch/x86/include/asm/fsp2/fsp_api.h
new file mode 100644
index 00000000000..3e41ecbede8
--- /dev/null
+++ b/arch/x86/include/asm/fsp2/fsp_api.h
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: Intel */
+/*
+ * Copyright (C) 2015-2016 Intel Corp.
+ * (Written by Andrey Petrov <andrey.pet...@intel.com> for Intel Corp.)
+ * (Written by Alexandru Gagniuc <alexandrux.gagn...@intel.com> for Intel 
Corp.)
+ * Mostly taken from coreboot fsp2_0/memory_init.c
+ */
+
+#ifndef __ASM_FSP_API_H
+#define __ASM_FSP_API_H
+
+struct fspm_upd;
+struct hob_header;
+
+enum fsp_boot_mode {
+       FSP_BOOT_WITH_FULL_CONFIGURATION = 0x00,
+       FSP_BOOT_WITH_MINIMAL_CONFIGURATION = 0x01,
+       FSP_BOOT_ASSUMING_NO_CONFIGURATION_CHANGES = 0x02,
+       FSP_BOOT_ON_S4_RESUME = 0x05,
+       FSP_BOOT_ON_S3_RESUME = 0x11,
+       FSP_BOOT_ON_FLASH_UPDATE = 0x12,
+       FSP_BOOT_IN_RECOVERY_MODE = 0x20
+};
+
+/**
+ * fsp_memory_init() - Init the SDRAM
+ *
+ * @s3wake: true if we are booting from resume, so cannot reinit the mememory
+ *     from scatch since we will lose its contents
+ * @use_spi_flash: true to use the fast SPI driver to read FSP, otherwise use
+ *     mapped SPI
+ */
+int fsp_memory_init(bool s3wake, bool use_spi_flash);
+
+typedef asmlinkage int (*fsp_memory_init_func)(struct fspm_upd *params,
+                                              struct hob_header **hobp);
+
+#endif
diff --git a/arch/x86/include/asm/fsp2/fsp_internal.h 
b/arch/x86/include/asm/fsp2/fsp_internal.h
new file mode 100644
index 00000000000..c69c90a931d
--- /dev/null
+++ b/arch/x86/include/asm/fsp2/fsp_internal.h
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: Intel */
+/*
+ * Copyright (C) 2015-2016 Intel Corp.
+ * (Written by Alexandru Gagniuc <alexandrux.gagn...@intel.com> for Intel 
Corp.)
+ * Mostly taken from coreboot
+ */
+
+#ifndef __ASM_FSP_INTERNAL_H
+#define __ASM_FSP_INTERNAL_H
+
+struct fsp_header;
+
+int fsp_get_header(ulong offset, ulong size, bool use_spi_flash,
+                  struct fsp_header **fspp, ulong *basep);
+
+#endif
diff --git a/arch/x86/lib/fsp2/Makefile b/arch/x86/lib/fsp2/Makefile
new file mode 100644
index 00000000000..e56e63a1094
--- /dev/null
+++ b/arch/x86/lib/fsp2/Makefile
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright 2019 Google LLC
+
+obj-y += fsp_dram.o
+obj-y += fsp_meminit.o
+obj-y += fsp_support.o
diff --git a/arch/x86/lib/fsp2/fsp_dram.c b/arch/x86/lib/fsp2/fsp_dram.c
new file mode 100644
index 00000000000..7f74cfa63fd
--- /dev/null
+++ b/arch/x86/lib/fsp2/fsp_dram.c
@@ -0,0 +1,73 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2019 Google LLC
+ * Written by Simon Glass <s...@chromium.org>
+ */
+
+#include <common.h>
+#include <acpi_s3.h>
+#include <handoff.h>
+#include <spl.h>
+#include <asm/arch/cpu.h>
+#include <asm/fsp/fsp_support.h>
+#include <asm/fsp2/fsp_api.h>
+
+int dram_init(void)
+{
+       int ret;
+
+       if (spl_phase() == PHASE_SPL) {
+#ifdef CONFIG_HAVE_ACPI_RESUME
+               bool s3wake = gd->arch.prev_sleep_state == ACPI_S3;
+#else
+               bool s3wake = false;
+#endif
+
+               ret = fsp_memory_init(s3wake, BOOT_FROM_FAST_SPI_FLASH);
+               if (ret) {
+                       debug("Memory init failed (err=%x)\n", ret);
+                       return ret;
+               }
+
+               /* The FSP has already set up DRAM, so grab the info we need */
+               ret = fsp_scan_for_ram_size();
+               if (ret)
+                       return ret;
+
+#ifdef CONFIG_ENABLE_MRC_CACHE
+               gd->arch.mrc[MRC_TYPE_NORMAL].buf =
+                       fsp_get_nvs_data(gd->arch.hob_list,
+                                        &gd->arch.mrc[MRC_TYPE_NORMAL].len);
+               gd->arch.mrc[MRC_TYPE_VAR].buf =
+                       fsp_get_var_nvs_data(gd->arch.hob_list,
+                                            &gd->arch.mrc[MRC_TYPE_VAR].len);
+               log_debug("normal %x, var %x\n",
+                         gd->arch.mrc[MRC_TYPE_NORMAL].len,
+                         gd->arch.mrc[MRC_TYPE_VAR].len);
+#endif
+       } else {
+#if CONFIG_IS_ENABLED(HANDOFF)
+               struct spl_handoff *ho = gd->spl_handoff;
+
+               if (!ho) {
+                       debug("No SPL handoff found\n");
+                       return -ESTRPIPE;
+               }
+               gd->ram_size = ho->ram_size;
+               handoff_load_dram_banks(ho);
+#endif
+       }
+
+       return 0;
+}
+
+ulong board_get_usable_ram_top(ulong total_size)
+{
+#if CONFIG_IS_ENABLED(HANDOFF)
+       struct spl_handoff *ho = gd->spl_handoff;
+
+       return ho->arch.usable_ram_top;
+#endif
+
+       return gd->ram_top;
+}
diff --git a/arch/x86/lib/fsp2/fsp_meminit.c b/arch/x86/lib/fsp2/fsp_meminit.c
new file mode 100644
index 00000000000..9eaa565c4a0
--- /dev/null
+++ b/arch/x86/lib/fsp2/fsp_meminit.c
@@ -0,0 +1,344 @@
+// SPDX-License-Identifier: Intel
+/*
+ * Copyright (C) 2015-2016 Intel Corp.
+ * (Written by Andrey Petrov <andrey.pet...@intel.com> for Intel Corp.)
+ * (Written by Alexandru Gagniuc <alexandrux.gagn...@intel.com> for Intel 
Corp.)
+ * Mostly taken from coreboot fsp2_0/memory_init.c
+ */
+
+#include <common.h>
+#include <binman_sym.h>
+#include <cbfs.h>
+#include <dm.h>
+#include <spi_flash.h>
+#include <asm/mrccache.h>
+#include <asm/hob.h>
+#include <asm/fsp/fsp_infoheader.h>
+#include <asm/fsp2/fsp_api.h>
+#include <asm/fsp2/fsp_internal.h>
+#include <asm/arch/fsp/fsp_configs.h>
+#include <asm/arch/fsp/fsp_m_upd.h>
+
+binman_sym_declare(ulong, intel_fsp_m, image_pos);
+binman_sym_declare(ulong, intel_fsp_m, size);
+
+/*
+ * ODT settings:
+ * If ODT PIN to LP4 DRAM is pulled HIGH for ODT_A and HIGH for ODT_B,
+ * choose ODT_A_B_HIGH_HIGH. If ODT PIN to LP4 DRAM is pulled HIGH for ODT_A
+ * and LOW for ODT_B, choose ODT_A_B_HIGH_LOW.
+ *
+ * Note that the enum values correspond to the interpreted UPD fields
+ * within Ch[3:0]_OdtConfig parameters.
+ */
+enum {
+       ODT_A_B_HIGH_LOW        = 0 << 1,
+       ODT_A_B_HIGH_HIGH       = 1 << 1,
+       N_WR_24                 = 1 << 5,
+};
+
+/*
+ * LPDDR4 helper routines for configuring the memory UPD for LPDDR4 operation.
+ * There are four physical LPDDR4 channels, each 32-bits wide. There are two
+ * logical channels using two physical channels together to form a 64-bit
+ * interface to memory for each logical channel.
+ */
+
+enum {
+       LP4_PHYS_CH0A,
+       LP4_PHYS_CH0B,
+       LP4_PHYS_CH1A,
+       LP4_PHYS_CH1B,
+
+       LP4_NUM_PHYS_CHANNELS,
+};
+
+/*
+ * The DQs within a physical channel can be bit-swizzled within each byte.
+ * Within a channel the bytes can be swapped, but the DQs need to be routed
+ * with the corresponding DQS (strobe).
+ */
+enum {
+       LP4_DQS0,
+       LP4_DQS1,
+       LP4_DQS2,
+       LP4_DQS3,
+
+       LP4_NUM_BYTE_LANES,
+       DQ_BITS_PER_DQS         = 8,
+};
+
+/* Provide bit swizzling per DQS and byte swapping within a channel. */
+struct lpddr4_chan_swizzle_cfg {
+       u8 dqs[LP4_NUM_BYTE_LANES][DQ_BITS_PER_DQS];
+};
+
+struct lpddr4_swizzle_cfg {
+       struct lpddr4_chan_swizzle_cfg phys[LP4_NUM_PHYS_CHANNELS];
+};
+
+static int prepare_mrc_cache_type(enum mrc_type_t type,
+                                 struct mrc_data_container **cachep)
+{
+       struct mrc_data_container *cache;
+       struct mrc_region entry;
+       int ret;
+
+       ret = mrccache_get_region(type, NULL, &entry);
+       if (ret)
+               return ret;
+       cache = mrccache_find_current(&entry);
+       if (!cache)
+               return -ENOENT;
+
+       log_debug("MRC at %x, size %x\n", (uint)cache->data, cache->data_size);
+       *cachep = cache;
+
+       return 0;
+}
+
+static int prepare_mrc_cache(struct fspm_upd *upd)
+{
+       struct mrc_data_container *cache;
+       int ret;
+
+       ret = prepare_mrc_cache_type(MRC_TYPE_NORMAL, &cache);
+       if (ret)
+               return log_msg_ret("Cannot get normal cache", ret);
+       upd->arch.nvs_buffer_ptr = cache->data;
+
+       ret = prepare_mrc_cache_type(MRC_TYPE_VAR, &cache);
+       if (ret)
+               return log_msg_ret("Cannot get var cache", ret);
+       upd->config.variable_nvs_buffer_ptr = cache->data;
+
+       return 0;
+}
+
+static int get_coreboot_fsp(ulong map_base, ulong *fsp_m_posp,
+                           ulong *fsp_m_sizep)
+{
+       /* Hard-coded position of CBFS in ROM */
+       ulong cbfs_base = 0x205000;
+       ulong cbfs_size = 0x1bb000;
+       struct cbfs_priv *cbfs;
+       int ret;
+
+       ret = cbfs_init_mem(map_base + cbfs_base, cbfs_size, &cbfs);
+       if (ret)
+               return ret;
+       if (!ret) {
+               const struct cbfs_cachenode *node;
+
+               node = cbfs_find_file(cbfs, "fspm.bin");
+               if (!node) {
+                       printf("no node\n");
+                       return -ENOENT;
+               }
+
+               *fsp_m_posp = (ulong)node->data;
+               *fsp_m_sizep = node->data_length;
+       }
+
+       return 0;
+}
+
+static void setup_sdram(struct fsp_m_config *cfg,
+                       const struct lpddr4_swizzle_cfg *swizzle_cfg)
+{
+       const struct lpddr4_chan_swizzle_cfg *sch;
+       /* Number of bytes to copy per DQS. */
+       const size_t sz = DQ_BITS_PER_DQS;
+       int chan;
+
+       cfg->memory_down = 1;
+       cfg->scrambler_support = 1;
+       cfg->channel_hash_mask = 0x36;
+       cfg->slice_hash_mask = 9;
+       cfg->interleaved_mode = 2;
+       cfg->channels_slices_enable = 0;
+       cfg->min_ref_rate2x_enable = 0;
+       cfg->dual_rank_support_enable = 1;
+
+       /* LPDDR4 is memory down so no SPD addresses. */
+       cfg->dimm0_spd_address = 0;
+       cfg->dimm1_spd_address = 0;
+
+       for (chan = 0; chan < 4; chan++) {
+               struct fsp_ram_channel *ch = &cfg->chan[chan];
+
+               ch->rank_enable = 1;
+               ch->device_width = 1;
+               ch->dram_density = 2;
+               ch->option = 3;
+               ch->odt_config = ODT_A_B_HIGH_HIGH;
+       }
+
+       /*
+        * CH0_DQB byte lanes in the bit swizzle configuration field are
+        * not 1:1. The mapping within the swizzling field is:
+        *   indices [0:7]   - byte lane 1 (DQS1) DQ[8:15]
+        *   indices [8:15]  - byte lane 0 (DQS0) DQ[0:7]
+        *   indices [16:23] - byte lane 3 (DQS3) DQ[24:31]
+        *   indices [24:31] - byte lane 2 (DQS2) DQ[16:23]
+        */
+       sch = &swizzle_cfg->phys[LP4_PHYS_CH0B];
+       memcpy(&cfg->ch_bit_swizzling[0][0], &sch->dqs[LP4_DQS1], sz);
+       memcpy(&cfg->ch_bit_swizzling[0][8], &sch->dqs[LP4_DQS0], sz);
+       memcpy(&cfg->ch_bit_swizzling[0][16], &sch->dqs[LP4_DQS3], sz);
+       memcpy(&cfg->ch_bit_swizzling[0][24], &sch->dqs[LP4_DQS2], sz);
+
+       /*
+        * CH0_DQA byte lanes in the bit swizzle configuration field are 1:1.
+        */
+       sch = &swizzle_cfg->phys[LP4_PHYS_CH0A];
+       memcpy(&cfg->ch_bit_swizzling[1][0], &sch->dqs[LP4_DQS0], sz);
+       memcpy(&cfg->ch_bit_swizzling[1][8], &sch->dqs[LP4_DQS1], sz);
+       memcpy(&cfg->ch_bit_swizzling[1][16], &sch->dqs[LP4_DQS2], sz);
+       memcpy(&cfg->ch_bit_swizzling[1][24], &sch->dqs[LP4_DQS3], sz);
+
+       sch = &swizzle_cfg->phys[LP4_PHYS_CH1B];
+       memcpy(&cfg->ch_bit_swizzling[2][0], &sch->dqs[LP4_DQS1], sz);
+       memcpy(&cfg->ch_bit_swizzling[2][8], &sch->dqs[LP4_DQS0], sz);
+       memcpy(&cfg->ch_bit_swizzling[2][16], &sch->dqs[LP4_DQS3], sz);
+       memcpy(&cfg->ch_bit_swizzling[2][24], &sch->dqs[LP4_DQS2], sz);
+
+       /*
+        * CH0_DQA byte lanes in the bit swizzle configuration field are 1:1.
+        */
+       sch = &swizzle_cfg->phys[LP4_PHYS_CH1A];
+       memcpy(&cfg->ch_bit_swizzling[3][0], &sch->dqs[LP4_DQS0], sz);
+       memcpy(&cfg->ch_bit_swizzling[3][8], &sch->dqs[LP4_DQS1], sz);
+       memcpy(&cfg->ch_bit_swizzling[3][16], &sch->dqs[LP4_DQS2], sz);
+       memcpy(&cfg->ch_bit_swizzling[3][24], &sch->dqs[LP4_DQS3], sz);
+}
+
+/**
+ * setup_config() - Set up the config structure for FSP-M
+ *
+ * @dev: Hostbridge device containing config
+ * @upd: Config data to fill in
+ * @return 0 if OK, -ve on error
+ */
+static int setup_config(struct udevice *dev, struct fspm_upd *upd)
+{
+       struct fsp_m_config *cfg = &upd->config;
+       struct fspm_arch_upd *arch = &upd->arch;
+
+       arch->nvs_buffer_ptr = NULL;
+       prepare_mrc_cache(upd);
+       arch->stack_base = (void *)0xfef96000;
+       arch->boot_loader_tolum_size = 0x2000;
+
+       arch->boot_mode = FSP_BOOT_WITH_FULL_CONFIGURATION;
+       cfg->serial_debug_port_type = 2;
+       cfg->serial_debug_port_device = 2;
+       cfg->serial_debug_port_stride_size = 2;
+       cfg->serial_debug_port_address = 0;
+
+       cfg->package = 1;
+       /* Don't enforce a memory size limit. */
+       cfg->memory_size_limit = 0;
+       cfg->low_memory_max_value = 2048;  /* 2 GB */
+       /* No restrictions on memory above 4GiB */
+       cfg->high_memory_max_value = 0;
+
+       /* Always default to attempt to use saved training data. */
+       cfg->disable_fast_boot = 0;
+
+       const u8 *swizzle_data;
+
+       swizzle_data = dev_read_u8_array_ptr(dev, "lpddr4-swizzle",
+                                            LP4_NUM_BYTE_LANES *
+                                            DQ_BITS_PER_DQS *
+                                            LP4_NUM_PHYS_CHANNELS);
+       if (!swizzle_data)
+               return log_msg_ret("Cannot read swizzel data", -EINVAL);
+
+       setup_sdram(cfg, (struct lpddr4_swizzle_cfg *)swizzle_data);
+
+       cfg->pre_mem_gpio_table_ptr = 0;
+
+       cfg->profile = 0xb;
+       cfg->msg_level_mask = 0;
+
+       /* other */
+       cfg->skip_cse_rbp = 1;
+       cfg->periodic_retraining_disable = 0;
+       cfg->enable_s3_heci2 = 0;
+
+       return 0;
+}
+
+int fsp_memory_init(bool s3wake, bool use_spi_flash)
+{
+       struct fspm_upd upd, *fsp_upd;
+       fsp_memory_init_func func;
+       struct fsp_header *hdr;
+       struct hob_header *hob;
+       struct udevice *dev, *sf;
+       ulong fsp_m_pos = 0;
+       ulong fsp_m_size = 0;
+       size_t map_size;
+       ulong map_base;
+       ulong base;
+       u32 offset;
+       int ret;
+
+       ret = uclass_first_device_err(UCLASS_NORTHBRIDGE, &dev);
+       if (ret)
+               return log_msg_ret("Cannot get northbridge", ret);
+       ret = uclass_first_device_err(UCLASS_SPI_FLASH, &sf);
+       if (ret)
+               return log_msg_ret("Cannot get SPI flash", ret);
+       ret = spi_flash_get_mmap(sf, &map_base, &map_size, &offset);
+       if (ret)
+               return log_msg_ret("Could not get flash mmap", ret);
+
+       ret = -ENOENT;
+       if (false)
+               /* Support using a hybrid image build by coreboot */
+               ret = get_coreboot_fsp(map_base, &fsp_m_pos, &fsp_m_size);
+       if (ret) {
+               ulong mask = CONFIG_ROM_SIZE - 1;
+
+               fsp_m_pos = binman_sym(ulong, intel_fsp_m, image_pos);
+               fsp_m_size = binman_sym(ulong, intel_fsp_m, size);
+               if (fsp_m_pos != BINMAN_SYM_MISSING) {
+                       ret = 0;
+                       if (use_spi_flash)
+                               fsp_m_pos &= mask;
+                       else
+                               fsp_m_pos += (map_base & mask);
+               } else {
+                       ret = -ENOENT;
+               }
+       }
+       if (ret)
+               return log_msg_ret("Cannot find FSP_M", ret);
+
+       /* Use memory-mapped SPI flash by default as it is simpler */
+       ret = fsp_get_header(fsp_m_pos, fsp_m_size, use_spi_flash, &hdr, &base);
+       if (ret)
+               return log_msg_ret("fsp_get_header", ret);
+
+       /* Copy over the default config */
+       fsp_upd = (struct fspm_upd *)(base + hdr->cfg_region_off);
+       if (fsp_upd->header.signature != FSPM_UPD_SIGNATURE)
+               return log_msg_ret("Bad UPD signature", -EPERM);
+       memcpy(&upd, fsp_upd, sizeof(upd));
+
+       ret = setup_config(dev, &upd);
+       if (ret)
+               return log_msg_ret("Could not setup config", ret);
+       debug("SDRAM init...");
+       func = (fsp_memory_init_func)(base + hdr->fsp_mem_init);
+       ret = func(&upd, &hob);
+       if (ret)
+               return log_msg_ret("SDRAM init fail\n", ret);
+
+       gd->arch.hob_list = hob;
+       debug("done\n");
+
+       return 0;
+}
diff --git a/arch/x86/lib/fsp2/fsp_support.c b/arch/x86/lib/fsp2/fsp_support.c
new file mode 100644
index 00000000000..b5e75aaf6d4
--- /dev/null
+++ b/arch/x86/lib/fsp2/fsp_support.c
@@ -0,0 +1,109 @@
+// SPDX-License-Identifier: Intel
+/*
+ * Copyright 2019 Google LLC
+ * Written by Simon Glass <s...@chromium.org>
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <spi_flash.h>
+#include <asm/fsp/fsp_support.h>
+#include <asm/fsp2/fsp_internal.h>
+
+/* The amount of the FSP header to probe to obtain what we need */
+#define PROBE_BUF_SIZE 0x180
+
+/* Not needed in SPL apparently */
+#define SAFETY_MARGIN  0
+
+int fsp_get_header(ulong offset, ulong size, bool use_spi_flash,
+                  struct fsp_header **fspp, ulong *basep)
+{
+       static efi_guid_t guid = FSP_HEADER_GUID;
+       struct fv_ext_header *exhdr;
+       struct fsp_header *fsp;
+       struct ffs_file_header *file_hdr;
+       struct fv_header *fv;
+       struct raw_section *raw;
+       void *ptr, *base;
+       u8 buf[PROBE_BUF_SIZE];
+       struct udevice *dev;
+       int ret;
+
+       /* You are in a maze of twisty headers all alike */
+       debug("offset=%x buf=%x\n", (uint)offset, (uint)buf);
+       if (use_spi_flash) {
+               ret = uclass_first_device_err(UCLASS_SPI_FLASH, &dev);
+               if (ret)
+                       return log_msg_ret("Cannot find flash device", ret);
+               ret = spi_flash_read_dm(dev, offset, PROBE_BUF_SIZE, buf);
+               if (ret)
+                       return log_msg_ret("Cannot read flash", ret);
+       } else {
+               memcpy(buf, (void *)offset, PROBE_BUF_SIZE);
+       }
+
+       /* Initalise the FSP base */
+       ptr = buf;
+       fv = ptr;
+
+       /* Check the FV signature, _FVH */
+       debug("offset=%x sign=%x\n", (uint)offset, (uint)fv->sign);
+       if (fv->sign != EFI_FVH_SIGNATURE)
+               return log_msg_ret("Base FV signature", -EINVAL);
+
+       /* Go to the end of the FV header and align the address */
+       debug("fv->ext_hdr_off = %x\n", fv->ext_hdr_off);
+       ptr += fv->ext_hdr_off;
+       exhdr = ptr;
+       ptr += ALIGN(exhdr->ext_hdr_size, 8);
+       debug("ptr=%x\n", ptr - (void *)buf);
+
+       /* Check the FFS GUID */
+       file_hdr = ptr;
+       if (memcmp(&file_hdr->name, &guid, sizeof(guid)))
+               return log_msg_ret("Base FFS GUID", -ENXIO);
+       /* Add the FFS header size to find the raw section header */
+       ptr = file_hdr + 1;
+
+       raw = ptr;
+       debug("raw->type = %x\n", raw->type);
+       if (raw->type != EFI_SECTION_RAW)
+               return log_msg_ret("Section type not RAW", -ENOEXEC);
+
+       /* Add the raw section header size to find the FSP header */
+       ptr = raw + 1;
+       fsp = ptr;
+
+       /* Check the FSPH header */
+       debug("fsp %x\n", (uint)fsp);
+       if (fsp->sign != EFI_FSPH_SIGNATURE)
+               return log_msg_ret("Base FSPH signature", -EACCES);
+
+       base = (void *)fsp->img_base;
+       debug("Image base %x\n", (uint)base);
+       debug("Image addr %x\n", (uint)fsp->fsp_mem_init);
+       if (use_spi_flash) {
+               ret = spi_flash_read_dm(dev, offset, size + SAFETY_MARGIN,
+                                       base);
+               if (ret)
+                       return log_msg_ret("Could not read FPS-M", ret);
+       } else {
+               memcpy(base, (void *)offset, size + SAFETY_MARGIN);
+       }
+       ptr = base + (ptr - (void *)buf);
+       *fspp = ptr;
+       *basep = fsp->img_base;
+
+       return 0;
+}
+
+int arch_fsp_init(void)
+{
+       return 0;
+}
+
+u32 fsp_notify(struct fsp_header *fsp_hdr, u32 phase)
+{
+       return 0;
+}
-- 
2.23.0.444.g18eeb5a265-goog

_______________________________________________
U-Boot mailing list
U-Boot@lists.denx.de
https://lists.denx.de/listinfo/u-boot

Reply via email to