Johan Jonker | ff1d73f | 2023-10-18 16:01:40 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * |
| 4 | * Driver interface derived from: |
| 5 | * /cmd/host.c |
| 6 | * Copyright (c) 2012, Google Inc. |
| 7 | * |
| 8 | * Copyright (C) 2023 Johan Jonker <jbx6244@gmail.com> |
| 9 | */ |
| 10 | |
Johan Jonker | ff1d73f | 2023-10-18 16:01:40 +0200 | [diff] [blame] | 11 | #include <blk.h> |
| 12 | #include <command.h> |
| 13 | #include <dm.h> |
| 14 | #include <rkmtd.h> |
| 15 | #include <stdio.h> |
| 16 | #include <dm/device-internal.h> |
| 17 | #include <dm/uclass-internal.h> |
| 18 | |
| 19 | static int do_rkmtd_bind(struct cmd_tbl *cmdtp, int flag, int argc, |
| 20 | char *const argv[]) |
| 21 | { |
| 22 | struct udevice *dev; |
| 23 | const char *label; |
| 24 | int ret; |
| 25 | |
| 26 | argc--; |
| 27 | argv++; |
| 28 | |
| 29 | if (argc < 1) |
| 30 | return CMD_RET_USAGE; |
| 31 | |
| 32 | if (argc > 1) |
| 33 | return CMD_RET_USAGE; |
| 34 | |
| 35 | label = argv[0]; |
| 36 | ret = rkmtd_create_attach_mtd(label, &dev); |
| 37 | if (ret) { |
| 38 | printf("Cannot create device / bind mtd\n"); |
| 39 | return CMD_RET_FAILURE; |
| 40 | } |
| 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | static struct udevice *parse_rkmtd_label(const char *label) |
| 46 | { |
| 47 | struct udevice *dev; |
| 48 | |
| 49 | dev = rkmtd_find_by_label(label); |
| 50 | if (!dev) { |
| 51 | int devnum; |
| 52 | char *ep; |
| 53 | |
| 54 | devnum = hextoul(label, &ep); |
| 55 | if (*ep || |
| 56 | uclass_find_device_by_seq(UCLASS_RKMTD, devnum, &dev)) { |
| 57 | printf("No such device '%s'\n", label); |
| 58 | return NULL; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return dev; |
| 63 | } |
| 64 | |
| 65 | static int do_rkmtd_unbind(struct cmd_tbl *cmdtp, int flag, int argc, |
| 66 | char *const argv[]) |
| 67 | { |
| 68 | struct udevice *dev; |
| 69 | const char *label; |
| 70 | int ret; |
| 71 | |
| 72 | if (argc < 2) |
| 73 | return CMD_RET_USAGE; |
| 74 | |
| 75 | label = argv[1]; |
| 76 | dev = parse_rkmtd_label(label); |
| 77 | if (!dev) |
| 78 | return CMD_RET_FAILURE; |
| 79 | |
| 80 | ret = rkmtd_detach(dev); |
| 81 | if (ret) { |
| 82 | printf("Cannot detach mtd\n"); |
| 83 | return CMD_RET_FAILURE; |
| 84 | } |
| 85 | |
| 86 | ret = device_unbind(dev); |
| 87 | if (ret) { |
| 88 | printf("Cannot unbind device '%s'\n", dev->name); |
| 89 | return CMD_RET_FAILURE; |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static void show_rkmtd_dev(struct udevice *dev) |
| 96 | { |
| 97 | struct rkmtd_dev *plat = dev_get_plat(dev); |
| 98 | struct blk_desc *desc; |
| 99 | struct udevice *blk; |
| 100 | int ret; |
| 101 | |
| 102 | printf("%3d ", dev_seq(dev)); |
| 103 | |
| 104 | ret = blk_get_from_parent(dev, &blk); |
| 105 | if (ret) |
| 106 | return; |
| 107 | |
| 108 | desc = dev_get_uclass_plat(blk); |
| 109 | printf("%12lu %-15s\n", (unsigned long)desc->lba, plat->label); |
| 110 | } |
| 111 | |
| 112 | static int do_rkmtd_info(struct cmd_tbl *cmdtp, int flag, int argc, |
| 113 | char *const argv[]) |
| 114 | { |
| 115 | struct udevice *dev; |
| 116 | |
| 117 | if (argc < 1) |
| 118 | return CMD_RET_USAGE; |
| 119 | |
| 120 | dev = NULL; |
| 121 | if (argc >= 2) { |
| 122 | dev = parse_rkmtd_label(argv[1]); |
| 123 | if (!dev) |
| 124 | return CMD_RET_FAILURE; |
| 125 | } |
| 126 | |
| 127 | printf("%3s %12s %-15s\n", "dev", "blocks", "label"); |
| 128 | if (dev) { |
| 129 | show_rkmtd_dev(dev); |
| 130 | } else { |
| 131 | struct uclass *uc; |
| 132 | |
| 133 | uclass_id_foreach_dev(UCLASS_RKMTD, dev, uc) |
| 134 | show_rkmtd_dev(dev); |
| 135 | } |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static int do_rkmtd_dev(struct cmd_tbl *cmdtp, int flag, int argc, |
| 141 | char *const argv[]) |
| 142 | { |
| 143 | struct udevice *dev; |
| 144 | const char *label; |
| 145 | |
| 146 | if (argc < 1 || argc > 3) |
| 147 | return CMD_RET_USAGE; |
| 148 | |
| 149 | if (argc == 1) { |
| 150 | struct rkmtd_dev *plat; |
| 151 | |
| 152 | dev = rkmtd_get_cur_dev(); |
| 153 | if (!dev) { |
| 154 | printf("No current rkmtd device\n"); |
| 155 | return CMD_RET_FAILURE; |
| 156 | } |
| 157 | plat = dev_get_plat(dev); |
| 158 | printf("Current rkmtd device: %d: %s\n", dev_seq(dev), |
| 159 | plat->label); |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | label = argv[1]; |
| 164 | dev = parse_rkmtd_label(argv[1]); |
| 165 | if (!dev) |
| 166 | return CMD_RET_FAILURE; |
| 167 | |
| 168 | rkmtd_set_cur_dev(dev); |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 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, "", ""), |
| 178 | }; |
| 179 | |
| 180 | static int do_rkmtd(struct cmd_tbl *cmdtp, int flag, int argc, |
| 181 | char *const argv[]) |
| 182 | { |
| 183 | struct cmd_tbl *c; |
| 184 | |
| 185 | argc--; |
| 186 | argv++; |
| 187 | |
| 188 | c = find_cmd_tbl(argv[0], cmd_rkmtd_sub, ARRAY_SIZE(cmd_rkmtd_sub)); |
| 189 | |
| 190 | if (c) |
| 191 | return c->cmd(cmdtp, flag, argc, argv); |
| 192 | else |
| 193 | return CMD_RET_USAGE; |
| 194 | } |
| 195 | |
| 196 | U_BOOT_CMD( |
| 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" |
| 203 | ); |