xref: /rk3399_rockchip-uboot/arch/arm/mach-rockchip/board.c (revision d1ffb5dd4317afb75fe2c8f8c0bfe2b996bb28f7)
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_bootloader.h>
10 #include <android_image.h>
11 #include <bidram.h>
12 #include <boot_rkimg.h>
13 #include <cli.h>
14 #include <clk.h>
15 #include <console.h>
16 #include <debug_uart.h>
17 #include <dm.h>
18 #include <dvfs.h>
19 #include <io-domain.h>
20 #include <image.h>
21 #include <key.h>
22 #include <memblk.h>
23 #include <misc.h>
24 #include <of_live.h>
25 #include <mtd_blk.h>
26 #include <ram.h>
27 #include <rockchip_debugger.h>
28 #include <syscon.h>
29 #include <sysmem.h>
30 #include <video_rockchip.h>
31 #include <asm/io.h>
32 #include <asm/gpio.h>
33 #include <dm/uclass-internal.h>
34 #include <dm/root.h>
35 #include <power/charge_display.h>
36 #include <power/regulator.h>
37 #include <optee_include/OpteeClientInterface.h>
38 #include <asm/arch/boot_mode.h>
39 #include <asm/arch/clock.h>
40 #include <asm/arch/cpu.h>
41 #include <asm/arch/hotkey.h>
42 #include <asm/arch/param.h>
43 #include <asm/arch/periph.h>
44 #include <asm/arch/resource_img.h>
45 #include <asm/arch/rk_atags.h>
46 #include <asm/arch/vendor.h>
47 
48 DECLARE_GLOBAL_DATA_PTR;
49 
50 __weak int rk_board_late_init(void)
51 {
52 	return 0;
53 }
54 
55 __weak int rk_board_fdt_fixup(void *blob)
56 {
57 	return 0;
58 }
59 
60 __weak int soc_clk_dump(void)
61 {
62 	return 0;
63 }
64 
65 __weak int set_armclk_rate(void)
66 {
67 	return 0;
68 }
69 
70 __weak int rk_board_init(void)
71 {
72 	return 0;
73 }
74 
75 /*
76  * define serialno max length, the max length is 512 Bytes
77  * The remaining bytes are used to ensure that the first 512 bytes
78  * are valid when executing 'env_set("serial#", value)'.
79  */
80 #define VENDOR_SN_MAX	513
81 #define CPUID_LEN	0x10
82 #define CPUID_OFF	0x07
83 
84 static int rockchip_set_ethaddr(void)
85 {
86 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION
87 	char buf[ARP_HLEN_ASCII + 1];
88 	u8 ethaddr[ARP_HLEN];
89 	int ret;
90 
91 	ret = vendor_storage_read(VENDOR_LAN_MAC_ID, ethaddr, sizeof(ethaddr));
92 	if (ret > 0 && is_valid_ethaddr(ethaddr)) {
93 		sprintf(buf, "%pM", ethaddr);
94 		env_set("ethaddr", buf);
95 	}
96 #endif
97 	return 0;
98 }
99 
100 static int rockchip_set_serialno(void)
101 {
102 	u8 low[CPUID_LEN / 2], high[CPUID_LEN / 2];
103 	u8 cpuid[CPUID_LEN] = {0};
104 	char serialno_str[VENDOR_SN_MAX];
105 	int ret = 0, i;
106 	u64 serialno;
107 
108 	/* Read serial number from vendor storage part */
109 	memset(serialno_str, 0, VENDOR_SN_MAX);
110 
111 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION
112 	ret = vendor_storage_read(VENDOR_SN_ID, serialno_str, (VENDOR_SN_MAX-1));
113 	if (ret > 0) {
114 		i = strlen(serialno_str);
115 		for (; i > 0; i--) {
116 			if ((serialno_str[i] >= 'a' && serialno_str[i] <= 'z') ||
117 			    (serialno_str[i] >= 'A' && serialno_str[i] <= 'Z') ||
118 			    (serialno_str[i] >= '0' && serialno_str[i] <= '9'))
119 				break;
120 		}
121 
122 		serialno_str[i + 1] = 0x0;
123 		env_set("serial#", serialno_str);
124 	} else {
125 #endif
126 #ifdef CONFIG_ROCKCHIP_EFUSE
127 		struct udevice *dev;
128 
129 		/* retrieve the device */
130 		ret = uclass_get_device_by_driver(UCLASS_MISC,
131 						  DM_GET_DRIVER(rockchip_efuse),
132 						  &dev);
133 		if (ret) {
134 			printf("%s: could not find efuse device\n", __func__);
135 			return ret;
136 		}
137 
138 		/* read the cpu_id range from the efuses */
139 		ret = misc_read(dev, CPUID_OFF, &cpuid, sizeof(cpuid));
140 		if (ret) {
141 			printf("%s: read cpuid from efuses failed, ret=%d\n",
142 			       __func__, ret);
143 			return ret;
144 		}
145 #else
146 		/* generate random cpuid */
147 		for (i = 0; i < CPUID_LEN; i++)
148 			cpuid[i] = (u8)(rand());
149 #endif
150 		/* Generate the serial number based on CPU ID */
151 		for (i = 0; i < 8; i++) {
152 			low[i] = cpuid[1 + (i << 1)];
153 			high[i] = cpuid[i << 1];
154 		}
155 
156 		serialno = crc32_no_comp(0, low, 8);
157 		serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32;
158 		snprintf(serialno_str, sizeof(serialno_str), "%llx", serialno);
159 
160 		env_set("serial#", serialno_str);
161 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION
162 	}
163 #endif
164 
165 	return ret;
166 }
167 
168 #if defined(CONFIG_USB_FUNCTION_FASTBOOT)
169 int fb_set_reboot_flag(void)
170 {
171 	printf("Setting reboot to fastboot flag ...\n");
172 	writel(BOOT_FASTBOOT, CONFIG_ROCKCHIP_BOOT_MODE_REG);
173 
174 	return 0;
175 }
176 #endif
177 
178 #ifdef CONFIG_ROCKCHIP_USB_BOOT
179 static int boot_from_udisk(void)
180 {
181 	struct blk_desc *desc;
182 	char *devtype;
183 	char *devnum;
184 
185 	devtype = env_get("devtype");
186 	devnum = env_get("devnum");
187 
188 	/* Booting priority: mmc1 > udisk */
189 	if (!strcmp(devtype, "mmc") && !strcmp(devnum, "1"))
190 		return 0;
191 
192 	if (!run_command("usb start", -1)) {
193 		desc = blk_get_devnum_by_type(IF_TYPE_USB, 0);
194 		if (!desc) {
195 			printf("No usb device found\n");
196 			return -ENODEV;
197 		}
198 
199 		if (!run_command("rkimgtest usb 0", -1)) {
200 			rockchip_set_bootdev(desc);
201 			env_set("devtype", "usb");
202 			env_set("devnum", "0");
203 			printf("Boot from usb 0\n");
204 		} else {
205 			printf("No usb dev 0 found\n");
206 			return -ENODEV;
207 		}
208 	}
209 
210 	return 0;
211 }
212 #endif
213 
214 static void env_fixup(void)
215 {
216 	struct memblock mem;
217 	ulong u_addr_r;
218 	phys_size_t end;
219 	char *addr_r;
220 
221 #ifdef ENV_MEM_LAYOUT_SETTINGS1
222 	const char *env_addr0[] = {
223 		"scriptaddr", "pxefile_addr_r",
224 		"fdt_addr_r", "kernel_addr_r", "ramdisk_addr_r",
225 	};
226 	const char *env_addr1[] = {
227 		"scriptaddr1", "pxefile_addr1_r",
228 		"fdt_addr1_r", "kernel_addr1_r", "ramdisk_addr1_r",
229 	};
230 	int i;
231 
232 	/* 128M is a typical ram size for most platform, so as default here */
233 	if (gd->ram_size <= SZ_128M) {
234 		/* Replace orignal xxx_addr_r */
235 		for (i = 0; i < ARRAY_SIZE(env_addr1); i++) {
236 			addr_r = env_get(env_addr1[i]);
237 			if (addr_r)
238 				env_set(env_addr0[i], addr_r);
239 		}
240 	}
241 #endif
242 	/* If BL32 is disabled, move kernel to lower address. */
243 	if (!(gd->flags & GD_FLG_BL32_ENABLED)) {
244 		addr_r = env_get("kernel_addr_no_bl32_r");
245 		if (addr_r)
246 			env_set("kernel_addr_r", addr_r);
247 
248 		/*
249 		 * 0x0a200000 and 0x08400000 are rockchip traditional address
250 		 * of BL32 and ramdisk:
251 		 *
252 		 * |------------|------------|
253 		 * |    BL32    |  ramdisk   |
254 		 * |------------|------------|
255 		 *
256 		 * Move ramdisk to BL32 address to fix sysmem alloc failed
257 		 * issue on the board with critical memory(ie. 256MB).
258 		 */
259 		if (gd->ram_size > SZ_128M && gd->ram_size <= SZ_256M) {
260 			u_addr_r = env_get_ulong("ramdisk_addr_r", 16, 0);
261 			if (u_addr_r == 0x0a200000)
262 				env_set("ramdisk_addr_r", "0x08400000");
263 		}
264 
265 	/* If BL32 is enlarged, move ramdisk right behind it */
266 	} else {
267 		mem = param_parse_optee_mem();
268 		end = mem.base + mem.size;
269 		u_addr_r = env_get_ulong("ramdisk_addr_r", 16, 0);
270 		if (u_addr_r >= mem.base && u_addr_r < end)
271 			env_set_hex("ramdisk_addr_r", end);
272 	}
273 }
274 
275 static void cmdline_handle(void)
276 {
277 #ifdef CONFIG_ROCKCHIP_PRELOADER_ATAGS
278 	struct tag *t;
279 
280 	t = atags_get_tag(ATAG_PUB_KEY);
281 	if (t) {
282 		/* Pass if efuse/otp programmed */
283 		if (t->u.pub_key.flag == PUBKEY_FUSE_PROGRAMMED)
284 			env_update("bootargs", "fuse.programmed=1");
285 		else
286 			env_update("bootargs", "fuse.programmed=0");
287 	}
288 #endif
289 }
290 
291 int board_late_init(void)
292 {
293 	rockchip_set_ethaddr();
294 	rockchip_set_serialno();
295 	setup_download_mode();
296 #if (CONFIG_ROCKCHIP_BOOT_MODE_REG > 0)
297 	setup_boot_mode();
298 #endif
299 #ifdef CONFIG_ROCKCHIP_USB_BOOT
300 	boot_from_udisk();
301 #endif
302 #ifdef CONFIG_DM_CHARGE_DISPLAY
303 	charge_display();
304 #endif
305 #ifdef CONFIG_DRM_ROCKCHIP
306 	rockchip_show_logo();
307 #endif
308 	env_fixup();
309 	soc_clk_dump();
310 	cmdline_handle();
311 
312 	return rk_board_late_init();
313 }
314 
315 static void early_download(void)
316 {
317 #if defined(CONFIG_PWRKEY_DNL_TRIGGER_NUM) && \
318 		(CONFIG_PWRKEY_DNL_TRIGGER_NUM > 0)
319 	if (pwrkey_download_init())
320 		printf("Pwrkey download init failed\n");
321 #endif
322 
323 #if (CONFIG_ROCKCHIP_BOOT_MODE_REG > 0)
324 	if (is_hotkey(HK_BROM_DNL)) {
325 		printf("Enter bootrom download...");
326 		flushc();
327 		writel(BOOT_BROM_DOWNLOAD, CONFIG_ROCKCHIP_BOOT_MODE_REG);
328 		do_reset(NULL, 0, 0, NULL);
329 		printf("failed!\n");
330 	}
331 #endif
332 }
333 
334 static void board_debug_init(void)
335 {
336 	if (!gd->serial.using_pre_serial)
337 		board_debug_uart_init();
338 
339 	if (tstc()) {
340 		gd->console_evt = getc();
341 		if (gd->console_evt <= 0x1a) /* 'z' */
342 			printf("Hotkey: ctrl+%c\n", gd->console_evt + 'a' - 1);
343 	}
344 
345 	if (IS_ENABLED(CONFIG_CONSOLE_DISABLE_CLI))
346 		printf("CLI: off\n");
347 }
348 
349 #ifdef CONFIG_MTD_BLK
350 static void board_mtd_blk_map_partitions(void)
351 {
352 	struct blk_desc *dev_desc;
353 
354 	dev_desc = rockchip_get_bootdev();
355 	if (dev_desc)
356 		mtd_blk_map_partitions(dev_desc);
357 }
358 #endif
359 
360 int board_init(void)
361 {
362 	board_debug_init();
363 
364 #ifdef DEBUG
365 	soc_clk_dump();
366 #endif
367 
368 #ifdef CONFIG_USING_KERNEL_DTB
369 #ifdef CONFIG_MTD_BLK
370 	board_mtd_blk_map_partitions();
371 #endif
372 	init_kernel_dtb();
373 #endif
374 	early_download();
375 
376 	/*
377 	 * pmucru isn't referenced on some platforms, so pmucru driver can't
378 	 * probe that the "assigned-clocks" is unused.
379 	 */
380 	clks_probe();
381 #ifdef CONFIG_DM_REGULATOR
382 	if (regulators_enable_boot_on(is_hotkey(HK_REGULATOR)))
383 		debug("%s: Can't enable boot on regulator\n", __func__);
384 #endif
385 
386 #ifdef CONFIG_ROCKCHIP_IO_DOMAIN
387 	io_domain_init();
388 #endif
389 
390 	set_armclk_rate();
391 
392 #ifdef CONFIG_DM_DVFS
393 	dvfs_init(true);
394 #endif
395 
396 	return rk_board_init();
397 }
398 
399 int interrupt_debugger_init(void)
400 {
401 #ifdef CONFIG_ROCKCHIP_DEBUGGER
402 	return rockchip_debugger_init();
403 #else
404 	return 0;
405 #endif
406 }
407 
408 int board_fdt_fixup(void *blob)
409 {
410 	/* Common fixup for DRM */
411 #ifdef CONFIG_DRM_ROCKCHIP
412 	rockchip_display_fixup(blob);
413 #endif
414 
415 	return rk_board_fdt_fixup(blob);
416 }
417 
418 #ifdef CONFIG_ARM64_BOOT_AARCH32
419 /*
420  * Fixup MMU region attr for OP-TEE on ARMv8 CPU:
421  *
422  * What ever U-Boot is 64-bit or 32-bit mode, the OP-TEE is always 64-bit mode.
423  *
424  * Common for OP-TEE:
425  *	64-bit mode: dcache is always enabled;
426  *	32-bit mode: dcache is always disabled(Due to some unknown issue);
427  *
428  * Common for U-Boot:
429  *	64-bit mode: MMU table is static defined in rkxxx.c file, all memory
430  *		     regions are mapped. That's good to match OP-TEE MMU policy.
431  *
432  *	32-bit mode: MMU table is setup according to gd->bd->bi_dram[..] where
433  *		     the OP-TEE region has been reserved, so it can not be
434  *		     mapped(i.e. dcache is disabled). That's also good to match
435  *		     OP-TEE MMU policy.
436  *
437  * For the data coherence when communication between U-Boot and OP-TEE, U-Boot
438  * should follow OP-TEE MMU policy.
439  *
440  * Here is the special:
441  *	When CONFIG_ARM64_BOOT_AARCH32 is enabled, U-Boot is 32-bit mode while
442  *	OP-TEE is still 64-bit mode. U-Boot would not map MMU table for OP-TEE
443  *	region(but OP-TEE requires it cacheable) so we fixup here.
444  */
445 int board_initr_caches_fixup(void)
446 {
447 	struct memblock mem;
448 
449 	mem = param_parse_optee_mem();
450 	if (mem.size)
451 		mmu_set_region_dcache_behaviour(mem.base, mem.size,
452 						DCACHE_WRITEBACK);
453 	return 0;
454 }
455 #endif
456 
457 void arch_preboot_os(uint32_t bootm_state)
458 {
459 	if (bootm_state & BOOTM_STATE_OS_PREP)
460 		hotkey_run(HK_CLI_OS_PRE);
461 }
462 
463 void board_quiesce_devices(void *images)
464 {
465 	hotkey_run(HK_CMDLINE);
466 	hotkey_run(HK_CLI_OS_GO);
467 
468 #ifdef CONFIG_ROCKCHIP_PRELOADER_ATAGS
469 	/* Destroy atags makes next warm boot safer */
470 	atags_destroy();
471 #endif
472 
473 #ifdef CONFIG_FIT_ROLLBACK_PROTECT
474 	/* TODO */
475 	printf("fit: rollback protect not implement\n");
476 #endif
477 }
478 
479 void enable_caches(void)
480 {
481 	icache_enable();
482 	dcache_enable();
483 }
484 
485 #ifdef CONFIG_LMB
486 /*
487  * Using last bi_dram[...] to initialize "bootm_low" and "bootm_mapsize".
488  * This makes lmb_alloc_base() always alloc from tail of sdram.
489  * If we don't assign it, bi_dram[0] is used by default and it may cause
490  * lmb_alloc_base() fail when bi_dram[0] range is small.
491  */
492 void board_lmb_reserve(struct lmb *lmb)
493 {
494 	char bootm_mapsize[32];
495 	char bootm_low[32];
496 	u64 start, size;
497 	int i;
498 
499 	for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
500 		if (!gd->bd->bi_dram[i].size)
501 			break;
502 	}
503 
504 	start = gd->bd->bi_dram[i - 1].start;
505 	size = gd->bd->bi_dram[i - 1].size;
506 
507 	/*
508 	 * 32-bit kernel: ramdisk/fdt shouldn't be loaded to highmem area(768MB+),
509 	 * otherwise "Unable to handle kernel paging request at virtual address ...".
510 	 *
511 	 * So that we hope limit highest address at 768M, but there comes the the
512 	 * problem: ramdisk is a compressed image and it expands after descompress,
513 	 * so it accesses 768MB+ and brings the above "Unable to handle kernel ...".
514 	 *
515 	 * We make a appointment that the highest memory address is 512MB, it
516 	 * makes lmb alloc safer.
517 	 */
518 #ifndef CONFIG_ARM64
519 	if (start >= ((u64)CONFIG_SYS_SDRAM_BASE + SZ_512M)) {
520 		start = gd->bd->bi_dram[i - 2].start;
521 		size = gd->bd->bi_dram[i - 2].size;
522 	}
523 
524 	if ((start + size) > ((u64)CONFIG_SYS_SDRAM_BASE + SZ_512M))
525 		size = (u64)CONFIG_SYS_SDRAM_BASE + SZ_512M - start;
526 #endif
527 	sprintf(bootm_low, "0x%llx", start);
528 	sprintf(bootm_mapsize, "0x%llx", size);
529 	env_set("bootm_low", bootm_low);
530 	env_set("bootm_mapsize", bootm_mapsize);
531 }
532 #endif
533 
534 #ifdef CONFIG_BIDRAM
535 int board_bidram_reserve(struct bidram *bidram)
536 {
537 	struct memblock mem;
538 	int ret;
539 
540 	/* ATF */
541 	mem = param_parse_atf_mem();
542 	ret = bidram_reserve(MEM_ATF, mem.base, mem.size);
543 	if (ret)
544 		return ret;
545 
546 	/* PSTORE/ATAGS/SHM */
547 	mem = param_parse_common_resv_mem();
548 	ret = bidram_reserve(MEM_SHM, mem.base, mem.size);
549 	if (ret)
550 		return ret;
551 
552 	/* OP-TEE */
553 	mem = param_parse_optee_mem();
554 	ret = bidram_reserve(MEM_OPTEE, mem.base, mem.size);
555 	if (ret)
556 		return ret;
557 
558 	return 0;
559 }
560 
561 parse_fn_t board_bidram_parse_fn(void)
562 {
563 	return param_parse_ddr_mem;
564 }
565 #endif
566 
567 #ifdef CONFIG_ROCKCHIP_AMP
568 void cpu_secondary_init_r(void)
569 {
570 	amp_cpus_on();
571 }
572 #endif
573 
574 #if defined(CONFIG_ROCKCHIP_PRELOADER_SERIAL) && \
575     defined(CONFIG_ROCKCHIP_PRELOADER_ATAGS)
576 int board_init_f_init_serial(void)
577 {
578 	struct tag *t = atags_get_tag(ATAG_SERIAL);
579 
580 	if (t) {
581 		gd->serial.using_pre_serial = t->u.serial.enable;
582 		gd->serial.addr = t->u.serial.addr;
583 		gd->serial.baudrate = t->u.serial.baudrate;
584 		gd->serial.id = t->u.serial.id;
585 
586 		debug("%s: enable=%d, addr=0x%lx, baudrate=%d, id=%d\n",
587 		      __func__, gd->serial.using_pre_serial,
588 		      gd->serial.addr, gd->serial.baudrate,
589 		      gd->serial.id);
590 	}
591 
592 	return 0;
593 }
594 #endif
595 
596 #if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
597 #include <fdt_support.h>
598 #include <usb.h>
599 #include <usb/dwc2_udc.h>
600 
601 static struct dwc2_plat_otg_data otg_data = {
602 	.rx_fifo_sz	= 512,
603 	.np_tx_fifo_sz	= 16,
604 	.tx_fifo_sz	= 128,
605 };
606 
607 int board_usb_init(int index, enum usb_init_type init)
608 {
609 	const void *blob = gd->fdt_blob;
610 	const fdt32_t *reg;
611 	fdt_addr_t addr;
612 	int node;
613 
614 	/* find the usb_otg node */
615 	node = fdt_node_offset_by_compatible(blob, -1, "snps,dwc2");
616 
617 retry:
618 	if (node > 0) {
619 		reg = fdt_getprop(blob, node, "reg", NULL);
620 		if (!reg)
621 			return -EINVAL;
622 
623 		addr = fdt_translate_address(blob, node, reg);
624 		if (addr == OF_BAD_ADDR) {
625 			pr_err("Not found usb_otg address\n");
626 			return -EINVAL;
627 		}
628 
629 #if defined(CONFIG_ROCKCHIP_RK3288)
630 		if (addr != 0xff580000) {
631 			node = fdt_node_offset_by_compatible(blob, node,
632 							     "snps,dwc2");
633 			goto retry;
634 		}
635 #endif
636 	} else {
637 		/*
638 		 * With kernel dtb support, rk3288 dwc2 otg node
639 		 * use the rockchip legacy dwc2 driver "dwc_otg_310"
640 		 * with the compatible "rockchip,rk3288_usb20_otg",
641 		 * and rk3368 also use the "dwc_otg_310" driver with
642 		 * the compatible "rockchip,rk3368-usb".
643 		 */
644 #if defined(CONFIG_ROCKCHIP_RK3288)
645 		node = fdt_node_offset_by_compatible(blob, -1,
646 				"rockchip,rk3288_usb20_otg");
647 #elif defined(CONFIG_ROCKCHIP_RK3368)
648 		node = fdt_node_offset_by_compatible(blob, -1,
649 				"rockchip,rk3368-usb");
650 #endif
651 		if (node > 0) {
652 			goto retry;
653 		} else {
654 			pr_err("Not found usb_otg device\n");
655 			return -ENODEV;
656 		}
657 	}
658 
659 	otg_data.regs_otg = (uintptr_t)addr;
660 
661 	return dwc2_udc_probe(&otg_data);
662 }
663 
664 int board_usb_cleanup(int index, enum usb_init_type init)
665 {
666 	return 0;
667 }
668 #endif
669 
670 static void bootm_no_reloc(void)
671 {
672 	char *ramdisk_high;
673 	char *fdt_high;
674 
675 	if (!env_get_yesno("bootm-no-reloc"))
676 		return;
677 
678 	ramdisk_high = env_get("initrd_high");
679 	fdt_high = env_get("fdt_high");
680 
681 	if (!fdt_high) {
682 		env_set_hex("fdt_high", -1UL);
683 		printf("Fdt ");
684 	}
685 
686 	if (!ramdisk_high) {
687 		env_set_hex("initrd_high", -1UL);
688 		printf("Ramdisk ");
689 	}
690 
691 	if (!fdt_high || !ramdisk_high)
692 		printf("skip relocation\n");
693 }
694 
695 int bootm_board_start(void)
696 {
697 	/*
698 	 * print console record data
699 	 *
700 	 * On some rockchip platforms, uart debug and sdmmc pin are multiplex.
701 	 * If boot from sdmmc mode, the console data would be record in buffer,
702 	 * we switch to uart debug function in order to print it after loading
703 	 * images.
704 	 */
705 #if defined(CONFIG_CONSOLE_RECORD)
706 	if (!strcmp("mmc", env_get("devtype")) &&
707 	    !strcmp("1", env_get("devnum"))) {
708 		printf("IOMUX: sdmmc => uart debug");
709 		pinctrl_select_state(gd->cur_serial_dev, "default");
710 		console_record_print_purge();
711 	}
712 #endif
713 	/* disable bootm relcation to save boot time */
714 	bootm_no_reloc();
715 
716 	/* sysmem */
717 	hotkey_run(HK_SYSMEM);
718 	sysmem_overflow_check();
719 
720 	return 0;
721 }
722 
723 /*
724  * Implement it to support CLI command:
725  *   - Android: bootm [aosp addr]
726  *   - FIT:     bootm [fit addr]
727  *   - uImage:  bootm [uimage addr]
728  *
729  * Purpose:
730  *   - The original bootm command args require fdt addr on AOSP,
731  *     which is not flexible on rockchip boot/recovery.img.
732  *   - Take Android/FIT/uImage image into sysmem management to avoid image
733  *     memory overlap.
734  */
735 #if defined(CONFIG_ANDROID_BOOTLOADER) ||	\
736 	defined(CONFIG_ROCKCHIP_FIT_IMAGE) ||	\
737 	defined(CONFIG_ROCKCHIP_UIMAGE)
738 int board_do_bootm(int argc, char * const argv[])
739 {
740 	int format;
741 	void *img;
742 
743 	if (argc != 2)
744 		return 0;
745 
746 	img = (void *)simple_strtoul(argv[1], NULL, 16);
747 	format = (genimg_get_format(img));
748 
749 	/* Android */
750 #ifdef CONFIG_ANDROID_BOOT_IMAGE
751 	if (format == IMAGE_FORMAT_ANDROID) {
752 		struct andr_img_hdr *hdr;
753 		ulong load_addr;
754 		ulong size;
755 		int ret;
756 
757 		hdr = (struct andr_img_hdr *)img;
758 		printf("BOOTM: transferring to board Android\n");
759 
760 #ifdef CONFIG_USING_KERNEL_DTB
761 		sysmem_free((phys_addr_t)gd->fdt_blob);
762 		/* erase magic */
763 		fdt_set_magic((void *)gd->fdt_blob, ~0);
764 		gd->fdt_blob = NULL;
765 #endif
766 		load_addr = env_get_ulong("kernel_addr_r", 16, 0);
767 		load_addr -= hdr->page_size;
768 		size = android_image_get_end(hdr) - (ulong)hdr;
769 
770 		if (!sysmem_alloc_base(MEM_ANDROID, (ulong)hdr, size))
771 			return -ENOMEM;
772 
773 		ret = android_image_memcpy_separate(hdr, &load_addr);
774 		if (ret) {
775 			printf("board do bootm failed, ret=%d\n", ret);
776 			return ret;
777 		}
778 
779 		return android_bootloader_boot_kernel(load_addr);
780 	}
781 #endif
782 
783 	/* FIT */
784 #if IMAGE_ENABLE_FIT
785 	if (format == IMAGE_FORMAT_FIT) {
786 		char boot_cmd[64];
787 
788 		printf("BOOTM: transferring to board FIT\n");
789 		snprintf(boot_cmd, sizeof(boot_cmd), "boot_fit %s", argv[1]);
790 		return run_command(boot_cmd, 0);
791 	}
792 #endif
793 
794 	/* uImage */
795 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
796 	if (format == IMAGE_FORMAT_LEGACY &&
797 	    image_get_type(img) == IH_TYPE_MULTI) {
798 		char boot_cmd[64];
799 
800 		printf("BOOTM: transferring to board uImage\n");
801 		snprintf(boot_cmd, sizeof(boot_cmd), "boot_uimage %s", argv[1]);
802 		return run_command(boot_cmd, 0);
803 	}
804 #endif
805 
806 	return 0;
807 }
808 #endif
809 
810 void autoboot_command_fail_handle(void)
811 {
812 #ifdef CONFIG_AVB_VBMETA_PUBLIC_KEY_VALIDATE
813 #ifdef CONFIG_ANDROID_AB
814 	run_command("fastboot usb 0;", 0);  /* use fastboot to ative slot */
815 #else
816 	run_command("rockusb 0 ${devtype} ${devnum}", 0);
817 	run_command("fastboot usb 0;", 0);
818 #endif
819 #endif
820 }
821 
822 int fit_board_verify_required_sigs(void)
823 {
824 	uint8_t vboot = 0;
825 #ifdef CONFIG_OPTEE_CLIENT
826 	int ret;
827 
828 	ret = trusty_read_vbootkey_enable_flag(&vboot);
829 	if (ret) {
830 		printf("Can't read verified-boot flag\n");
831 		return 1;
832 	}
833 #endif
834 	printf("## Verified-boot: %d\n", vboot);
835 
836 	return vboot;
837 }
838 
839 #ifdef CONFIG_FIT_IMAGE_POST_PROCESS
840 void board_fit_image_post_process(void **p_image, size_t *p_size)
841 {
842 	/* Avoid overriding proccessed(overlay, hw-dtb, ...) kernel dtb */
843 #ifdef CONFIG_USING_KERNEL_DTB
844 	if (!fdt_check_header(*p_image) && !fdt_check_header(gd->fdt_blob)) {
845 		*p_image = (void *)gd->fdt_blob;
846 		*p_size = (size_t)fdt_totalsize(gd->fdt_blob);
847 	}
848 #endif
849 }
850 #endif
851