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