1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2020 Wind River Systems, Inc.
6 * Bin Meng <bin.meng@windriver.com>
8 * A command interface to access misc devices with MISC uclass driver APIs.
22 static char *misc_op_str[] = {
27 static int do_misc_list(struct cmd_tbl *cmdtp, int flag,
28 int argc, char *const argv[])
32 printf("Device Index Driver\n");
33 printf("-------------------------------------\n");
34 for (uclass_first_device(UCLASS_MISC, &dev);
36 uclass_next_device(&dev)) {
37 printf("%-20s %5d %10s\n", dev->name, dev_seq(dev),
44 static int do_misc_op(struct cmd_tbl *cmdtp, int flag,
45 int argc, char *const argv[], enum misc_op op)
47 int (*misc_op)(struct udevice *, int, void *, int);
54 ret = uclass_get_device_by_name(UCLASS_MISC, argv[0], &dev);
56 printf("Unable to find device %s\n", argv[0]);
60 offset = hextoul(argv[1], NULL);
61 buf = (void *)hextoul(argv[2], NULL);
62 size = hextoul(argv[3], NULL);
64 if (op == MISC_OP_READ)
69 ret = misc_op(dev, offset, buf, size);
72 printf("The device does not support %s\n",
80 printf("Partially %s %d bytes\n", misc_op_str[op], ret);
86 static int do_misc_read(struct cmd_tbl *cmdtp, int flag,
87 int argc, char *const argv[])
89 return do_misc_op(cmdtp, flag, argc, argv, MISC_OP_READ);
92 static int do_misc_write(struct cmd_tbl *cmdtp, int flag,
93 int argc, char *const argv[])
95 return do_misc_op(cmdtp, flag, argc, argv, MISC_OP_WRITE);
98 static struct cmd_tbl misc_commands[] = {
99 U_BOOT_CMD_MKENT(list, 0, 1, do_misc_list, "", ""),
100 U_BOOT_CMD_MKENT(read, 4, 1, do_misc_read, "", ""),
101 U_BOOT_CMD_MKENT(write, 4, 1, do_misc_write, "", ""),
104 static int do_misc(struct cmd_tbl *cmdtp, int flag,
105 int argc, char *const argv[])
107 struct cmd_tbl *misc_cmd;
111 return CMD_RET_USAGE;
112 misc_cmd = find_cmd_tbl(argv[1], misc_commands,
113 ARRAY_SIZE(misc_commands));
116 if (!misc_cmd || argc != misc_cmd->maxargs)
117 return CMD_RET_USAGE;
119 ret = misc_cmd->cmd(misc_cmd, flag, argc, argv);
121 return cmd_process_error(misc_cmd, ret);
126 "Access miscellaneous devices with MISC uclass driver APIs",
127 "list - list all miscellaneous devices\n"
128 "misc read name offset addr len - read `len' bytes starting at\n"
129 " `offset' of device `name'\n"
130 " to memory at `addr'\n"
131 "misc write name offset addr len - write `len' bytes starting at\n"
132 " `offset' of device `name'\n"
133 " from memory at `addr'"