xref: /rk3399_rockchip-uboot/cmd/fastboot.c (revision cfb50d9acf53f8d5c833d9da69d239789c5ecdb6)
1 /*
2  * Copyright 2008 - 2009 Windriver, <www.windriver.com>
3  * Author: Tom Rix <Tom.Rix@windriver.com>
4  *
5  * (C) Copyright 2014 Linaro, Ltd.
6  * Rob Herring <robh@kernel.org>
7  *
8  * SPDX-License-Identifier:	GPL-2.0+
9  */
10 #include <common.h>
11 #include <command.h>
12 #include <console.h>
13 #include <g_dnl.h>
14 #include <net.h>
15 #include <usb.h>
16 
17 static int do_fastboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
18 {
19 #ifdef CONFIG_USB_FUNCTION_FASTBOOT
20 	int controller_index;
21 	char *usb_controller;
22 	int ret;
23 #endif
24 
25 	if (argc < 2)
26 		return CMD_RET_USAGE;
27 
28 	printf("Enter fastboot...");
29 
30 	if (!strcmp(argv[1], "udp")) {
31 #ifndef CONFIG_UDP_FUNCTION_FASTBOOT
32 		pr_err("Fastboot UDP not enabled\n");
33 		return -1;
34 #else
35 		return do_fastboot_udp(cmdtp, flag, argc, argv);
36 #endif
37 	}
38 
39 	if (strcmp(argv[1], "usb") || argc < 3)
40 		return CMD_RET_USAGE;
41 
42 #ifndef CONFIG_USB_FUNCTION_FASTBOOT
43 	pr_err("Fastboot USB not enabled\n");
44 	return -1;
45 #else
46 
47 	usb_controller = argv[2];
48 	controller_index = simple_strtoul(usb_controller, NULL, 0);
49 
50 	ret = board_usb_init(controller_index, USB_INIT_DEVICE);
51 	if (ret) {
52 		pr_err("USB init failed: %d", ret);
53 		return CMD_RET_FAILURE;
54 	}
55 
56 	g_dnl_clear_detach();
57 	ret = g_dnl_register("usb_dnl_fastboot");
58 	if (ret)
59 		return ret;
60 
61 	if (!g_dnl_board_usb_cable_connected()) {
62 		puts("\rUSB cable not detected.\n" \
63 		     "Command exit.\n");
64 		ret = CMD_RET_FAILURE;
65 		goto exit;
66 	}
67 
68 	printf("OK\n");
69 
70 	while (1) {
71 		if (g_dnl_detach())
72 			break;
73 		if (ctrlc())
74 			break;
75 		usb_gadget_handle_interrupts(controller_index);
76 	}
77 
78 	ret = CMD_RET_SUCCESS;
79 
80 exit:
81 	g_dnl_unregister();
82 	g_dnl_clear_detach();
83 	board_usb_cleanup(controller_index, USB_INIT_DEVICE);
84 
85 	return ret;
86 #endif
87 }
88 
89 U_BOOT_CMD(
90 	fastboot, 3, 1, do_fastboot,
91 	"use USB or UDP Fastboot protocol",
92 	"[usb,udp] <USB_controller>\n"
93 	" - run as a fastboot usb or udp device\n"
94 	"   usb: specify <USB_controller>\n"
95 	"   udp: requires ip_addr set and ethernet initialized\n"
96 );
97