Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Eddie Cai | 5e3020b | 2017-12-15 08:17:11 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2017 Eddie Cai <eddie.cai.linux@gmail.com> |
Eddie Cai | 5e3020b | 2017-12-15 08:17:11 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <command.h> |
| 8 | #include <console.h> |
| 9 | #include <g_dnl.h> |
| 10 | #include <usb.h> |
Kever Yang | 9fbe17c | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 11 | #include <asm/arch-rockchip/f_rockusb.h> |
Eddie Cai | 5e3020b | 2017-12-15 08:17:11 +0800 | [diff] [blame] | 12 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 13 | static int do_rockusb(struct cmd_tbl *cmdtp, int flag, int argc, |
| 14 | char *const argv[]) |
Eddie Cai | 5e3020b | 2017-12-15 08:17:11 +0800 | [diff] [blame] | 15 | { |
| 16 | int controller_index, dev_index; |
| 17 | char *usb_controller; |
Marek Vasut | fdf2ed7 | 2023-09-01 11:49:51 +0200 | [diff] [blame] | 18 | struct udevice *udc; |
Eddie Cai | 5e3020b | 2017-12-15 08:17:11 +0800 | [diff] [blame] | 19 | char *devtype; |
| 20 | char *devnum; |
| 21 | int ret; |
| 22 | |
| 23 | if (argc < 2) |
| 24 | return CMD_RET_USAGE; |
| 25 | |
| 26 | usb_controller = argv[1]; |
| 27 | controller_index = simple_strtoul(usb_controller, NULL, 0); |
| 28 | |
| 29 | if (argc >= 4) { |
| 30 | devtype = argv[2]; |
| 31 | devnum = argv[3]; |
| 32 | } else { |
| 33 | return CMD_RET_USAGE; |
| 34 | } |
| 35 | dev_index = simple_strtoul(devnum, NULL, 0); |
| 36 | rockusb_dev_init(devtype, dev_index); |
| 37 | |
Marek Vasut | fdf2ed7 | 2023-09-01 11:49:51 +0200 | [diff] [blame] | 38 | ret = udc_device_get_by_index(controller_index, &udc); |
Eddie Cai | 5e3020b | 2017-12-15 08:17:11 +0800 | [diff] [blame] | 39 | if (ret) { |
| 40 | printf("USB init failed: %d\n", ret); |
| 41 | return CMD_RET_FAILURE; |
| 42 | } |
| 43 | |
| 44 | g_dnl_clear_detach(); |
| 45 | ret = g_dnl_register("usb_dnl_rockusb"); |
| 46 | if (ret) |
| 47 | return CMD_RET_FAILURE; |
| 48 | |
| 49 | if (!g_dnl_board_usb_cable_connected()) { |
| 50 | puts("\rUSB cable not detected, Command exit.\n"); |
| 51 | ret = CMD_RET_FAILURE; |
| 52 | goto exit; |
| 53 | } |
| 54 | |
| 55 | while (1) { |
| 56 | if (g_dnl_detach()) |
| 57 | break; |
| 58 | if (ctrlc()) |
| 59 | break; |
Marek Vasut | fdf2ed7 | 2023-09-01 11:49:51 +0200 | [diff] [blame] | 60 | dm_usb_gadget_handle_interrupts(udc); |
Eddie Cai | 5e3020b | 2017-12-15 08:17:11 +0800 | [diff] [blame] | 61 | } |
| 62 | ret = CMD_RET_SUCCESS; |
| 63 | |
| 64 | exit: |
| 65 | g_dnl_unregister(); |
| 66 | g_dnl_clear_detach(); |
Marek Vasut | fdf2ed7 | 2023-09-01 11:49:51 +0200 | [diff] [blame] | 67 | udc_device_put(udc); |
Eddie Cai | 5e3020b | 2017-12-15 08:17:11 +0800 | [diff] [blame] | 68 | |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | U_BOOT_CMD(rockusb, 4, 1, do_rockusb, |
| 73 | "use the rockusb protocol", |
| 74 | "<USB_controller> <devtype> <dev[:part]> e.g. rockusb 0 mmc 0\n" |
| 75 | ); |