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