event: Use an event to replace last_stage_init()

Add a new event which handles this function. Convert existing use of
the function to use the new event instead.

Make sure that EVENT is enabled by affected boards, by selecting it from
the LAST_STAGE_INIT option. For x86, enable it by default since all boards
need it.

For controlcenterdc, inline the get_tpm() function and make sure the event
is not built in SPL.

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/arch/Kconfig b/arch/Kconfig
index c9a3359..90345cb 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -252,6 +252,7 @@
 	imply DM_SPI
 	imply DM_SPI_FLASH
 	imply DM_USB
+	imply LAST_STAGE_INIT
 	imply VIDEO
 	imply SYSRESET
 	imply SPL_SYSRESET
diff --git a/arch/mips/mach-mtmips/cpu.c b/arch/mips/mach-mtmips/cpu.c
index f1e9022..e88dab1 100644
--- a/arch/mips/mach-mtmips/cpu.c
+++ b/arch/mips/mach-mtmips/cpu.c
@@ -4,6 +4,7 @@
  */
 
 #include <common.h>
+#include <event.h>
 #include <init.h>
 #include <malloc.h>
 #include <asm/addrspace.h>
@@ -21,7 +22,8 @@
 	return 0;
 }
 
-int last_stage_init(void)
+#ifndef CONFIG_SPL_BUILD
+static int last_stage_init(void)
 {
 	void *src, *dst;
 
@@ -46,3 +48,5 @@
 
 	return 0;
 }
+EVENT_SPY_SIMPLE(EVT_LAST_STAGE_INIT, last_stage_init);
+#endif
diff --git a/arch/mips/mach-pic32/cpu.c b/arch/mips/mach-pic32/cpu.c
index 785a87b..7ed306e 100644
--- a/arch/mips/mach-pic32/cpu.c
+++ b/arch/mips/mach-pic32/cpu.c
@@ -57,7 +57,7 @@
 }
 
 /* initialize prefetch module related to cpu_clk */
-static void prefetch_init(void)
+static int prefetch_init(void)
 {
 	struct pic32_reg_atomic *regs;
 	const void __iomem *base;
@@ -93,6 +93,8 @@
 	/* Enable prefetch for all */
 	writel(0x30, &regs->set);
 	iounmap(regs);
+
+	return 0;
 }
 
 /* arch-specific CPU init after DM: flash prefetch */
diff --git a/arch/x86/cpu/coreboot/coreboot.c b/arch/x86/cpu/coreboot/coreboot.c
index d7eedbd..835b2c7 100644
--- a/arch/x86/cpu/coreboot/coreboot.c
+++ b/arch/x86/cpu/coreboot/coreboot.c
@@ -7,6 +7,7 @@
 
 #include <common.h>
 #include <cpu_func.h>
+#include <event.h>
 #include <fdtdec.h>
 #include <init.h>
 #include <usb.h>
@@ -74,8 +75,11 @@
 	}
 }
 
-int last_stage_init(void)
+static int last_stage_init(void)
 {
+	if (IS_ENABLED(CONFIG_SPL_BUILD))
+		return 0;
+
 	/* start usb so that usb keyboard can be used as input device */
 	if (IS_ENABLED(CONFIG_USB_KEYBOARD))
 		usb_init();
@@ -84,3 +88,4 @@
 
 	return 0;
 }
+EVENT_SPY_SIMPLE(EVT_LAST_STAGE_INIT, last_stage_init);
diff --git a/arch/x86/cpu/cpu.c b/arch/x86/cpu/cpu.c
index dddd281..ce55efc 100644
--- a/arch/x86/cpu/cpu.c
+++ b/arch/x86/cpu/cpu.c
@@ -26,6 +26,7 @@
 #include <cpu_func.h>
 #include <dm.h>
 #include <errno.h>
+#include <event.h>
 #include <init.h>
 #include <irq.h>
 #include <log.h>
@@ -185,7 +186,8 @@
 }
 #endif
 
-#if !defined(CONFIG_SYS_COREBOOT) && !defined(CONFIG_EFI_STUB)
+#if !defined(CONFIG_SYS_COREBOOT) && !defined(CONFIG_EFI_STUB) && \
+	!defined(CONFIG_SPL_BUILD)
 /*
  * Implement a weak default function for boards that need to do some final init
  * before the system is ready.
@@ -202,7 +204,7 @@
 {
 }
 
-int last_stage_init(void)
+static int last_stage_init(void)
 {
 	struct acpi_fadt __maybe_unused *fadt;
 	int ret;
@@ -245,7 +247,9 @@
 
 	return 0;
 }
-#endif
+EVENT_SPY_SIMPLE(EVT_LAST_STAGE_INIT, last_stage_init);
+
+#endif  /* !SYS_COREBOOT && !EFI_STUB && !SPL_BUILD */
 
 static int x86_init_cpus(void)
 {
diff --git a/arch/x86/cpu/efi/payload.c b/arch/x86/cpu/efi/payload.c
index d8920ef..708bfbe 100644
--- a/arch/x86/cpu/efi/payload.c
+++ b/arch/x86/cpu/efi/payload.c
@@ -9,6 +9,7 @@
 #include <efi.h>
 #include <efi_api.h>
 #include <errno.h>
+#include <event.h>
 #include <init.h>
 #include <log.h>
 #include <usb.h>
@@ -168,7 +169,7 @@
 	return 0;
 }
 
-int last_stage_init(void)
+static int last_stage_init(void)
 {
 	/* start usb so that usb keyboard can be used as input device */
 	if (IS_ENABLED(CONFIG_USB_KEYBOARD))
@@ -176,6 +177,7 @@
 
 	return 0;
 }
+EVENT_SPY_SIMPLE(EVT_LAST_STAGE_INIT, last_stage_init);
 
 unsigned int install_e820_map(unsigned int max_entries,
 			      struct e820_entry *entries)
diff --git a/arch/x86/cpu/quark/quark.c b/arch/x86/cpu/quark/quark.c
index 86d90d8..62b83c2 100644
--- a/arch/x86/cpu/quark/quark.c
+++ b/arch/x86/cpu/quark/quark.c
@@ -107,7 +107,7 @@
 		       CONFIG_PCIE_ECAM_BASE | MEM_BAR_EN);
 }
 
-static void quark_pcie_early_init(void)
+static int quark_pcie_early_init(void)
 {
 	/*
 	 * Step1: Assert PCIe signal PERST#
@@ -146,6 +146,8 @@
 	/* Mixer Load Lane 1 */
 	msg_port_io_clrbits(MSG_PORT_PCIE_AFE, PCIE_RXPICTRL0_L1,
 			    (1 << 6) | (1 << 7));
+
+	return 0;
 }
 
 static void quark_usb_early_init(void)