Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 1 | /* |
| 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 Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
Heiko Schocher | c884884 | 2015-04-14 09:53:13 +0200 | [diff] [blame] | 12 | #include <watchdog.h> |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 13 | #include <dfu.h> |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 14 | #include <g_dnl.h> |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 15 | #include <usb.h> |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 16 | |
| 17 | static int do_dfu(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 18 | { |
Lukasz Majewski | d9eae70 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 19 | bool dfu_reset = false; |
| 20 | |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 21 | 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 Majewski | 2968aa2 | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 28 | int ret, i = 0; |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 29 | |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 30 | ret = dfu_init_env_entities(interface, devstring); |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 31 | if (ret) |
Stephen Warren | f9d47dd | 2014-06-10 10:06:41 -0600 | [diff] [blame] | 32 | goto done; |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 33 | |
Stephen Warren | f9d47dd | 2014-06-10 10:06:41 -0600 | [diff] [blame] | 34 | ret = CMD_RET_SUCCESS; |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 35 | if (argc > 4 && strcmp(argv[4], "list") == 0) { |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 36 | dfu_show_entities(); |
| 37 | goto done; |
| 38 | } |
| 39 | |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 40 | int controller_index = simple_strtoul(usb_controller, NULL, 0); |
| 41 | board_usb_init(controller_index, USB_INIT_DEVICE); |
Rob Herring | d38f24e | 2014-12-10 14:43:03 -0600 | [diff] [blame] | 42 | g_dnl_clear_detach(); |
Mateusz Zalega | 69cb0bb | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 43 | g_dnl_register("usb_dnl_dfu"); |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 44 | while (1) { |
Rob Herring | d38f24e | 2014-12-10 14:43:03 -0600 | [diff] [blame] | 45 | if (g_dnl_detach()) { |
Lukasz Majewski | d9eae70 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 46 | /* |
| 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 Majewski | 2968aa2 | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 56 | /* |
| 57 | * This extra number of usb_gadget_handle_interrupts() |
| 58 | * calls is necessary to assure correct transmission |
| 59 | * completion with dfu-util |
| 60 | */ |
Lukasz Majewski | d9eae70 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 61 | if (++i == 10000) |
Lukasz Majewski | 2968aa2 | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 62 | goto exit; |
Lukasz Majewski | d9eae70 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 63 | } |
Lukasz Majewski | 2968aa2 | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 64 | |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 65 | if (ctrlc()) |
| 66 | goto exit; |
| 67 | |
Heiko Schocher | c884884 | 2015-04-14 09:53:13 +0200 | [diff] [blame] | 68 | WATCHDOG_RESET(); |
Kishon Vijay Abraham I | 4763e16 | 2015-02-23 18:40:23 +0530 | [diff] [blame] | 69 | usb_gadget_handle_interrupts(controller_index); |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 70 | } |
| 71 | exit: |
| 72 | g_dnl_unregister(); |
Kishon Vijay Abraham I | ae07a84 | 2015-02-23 18:40:18 +0530 | [diff] [blame] | 73 | board_usb_cleanup(controller_index, USB_INIT_DEVICE); |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 74 | done: |
| 75 | dfu_free_entities(); |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 76 | |
Lukasz Majewski | d9eae70 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 77 | if (dfu_reset) |
Lukasz Majewski | 2968aa2 | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 78 | run_command("reset", 0); |
| 79 | |
Rob Herring | d38f24e | 2014-12-10 14:43:03 -0600 | [diff] [blame] | 80 | g_dnl_clear_detach(); |
Lukasz Majewski | d9eae70 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 81 | |
Stephen Warren | f9d47dd | 2014-06-10 10:06:41 -0600 | [diff] [blame] | 82 | return ret; |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu, |
| 86 | "Device Firmware Upgrade", |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 87 | "<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 Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 92 | ); |