blob: 0eb8c08604b00ed3ac0a072dd8e10ed7223b62d6 [file] [log] [blame]
Angel Pons1ddb8942020-04-04 18:51:26 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Julius Werner45d2ff32013-08-12 18:04:06 -07002
Julius Werner80af4422014-10-20 13:18:56 -07003#include <arch/cache.h>
Aaron Durbinc6588c52015-05-15 13:15:34 -05004#include <boot_device.h>
Julius Werner80af4422014-10-20 13:18:56 -07005#include <console/console.h>
6#include <soc/alternate_cbfs.h>
7#include <soc/power.h>
8#include <soc/spi.h>
Julius Wernerec5e5e02014-08-20 15:29:56 -07009#include <symbols.h>
Julius Werner45d2ff32013-08-12 18:04:06 -070010
11/* This allows USB A-A firmware upload from a compatible host in four parts:
Martin Rothe18e6422017-06-03 20:03:18 -060012 * The first two are the bare BL1 and the coreboot boot block, which are just
Julius Werner45d2ff32013-08-12 18:04:06 -070013 * written to their respective loading addresses. These transfers are initiated
14 * by the IROM / BL1, so this code has nothing to do with them.
15 *
16 * The third transfer is a valid CBFS image that contains only the romstage,
Julius Wernerec5e5e02014-08-20 15:29:56 -070017 * and must be small enough to fit into the PRE_RAM CBFS cache in
Julius Werner45d2ff32013-08-12 18:04:06 -070018 * IRAM. It is loaded when this function gets called in the boot block, and
19 * the normal CBFS code extracts the romstage from it.
20 *
21 * The fourth transfer is also a CBFS image, but can be of arbitrary size and
22 * should contain all available stages/payloads/etc. It is loaded when this
23 * function is called a second time at the end of the romstage, and copied to
Julius Wernerec5e5e02014-08-20 15:29:56 -070024 * the romstage/ramstage CBFS cache in DRAM. It will reside there for the
Kyösti Mälkki16248e82019-08-18 14:56:21 +030025 * rest of the firmware's lifetime and all subsequent stages can just directly
26 * reference it there.
Julius Werner45d2ff32013-08-12 18:04:06 -070027 */
Aaron Durbinc6588c52015-05-15 13:15:34 -050028static int usb_cbfs_open(void)
Julius Werner45d2ff32013-08-12 18:04:06 -070029{
Kyösti Mälkki16248e82019-08-18 14:56:21 +030030 if (!ENV_ROMSTAGE_OR_BEFORE)
31 return 0;
32
Julius Werner45d2ff32013-08-12 18:04:06 -070033 static int first_run = 1;
34 int (*irom_load_usb)(void) = *irom_load_image_from_usb_ptr;
35
36 if (!first_run)
37 return 0;
38
Julius Wernerad4556f22013-08-21 17:33:31 -070039 dcache_mmu_disable();
Julius Werner45d2ff32013-08-12 18:04:06 -070040 if (!irom_load_usb()) {
Julius Wernerad4556f22013-08-21 17:33:31 -070041 dcache_mmu_enable();
42 printk(BIOS_EMERG, "Unable to load CBFS image via USB!\n");
Julius Werner45d2ff32013-08-12 18:04:06 -070043 return -1;
44 }
Julius Wernerad4556f22013-08-21 17:33:31 -070045 dcache_mmu_enable();
Julius Werner45d2ff32013-08-12 18:04:06 -070046
47 /*
48 * We need to trust the host/irom to copy the image to our
Julius Wernerec5e5e02014-08-20 15:29:56 -070049 * _cbfs_cache address... there is no way to control or even
Julius Werner45d2ff32013-08-12 18:04:06 -070050 * check the transfer size or target address from our side.
51 */
52
53 printk(BIOS_DEBUG, "USB A-A transfer successful, CBFS image should now"
Julius Wernerec5e5e02014-08-20 15:29:56 -070054 " be at %p\n", _cbfs_cache);
Julius Werner45d2ff32013-08-12 18:04:06 -070055 first_run = 0;
Julius Werner45d2ff32013-08-12 18:04:06 -070056 return 0;
57}
58
Julius Wernerad4556f22013-08-21 17:33:31 -070059/*
60 * SDMMC works very similar to USB A-A: we copy the CBFS image into memory
61 * and read it from there. While SDMMC would also allow direct block by block
62 * on-demand reading, we might run into problems if we call back into the IROM
63 * in very late boot stages (e.g. after initializing/changing MMC clocks)... so
64 * this seems like a safer approach. It also makes it easy to pass our image
65 * down to payloads.
66 */
Aaron Durbinc6588c52015-05-15 13:15:34 -050067static int sdmmc_cbfs_open(void)
Julius Wernerad4556f22013-08-21 17:33:31 -070068{
Kyösti Mälkki16248e82019-08-18 14:56:21 +030069 if (!ENV_ROMSTAGE_OR_BEFORE)
70 return 0;
71
Julius Wernerad4556f22013-08-21 17:33:31 -070072 /*
73 * In the bootblock, we just copy the small part that fits in the buffer
74 * and hope that it's enough (since the romstage is currently always the
75 * first component in the image, this should work out). In the romstage,
Julius Wernerec5e5e02014-08-20 15:29:56 -070076 * we copy until our cache is full (currently 12M) to avoid the pain of
Julius Wernerad4556f22013-08-21 17:33:31 -070077 * figuring out the true image size from in here. Since this is mainly a
78 * developer/debug boot mode, those shortcomings should be bearable.
79 */
Julius Werner7e0dea62019-02-20 18:39:22 -080080 const u32 count = REGION_SIZE(cbfs_cache) / 512;
Julius Wernerad4556f22013-08-21 17:33:31 -070081 static int first_run = 1;
82 int (*irom_load_sdmmc)(u32 start, u32 count, void *dst) =
83 *irom_sdmmc_read_blocks_ptr;
84
85 if (!first_run)
86 return 0;
87
88 dcache_mmu_disable();
Julius Wernerec5e5e02014-08-20 15:29:56 -070089 if (!irom_load_sdmmc(1, count, _cbfs_cache)) {
Julius Wernerad4556f22013-08-21 17:33:31 -070090 dcache_mmu_enable();
91 printk(BIOS_EMERG, "Unable to load CBFS image from SDMMC!\n");
92 return -1;
93 }
94 dcache_mmu_enable();
95
96 printk(BIOS_DEBUG, "SDMMC read successful, CBFS image should now be"
Julius Wernerec5e5e02014-08-20 15:29:56 -070097 " at %p\n", _cbfs_cache);
Julius Wernerad4556f22013-08-21 17:33:31 -070098 first_run = 0;
Julius Wernerad4556f22013-08-21 17:33:31 -070099 return 0;
100}
101
Antonello Dettorie5f48d22016-06-22 21:09:08 +0200102static struct mem_region_device alternate_rdev =
103 MEM_REGION_DEV_RO_INIT(NULL, 0);
Aaron Durbinc6588c52015-05-15 13:15:34 -0500104
105const struct region_device *boot_device_ro(void)
106{
Julius Werner45d2ff32013-08-12 18:04:06 -0700107 if (*iram_secondary_base == SECONDARY_BASE_BOOT_USB)
Aaron Durbinc6588c52015-05-15 13:15:34 -0500108 return &alternate_rdev.rdev;
Julius Werner45d2ff32013-08-12 18:04:06 -0700109
Julius Wernerfa938c72013-08-29 14:17:36 -0700110 switch (exynos_power->om_stat & OM_STAT_MASK) {
Julius Wernerad4556f22013-08-21 17:33:31 -0700111 case OM_STAT_SDMMC:
Aaron Durbinc6588c52015-05-15 13:15:34 -0500112 return &alternate_rdev.rdev;
Julius Wernerad4556f22013-08-21 17:33:31 -0700113 case OM_STAT_SPI:
Aaron Durbinc6588c52015-05-15 13:15:34 -0500114 return exynos_spi_boot_device();
Julius Wernerad4556f22013-08-21 17:33:31 -0700115 default:
116 printk(BIOS_EMERG, "Exynos OM_STAT value 0x%x not supported!\n",
Julius Wernerfa938c72013-08-29 14:17:36 -0700117 exynos_power->om_stat);
Aaron Durbinc6588c52015-05-15 13:15:34 -0500118 return NULL;
119 }
120}
121
122void boot_device_init(void)
123{
Antonello Dettorie5f48d22016-06-22 21:09:08 +0200124 mem_region_device_ro_init(&alternate_rdev, _cbfs_cache,
Julius Werner7e0dea62019-02-20 18:39:22 -0800125 REGION_SIZE(cbfs_cache));
Aaron Durbinc6588c52015-05-15 13:15:34 -0500126
127 if (*iram_secondary_base == SECONDARY_BASE_BOOT_USB) {
128 printk(BIOS_DEBUG, "Using Exynos alternate boot mode USB A-A\n");
129 usb_cbfs_open();
130 return;
131 }
132
133 switch (exynos_power->om_stat & OM_STAT_MASK) {
134 case OM_STAT_SDMMC:
135 printk(BIOS_DEBUG, "Using Exynos alternate boot mode SDMMC\n");
136 sdmmc_cbfs_open();
137 break;
138 case OM_STAT_SPI:
139 exynos_init_spi_boot_device();
140 break;
141 default:
142 printk(BIOS_EMERG, "Exynos OM_STAT value 0x%x not supported!\n",
143 exynos_power->om_stat);
Julius Wernerad4556f22013-08-21 17:33:31 -0700144 }
Julius Werner45d2ff32013-08-12 18:04:06 -0700145}