blob: 857148f8afef2562098358bc6615506d14716fc3 [file] [log] [blame]
Lukasz Majewskia006a5d2012-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 Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskia006a5d2012-08-06 14:41:09 +02009 */
10
11#include <common.h>
Heiko Schocher0a9ac5c2015-04-14 09:53:13 +020012#include <watchdog.h>
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020013#include <dfu.h>
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020014#include <g_dnl.h>
Mateusz Zalega16297cf2013-10-04 19:22:26 +020015#include <usb.h>
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020016
17static int do_dfu(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
18{
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020019 bool dfu_reset = false;
20
Mateusz Zalega16297cf2013-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 Majewski6bed7ce2013-07-18 13:19:14 +020028 int ret, i = 0;
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020029
Stephen Warrendd648272014-06-11 16:03:33 -060030 ret = dfu_init_env_entities(interface, devstring);
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020031 if (ret)
Stephen Warrenafb8e712014-06-10 10:06:41 -060032 goto done;
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020033
Stephen Warrenafb8e712014-06-10 10:06:41 -060034 ret = CMD_RET_SUCCESS;
Mateusz Zalega16297cf2013-10-04 19:22:26 +020035 if (argc > 4 && strcmp(argv[4], "list") == 0) {
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020036 dfu_show_entities();
37 goto done;
38 }
39
Mateusz Zalega16297cf2013-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 Herringfe1b28c2014-12-10 14:43:03 -060042 g_dnl_clear_detach();
Mateusz Zalegac4d0e852014-04-28 21:13:28 +020043 g_dnl_register("usb_dnl_dfu");
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020044 while (1) {
Rob Herringfe1b28c2014-12-10 14:43:03 -060045 if (g_dnl_detach()) {
Lukasz Majewski1cc03c52014-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 Majewski6bed7ce2013-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 Majewski1cc03c52014-08-25 11:07:28 +020061 if (++i == 10000)
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020062 goto exit;
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020063 }
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020064
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020065 if (ctrlc())
66 goto exit;
67
Heiko Schocher0a9ac5c2015-04-14 09:53:13 +020068 WATCHDOG_RESET();
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +053069 usb_gadget_handle_interrupts(controller_index);
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020070 }
71exit:
72 g_dnl_unregister();
Kishon Vijay Abraham Idb378d72015-02-23 18:40:18 +053073 board_usb_cleanup(controller_index, USB_INIT_DEVICE);
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020074done:
75 dfu_free_entities();
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020076
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020077 if (dfu_reset)
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020078 run_command("reset", 0);
79
Rob Herringfe1b28c2014-12-10 14:43:03 -060080 g_dnl_clear_detach();
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020081
Stephen Warrenafb8e712014-06-10 10:06:41 -060082 return ret;
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020083}
84
85U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu,
86 "Device Firmware Upgrade",
Mateusz Zalega16297cf2013-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 Majewskia006a5d2012-08-06 14:41:09 +020092);