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