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 if (rockchip_get_boot_mode() == BOOT_MODE_QUIESCENT) 417 env_update("bootargs", "androidboot.quiescent=1 pwm_bl.quiescent=1"); 418 } 419 420 static void scan_run_cmd(void) 421 { 422 char *config = CONFIG_ROCKCHIP_CMD; 423 char *cmd, *key; 424 425 key = strchr(config, ' '); 426 if (!key) 427 return; 428 429 cmd = strdup(config); 430 cmd[key - config] = 0; 431 key++; 432 433 if (!strcmp(key, "-")) { 434 run_command(cmd, 0); 435 } else { 436 #ifdef CONFIG_DM_KEY 437 ulong map; 438 439 map = simple_strtoul(key, NULL, 10); 440 if (key_is_pressed(key_read(map))) { 441 printf("## Key<%ld> pressed... run cmd '%s'\n", map, cmd); 442 run_command(cmd, 0); 443 } 444 #endif 445 } 446 } 447 448 int board_late_init(void) 449 { 450 #ifdef CONFIG_ROCKCHIP_SET_ETHADDR 451 rockchip_set_ethaddr(); 452 #endif 453 #ifdef CONFIG_ROCKCHIP_SET_SN 454 rockchip_set_serialno(); 455 #endif 456 setup_download_mode(); 457 scan_run_cmd(); 458 #ifdef CONFIG_ROCKCHIP_USB_BOOT 459 boot_from_udisk(); 460 #endif 461 #ifdef CONFIG_DM_CHARGE_DISPLAY 462 charge_display(); 463 #endif 464 #ifdef CONFIG_DRM_ROCKCHIP 465 if (rockchip_get_boot_mode() != BOOT_MODE_QUIESCENT) 466 rockchip_show_logo(); 467 #endif 468 #ifdef CONFIG_ROCKCHIP_EINK_DISPLAY 469 rockchip_eink_show_uboot_logo(); 470 #endif 471 #if (CONFIG_ROCKCHIP_BOOT_MODE_REG > 0) 472 setup_boot_mode(); 473 #endif 474 env_fixup(); 475 soc_clk_dump(); 476 cmdline_handle(); 477 #ifdef CONFIG_AMP 478 amp_cpus_on(); 479 #endif 480 return rk_board_late_init(); 481 } 482 483 static void early_download(void) 484 { 485 #if defined(CONFIG_PWRKEY_DNL_TRIGGER_NUM) && \ 486 (CONFIG_PWRKEY_DNL_TRIGGER_NUM > 0) 487 if (pwrkey_download_init()) 488 printf("Pwrkey download init failed\n"); 489 #endif 490 491 #if (CONFIG_ROCKCHIP_BOOT_MODE_REG > 0) 492 if (is_hotkey(HK_BROM_DNL)) { 493 printf("Enter bootrom download..."); 494 flushc(); 495 writel(BOOT_BROM_DOWNLOAD, CONFIG_ROCKCHIP_BOOT_MODE_REG); 496 do_reset(NULL, 0, 0, NULL); 497 printf("failed!\n"); 498 } 499 #endif 500 } 501 502 static void board_debug_init(void) 503 { 504 if (!gd->serial.using_pre_serial && 505 !(gd->flags & GD_FLG_DISABLE_CONSOLE)) 506 debug_uart_init(); 507 508 if (tstc()) { 509 gd->console_evt = getc(); 510 if (gd->console_evt <= 0x1a) /* 'z' */ 511 printf("Hotkey: ctrl+%c\n", gd->console_evt + 'a' - 1); 512 } 513 514 if (IS_ENABLED(CONFIG_CONSOLE_DISABLE_CLI)) 515 printf("Cmd interface: disabled\n"); 516 } 517 518 int board_init(void) 519 { 520 board_debug_init(); 521 #ifdef DEBUG 522 soc_clk_dump(); 523 #endif 524 #ifdef CONFIG_OPTEE_CLIENT 525 optee_client_init(); 526 #endif 527 #ifdef CONFIG_USING_KERNEL_DTB 528 init_kernel_dtb(); 529 #endif 530 early_download(); 531 532 clks_probe(); 533 #ifdef CONFIG_DM_REGULATOR 534 regulators_enable_boot_on(is_hotkey(HK_REGULATOR)); 535 #endif 536 #ifdef CONFIG_ROCKCHIP_IO_DOMAIN 537 io_domain_init(); 538 #endif 539 set_armclk_rate(); 540 #ifdef CONFIG_DM_DVFS 541 dvfs_init(true); 542 #endif 543 #ifdef CONFIG_ANDROID_AB 544 if (ab_decrease_tries()) 545 printf("Decrease ab tries count fail!\n"); 546 #endif 547 548 return rk_board_init(); 549 } 550 551 int interrupt_debugger_init(void) 552 { 553 #ifdef CONFIG_ROCKCHIP_DEBUGGER 554 return rockchip_debugger_init(); 555 #else 556 return 0; 557 #endif 558 } 559 560 int board_fdt_fixup(void *blob) 561 { 562 /* 563 * Device's platdata points to orignal fdt blob property, 564 * access DM device before any fdt fixup. 565 */ 566 rk_board_dm_fdt_fixup(blob); 567 568 /* Common fixup for DRM */ 569 #ifdef CONFIG_DRM_ROCKCHIP 570 rockchip_display_fixup(blob); 571 #endif 572 573 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION 574 vendor_storage_fixup(blob); 575 #endif 576 577 return rk_board_fdt_fixup(blob); 578 } 579 580 #if defined(CONFIG_ARM64_BOOT_AARCH32) || !defined(CONFIG_ARM64) 581 /* 582 * Common for OP-TEE: 583 * 64-bit & 32-bit mode: share memory dcache is always enabled; 584 * 585 * Common for U-Boot: 586 * 64-bit mode: MMU table is static defined in rkxxx.c file, all memory 587 * regions are mapped. That's good to match OP-TEE MMU policy. 588 * 589 * 32-bit mode: MMU table is setup according to gd->bd->bi_dram[..] where 590 * the OP-TEE region has been reserved, so it can not be 591 * mapped(i.e. dcache is disabled). That's *NOT* good to match 592 * OP-TEE MMU policy. 593 * 594 * For the data coherence when communication between U-Boot and OP-TEE, U-Boot 595 * should follow OP-TEE MMU policy. 596 * 597 * So 32-bit mode U-Boot should map OP-TEE share memory as dcache enabled. 598 */ 599 int board_initr_caches_fixup(void) 600 { 601 #ifdef CONFIG_OPTEE_CLIENT 602 struct memblock mem; 603 604 mem.base = 0; 605 mem.size = 0; 606 607 optee_get_shm_config(&mem.base, &mem.size); 608 if (mem.size) 609 mmu_set_region_dcache_behaviour(mem.base, mem.size, 610 DCACHE_WRITEBACK); 611 #endif 612 return 0; 613 } 614 #endif 615 616 void arch_preboot_os(uint32_t bootm_state, bootm_headers_t *images) 617 { 618 if (!(bootm_state & BOOTM_STATE_OS_PREP)) 619 return; 620 621 #ifdef CONFIG_ARM64 622 u8 *data = (void *)images->ep; 623 624 /* 625 * Fix kernel 5.10 arm64 boot warning: 626 * "[Firmware Bug]: Kernel image misaligned at boot, please fix your bootloader!" 627 * 628 * kernel: 5.10 commit 120dc60d0bdb ("arm64: get rid of TEXT_OFFSET") 629 * arm64 kernel version: 630 * data[10] == 0x00 if kernel version >= 5.10: N*2MB align 631 * data[10] == 0x08 if kernel version < 5.10: N*2MB + 0x80000(TEXT_OFFSET) 632 * 633 * Why fix here? 634 * 1. this is the common and final path for any boot command. 635 * 2. don't influence original boot flow, just fix it exactly before 636 * jumping kernel. 637 * 638 * But relocation is in board_quiesce_devices() until all decompress 639 * done, mainly for saving boot time. 640 */ 641 642 orig_images_ep = images->ep; 643 644 if (data[10] == 0x00) { 645 if (round_down(images->ep, SZ_2M) != images->ep) 646 images->ep = round_down(images->ep, SZ_2M); 647 } else { 648 if (IS_ALIGNED(images->ep, SZ_2M)) 649 images->ep += 0x80000; 650 } 651 #endif 652 hotkey_run(HK_CLI_OS_PRE); 653 } 654 655 void enable_caches(void) 656 { 657 icache_enable(); 658 dcache_enable(); 659 } 660 661 #ifdef CONFIG_LMB 662 /* 663 * Using last bi_dram[...] to initialize "bootm_low" and "bootm_mapsize". 664 * This makes lmb_alloc_base() always alloc from tail of sdram. 665 * If we don't assign it, bi_dram[0] is used by default and it may cause 666 * lmb_alloc_base() fail when bi_dram[0] range is small. 667 */ 668 void board_lmb_reserve(struct lmb *lmb) 669 { 670 char bootm_mapsize[32]; 671 char bootm_low[32]; 672 u64 start, size; 673 int i; 674 675 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { 676 if (!gd->bd->bi_dram[i].size) 677 break; 678 } 679 680 start = gd->bd->bi_dram[i - 1].start; 681 size = gd->bd->bi_dram[i - 1].size; 682 683 /* 684 * 32-bit kernel: ramdisk/fdt shouldn't be loaded to highmem area(768MB+), 685 * otherwise "Unable to handle kernel paging request at virtual address ...". 686 * 687 * So that we hope limit highest address at 768M, but there comes the the 688 * problem: ramdisk is a compressed image and it expands after descompress, 689 * so it accesses 768MB+ and brings the above "Unable to handle kernel ...". 690 * 691 * We make a appointment that the highest memory address is 512MB, it 692 * makes lmb alloc safer. 693 */ 694 #ifndef CONFIG_ARM64 695 if (start >= ((u64)CONFIG_SYS_SDRAM_BASE + SZ_512M)) { 696 start = gd->bd->bi_dram[i - 2].start; 697 size = gd->bd->bi_dram[i - 2].size; 698 } 699 700 if ((start + size) > ((u64)CONFIG_SYS_SDRAM_BASE + SZ_512M)) 701 size = (u64)CONFIG_SYS_SDRAM_BASE + SZ_512M - start; 702 #endif 703 sprintf(bootm_low, "0x%llx", start); 704 sprintf(bootm_mapsize, "0x%llx", size); 705 env_set("bootm_low", bootm_low); 706 env_set("bootm_mapsize", bootm_mapsize); 707 } 708 #endif 709 710 #ifdef CONFIG_BIDRAM 711 int board_bidram_reserve(struct bidram *bidram) 712 { 713 struct memblock mem; 714 int ret; 715 716 /* ATF */ 717 mem = param_parse_atf_mem(); 718 ret = bidram_reserve(MEM_ATF, mem.base, mem.size); 719 if (ret) 720 return ret; 721 722 /* PSTORE/ATAGS/SHM */ 723 mem = param_parse_common_resv_mem(); 724 ret = bidram_reserve(MEM_SHM, mem.base, mem.size); 725 if (ret) 726 return ret; 727 728 /* OP-TEE */ 729 mem = param_parse_optee_mem(); 730 ret = bidram_reserve(MEM_OPTEE, mem.base, mem.size); 731 if (ret) 732 return ret; 733 734 return 0; 735 } 736 737 #ifdef CONFIG_SYSMEM 738 int board_sysmem_reserve(struct sysmem *sysmem) 739 { 740 #ifdef CONFIG_SKIP_RELOCATE_UBOOT 741 if (!sysmem_alloc_base_by_name("NO-RELOC-CODE", 742 CONFIG_SYS_TEXT_BASE, SZ_2M)) { 743 printf("Failed to reserve sysmem for U-Boot code\n"); 744 return -ENOMEM; 745 } 746 #endif 747 return 0; 748 } 749 #endif 750 751 parse_fn_t board_bidram_parse_fn(void) 752 { 753 return param_parse_ddr_mem; 754 } 755 #endif 756 757 int board_init_f_boot_flags(void) 758 { 759 int boot_flags = 0; 760 761 #ifdef CONFIG_FPGA_ROCKCHIP 762 arch_fpga_init(); 763 #endif 764 #ifdef CONFIG_PSTORE 765 param_parse_pstore(); 766 #endif 767 param_parse_pre_serial(&boot_flags); 768 769 /* The highest priority to turn off (override) console */ 770 #if defined(CONFIG_DISABLE_CONSOLE) 771 boot_flags |= GD_FLG_DISABLE_CONSOLE; 772 #endif 773 774 return boot_flags; 775 } 776 777 #if defined(CONFIG_USB_GADGET) 778 #include <usb.h> 779 #if defined(CONFIG_USB_GADGET_DWC2_OTG) 780 #include <fdt_support.h> 781 #include <usb/dwc2_udc.h> 782 783 static struct dwc2_plat_otg_data otg_data = { 784 .rx_fifo_sz = 512, 785 .np_tx_fifo_sz = 16, 786 .tx_fifo_sz = 128, 787 }; 788 789 int board_usb_init(int index, enum usb_init_type init) 790 { 791 const void *blob = gd->fdt_blob; 792 const fdt32_t *reg; 793 fdt_addr_t addr; 794 int node; 795 796 /* find the usb_otg node */ 797 node = fdt_node_offset_by_compatible(blob, -1, "snps,dwc2"); 798 799 retry: 800 if (node > 0) { 801 reg = fdt_getprop(blob, node, "reg", NULL); 802 if (!reg) 803 return -EINVAL; 804 805 addr = fdt_translate_address(blob, node, reg); 806 if (addr == OF_BAD_ADDR) { 807 pr_err("Not found usb_otg address\n"); 808 return -EINVAL; 809 } 810 811 #if defined(CONFIG_ROCKCHIP_RK3288) 812 if (addr != 0xff580000) { 813 node = fdt_node_offset_by_compatible(blob, node, 814 "snps,dwc2"); 815 goto retry; 816 } 817 #endif 818 } else { 819 /* 820 * With kernel dtb support, rk3288 dwc2 otg node 821 * use the rockchip legacy dwc2 driver "dwc_otg_310" 822 * with the compatible "rockchip,rk3288_usb20_otg", 823 * and rk3368 also use the "dwc_otg_310" driver with 824 * the compatible "rockchip,rk3368-usb". 825 */ 826 #if defined(CONFIG_ROCKCHIP_RK3288) 827 node = fdt_node_offset_by_compatible(blob, -1, 828 "rockchip,rk3288_usb20_otg"); 829 #elif defined(CONFIG_ROCKCHIP_RK3368) 830 node = fdt_node_offset_by_compatible(blob, -1, 831 "rockchip,rk3368-usb"); 832 #endif 833 if (node > 0) { 834 goto retry; 835 } else { 836 pr_err("Not found usb_otg device\n"); 837 return -ENODEV; 838 } 839 } 840 841 otg_data.regs_otg = (uintptr_t)addr; 842 843 return dwc2_udc_probe(&otg_data); 844 } 845 846 int board_usb_cleanup(int index, enum usb_init_type init) 847 { 848 return 0; 849 } 850 #elif defined(CONFIG_USB_DWC3_GADGET) /* CONFIG_USB_GADGET_DWC2_OTG */ 851 #include <dwc3-uboot.h> 852 853 int board_usb_cleanup(int index, enum usb_init_type init) 854 { 855 dwc3_uboot_exit(index); 856 return 0; 857 } 858 859 #endif /* CONFIG_USB_DWC3_GADGET */ 860 #endif /* CONFIG_USB_GADGET */ 861 862 static void bootm_no_reloc(void) 863 { 864 char *ramdisk_high; 865 char *fdt_high; 866 867 if (!env_get_yesno("bootm-no-reloc")) 868 return; 869 870 ramdisk_high = env_get("initrd_high"); 871 fdt_high = env_get("fdt_high"); 872 873 if (!fdt_high) { 874 env_set_hex("fdt_high", -1UL); 875 printf("Fdt "); 876 } 877 878 if (!ramdisk_high) { 879 env_set_hex("initrd_high", -1UL); 880 printf("Ramdisk "); 881 } 882 883 if (!fdt_high || !ramdisk_high) 884 printf("skip relocation\n"); 885 } 886 887 int bootm_board_start(void) 888 { 889 /* 890 * print console record data 891 * 892 * On some rockchip platforms, uart debug and sdmmc pin are multiplex. 893 * If boot from sdmmc mode, the console data would be record in buffer, 894 * we switch to uart debug function in order to print it after loading 895 * images. 896 */ 897 #if defined(CONFIG_CONSOLE_RECORD) 898 if (!strcmp("mmc", env_get("devtype")) && 899 !strcmp("1", env_get("devnum"))) { 900 printf("IOMUX: sdmmc => uart debug"); 901 pinctrl_select_state(gd->cur_serial_dev, "default"); 902 console_record_print_purge(); 903 } 904 #endif 905 /* disable bootm relcation to save boot time */ 906 bootm_no_reloc(); 907 908 /* PCBA test needs more permission */ 909 if (get_bcb_recovery_msg() == BCB_MSG_RECOVERY_PCBA) 910 env_update("bootargs", "androidboot.selinux=permissive"); 911 912 /* sysmem */ 913 hotkey_run(HK_SYSMEM); 914 sysmem_overflow_check(); 915 916 return 0; 917 } 918 919 int bootm_image_populate_dtb(void *img) 920 { 921 if ((gd->flags & GD_FLG_KDTB_READY) && !gd->fdt_blob_kern) 922 sysmem_free((phys_addr_t)gd->fdt_blob); 923 else 924 gd->fdt_blob = (void *)env_get_ulong("fdt_addr_r", 16, 0); 925 926 return rockchip_ram_read_dtb_file(img, (void *)gd->fdt_blob); 927 } 928 929 /* 930 * Implement it to support CLI command: 931 * - Android: bootm [aosp addr] 932 * - FIT: bootm [fit addr] 933 * - uImage: bootm [uimage addr] 934 * 935 * Purpose: 936 * - The original bootm command args require fdt addr on AOSP, 937 * which is not flexible on rockchip boot/recovery.img. 938 * - Take Android/FIT/uImage image into sysmem management to avoid image 939 * memory overlap. 940 */ 941 #if defined(CONFIG_ANDROID_BOOTLOADER) || \ 942 defined(CONFIG_ROCKCHIP_FIT_IMAGE) || \ 943 defined(CONFIG_ROCKCHIP_UIMAGE) 944 int board_do_bootm(int argc, char * const argv[]) 945 { 946 int format; 947 void *img; 948 949 /* only 'bootm' full image goes further */ 950 if (argc != 2) 951 return 0; 952 953 img = (void *)simple_strtoul(argv[1], NULL, 16); 954 format = (genimg_get_format(img)); 955 956 /* Android */ 957 #ifdef CONFIG_ANDROID_BOOT_IMAGE 958 if (format == IMAGE_FORMAT_ANDROID) { 959 struct andr_img_hdr *hdr; 960 ulong load_addr; 961 ulong size; 962 int ret; 963 964 hdr = (struct andr_img_hdr *)img; 965 printf("BOOTM: transferring to board Android\n"); 966 967 load_addr = env_get_ulong("kernel_addr_r", 16, 0); 968 load_addr -= hdr->page_size; 969 size = android_image_get_end(hdr) - (ulong)hdr; 970 971 if (!sysmem_alloc_base(MEM_ANDROID, (ulong)hdr, size)) 972 return -ENOMEM; 973 974 ret = bootm_image_populate_dtb(img); 975 if (ret) { 976 printf("bootm can't read dtb, ret=%d\n", ret); 977 return ret; 978 } 979 980 ret = android_image_memcpy_separate(hdr, &load_addr); 981 if (ret) { 982 printf("board do bootm failed, ret=%d\n", ret); 983 return ret; 984 } 985 986 return android_bootloader_boot_kernel(load_addr); 987 } 988 #endif 989 990 /* FIT */ 991 #if IMAGE_ENABLE_FIT 992 if (format == IMAGE_FORMAT_FIT) { 993 char boot_cmd[64]; 994 int ret; 995 996 printf("BOOTM: transferring to board FIT\n"); 997 998 ret = bootm_image_populate_dtb(img); 999 if (ret) { 1000 printf("bootm can't read dtb, ret=%d\n", ret); 1001 return ret; 1002 } 1003 snprintf(boot_cmd, sizeof(boot_cmd), "boot_fit %s", argv[1]); 1004 return run_command(boot_cmd, 0); 1005 } 1006 #endif 1007 1008 /* uImage */ 1009 #if 0 1010 #if defined(CONFIG_IMAGE_FORMAT_LEGACY) 1011 if (format == IMAGE_FORMAT_LEGACY && 1012 image_get_type(img) == IH_TYPE_MULTI) { 1013 char boot_cmd[64]; 1014 1015 printf("BOOTM: transferring to board uImage\n"); 1016 snprintf(boot_cmd, sizeof(boot_cmd), "boot_uimage %s", argv[1]); 1017 return run_command(boot_cmd, 0); 1018 } 1019 #endif 1020 #endif 1021 return 0; 1022 } 1023 #endif 1024 1025 void autoboot_command_fail_handle(void) 1026 { 1027 #ifdef CONFIG_ANDROID_AB 1028 if (rk_avb_ab_have_bootable_slot() == true) 1029 run_command("reset;", 0); 1030 else 1031 run_command("fastboot usb 0;", 0); 1032 #endif 1033 1034 #ifdef CONFIG_AVB_VBMETA_PUBLIC_KEY_VALIDATE 1035 run_command("download", 0); 1036 run_command("fastboot usb 0;", 0); 1037 #endif 1038 1039 } 1040 1041 #ifdef CONFIG_FIT_ROLLBACK_PROTECT 1042 1043 #define FIT_ROLLBACK_INDEX_LOCATION 0x66697472 /* "fitr" */ 1044 1045 int fit_read_otp_rollback_index(uint32_t fit_index, uint32_t *otp_index) 1046 { 1047 #ifdef CONFIG_OPTEE_CLIENT 1048 u64 index; 1049 int ret; 1050 1051 ret = trusty_read_rollback_index(FIT_ROLLBACK_INDEX_LOCATION, &index); 1052 if (ret) { 1053 if (ret != TEE_ERROR_ITEM_NOT_FOUND) 1054 return ret; 1055 1056 index = 0; 1057 printf("Initial otp index as %d\n", fit_index); 1058 } 1059 1060 *otp_index = (uint32_t)index; 1061 #else 1062 *otp_index = 0; 1063 #endif 1064 1065 return 0; 1066 } 1067 1068 int fit_write_trusty_rollback_index(u32 trusty_index) 1069 { 1070 if (!trusty_index) 1071 return 0; 1072 #ifdef CONFIG_OPTEE_CLIENT 1073 return trusty_write_rollback_index(FIT_ROLLBACK_INDEX_LOCATION, 1074 (u64)trusty_index); 1075 #else 1076 return 0; 1077 #endif 1078 } 1079 #endif 1080 1081 void board_quiesce_devices(void *images) 1082 { 1083 #ifdef CONFIG_ROCKCHIP_PRELOADER_ATAGS 1084 /* Destroy atags makes next warm boot safer */ 1085 atags_destroy(); 1086 #endif 1087 #ifdef CONFIG_FIT_ROLLBACK_PROTECT 1088 int ret; 1089 1090 ret = fit_write_trusty_rollback_index(gd->rollback_index); 1091 if (ret) { 1092 panic("Failed to write fit rollback index %d, ret=%d", 1093 gd->rollback_index, ret); 1094 } 1095 #endif 1096 #ifdef CONFIG_ROCKCHIP_HW_DECOMPRESS 1097 misc_decompress_cleanup(); 1098 #endif 1099 #ifdef CONFIG_ARM64 1100 bootm_headers_t *bootm_images = (bootm_headers_t *)images; 1101 1102 /* relocate kernel after decompress cleanup */ 1103 if (orig_images_ep && orig_images_ep != bootm_images->ep) { 1104 memmove((char *)bootm_images->ep, (const char *)orig_images_ep, 1105 bootm_images->os.image_len); 1106 printf("== DO RELOCATE == Kernel from 0x%08lx to 0x%08lx\n", 1107 orig_images_ep, bootm_images->ep); 1108 } 1109 #endif 1110 1111 hotkey_run(HK_CMDLINE); 1112 hotkey_run(HK_CLI_OS_GO); 1113 #ifdef CONFIG_ROCKCHIP_REBOOT_TEST 1114 do_reset(NULL, 0, 0, NULL); 1115 #endif 1116 } 1117 1118 char *board_fdt_chosen_bootargs(void *fdt) 1119 { 1120 /* bootargs_ext is used when dtbo is applied. */ 1121 const char *arr_bootargs[] = { "bootargs", "bootargs_ext" }; 1122 const char *bootargs; 1123 int nodeoffset; 1124 int i, dump; 1125 char *msg = "kernel"; 1126 1127 /* debug */ 1128 hotkey_run(HK_INITCALL); 1129 dump = is_hotkey(HK_CMDLINE); 1130 if (dump) 1131 printf("## bootargs(u-boot): %s\n\n", env_get("bootargs")); 1132 1133 /* find or create "/chosen" node. */ 1134 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen"); 1135 if (nodeoffset < 0) 1136 return NULL; 1137 1138 for (i = 0; i < ARRAY_SIZE(arr_bootargs); i++) { 1139 bootargs = fdt_getprop(fdt, nodeoffset, arr_bootargs[i], NULL); 1140 if (!bootargs) 1141 continue; 1142 if (dump) 1143 printf("## bootargs(%s-%s): %s\n\n", 1144 msg, arr_bootargs[i], bootargs); 1145 /* 1146 * Append kernel bootargs 1147 * If use AB system, delete default "root=" which route 1148 * to rootfs. Then the ab bootctl will choose the 1149 * high priority system to boot and add its UUID 1150 * to cmdline. The format is "roo=PARTUUID=xxxx...". 1151 */ 1152 #ifdef CONFIG_ANDROID_AB 1153 env_update_filter("bootargs", bootargs, "root="); 1154 #else 1155 env_update("bootargs", bootargs); 1156 #endif 1157 } 1158 1159 #if defined(CONFIG_ENVF) || defined(CONFIG_ENV_PARTITION) 1160 char *part_type[] = { "mtdparts", "blkdevparts" }; 1161 char *part_list; 1162 char *env; 1163 int id = 0; 1164 1165 env = env_get(part_type[id]); 1166 if (!env) 1167 env = env_get(part_type[++id]); 1168 if (env) { 1169 if (!strstr(env, part_type[id])) { 1170 part_list = calloc(1, strlen(env) + strlen(part_type[id]) + 2); 1171 if (part_list) { 1172 strcat(part_list, part_type[id]); 1173 strcat(part_list, "="); 1174 strcat(part_list, env); 1175 } 1176 } else { 1177 part_list = env; 1178 } 1179 env_update("bootargs", part_list); 1180 if (dump) 1181 printf("## parts: %s\n\n", part_list); 1182 } 1183 1184 env = env_get("sys_bootargs"); 1185 if (env) { 1186 env_update("bootargs", env); 1187 if (dump) 1188 printf("## sys_bootargs: %s\n\n", env); 1189 } 1190 #endif 1191 1192 #ifdef CONFIG_MTD_BLK 1193 if (!env_get("mtdparts")) { 1194 char *mtd_par_info = mtd_part_parse(NULL); 1195 1196 if (mtd_par_info) { 1197 if (memcmp(env_get("devtype"), "mtd", 3) == 0) 1198 env_update("bootargs", mtd_par_info); 1199 } 1200 } 1201 #endif 1202 1203 #ifdef CONFIG_ANDROID_AB 1204 ab_update_root_partition(); 1205 #endif 1206 /* 1207 * Initrd fixup: remove unused "initrd=0x...,0x...", 1208 * this for compatible with legacy parameter.txt 1209 */ 1210 env_delete("bootargs", "initrd=", 0); 1211 1212 /* 1213 * If uart is required to be disabled during 1214 * power on, it would be not initialized by 1215 * any pre-loader and U-Boot. 1216 * 1217 * If we don't remove earlycon from commandline, 1218 * kernel hangs while using earlycon to putc/getc 1219 * which may dead loop for waiting uart status. 1220 * (It seems the root cause is baundrate is not 1221 * initilalized) 1222 * 1223 * So let's remove earlycon from commandline. 1224 */ 1225 if (gd->flags & GD_FLG_DISABLE_CONSOLE) 1226 env_delete("bootargs", "earlycon=", 0); 1227 1228 /* Android header v4+ need this handle */ 1229 #ifdef CONFIG_ANDROID_BOOT_IMAGE 1230 struct andr_img_hdr *hdr; 1231 1232 hdr = (void *)env_get_ulong("android_addr_r", 16, 0); 1233 if (hdr && !android_image_check_header(hdr) && hdr->header_version >= 4) { 1234 if (env_update_extract_subset("bootargs", "andr_bootargs", "androidboot.")) 1235 printf("extract androidboot.xxx error\n"); 1236 if (dump) 1237 printf("## bootargs(android): %s\n\n", env_get("andr_bootargs")); 1238 } 1239 #endif 1240 bootargs = env_get("bootargs"); 1241 if (dump) 1242 printf("## bootargs(merged): %s\n\n", bootargs); 1243 1244 return (char *)bootargs; 1245 } 1246 1247 int ft_verify_fdt(void *fdt) 1248 { 1249 /* for android header v4+, we load bootparams and fixup initrd */ 1250 #if defined(CONFIG_ANDROID_BOOT_IMAGE) && defined(CONFIG_XBC) 1251 struct andr_img_hdr *hdr; 1252 uint64_t initrd_start, initrd_end; 1253 char *bootargs, *p; 1254 int nodeoffset; 1255 int is_u64, err; 1256 u32 len; 1257 1258 hdr = (void *)env_get_ulong("android_addr_r", 16, 0); 1259 if (!hdr || android_image_check_header(hdr) || 1260 hdr->header_version < 4) 1261 return 1; 1262 1263 bootargs = env_get("andr_bootargs"); 1264 if (!bootargs) 1265 return 1; 1266 1267 /* trans character: space to new line */ 1268 p = bootargs; 1269 while (*p++) { 1270 if (*p == ' ') 1271 *p = '\n'; 1272 } 1273 1274 debug("## andr_bootargs: %s\n", bootargs); 1275 1276 /* 1277 * add boot params right after bootconfig 1278 * 1279 * because we can get final full bootargs in board_fdt_chosen_bootargs(), 1280 * android_image_get_ramdisk() is early than that. 1281 * 1282 * we have to add boot params by now. 1283 */ 1284 len = addBootConfigParameters((char *)bootargs, strlen(bootargs), 1285 (u64)hdr->ramdisk_addr + hdr->ramdisk_size + 1286 hdr->vendor_ramdisk_size, hdr->vendor_bootconfig_size); 1287 if (len < 0) { 1288 printf("error: addBootConfigParameters\n"); 1289 return 0; 1290 } 1291 1292 nodeoffset = fdt_subnode_offset(fdt, 0, "chosen"); 1293 if (nodeoffset < 0) { 1294 printf("error: No /chosen node\n"); 1295 return 0; 1296 } 1297 1298 /* fixup initrd with real value */ 1299 fdt_delprop(fdt, nodeoffset, "linux,initrd-start"); 1300 fdt_delprop(fdt, nodeoffset, "linux,initrd-end"); 1301 1302 is_u64 = (fdt_address_cells(fdt, 0) == 2); 1303 initrd_start = hdr->ramdisk_addr; 1304 initrd_end = initrd_start + hdr->ramdisk_size + 1305 hdr->vendor_ramdisk_size + 1306 hdr->vendor_bootconfig_size + len; 1307 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-start", 1308 initrd_start, is_u64); 1309 if (err < 0) { 1310 printf("WARNING: could not set linux,initrd-start %s.\n", 1311 fdt_strerror(err)); 1312 return 0; 1313 } 1314 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-end", 1315 initrd_end, is_u64); 1316 if (err < 0) { 1317 printf("WARNING: could not set linux,initrd-end %s.\n", 1318 fdt_strerror(err)); 1319 return 0; 1320 } 1321 #endif 1322 return 1; 1323 } 1324 1325