1 /* 2 * (C) Copyright 2010 3 * Texas Instruments, <www.ti.com> 4 * 5 * Aneesh V <aneesh@ti.com> 6 * 7 * SPDX-License-Identifier: GPL-2.0+ 8 */ 9 #include <common.h> 10 #include <dm.h> 11 #include <spl.h> 12 #include <asm/u-boot.h> 13 #include <nand.h> 14 #include <fat.h> 15 #include <version.h> 16 #include <i2c.h> 17 #include <image.h> 18 #include <malloc.h> 19 #include <dm/root.h> 20 #include <linux/compiler.h> 21 22 DECLARE_GLOBAL_DATA_PTR; 23 24 #ifndef CONFIG_SYS_UBOOT_START 25 #define CONFIG_SYS_UBOOT_START CONFIG_SYS_TEXT_BASE 26 #endif 27 #ifndef CONFIG_SYS_MONITOR_LEN 28 /* Unknown U-Boot size, let's assume it will not be more than 200 KB */ 29 #define CONFIG_SYS_MONITOR_LEN (200 * 1024) 30 #endif 31 32 u32 *boot_params_ptr = NULL; 33 struct spl_image_info spl_image; 34 35 /* Define board data structure */ 36 static bd_t bdata __attribute__ ((section(".data"))); 37 38 /* 39 * Default function to determine if u-boot or the OS should 40 * be started. This implementation always returns 1. 41 * 42 * Please implement your own board specific funcion to do this. 43 * 44 * RETURN 45 * 0 to not start u-boot 46 * positive if u-boot should start 47 */ 48 #ifdef CONFIG_SPL_OS_BOOT 49 __weak int spl_start_uboot(void) 50 { 51 puts("SPL: Please implement spl_start_uboot() for your board\n"); 52 puts("SPL: Direct Linux boot not active!\n"); 53 return 1; 54 } 55 #endif 56 57 /* 58 * Weak default function for board specific cleanup/preparation before 59 * Linux boot. Some boards/platforms might not need it, so just provide 60 * an empty stub here. 61 */ 62 __weak void spl_board_prepare_for_linux(void) 63 { 64 /* Nothing to do! */ 65 } 66 67 __weak void spl_board_prepare_for_boot(void) 68 { 69 /* Nothing to do! */ 70 } 71 72 void spl_set_header_raw_uboot(void) 73 { 74 spl_image.size = CONFIG_SYS_MONITOR_LEN; 75 spl_image.entry_point = CONFIG_SYS_UBOOT_START; 76 spl_image.load_addr = CONFIG_SYS_TEXT_BASE; 77 spl_image.os = IH_OS_U_BOOT; 78 spl_image.name = "U-Boot"; 79 } 80 81 int spl_parse_image_header(const struct image_header *header) 82 { 83 u32 header_size = sizeof(struct image_header); 84 85 if (image_get_magic(header) == IH_MAGIC) { 86 if (spl_image.flags & SPL_COPY_PAYLOAD_ONLY) { 87 /* 88 * On some system (e.g. powerpc), the load-address and 89 * entry-point is located at address 0. We can't load 90 * to 0-0x40. So skip header in this case. 91 */ 92 spl_image.load_addr = image_get_load(header); 93 spl_image.entry_point = image_get_ep(header); 94 spl_image.size = image_get_data_size(header); 95 } else { 96 spl_image.entry_point = image_get_load(header); 97 /* Load including the header */ 98 spl_image.load_addr = spl_image.entry_point - 99 header_size; 100 spl_image.size = image_get_data_size(header) + 101 header_size; 102 } 103 spl_image.os = image_get_os(header); 104 spl_image.name = image_get_name(header); 105 debug("spl: payload image: %.*s load addr: 0x%x size: %d\n", 106 (int)sizeof(spl_image.name), spl_image.name, 107 spl_image.load_addr, spl_image.size); 108 } else { 109 #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE 110 /* 111 * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the 112 * code which loads images in SPL cannot guarantee that 113 * absolutely all read errors will be reported. 114 * An example is the LPC32XX MLC NAND driver, which 115 * will consider that a completely unreadable NAND block 116 * is bad, and thus should be skipped silently. 117 */ 118 panic("** no mkimage signature but raw image not supported"); 119 #elif defined(CONFIG_SPL_ABORT_ON_RAW_IMAGE) 120 /* Signature not found, proceed to other boot methods. */ 121 return -EINVAL; 122 #else 123 /* Signature not found - assume u-boot.bin */ 124 debug("mkimage signature not found - ih_magic = %x\n", 125 header->ih_magic); 126 spl_set_header_raw_uboot(); 127 #endif 128 } 129 return 0; 130 } 131 132 __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image) 133 { 134 typedef void __noreturn (*image_entry_noargs_t)(void); 135 136 image_entry_noargs_t image_entry = 137 (image_entry_noargs_t)(unsigned long)spl_image->entry_point; 138 139 debug("image entry point: 0x%X\n", spl_image->entry_point); 140 image_entry(); 141 } 142 143 #ifdef CONFIG_SPL_RAM_DEVICE 144 static int spl_ram_load_image(void) 145 { 146 const struct image_header *header; 147 148 /* 149 * Get the header. It will point to an address defined by handoff 150 * which will tell where the image located inside the flash. For 151 * now, it will temporary fixed to address pointed by U-Boot. 152 */ 153 header = (struct image_header *) 154 (CONFIG_SYS_TEXT_BASE - sizeof(struct image_header)); 155 156 spl_parse_image_header(header); 157 158 return 0; 159 } 160 #endif 161 162 int spl_init(void) 163 { 164 int ret; 165 166 debug("spl_init()\n"); 167 #if defined(CONFIG_SYS_MALLOC_F_LEN) 168 gd->malloc_limit = CONFIG_SYS_MALLOC_F_LEN; 169 gd->malloc_ptr = 0; 170 #endif 171 if (CONFIG_IS_ENABLED(OF_CONTROL)) { 172 ret = fdtdec_setup(); 173 if (ret) { 174 debug("fdtdec_setup() returned error %d\n", ret); 175 return ret; 176 } 177 } 178 if (IS_ENABLED(CONFIG_SPL_DM)) { 179 ret = dm_init_and_scan(true); 180 if (ret) { 181 debug("dm_init_and_scan() returned error %d\n", ret); 182 return ret; 183 } 184 } 185 gd->flags |= GD_FLG_SPL_INIT; 186 187 return 0; 188 } 189 190 #ifndef BOOT_DEVICE_NONE 191 #define BOOT_DEVICE_NONE 0xdeadbeef 192 #endif 193 194 static u32 spl_boot_list[] = { 195 BOOT_DEVICE_NONE, 196 BOOT_DEVICE_NONE, 197 BOOT_DEVICE_NONE, 198 BOOT_DEVICE_NONE, 199 BOOT_DEVICE_NONE, 200 }; 201 202 __weak void board_boot_order(u32 *spl_boot_list) 203 { 204 spl_boot_list[0] = spl_boot_device(); 205 } 206 207 #ifdef CONFIG_SPL_BOARD_LOAD_IMAGE 208 __weak void spl_board_announce_boot_device(void) { } 209 #endif 210 211 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 212 struct boot_device_name { 213 u32 boot_dev; 214 const char *name; 215 }; 216 217 struct boot_device_name boot_name_table[] = { 218 #ifdef CONFIG_SPL_RAM_DEVICE 219 { BOOT_DEVICE_RAM, "RAM" }, 220 #endif 221 #ifdef CONFIG_SPL_MMC_SUPPORT 222 { BOOT_DEVICE_MMC1, "MMC1" }, 223 { BOOT_DEVICE_MMC2, "MMC2" }, 224 { BOOT_DEVICE_MMC2_2, "MMC2_2" }, 225 #endif 226 #ifdef CONFIG_SPL_NAND_SUPPORT 227 { BOOT_DEVICE_NAND, "NAND" }, 228 #endif 229 #ifdef CONFIG_SPL_ONENAND_SUPPORT 230 { BOOT_DEVICE_ONENAND, "OneNAND" }, 231 #endif 232 #ifdef CONFIG_SPL_NOR_SUPPORT 233 { BOOT_DEVICE_NOR, "NOR" }, 234 #endif 235 #ifdef CONFIG_SPL_YMODEM_SUPPORT 236 { BOOT_DEVICE_UART, "UART" }, 237 #endif 238 #ifdef CONFIG_SPL_SPI_SUPPORT 239 { BOOT_DEVICE_SPI, "SPI" }, 240 #endif 241 #ifdef CONFIG_SPL_ETH_SUPPORT 242 #ifdef CONFIG_SPL_ETH_DEVICE 243 { BOOT_DEVICE_CPGMAC, "eth device" }, 244 #else 245 { BOOT_DEVICE_CPGMAC, "net" }, 246 #endif 247 #endif 248 #ifdef CONFIG_SPL_USBETH_SUPPORT 249 { BOOT_DEVICE_USBETH, "USB eth" }, 250 #endif 251 #ifdef CONFIG_SPL_USB_SUPPORT 252 { BOOT_DEVICE_USB, "USB" }, 253 #endif 254 #ifdef CONFIG_SPL_SATA_SUPPORT 255 { BOOT_DEVICE_SATA, "SATA" }, 256 #endif 257 /* Keep this entry last */ 258 { BOOT_DEVICE_NONE, "unknown boot device" }, 259 }; 260 261 static void announce_boot_device(u32 boot_device) 262 { 263 int i; 264 265 puts("Trying to boot from "); 266 267 #ifdef CONFIG_SPL_BOARD_LOAD_IMAGE 268 if (boot_device == BOOT_DEVICE_BOARD) { 269 spl_board_announce_boot_device(); 270 puts("\n"); 271 return; 272 } 273 #endif 274 for (i = 0; i < ARRAY_SIZE(boot_name_table) - 1; i++) { 275 if (boot_name_table[i].boot_dev == boot_device) 276 break; 277 } 278 279 printf("%s\n", boot_name_table[i].name); 280 } 281 #else 282 static inline void announce_boot_device(u32 boot_device) { } 283 #endif 284 285 static int spl_load_image(u32 boot_device) 286 { 287 switch (boot_device) { 288 #ifdef CONFIG_SPL_RAM_DEVICE 289 case BOOT_DEVICE_RAM: 290 return spl_ram_load_image(); 291 #endif 292 #ifdef CONFIG_SPL_MMC_SUPPORT 293 case BOOT_DEVICE_MMC1: 294 case BOOT_DEVICE_MMC2: 295 case BOOT_DEVICE_MMC2_2: 296 return spl_mmc_load_image(boot_device); 297 #endif 298 #ifdef CONFIG_SPL_NAND_SUPPORT 299 case BOOT_DEVICE_NAND: 300 return spl_nand_load_image(); 301 #endif 302 #ifdef CONFIG_SPL_ONENAND_SUPPORT 303 case BOOT_DEVICE_ONENAND: 304 return spl_onenand_load_image(); 305 #endif 306 #ifdef CONFIG_SPL_NOR_SUPPORT 307 case BOOT_DEVICE_NOR: 308 return spl_nor_load_image(); 309 #endif 310 #ifdef CONFIG_SPL_YMODEM_SUPPORT 311 case BOOT_DEVICE_UART: 312 return spl_ymodem_load_image(); 313 #endif 314 #ifdef CONFIG_SPL_SPI_SUPPORT 315 case BOOT_DEVICE_SPI: 316 return spl_spi_load_image(); 317 #endif 318 #ifdef CONFIG_SPL_ETH_SUPPORT 319 case BOOT_DEVICE_CPGMAC: 320 #ifdef CONFIG_SPL_ETH_DEVICE 321 return spl_net_load_image(CONFIG_SPL_ETH_DEVICE); 322 #else 323 return spl_net_load_image(NULL); 324 #endif 325 #endif 326 #ifdef CONFIG_SPL_USBETH_SUPPORT 327 case BOOT_DEVICE_USBETH: 328 return spl_net_load_image("usb_ether"); 329 #endif 330 #ifdef CONFIG_SPL_USB_SUPPORT 331 case BOOT_DEVICE_USB: 332 return spl_usb_load_image(); 333 #endif 334 #ifdef CONFIG_SPL_SATA_SUPPORT 335 case BOOT_DEVICE_SATA: 336 return spl_sata_load_image(); 337 #endif 338 #ifdef CONFIG_SPL_BOARD_LOAD_IMAGE 339 case BOOT_DEVICE_BOARD: 340 return spl_board_load_image(); 341 #endif 342 default: 343 #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 344 puts("SPL: Unsupported Boot Device!\n"); 345 #endif 346 return -ENODEV; 347 } 348 349 return -EINVAL; 350 } 351 352 void board_init_r(gd_t *dummy1, ulong dummy2) 353 { 354 int i; 355 356 debug(">>spl:board_init_r()\n"); 357 358 #if defined(CONFIG_SYS_SPL_MALLOC_START) 359 mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START, 360 CONFIG_SYS_SPL_MALLOC_SIZE); 361 gd->flags |= GD_FLG_FULL_MALLOC_INIT; 362 #endif 363 if (!(gd->flags & GD_FLG_SPL_INIT)) { 364 if (spl_init()) 365 hang(); 366 } 367 #ifndef CONFIG_PPC 368 /* 369 * timer_init() does not exist on PPC systems. The timer is initialized 370 * and enabled (decrementer) in interrupt_init() here. 371 */ 372 timer_init(); 373 #endif 374 375 #ifdef CONFIG_SPL_BOARD_INIT 376 spl_board_init(); 377 #endif 378 379 board_boot_order(spl_boot_list); 380 for (i = 0; i < ARRAY_SIZE(spl_boot_list) && 381 spl_boot_list[i] != BOOT_DEVICE_NONE; i++) { 382 announce_boot_device(spl_boot_list[i]); 383 if (!spl_load_image(spl_boot_list[i])) 384 break; 385 } 386 387 if (i == ARRAY_SIZE(spl_boot_list) || 388 spl_boot_list[i] == BOOT_DEVICE_NONE) { 389 puts("SPL: failed to boot from all boot devices\n"); 390 hang(); 391 } 392 393 switch (spl_image.os) { 394 case IH_OS_U_BOOT: 395 debug("Jumping to U-Boot\n"); 396 break; 397 #ifdef CONFIG_SPL_OS_BOOT 398 case IH_OS_LINUX: 399 debug("Jumping to Linux\n"); 400 spl_board_prepare_for_linux(); 401 jump_to_image_linux((void *)CONFIG_SYS_SPL_ARGS_ADDR); 402 #endif 403 default: 404 debug("Unsupported OS image.. Jumping nevertheless..\n"); 405 } 406 #if defined(CONFIG_SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE) 407 debug("SPL malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr, 408 gd->malloc_ptr / 1024); 409 #endif 410 411 debug("loaded - jumping to U-Boot..."); 412 spl_board_prepare_for_boot(); 413 jump_to_image_no_args(&spl_image); 414 } 415 416 /* 417 * This requires UART clocks to be enabled. In order for this to work the 418 * caller must ensure that the gd pointer is valid. 419 */ 420 void preloader_console_init(void) 421 { 422 gd->bd = &bdata; 423 gd->baudrate = CONFIG_BAUDRATE; 424 425 serial_init(); /* serial communications setup */ 426 427 gd->have_console = 1; 428 429 puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \ 430 U_BOOT_TIME ")\n"); 431 #ifdef CONFIG_SPL_DISPLAY_PRINT 432 spl_display_print(); 433 #endif 434 } 435 436 /** 437 * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution 438 * 439 * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM 440 * for the main board_init_r() execution. This is typically because we need 441 * more stack space for things like the MMC sub-system. 442 * 443 * This function calculates the stack position, copies the global_data into 444 * place, sets the new gd (except for ARM, for which setting GD within a C 445 * function may not always work) and returns the new stack position. The 446 * caller is responsible for setting up the sp register and, in the case 447 * of ARM, setting up gd. 448 * 449 * All of this is done using the same layout and alignments as done in 450 * board_init_f_init_reserve() / board_init_f_alloc_reserve(). 451 * 452 * @return new stack location, or 0 to use the same stack 453 */ 454 ulong spl_relocate_stack_gd(void) 455 { 456 #ifdef CONFIG_SPL_STACK_R 457 gd_t *new_gd; 458 ulong ptr = CONFIG_SPL_STACK_R_ADDR; 459 460 #ifdef CONFIG_SPL_SYS_MALLOC_SIMPLE 461 if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) { 462 if (!(gd->flags & GD_FLG_SPL_INIT)) 463 panic_str("spl_init must be called before heap reloc"); 464 465 ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN; 466 gd->malloc_base = ptr; 467 gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN; 468 gd->malloc_ptr = 0; 469 } 470 #endif 471 /* Get stack position: use 8-byte alignment for ABI compliance */ 472 ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16); 473 new_gd = (gd_t *)ptr; 474 memcpy(new_gd, (void *)gd, sizeof(gd_t)); 475 #if !defined(CONFIG_ARM) 476 gd = new_gd; 477 #endif 478 return ptr; 479 #else 480 return 0; 481 #endif 482 } 483