3 * Kyle Harris, kharris@nexus-tech.net
5 * SPDX-License-Identifier: GPL-2.0+
12 static int curr_device = -1;
13 #ifndef CONFIG_GENERIC_MMC
14 int do_mmc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
21 if (strcmp(argv[1], "init") == 0) {
27 } else if (argc == 3) {
28 dev = (int)simple_strtoul(argv[2], NULL, 10);
33 if (mmc_legacy_init(dev) != 0) {
34 puts("No MMC card found\n");
39 printf("mmc%d is available\n", curr_device);
40 } else if (strcmp(argv[1], "device") == 0) {
42 if (curr_device < 0) {
43 puts("No MMC device available\n");
46 } else if (argc == 3) {
47 dev = (int)simple_strtoul(argv[2], NULL, 10);
49 #ifdef CONFIG_SYS_MMC_SET_DEV
50 if (mmc_set_dev(dev) != 0)
58 printf("mmc%d is current device\n", curr_device);
69 "init [dev] - init MMC sub system\n"
70 "mmc device [dev] - show or set current device"
72 #else /* !CONFIG_GENERIC_MMC */
80 static void print_mmcinfo(struct mmc *mmc)
82 printf("Device: %s\n", mmc->name);
83 printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
84 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
85 printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
86 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
87 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
89 printf("Tran Speed: %d\n", mmc->tran_speed);
90 printf("Rd Block Len: %d\n", mmc->read_bl_len);
92 printf("%s version %d.%d\n", IS_SD(mmc) ? "SD" : "MMC",
93 (mmc->version >> 8) & 0xf, mmc->version & 0xff);
95 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
97 print_size(mmc->capacity, "\n");
99 printf("Bus Width: %d-bit\n", mmc->bus_width);
102 static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
106 if (curr_device < 0) {
107 if (get_mmc_num() > 0)
110 puts("No MMC device available\n");
115 mmc = find_mmc_device(curr_device);
123 printf("no mmc device at slot %x\n", curr_device);
129 mmcinfo, 1, 0, do_mmcinfo,
131 "- display info of the current MMC device"
134 static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
136 enum mmc_state state;
139 return CMD_RET_USAGE;
141 if (curr_device < 0) {
142 if (get_mmc_num() > 0)
145 puts("No MMC device available\n");
150 if (strcmp(argv[1], "rescan") == 0) {
154 return CMD_RET_USAGE;
156 mmc = find_mmc_device(curr_device);
158 printf("no mmc device at slot %x\n", curr_device);
168 } else if (strcmp(argv[1], "part") == 0) {
169 block_dev_desc_t *mmc_dev;
173 return CMD_RET_USAGE;
175 mmc = find_mmc_device(curr_device);
177 printf("no mmc device at slot %x\n", curr_device);
181 mmc_dev = mmc_get_dev(curr_device);
182 if (mmc_dev != NULL &&
183 mmc_dev->type != DEV_TYPE_UNKNOWN) {
188 puts("get mmc type error!\n");
190 } else if (strcmp(argv[1], "list") == 0) {
192 return CMD_RET_USAGE;
193 print_mmc_devices('\n');
195 } else if (strcmp(argv[1], "dev") == 0) {
202 dev = simple_strtoul(argv[2], NULL, 10);
203 else if (argc == 4) {
204 dev = (int)simple_strtoul(argv[2], NULL, 10);
205 part = (int)simple_strtoul(argv[3], NULL, 10);
206 if (part > PART_ACCESS_MASK) {
207 printf("#part_num shouldn't be larger"
208 " than %d\n", PART_ACCESS_MASK);
212 return CMD_RET_USAGE;
214 mmc = find_mmc_device(dev);
216 printf("no mmc device at slot %x\n", dev);
223 if (mmc->part_config == MMCPART_NOAVAILABLE) {
224 printf("Card doesn't support part_switch\n");
228 if (part != mmc->part_num) {
229 ret = mmc_switch_part(dev, part);
231 mmc->part_num = part;
233 printf("switch to partitions #%d, %s\n",
234 part, (!ret) ? "OK" : "ERROR");
238 if (mmc->part_config == MMCPART_NOAVAILABLE)
239 printf("mmc%d is current device\n", curr_device);
241 printf("mmc%d(part %d) is current device\n",
242 curr_device, mmc->part_num);
245 #ifdef CONFIG_SUPPORT_EMMC_BOOT
246 } else if (strcmp(argv[1], "partconf") == 0) {
249 u8 ack, part_num, access;
252 dev = simple_strtoul(argv[2], NULL, 10);
253 ack = simple_strtoul(argv[3], NULL, 10);
254 part_num = simple_strtoul(argv[4], NULL, 10);
255 access = simple_strtoul(argv[5], NULL, 10);
257 return CMD_RET_USAGE;
260 mmc = find_mmc_device(dev);
262 printf("no mmc device at slot %x\n", dev);
267 puts("PARTITION_CONFIG only exists on eMMC\n");
271 /* acknowledge to be sent during boot operation */
272 return mmc_set_part_conf(mmc, ack, part_num, access);
273 } else if (strcmp(argv[1], "bootbus") == 0) {
276 u8 width, reset, mode;
279 dev = simple_strtoul(argv[2], NULL, 10);
280 width = simple_strtoul(argv[3], NULL, 10);
281 reset = simple_strtoul(argv[4], NULL, 10);
282 mode = simple_strtoul(argv[5], NULL, 10);
284 return CMD_RET_USAGE;
287 mmc = find_mmc_device(dev);
289 printf("no mmc device at slot %x\n", dev);
294 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
298 /* acknowledge to be sent during boot operation */
299 return mmc_set_boot_bus_width(mmc, width, reset, mode);
300 } else if (strcmp(argv[1], "bootpart-resize") == 0) {
303 u32 bootsize, rpmbsize;
306 dev = simple_strtoul(argv[2], NULL, 10);
307 bootsize = simple_strtoul(argv[3], NULL, 10);
308 rpmbsize = simple_strtoul(argv[4], NULL, 10);
310 return CMD_RET_USAGE;
313 mmc = find_mmc_device(dev);
315 printf("no mmc device at slot %x\n", dev);
320 printf("It is not a EMMC device\n");
324 if (0 == mmc_boot_partition_size_change(mmc,
325 bootsize, rpmbsize)) {
326 printf("EMMC boot partition Size %d MB\n", bootsize);
327 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
330 printf("EMMC boot partition Size change Failed.\n");
333 #endif /* CONFIG_SUPPORT_EMMC_BOOT */
336 else if (argc == 3 && strcmp(argv[1], "setdsr") == 0) {
337 struct mmc *mmc = find_mmc_device(curr_device);
338 u32 val = simple_strtoul(argv[2], NULL, 16);
342 printf("no mmc device at slot %x\n", curr_device);
345 ret = mmc_set_dsr(mmc, val);
346 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
358 if (argc == 5 && strcmp(argv[1], "read") == 0)
360 else if (argc == 5 && strcmp(argv[1], "write") == 0)
362 else if (argc == 4 && strcmp(argv[1], "erase") == 0)
365 if (state != MMC_INVALID) {
366 struct mmc *mmc = find_mmc_device(curr_device);
371 if (state != MMC_ERASE) {
372 addr = (void *)simple_strtoul(argv[idx], NULL, 16);
376 blk = simple_strtoul(argv[idx], NULL, 16);
377 cnt = simple_strtoul(argv[idx + 1], NULL, 16);
380 printf("no mmc device at slot %x\n", curr_device);
384 printf("\nMMC %s: dev # %d, block # %d, count %d ... ",
385 argv[1], curr_device, blk, cnt);
389 if ((state == MMC_WRITE || state == MMC_ERASE)) {
390 if (mmc_getwp(mmc) == 1) {
391 printf("Error: card is write protected!\n");
398 n = mmc->block_dev.block_read(curr_device, blk,
400 /* flush cache after read */
401 flush_cache((ulong)addr, cnt * 512); /* FIXME */
404 n = mmc->block_dev.block_write(curr_device, blk,
408 n = mmc->block_dev.block_erase(curr_device, blk, cnt);
414 printf("%d blocks %s: %s\n",
415 n, argv[1], (n == cnt) ? "OK" : "ERROR");
416 return (n == cnt) ? 0 : 1;
419 return CMD_RET_USAGE;
423 mmc, 6, 1, do_mmcops,
425 "read addr blk# cnt\n"
426 "mmc write addr blk# cnt\n"
427 "mmc erase blk# cnt\n"
429 "mmc part - lists available partition on current mmc device\n"
430 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
431 "mmc list - lists available devices\n"
432 #ifdef CONFIG_SUPPORT_EMMC_BOOT
433 "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
434 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
435 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
436 " - Change sizes of boot and RPMB partitions of specified device\n"
437 "mmc partconf dev boot_ack boot_partition partition_access\n"
438 " - Change the bits of the PARTITION_CONFIG field of the specified device\n"
440 "mmc setdsr - set DSR register value\n"
442 #endif /* !CONFIG_GENERIC_MMC */