]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_mmc.c
arm: mx5: clock: fix PLL_FREQ_MIN() calculation
[karo-tx-uboot.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 CMD_RET_FAILURE;
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 CMD_RET_FAILURE;
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 CMD_RET_FAILURE;
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 CMD_RET_SUCCESS;
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 CMD_RET_FAILURE;
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 CMD_RET_SUCCESS;
122         } else {
123                 printf("no mmc device at slot %x\n", curr_device);
124                 return CMD_RET_FAILURE;
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("Notice!\n");
142
143                 printf("You must close EMMC boot Partition after all images are written\n");
144                 printf("EMMC boot partition has continuity at image writing time.\n");
145                 printf("So, do not close the boot partition before all images are written.\n");
146                 return CMD_RET_SUCCESS;
147         } else if ((err == 0) && (access == 0))
148                 return CMD_RET_SUCCESS;
149         else if ((err != 0) && (access != 0)) {
150                 printf("EMMC boot partition-%d OPEN Failed.\n", part_num);
151                 return CMD_RET_FAILURE;
152         } else {
153                 printf("EMMC boot partition-%d CLOSE Failed.\n", part_num);
154                 return CMD_RET_FAILURE;
155         }
156 }
157 #endif
158
159 static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
160 {
161         enum mmc_state state;
162
163         if (argc < 2)
164                 return CMD_RET_USAGE;
165
166         if (curr_device < 0) {
167                 if (get_mmc_num() > 0)
168                         curr_device = 0;
169                 else {
170                         puts("No MMC device available\n");
171                         return CMD_RET_FAILURE;
172                 }
173         }
174
175         if (strcmp(argv[1], "rescan") == 0) {
176                 struct mmc *mmc;
177
178                 if (argc != 2)
179                         return CMD_RET_USAGE;
180
181                 mmc = find_mmc_device(curr_device);
182                 if (!mmc) {
183                         printf("no mmc device at slot %x\n", curr_device);
184                         return CMD_RET_FAILURE;
185                 }
186
187                 mmc->has_init = 0;
188
189                 if (mmc_init(mmc))
190                         return CMD_RET_FAILURE;
191                 else
192                         return CMD_RET_SUCCESS;
193         } else if (strncmp(argv[1], "part", 4) == 0) {
194                 block_dev_desc_t *mmc_dev;
195                 struct mmc *mmc;
196
197                 if (argc != 2)
198                         return CMD_RET_USAGE;
199
200                 mmc = find_mmc_device(curr_device);
201                 if (!mmc) {
202                         printf("no mmc device at slot %x\n", curr_device);
203                         return CMD_RET_FAILURE;
204                 }
205                 mmc_init(mmc);
206                 mmc_dev = mmc_get_dev(curr_device);
207                 if (mmc_dev != NULL &&
208                                 mmc_dev->type != DEV_TYPE_UNKNOWN) {
209                         print_part(mmc_dev);
210                         return CMD_RET_SUCCESS;
211                 }
212
213                 puts("get mmc type error!\n");
214                 return CMD_RET_FAILURE;
215         } else if (strcmp(argv[1], "list") == 0) {
216                 if (argc != 2)
217                         return CMD_RET_USAGE;
218                 print_mmc_devices('\n');
219                 return CMD_RET_SUCCESS;
220         } else if (strcmp(argv[1], "dev") == 0) {
221                 int dev, part = -1;
222                 struct mmc *mmc;
223
224                 if (argc == 2)
225                         dev = curr_device;
226                 else if (argc == 3)
227                         dev = simple_strtoul(argv[2], NULL, 10);
228                 else if (argc == 4) {
229                         dev = (int)simple_strtoul(argv[2], NULL, 10);
230                         part = (int)simple_strtoul(argv[3], NULL, 10);
231                         if (part > PART_ACCESS_MASK) {
232                                 printf("#part_num shouldn't be larger"
233                                         " than %d\n", PART_ACCESS_MASK);
234                                 return CMD_RET_FAILURE;
235                         }
236                 } else
237                         return CMD_RET_USAGE;
238
239                 mmc = find_mmc_device(dev);
240                 if (!mmc) {
241                         printf("no mmc device at slot %x\n", dev);
242                         return CMD_RET_FAILURE;
243                 }
244
245                 mmc_init(mmc);
246                 if (part != -1) {
247                         int ret;
248                         if (mmc->part_config == MMCPART_NOAVAILABLE) {
249                                 printf("Card doesn't support part_switch\n");
250                                 return CMD_RET_FAILURE;
251                         }
252
253                         if (part != mmc->part_num) {
254                                 ret = mmc_switch_part(dev, part);
255                                 if (!ret)
256                                         mmc->part_num = part;
257
258                                 printf("switch to partions #%d, %s\n",
259                                                 part, (!ret) ? "OK" : "ERROR");
260                         }
261                 }
262                 curr_device = dev;
263                 if (mmc->part_config == MMCPART_NOAVAILABLE)
264                         printf("mmc%d is current device\n", curr_device);
265                 else
266                         printf("mmc%d(part %d) is current device\n",
267                                 curr_device, mmc->part_num);
268
269                 return CMD_RET_SUCCESS;
270 #ifdef CONFIG_SUPPORT_EMMC_BOOT
271         } else if ((strcmp(argv[1], "open") == 0) ||
272                         (strcmp(argv[1], "close") == 0)) {
273                 int dev;
274                 struct mmc *mmc;
275                 u8 part_num, access = 0;
276
277                 if (argc == 4) {
278                         dev = simple_strtoul(argv[2], NULL, 10);
279                         part_num = simple_strtoul(argv[3], NULL, 10);
280                 } else {
281                         return CMD_RET_USAGE;
282                 }
283
284                 mmc = find_mmc_device(dev);
285                 if (!mmc) {
286                         printf("no mmc device at slot %x\n", dev);
287                         return CMD_RET_FAILURE;
288                 }
289
290                 if (IS_SD(mmc)) {
291                         printf("SD device cannot be opened/closed\n");
292                         return CMD_RET_FAILURE;
293                 }
294
295                 if ((part_num <= 0) || (part_num > MMC_NUM_BOOT_PARTITION)) {
296                         printf("Invalid boot partition number:\n");
297                         printf("Boot partition number cannot be <= 0\n");
298                         printf("EMMC44 supports only 2 boot partitions\n");
299                         return CMD_RET_FAILURE;
300                 }
301
302                 if (strcmp(argv[1], "open") == 0)
303                         access = part_num; /* enable R/W access to boot part*/
304                 else
305                         access = 0; /* No access to boot partition */
306
307                 /* acknowledge to be sent during boot operation */
308                 return boot_part_access(mmc, 1, part_num, access);
309
310         } else if (strcmp(argv[1], "bootpart") == 0) {
311                 int dev;
312
313                 if (argc != 5)
314                         return CMD_RET_USAGE;
315
316                 dev = simple_strtoul(argv[2], NULL, 10);
317
318                 u32 bootsize = simple_strtoul(argv[3], NULL, 10);
319                 u32 rpmbsize = simple_strtoul(argv[4], NULL, 10);
320                 struct mmc *mmc = find_mmc_device(dev);
321                 if (!mmc) {
322                         printf("no mmc device at slot %x\n", dev);
323                         return CMD_RET_FAILURE;
324                 }
325
326                 if (IS_SD(mmc)) {
327                         printf("It is not a EMMC device\n");
328                         return CMD_RET_FAILURE;
329                 }
330
331                 if (0 == mmc_boot_partition_size_change(mmc,
332                                                         bootsize, rpmbsize)) {
333                         printf("EMMC boot partition Size %d MB\n", bootsize);
334                         printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
335                         return CMD_RET_SUCCESS;
336                 } else {
337                         printf("EMMC boot partition Size change Failed.\n");
338                         return CMD_RET_FAILURE;
339                 }
340 #endif /* CONFIG_SUPPORT_EMMC_BOOT */
341         }
342         state = MMC_INVALID;
343         if (argc == 5 && strcmp(argv[1], "read") == 0)
344                 state = MMC_READ;
345         else if (argc == 5 && strcmp(argv[1], "write") == 0)
346                 state = MMC_WRITE;
347         else if (argc == 4 && strcmp(argv[1], "erase") == 0)
348                 state = MMC_ERASE;
349
350         if (state != MMC_INVALID) {
351                 struct mmc *mmc = find_mmc_device(curr_device);
352                 int idx = 2;
353                 u32 blk, cnt, n;
354                 void *addr;
355
356                 if (state != MMC_ERASE) {
357                         addr = (void *)simple_strtoul(argv[idx], NULL, 16);
358                         ++idx;
359                 } else
360                         addr = NULL;
361                 blk = simple_strtoul(argv[idx], NULL, 16);
362                 cnt = simple_strtoul(argv[idx + 1], NULL, 16);
363
364                 if (!mmc) {
365                         printf("no mmc device at slot %x\n", curr_device);
366                         return CMD_RET_FAILURE;
367                 }
368
369                 printf("\nMMC %s: dev # %d, block # %d, count %d ... ",
370                                 argv[1], curr_device, blk, cnt);
371
372                 mmc_init(mmc);
373
374                 if ((state == MMC_WRITE || state == MMC_ERASE)) {
375                         if (mmc_getwp(mmc) == 1) {
376                                 printf("Error: card is write protected!\n");
377                                 return CMD_RET_FAILURE;
378                         }
379                 }
380
381                 switch (state) {
382                 case MMC_READ:
383                         n = mmc->block_dev.block_read(curr_device, blk,
384                                                       cnt, addr);
385                         /* flush cache after read */
386                         flush_cache((ulong)addr, cnt * 512); /* FIXME */
387                         break;
388                 case MMC_WRITE:
389                         n = mmc->block_dev.block_write(curr_device, blk,
390                                                       cnt, addr);
391                         break;
392                 case MMC_ERASE:
393                         n = mmc->block_dev.block_erase(curr_device, blk, cnt);
394                         break;
395                 default:
396                         BUG();
397                 }
398
399                 printf("%d blocks %s: %s\n",
400                                 n, argv[1], (n == cnt) ? "OK" : "ERROR");
401                 return (n == cnt) ? 0 : 1;
402         }
403
404         return CMD_RET_USAGE;
405 }
406
407 U_BOOT_CMD(
408         mmc, 6, 1, do_mmcops,
409         "MMC sub system",
410         "read addr blk# cnt\n"
411         "mmc write addr blk# cnt\n"
412         "mmc erase blk# cnt\n"
413         "mmc rescan\n"
414         "mmc part - lists available partition on current mmc device\n"
415         "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
416         "mmc list - lists available devices\n"
417 #ifdef CONFIG_SUPPORT_EMMC_BOOT
418         "mmc open <dev> <boot_partition>\n"
419         " - Enable boot_part for booting and enable R/W access of boot_part\n"
420         "mmc close <dev> <boot_partition>\n"
421         " - Enable boot_part for booting and disable access to boot_part\n"
422         "mmc bootpart <device num> <boot part size MB> <RPMB part size MB>\n"
423         " - change sizes of boot and RPMB partions of specified device\n"
424 #endif
425         );
426 #endif /* !CONFIG_GENERIC_MMC */