Merge tag 'u-boot-imx-20200108' of https://gitlab.denx.de/u-boot/custodians/u-boot-imx
[oweals/u-boot.git] / arch / x86 / lib / fsp1 / fsp_common.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2014, Bin Meng <bmeng.cn@gmail.com>
4  */
5
6 #include <common.h>
7 #include <acpi_s3.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <rtc.h>
11 #include <asm/cmos_layout.h>
12 #include <asm/early_cmos.h>
13 #include <asm/io.h>
14 #include <asm/mrccache.h>
15 #include <asm/post.h>
16 #include <asm/processor.h>
17 #include <asm/fsp1/fsp_support.h>
18
19 DECLARE_GLOBAL_DATA_PTR;
20
21 static void *fsp_prepare_mrc_cache(void)
22 {
23         struct mrc_data_container *cache;
24         struct mrc_region entry;
25         int ret;
26
27         ret = mrccache_get_region(MRC_TYPE_NORMAL, NULL, &entry);
28         if (ret)
29                 return NULL;
30
31         cache = mrccache_find_current(&entry);
32         if (!cache)
33                 return NULL;
34
35         debug("%s: mrc cache at %p, size %x checksum %04x\n", __func__,
36               cache->data, cache->data_size, cache->checksum);
37
38         return cache->data;
39 }
40
41 int arch_fsp_init(void)
42 {
43         void *nvs;
44         int stack = CONFIG_FSP_TEMP_RAM_ADDR;
45         int boot_mode = BOOT_FULL_CONFIG;
46 #ifdef CONFIG_HAVE_ACPI_RESUME
47         int prev_sleep_state = chipset_prev_sleep_state();
48         gd->arch.prev_sleep_state = prev_sleep_state;
49 #endif
50
51         if (!gd->arch.hob_list) {
52                 if (IS_ENABLED(CONFIG_ENABLE_MRC_CACHE))
53                         nvs = fsp_prepare_mrc_cache();
54                 else
55                         nvs = NULL;
56
57 #ifdef CONFIG_HAVE_ACPI_RESUME
58                 if (prev_sleep_state == ACPI_S3) {
59                         if (nvs == NULL) {
60                                 /* If waking from S3 and no cache then */
61                                 debug("No MRC cache found in S3 resume path\n");
62                                 post_code(POST_RESUME_FAILURE);
63                                 /* Clear Sleep Type */
64                                 chipset_clear_sleep_state();
65                                 /* Reboot */
66                                 debug("Rebooting..\n");
67                                 outb(SYS_RST | RST_CPU, IO_PORT_RESET);
68                                 /* Should not reach here.. */
69                                 panic("Reboot System");
70                         }
71
72                         /*
73                          * DM is not available yet at this point, hence call
74                          * CMOS access library which does not depend on DM.
75                          */
76                         stack = cmos_read32(CMOS_FSP_STACK_ADDR);
77                         boot_mode = BOOT_ON_S3_RESUME;
78                 }
79 #endif
80                 /*
81                  * The first time we enter here, call fsp_init().
82                  * Note the execution does not return to this function,
83                  * instead it jumps to fsp_continue().
84                  */
85                 fsp_init(stack, boot_mode, nvs);
86         } else {
87                 /*
88                  * The second time we enter here, adjust the size of malloc()
89                  * pool before relocation. Given gd->malloc_base was adjusted
90                  * after the call to board_init_f_init_reserve() in arch/x86/
91                  * cpu/start.S, we should fix up gd->malloc_limit here.
92                  */
93                 gd->malloc_limit += CONFIG_FSP_SYS_MALLOC_F_LEN;
94         }
95
96         return 0;
97 }