2 * (C) Copyright 2014 CompuLab, Ltd. <www.compulab.co.il>
4 * Authors: Igor Grinberg <grinberg@compulab.co.il>
6 * SPDX-License-Identifier: GPL-2.0+
13 #include <spi_flash.h>
15 #include <bmp_layout.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 #ifdef CONFIG_SPI_FLASH
21 static struct spi_flash *sf;
22 static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
25 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
27 CONFIG_SF_DEFAULT_SPEED,
28 CONFIG_SF_DEFAULT_MODE);
33 return spi_flash_read(sf, offset, read_size, (void *)bmp_load_addr);
36 static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
38 debug("%s: sf support not available\n", __func__);
43 #ifdef CONFIG_CMD_NAND
44 static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
46 return nand_read_skip_bad(&nand_info[nand_curr_device], offset,
48 nand_info[nand_curr_device].size,
49 (u_char *)bmp_load_addr);
52 static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
54 debug("%s: nand support not available\n", __func__);
59 static int splash_storage_read_raw(struct splash_location *location,
60 u32 bmp_load_addr, size_t read_size)
67 offset = location->offset;
68 switch (location->storage) {
69 case SPLASH_STORAGE_NAND:
70 return splash_nand_read_raw(bmp_load_addr, offset, read_size);
71 case SPLASH_STORAGE_SF:
72 return splash_sf_read_raw(bmp_load_addr, offset, read_size);
74 printf("Unknown splash location\n");
80 static int splash_load_raw(struct splash_location *location, u32 bmp_load_addr)
82 struct bmp_header *bmp_hdr;
84 size_t bmp_size, bmp_header_size = sizeof(struct bmp_header);
86 if (bmp_load_addr + bmp_header_size >= gd->start_addr_sp)
87 goto splash_address_too_high;
89 res = splash_storage_read_raw(location, bmp_load_addr, bmp_header_size);
93 bmp_hdr = (struct bmp_header *)bmp_load_addr;
94 bmp_size = le32_to_cpu(bmp_hdr->file_size);
96 if (bmp_load_addr + bmp_size >= gd->start_addr_sp)
97 goto splash_address_too_high;
99 return splash_storage_read_raw(location, bmp_load_addr, bmp_size);
101 splash_address_too_high:
102 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
107 static int splash_select_fs_dev(struct splash_location *location)
111 switch (location->storage) {
112 case SPLASH_STORAGE_MMC:
113 res = fs_set_blk_dev("mmc", location->devpart, FS_TYPE_ANY);
116 printf("Error: unsupported location storage.\n");
121 printf("Error: could not access storage.\n");
126 #define SPLASH_SOURCE_DEFAULT_FILE_NAME "splash.bmp"
128 static int splash_load_fs(struct splash_location *location, u32 bmp_load_addr)
134 splash_file = getenv("splashfile");
136 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
138 res = splash_select_fs_dev(location);
142 res = fs_size(splash_file, &bmp_size);
144 printf("Error (%d): cannot determine file size\n", res);
148 if (bmp_load_addr + bmp_size >= gd->start_addr_sp) {
149 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
153 splash_select_fs_dev(location);
154 return fs_read(splash_file, bmp_load_addr, 0, 0, NULL);
158 * select_splash_location - return the splash location based on board support
159 * and env variable "splashsource".
161 * @locations: An array of supported splash locations.
162 * @size: Size of splash_locations array.
164 * @return: If a null set of splash locations is given, or
165 * splashsource env variable is set to unsupported value
167 * If splashsource env variable is not defined
168 * return the first entry in splash_locations as default.
169 * If splashsource env variable contains a supported value
170 * return the location selected by splashsource.
172 static struct splash_location *select_splash_location(
173 struct splash_location *locations, uint size)
176 char *env_splashsource;
178 if (!locations || size == 0)
181 env_splashsource = getenv("splashsource");
182 if (env_splashsource == NULL)
183 return &locations[0];
185 for (i = 0; i < size; i++) {
186 if (!strcmp(locations[i].name, env_splashsource))
187 return &locations[i];
190 printf("splashsource env variable set to unsupported value\n");
195 * splash_source_load - load splash image from a supported location.
197 * Select a splash image location based on the value of splashsource environment
198 * variable and the board supported splash source locations, and load a
199 * splashimage to the address pointed to by splashimage environment variable.
201 * @locations: An array of supported splash locations.
202 * @size: Size of splash_locations array.
204 * @return: 0 on success, negative value on failure.
206 int splash_source_load(struct splash_location *locations, uint size)
208 struct splash_location *splash_location;
209 char *env_splashimage_value;
212 env_splashimage_value = getenv("splashimage");
213 if (env_splashimage_value == NULL)
216 bmp_load_addr = simple_strtoul(env_splashimage_value, 0, 16);
217 if (bmp_load_addr == 0) {
218 printf("Error: bad splashimage address specified\n");
222 splash_location = select_splash_location(locations, size);
223 if (!splash_location)
226 if (splash_location->flags & SPLASH_STORAGE_RAW)
227 return splash_load_raw(splash_location, bmp_load_addr);
228 else if (splash_location->flags & SPLASH_STORAGE_FS)
229 return splash_load_fs(splash_location, bmp_load_addr);