blob: a540c3cadf9031e75fc58827603e0eeac92c5bb3 [file] [log] [blame]
Angel Pons1ddb8942020-04-04 18:51:26 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Julius Wernerce011ec2013-08-06 16:00:37 -07002
Aaron Durbinc6588c52015-05-15 13:15:34 -05003#include <boot_device.h>
Julius Werner1ed0c8c2014-10-20 13:16:29 -07004#include <console/console.h>
5#include <soc/alternate_cbfs.h>
6#include <soc/power.h>
7#include <soc/spi.h>
Julius Wernerec5e5e02014-08-20 15:29:56 -07008#include <symbols.h>
Julius Wernerce011ec2013-08-06 16:00:37 -07009
10/* This allows USB A-A firmware upload from a compatible host in four parts:
Martin Rothe18e6422017-06-03 20:03:18 -060011 * The first two are the bare BL1 and the coreboot boot block, which are just
Julius Wernerce011ec2013-08-06 16:00:37 -070012 * written to their respective loading addresses. These transfers are initiated
13 * by the IROM / BL1, so this code has nothing to do with them.
14 *
15 * The third transfer is a valid CBFS image that contains only the romstage,
Julius Wernerec5e5e02014-08-20 15:29:56 -070016 * and must be small enough to fit into the PRE_RAM CBFS cache in
Julius Wernerce011ec2013-08-06 16:00:37 -070017 * IRAM. It is loaded when this function gets called in the boot block, and
18 * the normal CBFS code extracts the romstage from it.
19 *
20 * The fourth transfer is also a CBFS image, but can be of arbitrary size and
21 * should contain all available stages/payloads/etc. It is loaded when this
22 * function is called a second time at the end of the romstage, and copied to
Julius Wernerec5e5e02014-08-20 15:29:56 -070023 * the romstage/ramstage CBFS cache in DRAM. It will reside there for the
Kyösti Mälkki16248e82019-08-18 14:56:21 +030024 * rest of the firmware's lifetime and all subsequent stages can just directly
25 * reference it there.
Julius Wernerce011ec2013-08-06 16:00:37 -070026 */
Aaron Durbinc6588c52015-05-15 13:15:34 -050027static int usb_cbfs_open(void)
Julius Werner45d2ff32013-08-12 18:04:06 -070028{
Kyösti Mälkki16248e82019-08-18 14:56:21 +030029 if (!ENV_ROMSTAGE_OR_BEFORE)
30 return 0;
31
Julius Wernerce011ec2013-08-06 16:00:37 -070032 static int first_run = 1;
33 int (*irom_load_usb)(void) = *irom_load_image_from_usb_ptr;
34
35 if (!first_run)
36 return 0;
37
38 if (!irom_load_usb()) {
Julius Wernerad4556f22013-08-21 17:33:31 -070039 printk(BIOS_EMERG, "Unable to load CBFS image via USB!\n");
Julius Wernerce011ec2013-08-06 16:00:37 -070040 return -1;
41 }
42
43 /*
44 * We need to trust the host/irom to copy the image to our
Julius Wernerec5e5e02014-08-20 15:29:56 -070045 * _cbfs_cache address... there is no way to control or even
Julius Wernerce011ec2013-08-06 16:00:37 -070046 * check the transfer size or target address from our side.
47 */
48
49 printk(BIOS_DEBUG, "USB A-A transfer successful, CBFS image should now"
Julius Wernerec5e5e02014-08-20 15:29:56 -070050 " be at %p\n", _cbfs_cache);
Julius Wernerce011ec2013-08-06 16:00:37 -070051 first_run = 0;
Julius Wernerce011ec2013-08-06 16:00:37 -070052 return 0;
53}
54
Julius Wernerad4556f22013-08-21 17:33:31 -070055/*
56 * SDMMC works very similar to USB A-A: we copy the CBFS image into memory
57 * and read it from there. While SDMMC would also allow direct block by block
58 * on-demand reading, we might run into problems if we call back into the IROM
59 * in very late boot stages (e.g. after initializing/changing MMC clocks)... so
60 * this seems like a safer approach. It also makes it easy to pass our image
61 * down to payloads.
62 */
Aaron Durbinc6588c52015-05-15 13:15:34 -050063static int sdmmc_cbfs_open(void)
Julius Wernerad4556f22013-08-21 17:33:31 -070064{
Kyösti Mälkki16248e82019-08-18 14:56:21 +030065 if (!ENV_ROMSTAGE_OR_BEFORE)
66 return 0;
67
Julius Wernerad4556f22013-08-21 17:33:31 -070068 /*
69 * In the bootblock, we just copy the small part that fits in the buffer
70 * and hope that it's enough (since the romstage is currently always the
71 * first component in the image, this should work out). In the romstage,
Julius Wernerec5e5e02014-08-20 15:29:56 -070072 * we copy until our cache is full (currently 12M) to avoid the pain of
Julius Wernerad4556f22013-08-21 17:33:31 -070073 * figuring out the true image size from in here. Since this is mainly a
74 * developer/debug boot mode, those shortcomings should be bearable.
75 */
Julius Werner7e0dea62019-02-20 18:39:22 -080076 const u32 count = REGION_SIZE(cbfs_cache) / 512;
Julius Wernerad4556f22013-08-21 17:33:31 -070077 static int first_run = 1;
78 int (*irom_load_sdmmc)(u32 start, u32 count, void *dst) =
79 *irom_sdmmc_read_blocks_ptr;
80
81 if (!first_run)
82 return 0;
83
Julius Wernerec5e5e02014-08-20 15:29:56 -070084 if (!irom_load_sdmmc(1, count, _cbfs_cache)) {
Julius Wernerad4556f22013-08-21 17:33:31 -070085 printk(BIOS_EMERG, "Unable to load CBFS image from SDMMC!\n");
86 return -1;
87 }
88
89 printk(BIOS_DEBUG, "SDMMC read successful, CBFS image should now be"
Julius Wernerec5e5e02014-08-20 15:29:56 -070090 " at %p\n", _cbfs_cache);
Julius Wernerad4556f22013-08-21 17:33:31 -070091 first_run = 0;
Julius Wernerad4556f22013-08-21 17:33:31 -070092 return 0;
93}
94
Julius Wernerc8931972021-04-16 16:48:32 -070095const static struct mem_region_device alternate_rdev =
96 MEM_REGION_DEV_RO_INIT(_cbfs_cache, REGION_SIZE(cbfs_cache));
Aaron Durbinc6588c52015-05-15 13:15:34 -050097
98const struct region_device *boot_device_ro(void)
99{
Julius Wernerce011ec2013-08-06 16:00:37 -0700100 if (*iram_secondary_base == SECONDARY_BASE_BOOT_USB)
Aaron Durbinc6588c52015-05-15 13:15:34 -0500101 return &alternate_rdev.rdev;
Julius Wernerce011ec2013-08-06 16:00:37 -0700102
Julius Wernerfa938c72013-08-29 14:17:36 -0700103 switch (exynos_power->om_stat & OM_STAT_MASK) {
Julius Wernerad4556f22013-08-21 17:33:31 -0700104 case OM_STAT_SDMMC:
Aaron Durbinc6588c52015-05-15 13:15:34 -0500105 return &alternate_rdev.rdev;
Julius Wernerad4556f22013-08-21 17:33:31 -0700106 case OM_STAT_SPI:
Aaron Durbinc6588c52015-05-15 13:15:34 -0500107 return exynos_spi_boot_device();
Julius Wernerad4556f22013-08-21 17:33:31 -0700108 default:
109 printk(BIOS_EMERG, "Exynos OM_STAT value 0x%x not supported!\n",
Julius Wernerfa938c72013-08-29 14:17:36 -0700110 exynos_power->om_stat);
Aaron Durbinc6588c52015-05-15 13:15:34 -0500111 return NULL;
112 }
113}
114
115void boot_device_init(void)
116{
Aaron Durbinc6588c52015-05-15 13:15:34 -0500117 if (*iram_secondary_base == SECONDARY_BASE_BOOT_USB) {
118 printk(BIOS_DEBUG, "Using Exynos alternate boot mode USB A-A\n");
119 usb_cbfs_open();
120 return;
121 }
122
123 switch (exynos_power->om_stat & OM_STAT_MASK) {
124 case OM_STAT_SDMMC:
125 printk(BIOS_DEBUG, "Using Exynos alternate boot mode SDMMC\n");
126 sdmmc_cbfs_open();
127 break;
128 case OM_STAT_SPI:
129 exynos_init_spi_boot_device();
130 break;
131 default:
132 printk(BIOS_EMERG, "Exynos OM_STAT value 0x%x not supported!\n",
133 exynos_power->om_stat);
Julius Wernerad4556f22013-08-21 17:33:31 -0700134 }
Julius Wernerce011ec2013-08-06 16:00:37 -0700135}