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