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 10 #include <common.h> 11 #include <dm.h> 12 #include <spl.h> 13 #include <asm/sections.h> 14 #include <asm/u-boot.h> 15 #include <nand.h> 16 #include <fat.h> 17 #include <version.h> 18 #include <image.h> 19 #include <malloc.h> 20 #include <dm/root.h> 21 #include <linux/compiler.h> 22 #include <fdt_support.h> 23 24 DECLARE_GLOBAL_DATA_PTR; 25 26 #ifndef CONFIG_SYS_UBOOT_START 27 #define CONFIG_SYS_UBOOT_START CONFIG_SYS_TEXT_BASE 28 #endif 29 #ifndef CONFIG_SYS_MONITOR_LEN 30 /* Unknown U-Boot size, let's assume it will not be more than 200 KB */ 31 #define CONFIG_SYS_MONITOR_LEN (200 * 1024) 32 #endif 33 34 u32 *boot_params_ptr = NULL; 35 36 /* Define board data structure */ 37 static bd_t bdata __attribute__ ((section(".data"))); 38 39 /* 40 * Board-specific Platform code can reimplement show_boot_progress () if needed 41 */ 42 __weak void show_boot_progress(int val) {} 43 44 /* 45 * Default function to determine if u-boot or the OS should 46 * be started. This implementation always returns 1. 47 * 48 * Please implement your own board specific funcion to do this. 49 * 50 * RETURN 51 * 0 to not start u-boot 52 * positive if u-boot should start 53 */ 54 #ifdef CONFIG_SPL_OS_BOOT 55 __weak int spl_start_uboot(void) 56 { 57 puts("SPL: Please implement spl_start_uboot() for your board\n"); 58 puts("SPL: Direct Linux boot not active!\n"); 59 return 1; 60 } 61 62 /* weak default platform specific function to initialize 63 * dram banks 64 */ 65 __weak int dram_init_banksize(void) 66 { 67 return 0; 68 } 69 70 /* 71 * Weak default function for arch specific zImage check. Return zero 72 * and fill start and end address if image is recognized. 73 */ 74 int __weak bootz_setup(ulong image, ulong *start, ulong *end) 75 { 76 return 1; 77 } 78 #endif 79 80 /* Weak default function for arch/board-specific fixups to the spl_image_info */ 81 void __weak spl_perform_fixups(struct spl_image_info *spl_image) 82 { 83 } 84 85 void spl_fixup_fdt(void) 86 { 87 #if defined(CONFIG_SPL_OF_LIBFDT) && defined(CONFIG_SYS_SPL_ARGS_ADDR) 88 void *fdt_blob = (void *)CONFIG_SYS_SPL_ARGS_ADDR; 89 int err; 90 91 err = fdt_check_header(fdt_blob); 92 if (err < 0) { 93 printf("fdt_root: %s\n", fdt_strerror(err)); 94 return; 95 } 96 97 /* fixup the memory dt node */ 98 err = fdt_shrink_to_minimum(fdt_blob, 0); 99 if (err == 0) { 100 printf("spl: fdt_shrink_to_minimum err - %d\n", err); 101 return; 102 } 103 104 err = arch_fixup_fdt(fdt_blob); 105 if (err) { 106 printf("spl: arch_fixup_fdt err - %d\n", err); 107 return; 108 } 109 #endif 110 } 111 112 /* 113 * Weak default function for board specific cleanup/preparation before 114 * Linux boot. Some boards/platforms might not need it, so just provide 115 * an empty stub here. 116 */ 117 __weak void spl_board_prepare_for_linux(void) 118 { 119 /* Nothing to do! */ 120 } 121 122 __weak void spl_board_prepare_for_boot(void) 123 { 124 /* Nothing to do! */ 125 } 126 127 void spl_set_header_raw_uboot(struct spl_image_info *spl_image) 128 { 129 spl_image->size = CONFIG_SYS_MONITOR_LEN; 130 spl_image->entry_point = CONFIG_SYS_UBOOT_START; 131 spl_image->load_addr = CONFIG_SYS_TEXT_BASE; 132 spl_image->os = IH_OS_U_BOOT; 133 spl_image->name = "U-Boot"; 134 } 135 136 int spl_parse_image_header(struct spl_image_info *spl_image, 137 const struct image_header *header) 138 { 139 if (image_get_magic(header) == IH_MAGIC) { 140 #ifdef CONFIG_SPL_LEGACY_IMAGE_SUPPORT 141 u32 header_size = sizeof(struct image_header); 142 143 if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) { 144 /* 145 * On some system (e.g. powerpc), the load-address and 146 * entry-point is located at address 0. We can't load 147 * to 0-0x40. So skip header in this case. 148 */ 149 spl_image->load_addr = image_get_load(header); 150 spl_image->entry_point = image_get_ep(header); 151 spl_image->size = image_get_data_size(header); 152 } else { 153 spl_image->entry_point = image_get_load(header); 154 /* Load including the header */ 155 spl_image->load_addr = spl_image->entry_point - 156 header_size; 157 spl_image->size = image_get_data_size(header) + 158 header_size; 159 } 160 spl_image->os = image_get_os(header); 161 spl_image->name = image_get_name(header); 162 debug("spl: payload image: %.*s load addr: 0x%lx size: %d\n", 163 IH_NMLEN, spl_image->name, 164 spl_image->load_addr, spl_image->size); 165 #else 166 /* LEGACY image not supported */ 167 debug("Legacy boot image support not enabled, proceeding to other boot methods\n"); 168 return -EINVAL; 169 #endif 170 } else { 171 #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE 172 /* 173 * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the 174 * code which loads images in SPL cannot guarantee that 175 * absolutely all read errors will be reported. 176 * An example is the LPC32XX MLC NAND driver, which 177 * will consider that a completely unreadable NAND block 178 * is bad, and thus should be skipped silently. 179 */ 180 panic("** no mkimage signature but raw image not supported"); 181 #endif 182 183 #ifdef CONFIG_SPL_OS_BOOT 184 ulong start, end; 185 186 if (!bootz_setup((ulong)header, &start, &end)) { 187 spl_image->name = "Linux"; 188 spl_image->os = IH_OS_LINUX; 189 spl_image->load_addr = CONFIG_SYS_LOAD_ADDR; 190 spl_image->entry_point = CONFIG_SYS_LOAD_ADDR; 191 spl_image->size = end - start; 192 debug("spl: payload zImage, load addr: 0x%lx size: %d\n", 193 spl_image->load_addr, spl_image->size); 194 return 0; 195 } 196 #endif 197 198 #ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT 199 /* Signature not found - assume u-boot.bin */ 200 debug("mkimage signature not found - ih_magic = %x\n", 201 header->ih_magic); 202 spl_set_header_raw_uboot(spl_image); 203 #else 204 /* RAW image not supported, proceed to other boot methods. */ 205 debug("Raw boot image support not enabled, proceeding to other boot methods\n"); 206 return -EINVAL; 207 #endif 208 } 209 210 return 0; 211 } 212 213 __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image) 214 { 215 typedef void __noreturn (*image_entry_noargs_t)(void); 216 217 image_entry_noargs_t image_entry = 218 (image_entry_noargs_t)spl_image->entry_point; 219 220 debug("image entry point: 0x%lX\n", spl_image->entry_point); 221 image_entry(); 222 } 223 224 static int spl_common_init(bool setup_malloc) 225 { 226 int ret; 227 228 debug("spl_early_init()\n"); 229 230 #if CONFIG_VAL(SYS_MALLOC_F_LEN) 231 if (setup_malloc) { 232 #ifdef CONFIG_MALLOC_F_ADDR 233 gd->malloc_base = CONFIG_MALLOC_F_ADDR; 234 #endif 235 gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN); 236 gd->malloc_ptr = 0; 237 } 238 #endif 239 240 /* 241 * setup D-cache as early as possible after malloc setup 242 * I-cache has been setup at early assembly code by default. 243 */ 244 #if !defined(CONFIG_TPL_BUILD) 245 /* tlb memory should be 64KB align for base and 4KB align for end */ 246 gd->arch.tlb_size = PGTABLE_SIZE; 247 gd->arch.tlb_addr = (ulong)memalign(SZ_64K, ALIGN(PGTABLE_SIZE, SZ_4K)); 248 if (gd->arch.tlb_addr) 249 dcache_enable(); 250 else 251 debug("spl: no tlb memory\n"); 252 #endif 253 254 ret = bootstage_init(true); 255 if (ret) { 256 debug("%s: Failed to set up bootstage: ret=%d\n", __func__, 257 ret); 258 return ret; 259 } 260 bootstage_mark_name(BOOTSTAGE_ID_START_SPL, "spl"); 261 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) { 262 ret = fdtdec_setup(); 263 if (ret) { 264 debug("fdtdec_setup() returned error %d\n", ret); 265 return ret; 266 } 267 } 268 if (CONFIG_IS_ENABLED(DM)) { 269 bootstage_start(BOOTSTATE_ID_ACCUM_DM_SPL, "dm_spl"); 270 /* With CONFIG_SPL_OF_PLATDATA, bring in all devices */ 271 ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA)); 272 bootstage_accum(BOOTSTATE_ID_ACCUM_DM_SPL); 273 if (ret) { 274 debug("dm_init_and_scan() returned error %d\n", ret); 275 return ret; 276 } 277 } 278 279 return 0; 280 } 281 282 #if !defined(CONFIG_SPL_SKIP_RELOCATE) && !defined(CONFIG_TPL_BUILD) 283 static void spl_setup_relocate(void) 284 { 285 gd->relocaddr = CONFIG_SPL_RELOC_TEXT_BASE; 286 gd->new_gd = (gd_t *)gd; 287 gd->start_addr_sp = gd->relocaddr; 288 gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32); 289 290 gd->start_addr_sp -= gd->fdt_size; 291 gd->new_fdt = (void *)gd->start_addr_sp; 292 memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size); 293 gd->fdt_blob = gd->new_fdt; 294 295 gd->reloc_off = gd->relocaddr - (unsigned long)__image_copy_start; 296 } 297 #else 298 static void spl_setup_relocate(void) 299 { 300 301 } 302 #endif 303 304 void spl_set_bd(void) 305 { 306 if (!gd->bd) 307 gd->bd = &bdata; 308 } 309 310 int spl_early_init(void) 311 { 312 int ret; 313 314 ret = spl_common_init(true); 315 if (ret) 316 return ret; 317 gd->flags |= GD_FLG_SPL_EARLY_INIT; 318 319 spl_setup_relocate(); 320 321 return 0; 322 } 323 324 int spl_init(void) 325 { 326 int ret; 327 bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) && 328 IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE)); 329 330 if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) { 331 ret = spl_common_init(setup_malloc); 332 if (ret) 333 return ret; 334 } 335 gd->flags |= GD_FLG_SPL_INIT; 336 337 return 0; 338 } 339 340 #ifndef BOOT_DEVICE_NONE 341 #define BOOT_DEVICE_NONE 0xdeadbeef 342 #endif 343 344 __weak void board_boot_order(u32 *spl_boot_list) 345 { 346 spl_boot_list[0] = spl_boot_device(); 347 } 348 349 static struct spl_image_loader *spl_ll_find_loader(uint boot_device) 350 { 351 struct spl_image_loader *drv = 352 ll_entry_start(struct spl_image_loader, spl_image_loader); 353 const int n_ents = 354 ll_entry_count(struct spl_image_loader, spl_image_loader); 355 struct spl_image_loader *entry; 356 357 for (entry = drv; entry != drv + n_ents; entry++) { 358 if (boot_device == entry->boot_device) 359 return entry; 360 } 361 362 /* Not found */ 363 return NULL; 364 } 365 366 static int spl_load_image(struct spl_image_info *spl_image, 367 struct spl_image_loader *loader) 368 { 369 struct spl_boot_device bootdev; 370 371 bootdev.boot_device = loader->boot_device; 372 bootdev.boot_device_name = NULL; 373 374 return loader->load_image(spl_image, &bootdev); 375 } 376 377 /** 378 * boot_from_devices() - Try loading an booting U-Boot from a list of devices 379 * 380 * @spl_image: Place to put the image details if successful 381 * @spl_boot_list: List of boot devices to try 382 * @count: Number of elements in spl_boot_list 383 * @return 0 if OK, -ve on error 384 */ 385 static int boot_from_devices(struct spl_image_info *spl_image, 386 u32 spl_boot_list[], int count) 387 { 388 int i; 389 390 for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) { 391 struct spl_image_loader *loader; 392 393 loader = spl_ll_find_loader(spl_boot_list[i]); 394 #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 395 if (loader) 396 printf("Trying to boot from %s\n", loader->name); 397 else 398 puts("SPL: Unsupported Boot Device!\n"); 399 #endif 400 if (loader && !spl_load_image(spl_image, loader)) { 401 spl_image->boot_device = spl_boot_list[i]; 402 return 0; 403 } 404 } 405 406 return -ENODEV; 407 } 408 409 #if defined(CONFIG_DM) && !defined(CONFIG_SPL_SKIP_RELOCATE) && !defined(CONFIG_TPL_BUILD) 410 static int spl_initr_dm(void) 411 { 412 int ret; 413 414 /* Save the pre-reloc driver model and start a new one */ 415 gd->dm_root_f = gd->dm_root; 416 gd->dm_root = NULL; 417 bootstage_start(BOOTSTATE_ID_ACCUM_DM_R, "dm_r"); 418 ret = dm_init_and_scan(false); 419 bootstage_accum(BOOTSTATE_ID_ACCUM_DM_R); 420 if (ret) 421 return ret; 422 423 #if defined(CONFIG_TIMER) 424 gd->timer = NULL; 425 #endif 426 serial_init(); 427 428 return 0; 429 } 430 #else 431 static int spl_initr_dm(void) 432 { 433 return 0; 434 } 435 #endif 436 437 void board_init_r(gd_t *dummy1, ulong dummy2) 438 { 439 u32 spl_boot_list[] = { 440 BOOT_DEVICE_NONE, 441 BOOT_DEVICE_NONE, 442 BOOT_DEVICE_NONE, 443 BOOT_DEVICE_NONE, 444 BOOT_DEVICE_NONE, 445 }; 446 struct spl_image_info spl_image; 447 448 debug(">>spl:board_init_r()\n"); 449 450 spl_initr_dm(); 451 452 spl_set_bd(); 453 454 #ifdef CONFIG_SPL_OS_BOOT 455 dram_init_banksize(); 456 #endif 457 458 #if defined(CONFIG_SYS_SPL_MALLOC_START) 459 mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START, 460 CONFIG_SYS_SPL_MALLOC_SIZE); 461 gd->flags |= GD_FLG_FULL_MALLOC_INIT; 462 #endif 463 if (!(gd->flags & GD_FLG_SPL_INIT)) { 464 if (spl_init()) 465 hang(); 466 } 467 #if !defined(CONFIG_PPC) && !defined(CONFIG_ARCH_MX6) 468 /* 469 * timer_init() does not exist on PPC systems. The timer is initialized 470 * and enabled (decrementer) in interrupt_init() here. 471 */ 472 timer_init(); 473 #endif 474 475 #if CONFIG_IS_ENABLED(BOARD_INIT) 476 spl_board_init(); 477 #endif 478 479 memset(&spl_image, '\0', sizeof(spl_image)); 480 481 #if CONFIG_IS_ENABLED(ATF) 482 /* 483 * Bl32 ep is optional, initial it as an invalid value. 484 * BL33 ep is mandatory, but initial it as a default value is better. 485 */ 486 spl_image.entry_point_bl32 = -1; 487 spl_image.entry_point_bl33 = CONFIG_SYS_TEXT_BASE; 488 #endif 489 490 #ifdef CONFIG_SYS_SPL_ARGS_ADDR 491 spl_image.arg = (void *)CONFIG_SYS_SPL_ARGS_ADDR; 492 #endif 493 spl_image.boot_device = BOOT_DEVICE_NONE; 494 board_boot_order(spl_boot_list); 495 496 if (boot_from_devices(&spl_image, spl_boot_list, 497 ARRAY_SIZE(spl_boot_list))) { 498 puts("SPL: failed to boot from all boot devices\n"); 499 hang(); 500 } 501 502 spl_perform_fixups(&spl_image); 503 504 #ifdef CONFIG_CPU_V7M 505 spl_image.entry_point |= 0x1; 506 #endif 507 switch (spl_image.os) { 508 case IH_OS_U_BOOT: 509 debug("Jumping to U-Boot\n"); 510 break; 511 #if CONFIG_IS_ENABLED(ATF) 512 case IH_OS_ARM_TRUSTED_FIRMWARE: 513 printf("Jumping to U-Boot via ARM Trusted Firmware\n\n"); 514 spl_invoke_atf(&spl_image); 515 break; 516 #endif 517 #if CONFIG_IS_ENABLED(OPTEE) 518 case IH_OS_OP_TEE: 519 debug("Jumping to U-Boot via OP-TEE\n"); 520 spl_optee_entry(NULL, NULL, (void *)spl_image.fdt_addr, 521 (void *)spl_image.entry_point); 522 break; 523 #endif 524 #ifdef CONFIG_SPL_OS_BOOT 525 case IH_OS_LINUX: 526 debug("Jumping to Linux\n"); 527 spl_fixup_fdt(); 528 spl_board_prepare_for_linux(); 529 jump_to_image_linux(&spl_image); 530 #endif 531 default: 532 debug("Unsupported OS image.. Jumping nevertheless..\n"); 533 } 534 #if CONFIG_VAL(SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE) 535 debug("SPL malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr, 536 gd->malloc_ptr / 1024); 537 #endif 538 539 debug("loaded - jumping to U-Boot...\n"); 540 #ifdef CONFIG_BOOTSTAGE_STASH 541 int ret; 542 543 bootstage_mark_name(BOOTSTAGE_ID_END_SPL, "end_spl"); 544 ret = bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR, 545 CONFIG_BOOTSTAGE_STASH_SIZE); 546 if (ret) 547 debug("Failed to stash bootstage: err=%d\n", ret); 548 #endif 549 550 debug("loaded - jumping to U-Boot...\n"); 551 spl_board_prepare_for_boot(); 552 jump_to_image_no_args(&spl_image); 553 } 554 555 /* 556 * This requires UART clocks to be enabled. In order for this to work the 557 * caller must ensure that the gd pointer is valid. 558 */ 559 void preloader_console_init(void) 560 { 561 gd->baudrate = CONFIG_BAUDRATE; 562 563 serial_init(); /* serial communications setup */ 564 565 gd->have_console = 1; 566 567 puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \ 568 U_BOOT_TIME ")\n"); 569 #ifdef CONFIG_SPL_DISPLAY_PRINT 570 spl_display_print(); 571 #endif 572 } 573 574 /** 575 * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution 576 * 577 * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM 578 * for the main board_init_r() execution. This is typically because we need 579 * more stack space for things like the MMC sub-system. 580 * 581 * This function calculates the stack position, copies the global_data into 582 * place, sets the new gd (except for ARM, for which setting GD within a C 583 * function may not always work) and returns the new stack position. The 584 * caller is responsible for setting up the sp register and, in the case 585 * of ARM, setting up gd. 586 * 587 * All of this is done using the same layout and alignments as done in 588 * board_init_f_init_reserve() / board_init_f_alloc_reserve(). 589 * 590 * @return new stack location, or 0 to use the same stack 591 */ 592 ulong spl_relocate_stack_gd(void) 593 { 594 #ifdef CONFIG_SPL_STACK_R 595 gd_t *new_gd; 596 ulong ptr = CONFIG_SPL_STACK_R_ADDR; 597 598 #if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_VAL(SYS_MALLOC_F_LEN) 599 if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) { 600 ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN; 601 gd->malloc_base = ptr; 602 gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN; 603 gd->malloc_ptr = 0; 604 } 605 #endif 606 /* Get stack position: use 8-byte alignment for ABI compliance */ 607 ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16); 608 new_gd = (gd_t *)ptr; 609 memcpy(new_gd, (void *)gd, sizeof(gd_t)); 610 #if CONFIG_IS_ENABLED(DM) 611 dm_fixup_for_gd_move(new_gd); 612 #endif 613 #if !defined(CONFIG_ARM) 614 gd = new_gd; 615 #endif 616 return ptr; 617 #else 618 return 0; 619 #endif 620 } 621