1 /* 2 * (C) Copyright 2017 Rockchip Electronics Co., Ltd. 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #include <common.h> 8 #include <abuf.h> 9 #include <amp.h> 10 #include <android_ab.h> 11 #include <android_bootloader.h> 12 #include <android_image.h> 13 #include <bidram.h> 14 #include <boot_rkimg.h> 15 #include <cli.h> 16 #include <clk.h> 17 #include <console.h> 18 #include <debug_uart.h> 19 #include <dm.h> 20 #include <dvfs.h> 21 #include <fdt_support.h> 22 #include <io-domain.h> 23 #include <image.h> 24 #include <key.h> 25 #include <memblk.h> 26 #include <misc.h> 27 #include <of_live.h> 28 #include <mtd_blk.h> 29 #include <ram.h> 30 #include <rng.h> 31 #include <rockchip_debugger.h> 32 #include <syscon.h> 33 #include <sysmem.h> 34 #include <video_rockchip.h> 35 #include <xbc.h> 36 #include <asm/io.h> 37 #include <asm/gpio.h> 38 #include <android_avb/rk_avb_ops_user.h> 39 #include <dm/uclass-internal.h> 40 #include <dm/root.h> 41 #include <power/charge_display.h> 42 #include <power/regulator.h> 43 #include <optee_include/OpteeClientInterface.h> 44 #include <optee_include/OpteeClientApiLib.h> 45 #include <optee_include/tee_api_defines.h> 46 #include <asm/arch/boot_mode.h> 47 #include <asm/arch/clock.h> 48 #include <asm/arch/cpu.h> 49 #include <asm/arch/hotkey.h> 50 #include <asm/arch/param.h> 51 #include <asm/arch/periph.h> 52 #include <asm/arch/resource_img.h> 53 #include <asm/arch/rk_atags.h> 54 #include <asm/arch/vendor.h> 55 #ifdef CONFIG_ROCKCHIP_EINK_DISPLAY 56 #include <rk_eink.h> 57 #endif 58 59 DECLARE_GLOBAL_DATA_PTR; 60 61 #ifdef CONFIG_ARM64 62 static ulong orig_images_ep; 63 #endif 64 65 __weak int rk_board_late_init(void) 66 { 67 return 0; 68 } 69 70 __weak int rk_board_fdt_fixup(void *blob) 71 { 72 return 0; 73 } 74 75 __weak int rk_board_dm_fdt_fixup(void *blob) 76 { 77 return 0; 78 } 79 80 __weak int soc_clk_dump(void) 81 { 82 return 0; 83 } 84 85 __weak int set_armclk_rate(void) 86 { 87 return 0; 88 } 89 90 __weak int rk_board_init(void) 91 { 92 return 0; 93 } 94 95 #ifdef CONFIG_ROCKCHIP_SET_ETHADDR 96 /* 97 * define serialno max length, the max length is 512 Bytes 98 * The remaining bytes are used to ensure that the first 512 bytes 99 * are valid when executing 'env_set("serial#", value)'. 100 */ 101 #define VENDOR_SN_MAX 513 102 #define CPUID_LEN 0x10 103 #define CPUID_OFF 0x07 104 105 #define MAX_ETHERNET 0x2 106 107 static int rockchip_set_ethaddr(void) 108 { 109 __maybe_unused bool need_write = false; 110 bool randomed = false; 111 char buf[ARP_HLEN_ASCII + 1], mac[16]; 112 u8 ethaddr[ARP_HLEN * MAX_ETHERNET] = {0}; 113 int i, ret = -EINVAL; 114 115 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION 116 ret = vendor_storage_read(LAN_MAC_ID, ethaddr, sizeof(ethaddr)); 117 #endif 118 for (i = 0; i < MAX_ETHERNET; i++) { 119 if (ret <= 0 || !is_valid_ethaddr(ðaddr[i * ARP_HLEN])) { 120 if (!randomed) { 121 net_random_ethaddr(ðaddr[i * ARP_HLEN]); 122 randomed = true; 123 } else { 124 if (i > 0) { 125 memcpy(ðaddr[i * ARP_HLEN], 126 ðaddr[(i - 1) * ARP_HLEN], 127 ARP_HLEN); 128 ethaddr[i * ARP_HLEN] |= 0x02; 129 ethaddr[i * ARP_HLEN] += (i << 2); 130 } 131 } 132 133 need_write = true; 134 } 135 136 if (is_valid_ethaddr(ðaddr[i * ARP_HLEN])) { 137 snprintf(buf, ARP_HLEN_ASCII + 1, "%pM", ðaddr[i * ARP_HLEN]); 138 if (i == 0) 139 memcpy(mac, "ethaddr", sizeof("ethaddr")); 140 else 141 sprintf(mac, "eth%daddr", i); 142 env_set(mac, buf); 143 } 144 } 145 146 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION 147 if (need_write) { 148 ret = vendor_storage_write(LAN_MAC_ID, 149 ethaddr, sizeof(ethaddr)); 150 if (ret < 0) 151 printf("%s: vendor_storage_write failed %d\n", 152 __func__, ret); 153 } 154 #endif 155 return 0; 156 } 157 #endif 158 159 #ifdef CONFIG_ROCKCHIP_SET_SN 160 static int rockchip_set_serialno(void) 161 { 162 u8 low[CPUID_LEN / 2], high[CPUID_LEN / 2]; 163 u8 cpuid[CPUID_LEN] = {0}; 164 char serialno_str[VENDOR_SN_MAX]; 165 int ret = 0, i; 166 u64 serialno; 167 168 /* Read serial number from vendor storage part */ 169 memset(serialno_str, 0, VENDOR_SN_MAX); 170 171 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION 172 int j; 173 174 ret = vendor_storage_read(SN_ID, serialno_str, (VENDOR_SN_MAX-1)); 175 if (ret > 0) { 176 j = strlen(serialno_str); 177 for (i = 0; i < j; i++) { 178 if ((serialno_str[i] >= 'a' && serialno_str[i] <= 'z') || 179 (serialno_str[i] >= 'A' && serialno_str[i] <= 'Z') || 180 (serialno_str[i] >= '0' && serialno_str[i] <= '9')) 181 continue; 182 else 183 break; 184 } 185 186 /* valid character count > 0 */ 187 if (i > 0) { 188 serialno_str[i + 1] = 0x0; 189 env_set("serial#", serialno_str); 190 } 191 } 192 #endif 193 if (!env_get("serial#")) { 194 #if defined(CONFIG_ROCKCHIP_EFUSE) || defined(CONFIG_ROCKCHIP_OTP) 195 struct udevice *dev; 196 197 /* retrieve the device */ 198 if (IS_ENABLED(CONFIG_ROCKCHIP_EFUSE)) 199 ret = uclass_get_device_by_driver(UCLASS_MISC, 200 DM_GET_DRIVER(rockchip_efuse), 201 &dev); 202 else 203 ret = uclass_get_device_by_driver(UCLASS_MISC, 204 DM_GET_DRIVER(rockchip_otp), 205 &dev); 206 207 if (ret) { 208 printf("%s: could not find efuse/otp device\n", __func__); 209 return ret; 210 } 211 212 /* read the cpu_id range from the efuses */ 213 ret = misc_read(dev, CPUID_OFF, &cpuid, sizeof(cpuid)); 214 if (ret) { 215 printf("%s: read cpuid from efuse/otp failed, ret=%d\n", 216 __func__, ret); 217 return ret; 218 } 219 #else 220 /* generate random cpuid */ 221 for (i = 0; i < CPUID_LEN; i++) 222 cpuid[i] = (u8)(rand()); 223 #endif 224 /* Generate the serial number based on CPU ID */ 225 for (i = 0; i < 8; i++) { 226 low[i] = cpuid[1 + (i << 1)]; 227 high[i] = cpuid[i << 1]; 228 } 229 230 serialno = crc32_no_comp(0, low, 8); 231 serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32; 232 snprintf(serialno_str, sizeof(serialno_str), "%llx", serialno); 233 234 env_set("serial#", serialno_str); 235 } 236 237 return ret; 238 } 239 #endif 240 241 #if defined(CONFIG_USB_FUNCTION_FASTBOOT) 242 int fb_set_reboot_flag(void) 243 { 244 printf("Setting reboot to fastboot flag ...\n"); 245 writel(BOOT_FASTBOOT, CONFIG_ROCKCHIP_BOOT_MODE_REG); 246 247 return 0; 248 } 249 #endif 250 251 #ifdef CONFIG_ROCKCHIP_USB_BOOT 252 static int boot_from_udisk(void) 253 { 254 struct blk_desc *desc; 255 struct udevice *dev; 256 int devnum = -1; 257 char buf[32]; 258 259 /* Booting priority: mmc1 > udisk */ 260 if (!strcmp(env_get("devtype"), "mmc") && !strcmp(env_get("devnum"), "1")) 261 return 0; 262 263 if (!run_command("usb start", -1)) { 264 for (blk_first_device(IF_TYPE_USB, &dev); 265 dev; 266 blk_next_device(&dev)) { 267 desc = dev_get_uclass_platdata(dev); 268 printf("Scanning usb %d ...\n", desc->devnum); 269 if (desc->type == DEV_TYPE_UNKNOWN) 270 continue; 271 272 if (desc->lba > 0L && desc->blksz > 0L) { 273 devnum = desc->devnum; 274 break; 275 } 276 } 277 if (devnum < 0) { 278 printf("No usb mass storage found\n"); 279 return -ENODEV; 280 } 281 282 desc = blk_get_devnum_by_type(IF_TYPE_USB, devnum); 283 if (!desc) { 284 printf("No usb %d found\n", devnum); 285 return -ENODEV; 286 } 287 288 snprintf(buf, 32, "rkimgtest usb %d", devnum); 289 if (!run_command(buf, -1)) { 290 snprintf(buf, 32, "%d", devnum); 291 rockchip_set_bootdev(desc); 292 env_set("devtype", "usb"); 293 env_set("devnum", buf); 294 printf("=== Booting from usb %d ===\n", devnum); 295 } else { 296 printf("No available udisk image on usb %d\n", devnum); 297 return -ENODEV; 298 } 299 } 300 301 return 0; 302 } 303 #endif 304 305 static void env_fixup(void) 306 { 307 struct memblock mem; 308 ulong u_addr_r; 309 phys_size_t end; 310 char *addr_r; 311 312 #ifdef ENV_MEM_LAYOUT_SETTINGS1 313 const char *env_addr0[] = { 314 "scriptaddr", "pxefile_addr_r", 315 "fdt_addr_r", "kernel_addr_r", "ramdisk_addr_r", 316 }; 317 const char *env_addr1[] = { 318 "scriptaddr1", "pxefile_addr1_r", 319 "fdt_addr1_r", "kernel_addr1_r", "ramdisk_addr1_r", 320 }; 321 int i; 322 323 /* 128M is a typical ram size for most platform, so as default here */ 324 if (gd->ram_size <= SZ_128M) { 325 /* Replace orignal xxx_addr_r */ 326 for (i = 0; i < ARRAY_SIZE(env_addr1); i++) { 327 addr_r = env_get(env_addr1[i]); 328 if (addr_r) 329 env_set(env_addr0[i], addr_r); 330 } 331 } 332 #endif 333 /* No BL32 ? */ 334 if (!(gd->flags & GD_FLG_BL32_ENABLED)) { 335 /* 336 * [1] Move kernel to lower address if possible. 337 */ 338 addr_r = env_get("kernel_addr_no_low_bl32_r"); 339 if (addr_r) 340 env_set("kernel_addr_r", addr_r); 341 342 /* 343 * [2] Move ramdisk at BL32 position if need. 344 * 345 * 0x0a200000 and 0x08400000 are rockchip traditional address 346 * of BL32 and ramdisk: 347 * 348 * |------------|------------| 349 * | BL32 | ramdisk | 350 * |------------|------------| 351 * 352 * Move ramdisk to BL32 address to fix sysmem alloc failed 353 * issue on the board with critical memory(ie. 256MB). 354 */ 355 if (gd->ram_size > SZ_128M && gd->ram_size <= SZ_256M) { 356 u_addr_r = env_get_ulong("ramdisk_addr_r", 16, 0); 357 if (u_addr_r == 0x0a200000) 358 env_set("ramdisk_addr_r", "0x08400000"); 359 } 360 } else { 361 mem = param_parse_optee_mem(); 362 363 /* 364 * [1] Move kernel forward if possible. 365 */ 366 if (mem.base > SZ_128M) { 367 addr_r = env_get("kernel_addr_no_low_bl32_r"); 368 if (addr_r) 369 env_set("kernel_addr_r", addr_r); 370 } 371 372 /* 373 * [2] Move ramdisk backward if optee enlarge. 374 */ 375 end = mem.base + mem.size; 376 u_addr_r = env_get_ulong("ramdisk_addr_r", 16, 0); 377 if (u_addr_r >= mem.base && u_addr_r < end) 378 env_set_hex("ramdisk_addr_r", end); 379 } 380 } 381 382 static void cmdline_handle(void) 383 { 384 struct blk_desc *dev_desc; 385 int if_type; 386 int devnum; 387 388 param_parse_pubkey_fuse_programmed(); 389 390 dev_desc = rockchip_get_bootdev(); 391 if (!dev_desc) 392 return; 393 394 /* 395 * 1. From rk356x, the sd/udisk recovery update flag was moved from 396 * IDB to Android BCB. 397 * 398 * 2. Udisk is init at the late boot_from_udisk(), but 399 * rockchip_get_boot_mode() actually only read once, 400 * we need to update boot mode according to udisk BCB. 401 */ 402 if_type = dev_desc->if_type; 403 devnum = dev_desc->devnum; 404 if ((if_type == IF_TYPE_MMC && devnum == 1) || (if_type == IF_TYPE_USB)) { 405 if (get_bcb_recovery_msg() == BCB_MSG_RECOVERY_RK_FWUPDATE) { 406 if (if_type == IF_TYPE_MMC && devnum == 1) { 407 env_update("bootargs", "sdfwupdate"); 408 } else if (if_type == IF_TYPE_USB) { 409 env_update("bootargs", "usbfwupdate"); 410 env_set("reboot_mode", "recovery-usb"); 411 } 412 } else { 413 if (if_type == IF_TYPE_USB) 414 env_set("reboot_mode", "normal"); 415 } 416 } 417 418 if (rockchip_get_boot_mode() == BOOT_MODE_QUIESCENT) 419 env_update("bootargs", "androidboot.quiescent=1 pwm_bl.quiescent=1"); 420 } 421 422 static void scan_run_cmd(void) 423 { 424 char *config = CONFIG_ROCKCHIP_CMD; 425 char *cmd, *key; 426 427 key = strchr(config, ' '); 428 if (!key) 429 return; 430 431 cmd = strdup(config); 432 cmd[key - config] = 0; 433 key++; 434 435 if (!strcmp(key, "-")) { 436 run_command(cmd, 0); 437 } else { 438 #ifdef CONFIG_DM_KEY 439 ulong map; 440 441 map = simple_strtoul(key, NULL, 10); 442 if (key_is_pressed(key_read(map))) { 443 printf("## Key<%ld> pressed... run cmd '%s'\n", map, cmd); 444 run_command(cmd, 0); 445 } 446 #endif 447 } 448 } 449 450 int board_late_init(void) 451 { 452 #ifdef CONFIG_ROCKCHIP_SET_ETHADDR 453 rockchip_set_ethaddr(); 454 #endif 455 #ifdef CONFIG_ROCKCHIP_SET_SN 456 rockchip_set_serialno(); 457 #endif 458 setup_download_mode(); 459 scan_run_cmd(); 460 #ifdef CONFIG_ROCKCHIP_USB_BOOT 461 boot_from_udisk(); 462 #endif 463 #ifdef CONFIG_DM_CHARGE_DISPLAY 464 charge_display(); 465 #endif 466 #ifdef CONFIG_DRM_ROCKCHIP 467 if (rockchip_get_boot_mode() != BOOT_MODE_QUIESCENT) 468 rockchip_show_logo(); 469 #endif 470 #ifdef CONFIG_ROCKCHIP_EINK_DISPLAY 471 rockchip_eink_show_uboot_logo(); 472 #endif 473 #if (CONFIG_ROCKCHIP_BOOT_MODE_REG > 0) 474 setup_boot_mode(); 475 #endif 476 env_fixup(); 477 soc_clk_dump(); 478 cmdline_handle(); 479 #ifdef CONFIG_AMP 480 amp_cpus_on(); 481 #endif 482 return rk_board_late_init(); 483 } 484 485 static void early_download(void) 486 { 487 #if defined(CONFIG_PWRKEY_DNL_TRIGGER_NUM) && \ 488 (CONFIG_PWRKEY_DNL_TRIGGER_NUM > 0) 489 if (pwrkey_download_init()) 490 printf("Pwrkey download init failed\n"); 491 #endif 492 493 #if (CONFIG_ROCKCHIP_BOOT_MODE_REG > 0) 494 if (is_hotkey(HK_BROM_DNL)) { 495 printf("Enter bootrom download..."); 496 flushc(); 497 writel(BOOT_BROM_DOWNLOAD, CONFIG_ROCKCHIP_BOOT_MODE_REG); 498 do_reset(NULL, 0, 0, NULL); 499 printf("failed!\n"); 500 } 501 #endif 502 } 503 504 static void board_debug_init(void) 505 { 506 if (!gd->serial.using_pre_serial && 507 !(gd->flags & GD_FLG_DISABLE_CONSOLE)) 508 debug_uart_init(); 509 510 if (tstc()) { 511 gd->console_evt = getc(); 512 if (gd->console_evt <= 0x1a) /* 'z' */ 513 printf("Hotkey: ctrl+%c\n", gd->console_evt + 'a' - 1); 514 } 515 516 if (IS_ENABLED(CONFIG_CONSOLE_DISABLE_CLI)) 517 printf("Cmd interface: disabled\n"); 518 } 519 520 int board_init(void) 521 { 522 board_debug_init(); 523 #ifdef DEBUG 524 soc_clk_dump(); 525 #endif 526 #ifdef CONFIG_OPTEE_CLIENT 527 optee_client_init(); 528 #endif 529 #ifdef CONFIG_USING_KERNEL_DTB 530 init_kernel_dtb(); 531 #endif 532 early_download(); 533 534 clks_probe(); 535 #ifdef CONFIG_DM_REGULATOR 536 regulators_enable_boot_on(is_hotkey(HK_REGULATOR)); 537 #endif 538 #ifdef CONFIG_ROCKCHIP_IO_DOMAIN 539 io_domain_init(); 540 #endif 541 set_armclk_rate(); 542 #ifdef CONFIG_DM_DVFS 543 dvfs_init(true); 544 #endif 545 #ifdef CONFIG_ANDROID_AB 546 if (ab_decrease_tries()) 547 printf("Decrease ab tries count fail!\n"); 548 #endif 549 550 return rk_board_init(); 551 } 552 553 int interrupt_debugger_init(void) 554 { 555 #ifdef CONFIG_ROCKCHIP_DEBUGGER 556 return rockchip_debugger_init(); 557 #else 558 return 0; 559 #endif 560 } 561 562 int board_fdt_fixup(void *blob) 563 { 564 /* 565 * Device's platdata points to orignal fdt blob property, 566 * access DM device before any fdt fixup. 567 */ 568 rk_board_dm_fdt_fixup(blob); 569 570 /* Common fixup for DRM */ 571 #ifdef CONFIG_DRM_ROCKCHIP 572 rockchip_display_fixup(blob); 573 #endif 574 575 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION 576 vendor_storage_fixup(blob); 577 #endif 578 579 return rk_board_fdt_fixup(blob); 580 } 581 582 #if defined(CONFIG_ARM64_BOOT_AARCH32) || !defined(CONFIG_ARM64) 583 /* 584 * Common for OP-TEE: 585 * 64-bit & 32-bit mode: share memory dcache is always enabled; 586 * 587 * Common for U-Boot: 588 * 64-bit mode: MMU table is static defined in rkxxx.c file, all memory 589 * regions are mapped. That's good to match OP-TEE MMU policy. 590 * 591 * 32-bit mode: MMU table is setup according to gd->bd->bi_dram[..] where 592 * the OP-TEE region has been reserved, so it can not be 593 * mapped(i.e. dcache is disabled). That's *NOT* good to match 594 * OP-TEE MMU policy. 595 * 596 * For the data coherence when communication between U-Boot and OP-TEE, U-Boot 597 * should follow OP-TEE MMU policy. 598 * 599 * So 32-bit mode U-Boot should map OP-TEE share memory as dcache enabled. 600 */ 601 int board_initr_caches_fixup(void) 602 { 603 #ifdef CONFIG_OPTEE_CLIENT 604 struct memblock mem; 605 606 mem.base = 0; 607 mem.size = 0; 608 609 optee_get_shm_config(&mem.base, &mem.size); 610 if (mem.size) 611 mmu_set_region_dcache_behaviour(mem.base, mem.size, 612 DCACHE_WRITEBACK); 613 #endif 614 return 0; 615 } 616 #endif 617 618 void arch_preboot_os(uint32_t bootm_state, bootm_headers_t *images) 619 { 620 if (!(bootm_state & BOOTM_STATE_OS_PREP)) 621 return; 622 623 #ifdef CONFIG_ARM64 624 u8 *data = (void *)images->ep; 625 626 /* 627 * Fix kernel 5.10 arm64 boot warning: 628 * "[Firmware Bug]: Kernel image misaligned at boot, please fix your bootloader!" 629 * 630 * kernel: 5.10 commit 120dc60d0bdb ("arm64: get rid of TEXT_OFFSET") 631 * arm64 kernel version: 632 * data[10] == 0x00 if kernel version >= 5.10: N*2MB align 633 * data[10] == 0x08 if kernel version < 5.10: N*2MB + 0x80000(TEXT_OFFSET) 634 * 635 * Why fix here? 636 * 1. this is the common and final path for any boot command. 637 * 2. don't influence original boot flow, just fix it exactly before 638 * jumping kernel. 639 * 640 * But relocation is in board_quiesce_devices() until all decompress 641 * done, mainly for saving boot time. 642 */ 643 644 orig_images_ep = images->ep; 645 646 if (data[10] == 0x00) { 647 if (round_down(images->ep, SZ_2M) != images->ep) 648 images->ep = round_down(images->ep, SZ_2M); 649 } else { 650 if (IS_ALIGNED(images->ep, SZ_2M)) 651 images->ep += 0x80000; 652 } 653 #endif 654 hotkey_run(HK_CLI_OS_PRE); 655 } 656 657 void enable_caches(void) 658 { 659 icache_enable(); 660 dcache_enable(); 661 } 662 663 #ifdef CONFIG_LMB 664 /* 665 * Using last bi_dram[...] to initialize "bootm_low" and "bootm_mapsize". 666 * This makes lmb_alloc_base() always alloc from tail of sdram. 667 * If we don't assign it, bi_dram[0] is used by default and it may cause 668 * lmb_alloc_base() fail when bi_dram[0] range is small. 669 */ 670 void board_lmb_reserve(struct lmb *lmb) 671 { 672 char bootm_mapsize[32]; 673 char bootm_low[32]; 674 u64 start, size; 675 int i; 676 677 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { 678 if (!gd->bd->bi_dram[i].size) 679 break; 680 } 681 682 start = gd->bd->bi_dram[i - 1].start; 683 size = gd->bd->bi_dram[i - 1].size; 684 685 /* 686 * 32-bit kernel: ramdisk/fdt shouldn't be loaded to highmem area(768MB+), 687 * otherwise "Unable to handle kernel paging request at virtual address ...". 688 * 689 * So that we hope limit highest address at 768M, but there comes the the 690 * problem: ramdisk is a compressed image and it expands after descompress, 691 * so it accesses 768MB+ and brings the above "Unable to handle kernel ...". 692 * 693 * We make a appointment that the highest memory address is 512MB, it 694 * makes lmb alloc safer. 695 */ 696 #ifndef CONFIG_ARM64 697 if (start >= ((u64)CONFIG_SYS_SDRAM_BASE + SZ_512M)) { 698 start = gd->bd->bi_dram[i - 2].start; 699 size = gd->bd->bi_dram[i - 2].size; 700 } 701 702 if ((start + size) > ((u64)CONFIG_SYS_SDRAM_BASE + SZ_512M)) 703 size = (u64)CONFIG_SYS_SDRAM_BASE + SZ_512M - start; 704 #endif 705 sprintf(bootm_low, "0x%llx", start); 706 sprintf(bootm_mapsize, "0x%llx", size); 707 env_set("bootm_low", bootm_low); 708 env_set("bootm_mapsize", bootm_mapsize); 709 } 710 #endif 711 712 #ifdef CONFIG_BIDRAM 713 int board_bidram_reserve(struct bidram *bidram) 714 { 715 struct memblock mem; 716 int ret; 717 718 /* ATF */ 719 mem = param_parse_atf_mem(); 720 ret = bidram_reserve(MEM_ATF, mem.base, mem.size); 721 if (ret) 722 return ret; 723 724 /* PSTORE/ATAGS/SHM */ 725 mem = param_parse_common_resv_mem(); 726 ret = bidram_reserve(MEM_SHM, mem.base, mem.size); 727 if (ret) 728 return ret; 729 730 /* OP-TEE */ 731 mem = param_parse_optee_mem(); 732 ret = bidram_reserve(MEM_OPTEE, mem.base, mem.size); 733 if (ret) 734 return ret; 735 736 return 0; 737 } 738 739 #ifdef CONFIG_SYSMEM 740 int board_sysmem_reserve(struct sysmem *sysmem) 741 { 742 #ifdef CONFIG_SKIP_RELOCATE_UBOOT 743 if (!sysmem_alloc_base_by_name("NO-RELOC-CODE", 744 CONFIG_SYS_TEXT_BASE, SZ_2M)) { 745 printf("Failed to reserve sysmem for U-Boot code\n"); 746 return -ENOMEM; 747 } 748 #endif 749 return 0; 750 } 751 #endif 752 753 parse_fn_t board_bidram_parse_fn(void) 754 { 755 return param_parse_ddr_mem; 756 } 757 #endif 758 759 int board_init_f_boot_flags(void) 760 { 761 int boot_flags = 0; 762 763 #ifdef CONFIG_FPGA_ROCKCHIP 764 arch_fpga_init(); 765 #endif 766 #ifdef CONFIG_PSTORE 767 param_parse_pstore(); 768 #endif 769 param_parse_pre_serial(&boot_flags); 770 771 /* The highest priority to turn off (override) console */ 772 #if defined(CONFIG_DISABLE_CONSOLE) 773 boot_flags |= GD_FLG_DISABLE_CONSOLE; 774 #endif 775 776 return boot_flags; 777 } 778 779 #if defined(CONFIG_USB_GADGET) 780 #include <usb.h> 781 #if defined(CONFIG_USB_GADGET_DWC2_OTG) 782 #include <fdt_support.h> 783 #include <usb/dwc2_udc.h> 784 785 static struct dwc2_plat_otg_data otg_data = { 786 .rx_fifo_sz = 512, 787 .np_tx_fifo_sz = 16, 788 .tx_fifo_sz = 128, 789 }; 790 791 int board_usb_init(int index, enum usb_init_type init) 792 { 793 const void *blob = gd->fdt_blob; 794 const fdt32_t *reg; 795 fdt_addr_t addr; 796 int node; 797 798 /* find the usb_otg node */ 799 node = fdt_node_offset_by_compatible(blob, -1, "snps,dwc2"); 800 801 retry: 802 if (node > 0) { 803 reg = fdt_getprop(blob, node, "reg", NULL); 804 if (!reg) 805 return -EINVAL; 806 807 addr = fdt_translate_address(blob, node, reg); 808 if (addr == OF_BAD_ADDR) { 809 pr_err("Not found usb_otg address\n"); 810 return -EINVAL; 811 } 812 813 #if defined(CONFIG_ROCKCHIP_RK3288) 814 if (addr != 0xff580000) { 815 node = fdt_node_offset_by_compatible(blob, node, 816 "snps,dwc2"); 817 goto retry; 818 } 819 #endif 820 } else { 821 /* 822 * With kernel dtb support, rk3288 dwc2 otg node 823 * use the rockchip legacy dwc2 driver "dwc_otg_310" 824 * with the compatible "rockchip,rk3288_usb20_otg", 825 * and rk3368 also use the "dwc_otg_310" driver with 826 * the compatible "rockchip,rk3368-usb". 827 */ 828 #if defined(CONFIG_ROCKCHIP_RK3288) 829 node = fdt_node_offset_by_compatible(blob, -1, 830 "rockchip,rk3288_usb20_otg"); 831 #elif defined(CONFIG_ROCKCHIP_RK3368) 832 node = fdt_node_offset_by_compatible(blob, -1, 833 "rockchip,rk3368-usb"); 834 #endif 835 if (node > 0) { 836 goto retry; 837 } else { 838 pr_err("Not found usb_otg device\n"); 839 return -ENODEV; 840 } 841 } 842 843 otg_data.regs_otg = (uintptr_t)addr; 844 845 return dwc2_udc_probe(&otg_data); 846 } 847 848 int board_usb_cleanup(int index, enum usb_init_type init) 849 { 850 return 0; 851 } 852 #elif defined(CONFIG_USB_DWC3_GADGET) /* CONFIG_USB_GADGET_DWC2_OTG */ 853 #include <dwc3-uboot.h> 854 855 int board_usb_cleanup(int index, enum usb_init_type init) 856 { 857 dwc3_uboot_exit(index); 858 return 0; 859 } 860 861 #endif /* CONFIG_USB_DWC3_GADGET */ 862 #endif /* CONFIG_USB_GADGET */ 863 864 static void bootm_no_reloc(void) 865 { 866 char *ramdisk_high; 867 char *fdt_high; 868 869 if (!env_get_yesno("bootm-no-reloc")) 870 return; 871 872 ramdisk_high = env_get("initrd_high"); 873 fdt_high = env_get("fdt_high"); 874 875 if (!fdt_high) { 876 env_set_hex("fdt_high", -1UL); 877 printf("Fdt "); 878 } 879 880 if (!ramdisk_high) { 881 env_set_hex("initrd_high", -1UL); 882 printf("Ramdisk "); 883 } 884 885 if (!fdt_high || !ramdisk_high) 886 printf("skip relocation\n"); 887 } 888 889 int bootm_board_start(void) 890 { 891 /* 892 * print console record data 893 * 894 * On some rockchip platforms, uart debug and sdmmc pin are multiplex. 895 * If boot from sdmmc mode, the console data would be record in buffer, 896 * we switch to uart debug function in order to print it after loading 897 * images. 898 */ 899 #if defined(CONFIG_CONSOLE_RECORD) 900 if (!strcmp("mmc", env_get("devtype")) && 901 !strcmp("1", env_get("devnum"))) { 902 printf("IOMUX: sdmmc => uart debug"); 903 pinctrl_select_state(gd->cur_serial_dev, "default"); 904 console_record_print_purge(); 905 } 906 #endif 907 /* disable bootm relcation to save boot time */ 908 bootm_no_reloc(); 909 910 /* PCBA test needs more permission */ 911 if (get_bcb_recovery_msg() == BCB_MSG_RECOVERY_PCBA) 912 env_update("bootargs", "androidboot.selinux=permissive"); 913 914 /* sysmem */ 915 hotkey_run(HK_SYSMEM); 916 sysmem_overflow_check(); 917 918 return 0; 919 } 920 921 int bootm_image_populate_dtb(void *img) 922 { 923 if ((gd->flags & GD_FLG_KDTB_READY) && !gd->fdt_blob_kern) 924 sysmem_free((phys_addr_t)gd->fdt_blob); 925 else 926 gd->fdt_blob = (void *)env_get_ulong("fdt_addr_r", 16, 0); 927 928 return rockchip_ram_read_dtb_file(img, (void *)gd->fdt_blob); 929 } 930 931 /* 932 * Implement it to support CLI command: 933 * - Android: bootm [aosp addr] 934 * - FIT: bootm [fit addr] 935 * - uImage: bootm [uimage addr] 936 * 937 * Purpose: 938 * - The original bootm command args require fdt addr on AOSP, 939 * which is not flexible on rockchip boot/recovery.img. 940 * - Take Android/FIT/uImage image into sysmem management to avoid image 941 * memory overlap. 942 */ 943 #if defined(CONFIG_ANDROID_BOOTLOADER) || \ 944 defined(CONFIG_ROCKCHIP_FIT_IMAGE) || \ 945 defined(CONFIG_ROCKCHIP_UIMAGE) 946 int board_do_bootm(int argc, char * const argv[]) 947 { 948 int format; 949 void *img; 950 951 /* only 'bootm' full image goes further */ 952 if (argc != 2) 953 return 0; 954 955 img = (void *)simple_strtoul(argv[1], NULL, 16); 956 format = (genimg_get_format(img)); 957 958 /* Android */ 959 #ifdef CONFIG_ANDROID_BOOT_IMAGE 960 if (format == IMAGE_FORMAT_ANDROID) { 961 struct andr_img_hdr *hdr; 962 ulong load_addr; 963 ulong size; 964 int ret; 965 966 hdr = (struct andr_img_hdr *)img; 967 printf("BOOTM: transferring to board Android\n"); 968 969 load_addr = env_get_ulong("kernel_addr_r", 16, 0); 970 load_addr -= hdr->page_size; 971 size = android_image_get_end(hdr) - (ulong)hdr; 972 973 if (!sysmem_alloc_base(MEM_ANDROID, (ulong)hdr, size)) 974 return -ENOMEM; 975 976 ret = bootm_image_populate_dtb(img); 977 if (ret) { 978 printf("bootm can't read dtb, ret=%d\n", ret); 979 return ret; 980 } 981 982 ret = android_image_memcpy_separate(hdr, &load_addr); 983 if (ret) { 984 printf("board do bootm failed, ret=%d\n", ret); 985 return ret; 986 } 987 988 return android_bootloader_boot_kernel(load_addr); 989 } 990 #endif 991 992 /* FIT */ 993 #if IMAGE_ENABLE_FIT 994 if (format == IMAGE_FORMAT_FIT) { 995 char boot_cmd[64]; 996 int ret; 997 998 printf("BOOTM: transferring to board FIT\n"); 999 1000 ret = bootm_image_populate_dtb(img); 1001 if (ret) { 1002 printf("bootm can't read dtb, ret=%d\n", ret); 1003 return ret; 1004 } 1005 snprintf(boot_cmd, sizeof(boot_cmd), "boot_fit %s", argv[1]); 1006 return run_command(boot_cmd, 0); 1007 } 1008 #endif 1009 1010 /* uImage */ 1011 #if 0 1012 #if defined(CONFIG_IMAGE_FORMAT_LEGACY) 1013 if (format == IMAGE_FORMAT_LEGACY && 1014 image_get_type(img) == IH_TYPE_MULTI) { 1015 char boot_cmd[64]; 1016 1017 printf("BOOTM: transferring to board uImage\n"); 1018 snprintf(boot_cmd, sizeof(boot_cmd), "boot_uimage %s", argv[1]); 1019 return run_command(boot_cmd, 0); 1020 } 1021 #endif 1022 #endif 1023 return 0; 1024 } 1025 #endif 1026 1027 void autoboot_command_fail_handle(void) 1028 { 1029 #ifdef CONFIG_ANDROID_AB 1030 if (rk_avb_ab_have_bootable_slot() == true) 1031 run_command("reset;", 0); 1032 else 1033 run_command("fastboot usb 0;", 0); 1034 #endif 1035 1036 #ifdef CONFIG_AVB_VBMETA_PUBLIC_KEY_VALIDATE 1037 run_command("download", 0); 1038 run_command("fastboot usb 0;", 0); 1039 #endif 1040 1041 } 1042 1043 #ifdef CONFIG_FIT_ROLLBACK_PROTECT 1044 1045 #define FIT_ROLLBACK_INDEX_LOCATION 0x66697472 /* "fitr" */ 1046 1047 int fit_read_otp_rollback_index(uint32_t fit_index, uint32_t *otp_index) 1048 { 1049 #ifdef CONFIG_OPTEE_CLIENT 1050 u64 index; 1051 int ret; 1052 1053 ret = trusty_read_rollback_index(FIT_ROLLBACK_INDEX_LOCATION, &index); 1054 if (ret) { 1055 if (ret != TEE_ERROR_ITEM_NOT_FOUND) 1056 return ret; 1057 1058 index = 0; 1059 printf("Initial otp index as %d\n", fit_index); 1060 } 1061 1062 *otp_index = (uint32_t)index; 1063 #else 1064 *otp_index = 0; 1065 #endif 1066 1067 return 0; 1068 } 1069 1070 int fit_write_trusty_rollback_index(u32 trusty_index) 1071 { 1072 if (!trusty_index) 1073 return 0; 1074 #ifdef CONFIG_OPTEE_CLIENT 1075 return trusty_write_rollback_index(FIT_ROLLBACK_INDEX_LOCATION, 1076 (u64)trusty_index); 1077 #else 1078 return 0; 1079 #endif 1080 } 1081 #endif 1082 1083 void board_quiesce_devices(void *images) 1084 { 1085 #ifdef CONFIG_ROCKCHIP_PRELOADER_ATAGS 1086 /* Destroy atags makes next warm boot safer */ 1087 atags_destroy(); 1088 #endif 1089 #ifdef CONFIG_FIT_ROLLBACK_PROTECT 1090 int ret; 1091 1092 ret = fit_write_trusty_rollback_index(gd->rollback_index); 1093 if (ret) { 1094 panic("Failed to write fit rollback index %d, ret=%d", 1095 gd->rollback_index, ret); 1096 } 1097 #endif 1098 #ifdef CONFIG_ROCKCHIP_HW_DECOMPRESS 1099 misc_decompress_cleanup(); 1100 #endif 1101 #ifdef CONFIG_ARM64 1102 bootm_headers_t *bootm_images = (bootm_headers_t *)images; 1103 1104 /* relocate kernel after decompress cleanup */ 1105 if (orig_images_ep && orig_images_ep != bootm_images->ep) { 1106 memmove((char *)bootm_images->ep, (const char *)orig_images_ep, 1107 bootm_images->os.image_len); 1108 printf("== DO RELOCATE == Kernel from 0x%08lx to 0x%08lx\n", 1109 orig_images_ep, bootm_images->ep); 1110 } 1111 #endif 1112 1113 hotkey_run(HK_CMDLINE); 1114 hotkey_run(HK_CLI_OS_GO); 1115 #ifdef CONFIG_ROCKCHIP_REBOOT_TEST 1116 do_reset(NULL, 0, 0, NULL); 1117 #endif 1118 } 1119 1120 /* 1121 * Use hardware rng to seed Linux random 1122 * 1123 * 'Android_14 + GKI' requires this information. 1124 */ 1125 int board_rng_seed(struct abuf *buf) 1126 { 1127 struct udevice *dev; 1128 size_t len = 32; 1129 u64 *data; 1130 1131 data = malloc(len); 1132 if (!data) { 1133 printf("Out of memory\n"); 1134 return -ENOMEM; 1135 } 1136 1137 if (uclass_get_device(UCLASS_RNG, 0, &dev) || !dev) { 1138 printf("No RNG device\n"); 1139 return -ENODEV; 1140 } 1141 1142 if (dm_rng_read(dev, data, len)) { 1143 printf("Reading RNG failed\n"); 1144 return -EIO; 1145 } 1146 1147 abuf_init_set(buf, data, len); 1148 1149 return 0; 1150 } 1151 1152 char *board_fdt_chosen_bootargs(void *fdt) 1153 { 1154 /* bootargs_ext is used when dtbo is applied. */ 1155 const char *arr_bootargs[] = { "bootargs", "bootargs_ext" }; 1156 const char *bootargs; 1157 int nodeoffset; 1158 int i, dump; 1159 char *msg = "kernel"; 1160 1161 /* debug */ 1162 hotkey_run(HK_INITCALL); 1163 dump = is_hotkey(HK_CMDLINE); 1164 if (dump) 1165 printf("## bootargs(u-boot): %s\n\n", env_get("bootargs")); 1166 1167 /* find or create "/chosen" node. */ 1168 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen"); 1169 if (nodeoffset < 0) 1170 return NULL; 1171 1172 for (i = 0; i < ARRAY_SIZE(arr_bootargs); i++) { 1173 bootargs = fdt_getprop(fdt, nodeoffset, arr_bootargs[i], NULL); 1174 if (!bootargs) 1175 continue; 1176 if (dump) 1177 printf("## bootargs(%s-%s): %s\n\n", 1178 msg, arr_bootargs[i], bootargs); 1179 /* 1180 * Append kernel bootargs 1181 * If use AB system, delete default "root=" which route 1182 * to rootfs. Then the ab bootctl will choose the 1183 * high priority system to boot and add its UUID 1184 * to cmdline. The format is "roo=PARTUUID=xxxx...". 1185 */ 1186 #ifdef CONFIG_ANDROID_AB 1187 env_update_filter("bootargs", bootargs, "root="); 1188 #else 1189 env_update("bootargs", bootargs); 1190 #endif 1191 } 1192 1193 #if defined(CONFIG_ENVF) || defined(CONFIG_ENV_PARTITION) 1194 char *part_type[] = { "mtdparts", "blkdevparts" }; 1195 char *part_list; 1196 char *env; 1197 int id = 0; 1198 1199 env = env_get(part_type[id]); 1200 if (!env) 1201 env = env_get(part_type[++id]); 1202 if (env) { 1203 if (!strstr(env, part_type[id])) { 1204 part_list = calloc(1, strlen(env) + strlen(part_type[id]) + 2); 1205 if (part_list) { 1206 strcat(part_list, part_type[id]); 1207 strcat(part_list, "="); 1208 strcat(part_list, env); 1209 } 1210 } else { 1211 part_list = env; 1212 } 1213 env_update("bootargs", part_list); 1214 if (dump) 1215 printf("## parts: %s\n\n", part_list); 1216 } 1217 1218 env = env_get("sys_bootargs"); 1219 if (env) { 1220 env_update("bootargs", env); 1221 if (dump) 1222 printf("## sys_bootargs: %s\n\n", env); 1223 } 1224 #endif 1225 1226 #ifdef CONFIG_MTD_BLK 1227 if (!env_get("mtdparts")) { 1228 char *mtd_par_info = mtd_part_parse(NULL); 1229 1230 if (mtd_par_info) { 1231 if (memcmp(env_get("devtype"), "mtd", 3) == 0) 1232 env_update("bootargs", mtd_par_info); 1233 } 1234 } 1235 #endif 1236 1237 #ifdef CONFIG_ANDROID_AB 1238 ab_update_root_partition(); 1239 #endif 1240 /* 1241 * Initrd fixup: remove unused "initrd=0x...,0x...", 1242 * this for compatible with legacy parameter.txt 1243 */ 1244 env_delete("bootargs", "initrd=", 0); 1245 1246 /* 1247 * If uart is required to be disabled during 1248 * power on, it would be not initialized by 1249 * any pre-loader and U-Boot. 1250 * 1251 * If we don't remove earlycon from commandline, 1252 * kernel hangs while using earlycon to putc/getc 1253 * which may dead loop for waiting uart status. 1254 * (It seems the root cause is baundrate is not 1255 * initilalized) 1256 * 1257 * So let's remove earlycon from commandline. 1258 */ 1259 if (gd->flags & GD_FLG_DISABLE_CONSOLE) 1260 env_delete("bootargs", "earlycon=", 0); 1261 1262 /* Android header v4+ need this handle */ 1263 #ifdef CONFIG_ANDROID_BOOT_IMAGE 1264 struct andr_img_hdr *hdr; 1265 1266 hdr = (void *)env_get_ulong("android_addr_r", 16, 0); 1267 if (hdr && !android_image_check_header(hdr) && hdr->header_version >= 4) { 1268 if (env_update_extract_subset("bootargs", "andr_bootargs", "androidboot.")) 1269 printf("extract androidboot.xxx error\n"); 1270 if (dump) 1271 printf("## bootargs(android): %s\n\n", env_get("andr_bootargs")); 1272 } 1273 #endif 1274 bootargs = env_get("bootargs"); 1275 if (dump) 1276 printf("## bootargs(merged): %s\n\n", bootargs); 1277 1278 return (char *)bootargs; 1279 } 1280 1281 int ft_verify_fdt(void *fdt) 1282 { 1283 /* for android header v4+, we load bootparams and fixup initrd */ 1284 #if defined(CONFIG_ANDROID_BOOT_IMAGE) && defined(CONFIG_XBC) 1285 struct andr_img_hdr *hdr; 1286 uint64_t initrd_start, initrd_end; 1287 char *bootargs, *p; 1288 int nodeoffset; 1289 int is_u64, err; 1290 u32 len; 1291 1292 hdr = (void *)env_get_ulong("android_addr_r", 16, 0); 1293 if (!hdr || android_image_check_header(hdr) || 1294 hdr->header_version < 4) 1295 return 1; 1296 1297 bootargs = env_get("andr_bootargs"); 1298 if (!bootargs) 1299 return 1; 1300 1301 /* trans character: space to new line */ 1302 p = bootargs; 1303 while (*p++) { 1304 if (*p == ' ') 1305 *p = '\n'; 1306 } 1307 1308 debug("## andr_bootargs: %s\n", bootargs); 1309 1310 /* 1311 * add boot params right after bootconfig 1312 * 1313 * because we can get final full bootargs in board_fdt_chosen_bootargs(), 1314 * android_image_get_ramdisk() is early than that. 1315 * 1316 * we have to add boot params by now. 1317 */ 1318 len = addBootConfigParameters((char *)bootargs, strlen(bootargs), 1319 (u64)hdr->ramdisk_addr + hdr->ramdisk_size + 1320 hdr->vendor_ramdisk_size, hdr->vendor_bootconfig_size); 1321 if (len < 0) { 1322 printf("error: addBootConfigParameters\n"); 1323 return 0; 1324 } 1325 1326 nodeoffset = fdt_subnode_offset(fdt, 0, "chosen"); 1327 if (nodeoffset < 0) { 1328 printf("error: No /chosen node\n"); 1329 return 0; 1330 } 1331 1332 /* fixup initrd with real value */ 1333 fdt_delprop(fdt, nodeoffset, "linux,initrd-start"); 1334 fdt_delprop(fdt, nodeoffset, "linux,initrd-end"); 1335 1336 is_u64 = (fdt_address_cells(fdt, 0) == 2); 1337 initrd_start = hdr->ramdisk_addr; 1338 initrd_end = initrd_start + hdr->ramdisk_size + 1339 hdr->vendor_ramdisk_size + 1340 hdr->vendor_bootconfig_size + len; 1341 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-start", 1342 initrd_start, is_u64); 1343 if (err < 0) { 1344 printf("WARNING: could not set linux,initrd-start %s.\n", 1345 fdt_strerror(err)); 1346 return 0; 1347 } 1348 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-end", 1349 initrd_end, is_u64); 1350 if (err < 0) { 1351 printf("WARNING: could not set linux,initrd-end %s.\n", 1352 fdt_strerror(err)); 1353 return 0; 1354 } 1355 #endif 1356 return 1; 1357 } 1358 1359