xref: /OK3568_Linux_fs/u-boot/common/dfu.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * dfu.c -- dfu command
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (C) 2015
5*4882a593Smuzhiyun  * Lukasz Majewski <l.majewski@majess.pl>
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (C) 2012 Samsung Electronics
8*4882a593Smuzhiyun  * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
9*4882a593Smuzhiyun  *	    Lukasz Majewski <l.majewski@samsung.com>
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
12*4882a593Smuzhiyun  */
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <common.h>
15*4882a593Smuzhiyun #include <watchdog.h>
16*4882a593Smuzhiyun #include <dfu.h>
17*4882a593Smuzhiyun #include <console.h>
18*4882a593Smuzhiyun #include <g_dnl.h>
19*4882a593Smuzhiyun #include <usb.h>
20*4882a593Smuzhiyun #include <net.h>
21*4882a593Smuzhiyun #include <android_avb/rk_avb_ops_user.h>
22*4882a593Smuzhiyun 
run_usb_dnl_gadget(int usbctrl_index,char * usb_dnl_gadget)23*4882a593Smuzhiyun int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun 	bool dfu_reset = false;
26*4882a593Smuzhiyun 	int ret, i = 0;
27*4882a593Smuzhiyun #ifdef CONFIG_ANDROID_AB
28*4882a593Smuzhiyun 	char select_slot[3] = {0};
29*4882a593Smuzhiyun 	unsigned int slot_number[2] = {0, 1};
30*4882a593Smuzhiyun #endif
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 	ret = usb_gadget_initialize(usbctrl_index);
33*4882a593Smuzhiyun 	if (ret) {
34*4882a593Smuzhiyun 		pr_err("usb_gadget_initialize failed\n");
35*4882a593Smuzhiyun 		return CMD_RET_FAILURE;
36*4882a593Smuzhiyun 	}
37*4882a593Smuzhiyun 	g_dnl_clear_detach();
38*4882a593Smuzhiyun 	ret = g_dnl_register(usb_dnl_gadget);
39*4882a593Smuzhiyun 	if (ret) {
40*4882a593Smuzhiyun 		pr_err("g_dnl_register failed");
41*4882a593Smuzhiyun 		return CMD_RET_FAILURE;
42*4882a593Smuzhiyun 	}
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	while (1) {
45*4882a593Smuzhiyun 		if (g_dnl_detach()) {
46*4882a593Smuzhiyun 			/*
47*4882a593Smuzhiyun 			 * Check if USB bus reset is performed after detach,
48*4882a593Smuzhiyun 			 * which indicates that -R switch has been passed to
49*4882a593Smuzhiyun 			 * dfu-util. In this case reboot the device
50*4882a593Smuzhiyun 			 */
51*4882a593Smuzhiyun 			if (dfu_usb_get_reset()) {
52*4882a593Smuzhiyun 				dfu_reset = true;
53*4882a593Smuzhiyun #ifdef CONFIG_ANDROID_AB
54*4882a593Smuzhiyun 				if (rk_avb_get_current_slot(select_slot))
55*4882a593Smuzhiyun 					printf("Obtain current slot failed!\n");
56*4882a593Smuzhiyun 				/*
57*4882a593Smuzhiyun 				 * After the firmware is successfully upgrade,
58*4882a593Smuzhiyun 				 * the device changes the slot priority during
59*4882a593Smuzhiyun 				 * reboot based on the current slot
60*4882a593Smuzhiyun 				 */
61*4882a593Smuzhiyun 				if (strcmp(select_slot, "_a") == 0)
62*4882a593Smuzhiyun 					rk_avb_set_slot_active(&slot_number[1]);
63*4882a593Smuzhiyun 				else
64*4882a593Smuzhiyun 					rk_avb_set_slot_active(&slot_number[0]);
65*4882a593Smuzhiyun #endif
66*4882a593Smuzhiyun 				goto exit;
67*4882a593Smuzhiyun 			}
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 			/*
70*4882a593Smuzhiyun 			 * This extra number of usb_gadget_handle_interrupts()
71*4882a593Smuzhiyun 			 * calls is necessary to assure correct transmission
72*4882a593Smuzhiyun 			 * completion with dfu-util
73*4882a593Smuzhiyun 			 */
74*4882a593Smuzhiyun 			if (++i == 10000)
75*4882a593Smuzhiyun 				goto exit;
76*4882a593Smuzhiyun 		}
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 		if (ctrlc())
79*4882a593Smuzhiyun 			goto exit;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 		if (dfu_get_defer_flush()) {
82*4882a593Smuzhiyun 			/*
83*4882a593Smuzhiyun 			 * Call to usb_gadget_handle_interrupts() is necessary
84*4882a593Smuzhiyun 			 * to act on ZLP OUT transaction from HOST PC after
85*4882a593Smuzhiyun 			 * transmitting the whole file.
86*4882a593Smuzhiyun 			 *
87*4882a593Smuzhiyun 			 * If this ZLP OUT packet is NAK'ed, the HOST libusb
88*4882a593Smuzhiyun 			 * function fails after timeout (by default it is set to
89*4882a593Smuzhiyun 			 * 5 seconds). In such situation the dfu-util program
90*4882a593Smuzhiyun 			 * exits with error message.
91*4882a593Smuzhiyun 			 */
92*4882a593Smuzhiyun 			usb_gadget_handle_interrupts(usbctrl_index);
93*4882a593Smuzhiyun 			ret = dfu_flush(dfu_get_defer_flush(), NULL, 0, 0);
94*4882a593Smuzhiyun 			dfu_set_defer_flush(NULL);
95*4882a593Smuzhiyun 			if (ret) {
96*4882a593Smuzhiyun 				pr_err("Deferred dfu_flush() failed!");
97*4882a593Smuzhiyun 				goto exit;
98*4882a593Smuzhiyun 			}
99*4882a593Smuzhiyun 		}
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 		WATCHDOG_RESET();
102*4882a593Smuzhiyun 		usb_gadget_handle_interrupts(usbctrl_index);
103*4882a593Smuzhiyun 	}
104*4882a593Smuzhiyun exit:
105*4882a593Smuzhiyun 	g_dnl_unregister();
106*4882a593Smuzhiyun 	usb_gadget_release(usbctrl_index);
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	if (dfu_reset)
109*4882a593Smuzhiyun 		do_reset(NULL, 0, 0, NULL);
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	g_dnl_clear_detach();
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	return ret;
114*4882a593Smuzhiyun }
115