1 // SPDX-License-Identifier: GPL-2.0+
4 * Driver interface derived from:
6 * Copyright (c) 2012, Google Inc.
16 #include <dm/device-internal.h>
17 #include <dm/uclass-internal.h>
19 static int do_rkmtd_bind(struct cmd_tbl *cmdtp, int flag, int argc,
36 ret = rkmtd_create_attach_mtd(label, &dev);
38 printf("Cannot create device / bind mtd\n");
39 return CMD_RET_FAILURE;
45 static struct udevice *parse_rkmtd_label(const char *label)
49 dev = rkmtd_find_by_label(label);
54 devnum = hextoul(label, &ep);
56 uclass_find_device_by_seq(UCLASS_RKMTD, devnum, &dev)) {
57 printf("No such device '%s'\n", label);
65 static int do_rkmtd_unbind(struct cmd_tbl *cmdtp, int flag, int argc,
76 dev = parse_rkmtd_label(label);
78 return CMD_RET_FAILURE;
80 ret = rkmtd_detach(dev);
82 printf("Cannot detach mtd\n");
83 return CMD_RET_FAILURE;
86 ret = device_unbind(dev);
88 printf("Cannot unbind device '%s'\n", dev->name);
89 return CMD_RET_FAILURE;
95 static void show_rkmtd_dev(struct udevice *dev)
97 struct rkmtd_dev *plat = dev_get_plat(dev);
98 struct blk_desc *desc;
102 printf("%3d ", dev_seq(dev));
104 ret = blk_get_from_parent(dev, &blk);
108 desc = dev_get_uclass_plat(blk);
109 printf("%12lu %-15s\n", (unsigned long)desc->lba, plat->label);
112 static int do_rkmtd_info(struct cmd_tbl *cmdtp, int flag, int argc,
118 return CMD_RET_USAGE;
122 dev = parse_rkmtd_label(argv[1]);
124 return CMD_RET_FAILURE;
127 printf("%3s %12s %-15s\n", "dev", "blocks", "label");
133 uclass_id_foreach_dev(UCLASS_RKMTD, dev, uc)
140 static int do_rkmtd_dev(struct cmd_tbl *cmdtp, int flag, int argc,
146 if (argc < 1 || argc > 3)
147 return CMD_RET_USAGE;
150 struct rkmtd_dev *plat;
152 dev = rkmtd_get_cur_dev();
154 printf("No current rkmtd device\n");
155 return CMD_RET_FAILURE;
157 plat = dev_get_plat(dev);
158 printf("Current rkmtd device: %d: %s\n", dev_seq(dev),
164 dev = parse_rkmtd_label(argv[1]);
166 return CMD_RET_FAILURE;
168 rkmtd_set_cur_dev(dev);
173 static struct cmd_tbl cmd_rkmtd_sub[] = {
174 U_BOOT_CMD_MKENT(bind, 4, 0, do_rkmtd_bind, "", ""),
175 U_BOOT_CMD_MKENT(unbind, 4, 0, do_rkmtd_unbind, "", ""),
176 U_BOOT_CMD_MKENT(info, 3, 0, do_rkmtd_info, "", ""),
177 U_BOOT_CMD_MKENT(dev, 0, 1, do_rkmtd_dev, "", ""),
180 static int do_rkmtd(struct cmd_tbl *cmdtp, int flag, int argc,
188 c = find_cmd_tbl(argv[0], cmd_rkmtd_sub, ARRAY_SIZE(cmd_rkmtd_sub));
191 return c->cmd(cmdtp, flag, argc, argv);
193 return CMD_RET_USAGE;
197 rkmtd, 8, 1, do_rkmtd,
198 "Rockchip MTD sub-system",
199 "bind <label> - bind RKMTD device\n"
200 "rkmtd unbind <label> - unbind RKMTD device\n"
201 "rkmtd info [<label>] - show all available RKMTD devices\n"
202 "rkmtd dev [<label>] - show or set current RKMTD device\n"