Merge tag 'efi-2022-07-rc1-2' of https://source.denx.de/u-boot/custodians/u-boot-efi
[platform/kernel/u-boot.git] / board / sandbox / sandbox.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2011 The Chromium OS Authors.
4  */
5
6 #include <common.h>
7 #include <cpu_func.h>
8 #include <cros_ec.h>
9 #include <dm.h>
10 #include <efi.h>
11 #include <efi_loader.h>
12 #include <env_internal.h>
13 #include <init.h>
14 #include <led.h>
15 #include <os.h>
16 #include <asm/global_data.h>
17 #include <asm/test.h>
18 #include <asm/u-boot-sandbox.h>
19 #include <linux/kernel.h>
20 #include <malloc.h>
21
22 #include <extension_board.h>
23
24 /*
25  * Pointer to initial global data area
26  *
27  * Here we initialize it.
28  */
29 gd_t *gd;
30
31 #if CONFIG_IS_ENABLED(EFI_HAVE_CAPSULE_SUPPORT)
32 struct efi_fw_image fw_images[] = {
33 #if defined(CONFIG_EFI_CAPSULE_FIRMWARE_RAW)
34         {
35                 .image_type_id = SANDBOX_UBOOT_IMAGE_GUID,
36                 .fw_name = u"SANDBOX-UBOOT",
37                 .image_index = 1,
38         },
39         {
40                 .image_type_id = SANDBOX_UBOOT_ENV_IMAGE_GUID,
41                 .fw_name = u"SANDBOX-UBOOT-ENV",
42                 .image_index = 2,
43         },
44 #elif defined(CONFIG_EFI_CAPSULE_FIRMWARE_FIT)
45         {
46                 .image_type_id = SANDBOX_FIT_IMAGE_GUID,
47                 .fw_name = u"SANDBOX-FIT",
48                 .image_index = 1,
49         },
50 #endif
51 };
52
53 struct efi_capsule_update_info update_info = {
54         .dfu_string = "sf 0:0=u-boot-bin raw 0x100000 0x50000;"
55                 "u-boot-env raw 0x150000 0x200000",
56         .images = fw_images,
57 };
58
59 u8 num_image_type_guids = ARRAY_SIZE(fw_images);
60 #endif /* EFI_HAVE_CAPSULE_SUPPORT */
61
62 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
63 /*
64  * Add a simple GPIO device (don't use with of-platdata as it interferes with
65  * the auto-generated devices)
66  */
67 U_BOOT_DRVINFO(gpio_sandbox) = {
68         .name = "sandbox_gpio",
69 };
70 #endif
71
72 #ifndef CONFIG_TIMER
73 /* system timer offset in ms */
74 static unsigned long sandbox_timer_offset;
75
76 void timer_test_add_offset(unsigned long offset)
77 {
78         sandbox_timer_offset += offset;
79 }
80
81 unsigned long timer_read_counter(void)
82 {
83         return os_get_nsec() / 1000 + sandbox_timer_offset * 1000;
84 }
85 #endif
86
87 /* specific order for sandbox: nowhere is the first value, used by default */
88 static enum env_location env_locations[] = {
89         ENVL_NOWHERE,
90         ENVL_EXT4,
91         ENVL_FAT,
92 };
93
94 enum env_location env_get_location(enum env_operation op, int prio)
95 {
96         if (prio >= ARRAY_SIZE(env_locations))
97                 return ENVL_UNKNOWN;
98
99         return env_locations[prio];
100 }
101
102 int dram_init(void)
103 {
104         gd->ram_size = CONFIG_SYS_SDRAM_SIZE;
105         return 0;
106 }
107
108 int board_init(void)
109 {
110         if (IS_ENABLED(CONFIG_LED))
111                 led_default_state();
112
113         return 0;
114 }
115
116 int ft_board_setup(void *fdt, struct bd_info *bd)
117 {
118         /* Create an arbitrary reservation to allow testing OF_BOARD_SETUP.*/
119         return fdt_add_mem_rsv(fdt, 0x00d02000, 0x4000);
120 }
121
122 #ifdef CONFIG_CMD_EXTENSION
123 int extension_board_scan(struct list_head *extension_list)
124 {
125         struct extension *extension;
126         int i;
127
128         for (i = 0; i < 2; i++) {
129                 extension = calloc(1, sizeof(struct extension));
130                 snprintf(extension->overlay, sizeof(extension->overlay), "overlay%d.dtbo", i);
131                 snprintf(extension->name, sizeof(extension->name), "extension board %d", i);
132                 snprintf(extension->owner, sizeof(extension->owner), "sandbox");
133                 snprintf(extension->version, sizeof(extension->version), "1.1");
134                 snprintf(extension->other, sizeof(extension->other), "Fictionnal extension board");
135                 list_add_tail(&extension->list, extension_list);
136         }
137
138         return i;
139 }
140 #endif
141
142 #ifdef CONFIG_BOARD_LATE_INIT
143 int board_late_init(void)
144 {
145         struct udevice *dev;
146         int ret;
147
148         ret = uclass_first_device_err(UCLASS_CROS_EC, &dev);
149         if (ret && ret != -ENODEV) {
150                 /* Force console on */
151                 gd->flags &= ~GD_FLG_SILENT;
152
153                 printf("cros-ec communications failure %d\n", ret);
154                 puts("\nPlease reset with Power+Refresh\n\n");
155                 panic("Cannot init cros-ec device");
156                 return -1;
157         }
158         return 0;
159 }
160 #endif