blob: 857148f8afef2562098358bc6615506d14716fc3 [file] [log] [blame]
Lukasz Majewski9273f4f2012-08-06 14:41:09 +02001/*
2 * cmd_dfu.c -- dfu command
3 *
4 * Copyright (C) 2012 Samsung Electronics
5 * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
6 * Lukasz Majewski <l.majewski@samsung.com>
7 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Lukasz Majewski9273f4f2012-08-06 14:41:09 +02009 */
10
11#include <common.h>
Heiko Schocherc8848842015-04-14 09:53:13 +020012#include <watchdog.h>
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020013#include <dfu.h>
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020014#include <g_dnl.h>
Mateusz Zalegad862f892013-10-04 19:22:26 +020015#include <usb.h>
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020016
17static int do_dfu(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
18{
Lukasz Majewskid9eae702014-08-25 11:07:28 +020019 bool dfu_reset = false;
20
Mateusz Zalegad862f892013-10-04 19:22:26 +020021 if (argc < 4)
22 return CMD_RET_USAGE;
23
24 char *usb_controller = argv[1];
25 char *interface = argv[2];
26 char *devstring = argv[3];
27
Lukasz Majewski2968aa22013-07-18 13:19:14 +020028 int ret, i = 0;
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020029
Stephen Warren4afe50f2014-06-11 16:03:33 -060030 ret = dfu_init_env_entities(interface, devstring);
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020031 if (ret)
Stephen Warrenf9d47dd2014-06-10 10:06:41 -060032 goto done;
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020033
Stephen Warrenf9d47dd2014-06-10 10:06:41 -060034 ret = CMD_RET_SUCCESS;
Mateusz Zalegad862f892013-10-04 19:22:26 +020035 if (argc > 4 && strcmp(argv[4], "list") == 0) {
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020036 dfu_show_entities();
37 goto done;
38 }
39
Mateusz Zalegad862f892013-10-04 19:22:26 +020040 int controller_index = simple_strtoul(usb_controller, NULL, 0);
41 board_usb_init(controller_index, USB_INIT_DEVICE);
Rob Herringd38f24e2014-12-10 14:43:03 -060042 g_dnl_clear_detach();
Mateusz Zalega69cb0bb2014-04-28 21:13:28 +020043 g_dnl_register("usb_dnl_dfu");
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020044 while (1) {
Rob Herringd38f24e2014-12-10 14:43:03 -060045 if (g_dnl_detach()) {
Lukasz Majewskid9eae702014-08-25 11:07:28 +020046 /*
47 * Check if USB bus reset is performed after detach,
48 * which indicates that -R switch has been passed to
49 * dfu-util. In this case reboot the device
50 */
51 if (dfu_usb_get_reset()) {
52 dfu_reset = true;
53 goto exit;
54 }
55
Lukasz Majewski2968aa22013-07-18 13:19:14 +020056 /*
57 * This extra number of usb_gadget_handle_interrupts()
58 * calls is necessary to assure correct transmission
59 * completion with dfu-util
60 */
Lukasz Majewskid9eae702014-08-25 11:07:28 +020061 if (++i == 10000)
Lukasz Majewski2968aa22013-07-18 13:19:14 +020062 goto exit;
Lukasz Majewskid9eae702014-08-25 11:07:28 +020063 }
Lukasz Majewski2968aa22013-07-18 13:19:14 +020064
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020065 if (ctrlc())
66 goto exit;
67
Heiko Schocherc8848842015-04-14 09:53:13 +020068 WATCHDOG_RESET();
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +053069 usb_gadget_handle_interrupts(controller_index);
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020070 }
71exit:
72 g_dnl_unregister();
Kishon Vijay Abraham Iae07a842015-02-23 18:40:18 +053073 board_usb_cleanup(controller_index, USB_INIT_DEVICE);
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020074done:
75 dfu_free_entities();
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020076
Lukasz Majewskid9eae702014-08-25 11:07:28 +020077 if (dfu_reset)
Lukasz Majewski2968aa22013-07-18 13:19:14 +020078 run_command("reset", 0);
79
Rob Herringd38f24e2014-12-10 14:43:03 -060080 g_dnl_clear_detach();
Lukasz Majewskid9eae702014-08-25 11:07:28 +020081
Stephen Warrenf9d47dd2014-06-10 10:06:41 -060082 return ret;
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020083}
84
85U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu,
86 "Device Firmware Upgrade",
Mateusz Zalegad862f892013-10-04 19:22:26 +020087 "<USB_controller> <interface> <dev> [list]\n"
88 " - device firmware upgrade via <USB_controller>\n"
89 " on device <dev>, attached to interface\n"
90 " <interface>\n"
91 " [list] - list available alt settings\n"
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020092);