a028149d906d72696d005ce00cfa8d489d3ca057
[platform/kernel/u-boot.git] / common / cmd_mmc.c
1 /*
2  * (C) Copyright 2003
3  * Kyle Harris, kharris@nexus-tech.net
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 #include <common.h>
9 #include <command.h>
10 #include <mmc.h>
11
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[])
15 {
16         int dev;
17
18         if (argc < 2)
19                 return CMD_RET_USAGE;
20
21         if (strcmp(argv[1], "init") == 0) {
22                 if (argc == 2) {
23                         if (curr_device < 0)
24                                 dev = 1;
25                         else
26                                 dev = curr_device;
27                 } else if (argc == 3) {
28                         dev = (int)simple_strtoul(argv[2], NULL, 10);
29                 } else {
30                         return CMD_RET_USAGE;
31                 }
32
33                 if (mmc_legacy_init(dev) != 0) {
34                         puts("No MMC card found\n");
35                         return 1;
36                 }
37
38                 curr_device = dev;
39                 printf("mmc%d is available\n", curr_device);
40         } else if (strcmp(argv[1], "device") == 0) {
41                 if (argc == 2) {
42                         if (curr_device < 0) {
43                                 puts("No MMC device available\n");
44                                 return 1;
45                         }
46                 } else if (argc == 3) {
47                         dev = (int)simple_strtoul(argv[2], NULL, 10);
48
49 #ifdef CONFIG_SYS_MMC_SET_DEV
50                         if (mmc_set_dev(dev) != 0)
51                                 return 1;
52 #endif
53                         curr_device = dev;
54                 } else {
55                         return CMD_RET_USAGE;
56                 }
57
58                 printf("mmc%d is current device\n", curr_device);
59         } else {
60                 return CMD_RET_USAGE;
61         }
62
63         return 0;
64 }
65
66 U_BOOT_CMD(
67         mmc, 3, 1, do_mmc,
68         "MMC sub-system",
69         "init [dev] - init MMC sub system\n"
70         "mmc device [dev] - show or set current device"
71 );
72 #else /* !CONFIG_GENERIC_MMC */
73
74 enum mmc_state {
75         MMC_INVALID,
76         MMC_READ,
77         MMC_WRITE,
78         MMC_ERASE,
79 };
80 static void print_mmcinfo(struct mmc *mmc)
81 {
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);
88
89         printf("Tran Speed: %d\n", mmc->tran_speed);
90         printf("Rd Block Len: %d\n", mmc->read_bl_len);
91
92         printf("%s version %d.%d\n", IS_SD(mmc) ? "SD" : "MMC",
93                         (mmc->version >> 8) & 0xf, mmc->version & 0xff);
94
95         printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
96         puts("Capacity: ");
97         print_size(mmc->capacity, "\n");
98
99         printf("Bus Width: %d-bit\n", mmc->bus_width);
100 }
101
102 static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
103 {
104         struct mmc *mmc;
105
106         if (curr_device < 0) {
107                 if (get_mmc_num() > 0)
108                         curr_device = 0;
109                 else {
110                         puts("No MMC device available\n");
111                         return 1;
112                 }
113         }
114
115         mmc = find_mmc_device(curr_device);
116
117         if (mmc) {
118                 mmc_init(mmc);
119
120                 print_mmcinfo(mmc);
121                 return 0;
122         } else {
123                 printf("no mmc device at slot %x\n", curr_device);
124                 return 1;
125         }
126 }
127
128 U_BOOT_CMD(
129         mmcinfo, 1, 0, do_mmcinfo,
130         "display MMC info",
131         "- display info of the current MMC device"
132 );
133
134 #ifdef CONFIG_SUPPORT_EMMC_BOOT
135 static int boot_part_access(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
136 {
137         int err;
138         err = mmc_boot_part_access(mmc, ack, part_num, access);
139
140         if ((err == 0) && (access != 0)) {
141                 printf("\t\t\t!!!Notice!!!\n");
142
143                 printf("!You must close EMMC boot Partition");
144                 printf("after all images are written\n");
145
146                 printf("!EMMC boot partition has continuity");
147                 printf("at image writing time.\n");
148
149                 printf("!So, do not close the boot partition");
150                 printf("before all images are written.\n");
151                 return 0;
152         } else if ((err == 0) && (access == 0))
153                 return 0;
154         else if ((err != 0) && (access != 0)) {
155                 printf("EMMC boot partition-%d OPEN Failed.\n", part_num);
156                 return 1;
157         } else {
158                 printf("EMMC boot partition-%d CLOSE Failed.\n", part_num);
159                 return 1;
160         }
161 }
162 #endif
163
164 static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
165 {
166         enum mmc_state state;
167
168         if (argc < 2)
169                 return CMD_RET_USAGE;
170
171         if (curr_device < 0) {
172                 if (get_mmc_num() > 0)
173                         curr_device = 0;
174                 else {
175                         puts("No MMC device available\n");
176                         return 1;
177                 }
178         }
179
180         if (strcmp(argv[1], "rescan") == 0) {
181                 struct mmc *mmc;
182
183                 if (argc != 2)
184                         return CMD_RET_USAGE;
185
186                 mmc = find_mmc_device(curr_device);
187                 if (!mmc) {
188                         printf("no mmc device at slot %x\n", curr_device);
189                         return 1;
190                 }
191
192                 mmc->has_init = 0;
193
194                 if (mmc_init(mmc))
195                         return 1;
196                 else
197                         return 0;
198         } else if (strcmp(argv[1], "part") == 0) {
199                 block_dev_desc_t *mmc_dev;
200                 struct mmc *mmc;
201
202                 if (argc != 2)
203                         return CMD_RET_USAGE;
204
205                 mmc = find_mmc_device(curr_device);
206                 if (!mmc) {
207                         printf("no mmc device at slot %x\n", curr_device);
208                         return 1;
209                 }
210                 mmc_init(mmc);
211                 mmc_dev = mmc_get_dev(curr_device);
212                 if (mmc_dev != NULL &&
213                                 mmc_dev->type != DEV_TYPE_UNKNOWN) {
214                         print_part(mmc_dev);
215                         return 0;
216                 }
217
218                 puts("get mmc type error!\n");
219                 return 1;
220         } else if (strcmp(argv[1], "list") == 0) {
221                 if (argc != 2)
222                         return CMD_RET_USAGE;
223                 print_mmc_devices('\n');
224                 return 0;
225         } else if (strcmp(argv[1], "dev") == 0) {
226                 int dev, part = -1;
227                 struct mmc *mmc;
228
229                 if (argc == 2)
230                         dev = curr_device;
231                 else if (argc == 3)
232                         dev = simple_strtoul(argv[2], NULL, 10);
233                 else if (argc == 4) {
234                         dev = (int)simple_strtoul(argv[2], NULL, 10);
235                         part = (int)simple_strtoul(argv[3], NULL, 10);
236                         if (part > PART_ACCESS_MASK) {
237                                 printf("#part_num shouldn't be larger"
238                                         " than %d\n", PART_ACCESS_MASK);
239                                 return 1;
240                         }
241                 } else
242                         return CMD_RET_USAGE;
243
244                 mmc = find_mmc_device(dev);
245                 if (!mmc) {
246                         printf("no mmc device at slot %x\n", dev);
247                         return 1;
248                 }
249
250                 mmc_init(mmc);
251                 if (part != -1) {
252                         int ret;
253                         if (mmc->part_config == MMCPART_NOAVAILABLE) {
254                                 printf("Card doesn't support part_switch\n");
255                                 return 1;
256                         }
257
258                         if (part != mmc->part_num) {
259                                 ret = mmc_switch_part(dev, part);
260                                 if (!ret)
261                                         mmc->part_num = part;
262
263                                 printf("switch to partitions #%d, %s\n",
264                                                 part, (!ret) ? "OK" : "ERROR");
265                         }
266                 }
267                 curr_device = dev;
268                 if (mmc->part_config == MMCPART_NOAVAILABLE)
269                         printf("mmc%d is current device\n", curr_device);
270                 else
271                         printf("mmc%d(part %d) is current device\n",
272                                 curr_device, mmc->part_num);
273
274                 return 0;
275 #ifdef CONFIG_SUPPORT_EMMC_BOOT
276         } else if ((strcmp(argv[1], "open") == 0) ||
277                         (strcmp(argv[1], "close") == 0)) {
278                 int dev;
279                 struct mmc *mmc;
280                 u8 part_num, access = 0;
281
282                 if (argc == 4) {
283                         dev = simple_strtoul(argv[2], NULL, 10);
284                         part_num = simple_strtoul(argv[3], NULL, 10);
285                 } else {
286                         return CMD_RET_USAGE;
287                 }
288
289                 mmc = find_mmc_device(dev);
290                 if (!mmc) {
291                         printf("no mmc device at slot %x\n", dev);
292                         return 1;
293                 }
294
295                 if (IS_SD(mmc)) {
296                         printf("SD device cannot be opened/closed\n");
297                         return 1;
298                 }
299
300                 if ((part_num <= 0) || (part_num > MMC_NUM_BOOT_PARTITION)) {
301                         printf("Invalid boot partition number:\n");
302                         printf("Boot partition number cannot be <= 0\n");
303                         printf("EMMC44 supports only 2 boot partitions\n");
304                         return 1;
305                 }
306
307                 if (strcmp(argv[1], "open") == 0)
308                         access = part_num; /* enable R/W access to boot part*/
309                 else
310                         access = 0; /* No access to boot partition */
311
312                 /* acknowledge to be sent during boot operation */
313                 return boot_part_access(mmc, 1, part_num, access);
314         } else if (strcmp(argv[1], "partconf") == 0) {
315                 int dev;
316                 struct mmc *mmc;
317                 u8 ack, part_num, access;
318
319                 if (argc == 6) {
320                         dev = simple_strtoul(argv[2], NULL, 10);
321                         ack = simple_strtoul(argv[3], NULL, 10);
322                         part_num = simple_strtoul(argv[4], NULL, 10);
323                         access = simple_strtoul(argv[5], NULL, 10);
324                 } else {
325                         return CMD_RET_USAGE;
326                 }
327
328                 mmc = find_mmc_device(dev);
329                 if (!mmc) {
330                         printf("no mmc device at slot %x\n", dev);
331                         return 1;
332                 }
333
334                 if (IS_SD(mmc)) {
335                         puts("PARTITION_CONFIG only exists on eMMC\n");
336                         return 1;
337                 }
338
339                 /* acknowledge to be sent during boot operation */
340                 return mmc_set_part_conf(mmc, ack, part_num, access);
341         } else if (strcmp(argv[1], "bootbus") == 0) {
342                 int dev;
343                 struct mmc *mmc;
344                 u8 width, reset, mode;
345
346                 if (argc == 6) {
347                         dev = simple_strtoul(argv[2], NULL, 10);
348                         width = simple_strtoul(argv[3], NULL, 10);
349                         reset = simple_strtoul(argv[4], NULL, 10);
350                         mode = simple_strtoul(argv[5], NULL, 10);
351                 } else {
352                         return CMD_RET_USAGE;
353                 }
354
355                 mmc = find_mmc_device(dev);
356                 if (!mmc) {
357                         printf("no mmc device at slot %x\n", dev);
358                         return 1;
359                 }
360
361                 if (IS_SD(mmc)) {
362                         puts("BOOT_BUS_WIDTH only exists on eMMC\n");
363                         return 1;
364                 }
365
366                 /* acknowledge to be sent during boot operation */
367                 return mmc_set_boot_bus_width(mmc, width, reset, mode);
368         } else if (strcmp(argv[1], "bootpart-resize") == 0) {
369                 int dev;
370                 struct mmc *mmc;
371                 u32 bootsize, rpmbsize;
372
373                 if (argc == 5) {
374                         dev = simple_strtoul(argv[2], NULL, 10);
375                         bootsize = simple_strtoul(argv[3], NULL, 10);
376                         rpmbsize = simple_strtoul(argv[4], NULL, 10);
377                 } else {
378                         return CMD_RET_USAGE;
379                 }
380
381                 mmc = find_mmc_device(dev);
382                 if (!mmc) {
383                         printf("no mmc device at slot %x\n", dev);
384                         return 1;
385                 }
386
387                 if (IS_SD(mmc)) {
388                         printf("It is not a EMMC device\n");
389                         return 1;
390                 }
391
392                 if (0 == mmc_boot_partition_size_change(mmc,
393                                                         bootsize, rpmbsize)) {
394                         printf("EMMC boot partition Size %d MB\n", bootsize);
395                         printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
396                         return 0;
397                 } else {
398                         printf("EMMC boot partition Size change Failed.\n");
399                         return 1;
400                 }
401 #endif /* CONFIG_SUPPORT_EMMC_BOOT */
402         }
403
404         else if (argc == 3 && strcmp(argv[1], "setdsr") == 0) {
405                 struct mmc *mmc = find_mmc_device(curr_device);
406                 u32 val = simple_strtoul(argv[2], NULL, 16);
407                 int ret;
408
409                 if (!mmc) {
410                         printf("no mmc device at slot %x\n", curr_device);
411                         return 1;
412                 }
413                 ret = mmc_set_dsr(mmc, val);
414                 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
415                 if (!ret) {
416                         mmc->has_init = 0;
417                         if (mmc_init(mmc))
418                                 return 1;
419                         else
420                                 return 0;
421                 }
422                 return ret;
423         }
424
425         state = MMC_INVALID;
426         if (argc == 5 && strcmp(argv[1], "read") == 0)
427                 state = MMC_READ;
428         else if (argc == 5 && strcmp(argv[1], "write") == 0)
429                 state = MMC_WRITE;
430         else if (argc == 4 && strcmp(argv[1], "erase") == 0)
431                 state = MMC_ERASE;
432
433         if (state != MMC_INVALID) {
434                 struct mmc *mmc = find_mmc_device(curr_device);
435                 int idx = 2;
436                 u32 blk, cnt, n;
437                 void *addr;
438
439                 if (state != MMC_ERASE) {
440                         addr = (void *)simple_strtoul(argv[idx], NULL, 16);
441                         ++idx;
442                 } else
443                         addr = NULL;
444                 blk = simple_strtoul(argv[idx], NULL, 16);
445                 cnt = simple_strtoul(argv[idx + 1], NULL, 16);
446
447                 if (!mmc) {
448                         printf("no mmc device at slot %x\n", curr_device);
449                         return 1;
450                 }
451
452                 printf("\nMMC %s: dev # %d, block # %d, count %d ... ",
453                                 argv[1], curr_device, blk, cnt);
454
455                 mmc_init(mmc);
456
457                 if ((state == MMC_WRITE || state == MMC_ERASE)) {
458                         if (mmc_getwp(mmc) == 1) {
459                                 printf("Error: card is write protected!\n");
460                                 return 1;
461                         }
462                 }
463
464                 switch (state) {
465                 case MMC_READ:
466                         n = mmc->block_dev.block_read(curr_device, blk,
467                                                       cnt, addr);
468                         /* flush cache after read */
469                         flush_cache((ulong)addr, cnt * 512); /* FIXME */
470                         break;
471                 case MMC_WRITE:
472                         n = mmc->block_dev.block_write(curr_device, blk,
473                                                       cnt, addr);
474                         break;
475                 case MMC_ERASE:
476                         n = mmc->block_dev.block_erase(curr_device, blk, cnt);
477                         break;
478                 default:
479                         BUG();
480                 }
481
482                 printf("%d blocks %s: %s\n",
483                                 n, argv[1], (n == cnt) ? "OK" : "ERROR");
484                 return (n == cnt) ? 0 : 1;
485         }
486
487         return CMD_RET_USAGE;
488 }
489
490 U_BOOT_CMD(
491         mmc, 6, 1, do_mmcops,
492         "MMC sub system",
493         "read addr blk# cnt\n"
494         "mmc write addr blk# cnt\n"
495         "mmc erase blk# cnt\n"
496         "mmc rescan\n"
497         "mmc part - lists available partition on current mmc device\n"
498         "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
499         "mmc list - lists available devices\n"
500 #ifdef CONFIG_SUPPORT_EMMC_BOOT
501         "mmc open <dev> <boot_partition>\n"
502         " - Enable boot_part for booting and enable R/W access of boot_part\n"
503         "mmc close <dev> <boot_partition>\n"
504         " - Enable boot_part for booting and disable access to boot_part\n"
505         "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
506         " - Set the BOOT_BUS_WIDTH field of the specified device\n"
507         "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
508         " - Change sizes of boot and RPMB partitions of specified device\n"
509         "mmc partconf dev boot_ack boot_partition partition_access\n"
510         " - Change the bits of the PARTITION_CONFIG field of the specified device\n"
511 #endif
512         "mmc setdsr - set DSR register value\n"
513         );
514 #endif /* !CONFIG_GENERIC_MMC */