Merge branch 'master' of git://git.denx.de/u-boot-socfpga
[platform/kernel/u-boot.git] / cmd / rockusb.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2017 Eddie Cai <eddie.cai.linux@gmail.com>
4  */
5
6 #include <common.h>
7 #include <command.h>
8 #include <console.h>
9 #include <g_dnl.h>
10 #include <usb.h>
11 #include <asm/arch/f_rockusb.h>
12
13 static int do_rockusb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
14 {
15         int controller_index, dev_index;
16         char *usb_controller;
17         char *devtype;
18         char *devnum;
19         int ret;
20
21         if (argc < 2)
22                 return CMD_RET_USAGE;
23
24         usb_controller = argv[1];
25         controller_index = simple_strtoul(usb_controller, NULL, 0);
26
27         if (argc >= 4) {
28                 devtype = argv[2];
29                 devnum  = argv[3];
30         } else {
31                 return CMD_RET_USAGE;
32         }
33         dev_index = simple_strtoul(devnum, NULL, 0);
34         rockusb_dev_init(devtype, dev_index);
35
36         ret = board_usb_init(controller_index, USB_INIT_DEVICE);
37         if (ret) {
38                 printf("USB init failed: %d\n", ret);
39                 return CMD_RET_FAILURE;
40         }
41
42         g_dnl_clear_detach();
43         ret = g_dnl_register("usb_dnl_rockusb");
44         if (ret)
45                 return CMD_RET_FAILURE;
46
47         if (!g_dnl_board_usb_cable_connected()) {
48                 puts("\rUSB cable not detected, Command exit.\n");
49                 ret = CMD_RET_FAILURE;
50                 goto exit;
51         }
52
53         while (1) {
54                 if (g_dnl_detach())
55                         break;
56                 if (ctrlc())
57                         break;
58                 usb_gadget_handle_interrupts(controller_index);
59         }
60         ret = CMD_RET_SUCCESS;
61
62 exit:
63         g_dnl_unregister();
64         g_dnl_clear_detach();
65         board_usb_cleanup(controller_index, USB_INIT_DEVICE);
66
67         return ret;
68 }
69
70 U_BOOT_CMD(rockusb, 4, 1, do_rockusb,
71            "use the rockusb protocol",
72            "<USB_controller> <devtype> <dev[:part]>  e.g. rockusb 0 mmc 0\n"
73 );