blob: 2e290c82c479c1440521b80d677ada2b83761b55 [file] [log] [blame]
Julius Wernerce011ec2013-08-06 16:00:37 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2013 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Julius Wernerce011ec2013-08-06 16:00:37 -070014 */
15
16
17#include <assert.h>
Aaron Durbinc6588c52015-05-15 13:15:34 -050018#include <boot_device.h>
Julius Wernerce011ec2013-08-06 16:00:37 -070019#include <cbfs.h> /* This driver serves as a CBFS media source. */
Julius Werner1ed0c8c2014-10-20 13:16:29 -070020#include <console/console.h>
21#include <soc/alternate_cbfs.h>
22#include <soc/power.h>
23#include <soc/spi.h>
Julius Wernerce011ec2013-08-06 16:00:37 -070024#include <stdlib.h>
25#include <string.h>
Julius Wernerec5e5e02014-08-20 15:29:56 -070026#include <symbols.h>
Julius Wernerce011ec2013-08-06 16:00:37 -070027
28/* This allows USB A-A firmware upload from a compatible host in four parts:
29 * The first two are the bare BL1 and the Coreboot boot block, which are just
30 * written to their respective loading addresses. These transfers are initiated
31 * by the IROM / BL1, so this code has nothing to do with them.
32 *
33 * The third transfer is a valid CBFS image that contains only the romstage,
Julius Wernerec5e5e02014-08-20 15:29:56 -070034 * and must be small enough to fit into the PRE_RAM CBFS cache in
Julius Wernerce011ec2013-08-06 16:00:37 -070035 * IRAM. It is loaded when this function gets called in the boot block, and
36 * the normal CBFS code extracts the romstage from it.
37 *
38 * The fourth transfer is also a CBFS image, but can be of arbitrary size and
39 * should contain all available stages/payloads/etc. It is loaded when this
40 * function is called a second time at the end of the romstage, and copied to
Julius Wernerec5e5e02014-08-20 15:29:56 -070041 * the romstage/ramstage CBFS cache in DRAM. It will reside there for the
Julius Wernerce011ec2013-08-06 16:00:37 -070042 * rest of the firmware's lifetime and all subsequent stages (which will not
43 * have __PRE_RAM__ defined) can just directly reference it there.
44 */
Aaron Durbinc6588c52015-05-15 13:15:34 -050045static int usb_cbfs_open(void)
Julius Werner45d2ff32013-08-12 18:04:06 -070046{
Julius Wernerce011ec2013-08-06 16:00:37 -070047#ifdef __PRE_RAM__
48 static int first_run = 1;
49 int (*irom_load_usb)(void) = *irom_load_image_from_usb_ptr;
50
51 if (!first_run)
52 return 0;
53
54 if (!irom_load_usb()) {
Julius Wernerad4556f22013-08-21 17:33:31 -070055 printk(BIOS_EMERG, "Unable to load CBFS image via USB!\n");
Julius Wernerce011ec2013-08-06 16:00:37 -070056 return -1;
57 }
58
59 /*
60 * We need to trust the host/irom to copy the image to our
Julius Wernerec5e5e02014-08-20 15:29:56 -070061 * _cbfs_cache address... there is no way to control or even
Julius Wernerce011ec2013-08-06 16:00:37 -070062 * check the transfer size or target address from our side.
63 */
64
65 printk(BIOS_DEBUG, "USB A-A transfer successful, CBFS image should now"
Julius Wernerec5e5e02014-08-20 15:29:56 -070066 " be at %p\n", _cbfs_cache);
Julius Wernerce011ec2013-08-06 16:00:37 -070067 first_run = 0;
68#endif
69 return 0;
70}
71
Julius Wernerad4556f22013-08-21 17:33:31 -070072/*
73 * SDMMC works very similar to USB A-A: we copy the CBFS image into memory
74 * and read it from there. While SDMMC would also allow direct block by block
75 * on-demand reading, we might run into problems if we call back into the IROM
76 * in very late boot stages (e.g. after initializing/changing MMC clocks)... so
77 * this seems like a safer approach. It also makes it easy to pass our image
78 * down to payloads.
79 */
Aaron Durbinc6588c52015-05-15 13:15:34 -050080static int sdmmc_cbfs_open(void)
Julius Wernerad4556f22013-08-21 17:33:31 -070081{
82#ifdef __PRE_RAM__
83 /*
84 * In the bootblock, we just copy the small part that fits in the buffer
85 * and hope that it's enough (since the romstage is currently always the
86 * first component in the image, this should work out). In the romstage,
Julius Wernerec5e5e02014-08-20 15:29:56 -070087 * we copy until our cache is full (currently 12M) to avoid the pain of
Julius Wernerad4556f22013-08-21 17:33:31 -070088 * figuring out the true image size from in here. Since this is mainly a
89 * developer/debug boot mode, those shortcomings should be bearable.
90 */
Julius Wernerec5e5e02014-08-20 15:29:56 -070091 const u32 count = _cbfs_cache_size / 512;
Julius Wernerad4556f22013-08-21 17:33:31 -070092 static int first_run = 1;
93 int (*irom_load_sdmmc)(u32 start, u32 count, void *dst) =
94 *irom_sdmmc_read_blocks_ptr;
95
96 if (!first_run)
97 return 0;
98
Julius Wernerec5e5e02014-08-20 15:29:56 -070099 if (!irom_load_sdmmc(1, count, _cbfs_cache)) {
Julius Wernerad4556f22013-08-21 17:33:31 -0700100 printk(BIOS_EMERG, "Unable to load CBFS image from SDMMC!\n");
101 return -1;
102 }
103
104 printk(BIOS_DEBUG, "SDMMC read successful, CBFS image should now be"
Julius Wernerec5e5e02014-08-20 15:29:56 -0700105 " at %p\n", _cbfs_cache);
Julius Wernerad4556f22013-08-21 17:33:31 -0700106 first_run = 0;
107#endif
108 return 0;
109}
110
Aaron Durbinc6588c52015-05-15 13:15:34 -0500111static struct mem_region_device alternate_rdev = MEM_REGION_DEV_INIT(NULL, 0);
112
113const struct region_device *boot_device_ro(void)
114{
Julius Wernerce011ec2013-08-06 16:00:37 -0700115 if (*iram_secondary_base == SECONDARY_BASE_BOOT_USB)
Aaron Durbinc6588c52015-05-15 13:15:34 -0500116 return &alternate_rdev.rdev;
Julius Wernerce011ec2013-08-06 16:00:37 -0700117
Julius Wernerfa938c72013-08-29 14:17:36 -0700118 switch (exynos_power->om_stat & OM_STAT_MASK) {
Julius Wernerad4556f22013-08-21 17:33:31 -0700119 case OM_STAT_SDMMC:
Aaron Durbinc6588c52015-05-15 13:15:34 -0500120 return &alternate_rdev.rdev;
Julius Wernerad4556f22013-08-21 17:33:31 -0700121 case OM_STAT_SPI:
Aaron Durbinc6588c52015-05-15 13:15:34 -0500122 return exynos_spi_boot_device();
Julius Wernerad4556f22013-08-21 17:33:31 -0700123 default:
124 printk(BIOS_EMERG, "Exynos OM_STAT value 0x%x not supported!\n",
Julius Wernerfa938c72013-08-29 14:17:36 -0700125 exynos_power->om_stat);
Aaron Durbinc6588c52015-05-15 13:15:34 -0500126 return NULL;
127 }
128}
129
130void boot_device_init(void)
131{
132 mem_region_device_init(&alternate_rdev, _cbfs_cache, _cbfs_cache_size);
133
134 if (*iram_secondary_base == SECONDARY_BASE_BOOT_USB) {
135 printk(BIOS_DEBUG, "Using Exynos alternate boot mode USB A-A\n");
136 usb_cbfs_open();
137 return;
138 }
139
140 switch (exynos_power->om_stat & OM_STAT_MASK) {
141 case OM_STAT_SDMMC:
142 printk(BIOS_DEBUG, "Using Exynos alternate boot mode SDMMC\n");
143 sdmmc_cbfs_open();
144 break;
145 case OM_STAT_SPI:
146 exynos_init_spi_boot_device();
147 break;
148 default:
149 printk(BIOS_EMERG, "Exynos OM_STAT value 0x%x not supported!\n",
150 exynos_power->om_stat);
Julius Wernerad4556f22013-08-21 17:33:31 -0700151 }
Julius Wernerce011ec2013-08-06 16:00:37 -0700152}