blob: c62b2a541085f2ab1514cee0fe18452ffce77ed2 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
2 * Copyright 2008, Freescale Semiconductor, Inc
3 * Andy Fleming
4 *
5 * Based vaguely on the Linux code
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10#include <config.h>
11#include <common.h>
12#include <command.h>
13#include <mmc.h>
14#include <part.h>
15#include <malloc.h>
16#include <linux/list.h>
17#include <div64.h>
18#include "mmc_private.h"
19
20/* Set block count limit because of 16 bit register limit on some hardware*/
21#ifndef CONFIG_SYS_MMC_MAX_BLK_COUNT
22#define CONFIG_SYS_MMC_MAX_BLK_COUNT 65535
23#endif
24
25static struct list_head mmc_devices;
26static int cur_dev_num = -1;
27
28int __weak board_mmc_getwp(struct mmc *mmc)
29{
30 return -1;
31}
32
33int mmc_getwp(struct mmc *mmc)
34{
35 int wp;
36
37 wp = board_mmc_getwp(mmc);
38
39 if (wp < 0) {
40 if (mmc->getwp)
41 wp = mmc->getwp(mmc);
42 else
43 wp = 0;
44 }
45
46 return wp;
47}
48
49int __board_mmc_getcd(struct mmc *mmc) {
50 return -1;
51}
52
53int board_mmc_getcd(struct mmc *mmc)__attribute__((weak,
54 alias("__board_mmc_getcd")));
55
56int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
57{
58 int ret;
59
60#ifdef CONFIG_MMC_TRACE
61 int i;
62 u8 *ptr;
63
64 printf("CMD_SEND:%d\n", cmd->cmdidx);
65 printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
66 ret = mmc->send_cmd(mmc, cmd, data);
67 switch (cmd->resp_type) {
68 case MMC_RSP_NONE:
69 printf("\t\tMMC_RSP_NONE\n");
70 break;
71 case MMC_RSP_R1:
72 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
73 cmd->response[0]);
74 break;
75 case MMC_RSP_R1b:
76 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
77 cmd->response[0]);
78 break;
79 case MMC_RSP_R2:
80 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
81 cmd->response[0]);
82 printf("\t\t \t\t 0x%08X \n",
83 cmd->response[1]);
84 printf("\t\t \t\t 0x%08X \n",
85 cmd->response[2]);
86 printf("\t\t \t\t 0x%08X \n",
87 cmd->response[3]);
88 printf("\n");
89 printf("\t\t\t\t\tDUMPING DATA\n");
90 for (i = 0; i < 4; i++) {
91 int j;
92 printf("\t\t\t\t\t%03d - ", i*4);
93 ptr = (u8 *)&cmd->response[i];
94 ptr += 3;
95 for (j = 0; j < 4; j++)
96 printf("%02X ", *ptr--);
97 printf("\n");
98 }
99 break;
100 case MMC_RSP_R3:
101 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
102 cmd->response[0]);
103 break;
104 default:
105 printf("\t\tERROR MMC rsp not supported\n");
106 break;
107 }
108#else
109 ret = mmc->send_cmd(mmc, cmd, data);
110#endif
111 return ret;
112}
113
114int mmc_send_status(struct mmc *mmc, int timeout)
115{
116 struct mmc_cmd cmd;
117 int err, retries = 5;
118#ifdef CONFIG_MMC_TRACE
119 int status;
120#endif
121
122 cmd.cmdidx = MMC_CMD_SEND_STATUS;
123 cmd.resp_type = MMC_RSP_R1;
124 if (!mmc_host_is_spi(mmc))
125 cmd.cmdarg = mmc->rca << 16;
126
127 do {
128 err = mmc_send_cmd(mmc, &cmd, NULL);
129 if (!err) {
130 if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
131 (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
132 MMC_STATE_PRG)
133 break;
134 else if (cmd.response[0] & MMC_STATUS_MASK) {
135#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
136 printf("Status Error: 0x%08X\n",
137 cmd.response[0]);
138#endif
139 return COMM_ERR;
140 }
141 } else if (--retries < 0)
142 return err;
143
144 udelay(1000);
145
146 } while (timeout--);
147
148#ifdef CONFIG_MMC_TRACE
149 status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
150 printf("CURR STATE:%d\n", status);
151#endif
152 if (timeout <= 0) {
153#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
154 printf("Timeout waiting card ready\n");
155#endif
156 return TIMEOUT;
157 }
158
159 return 0;
160}
161
162int mmc_set_blocklen(struct mmc *mmc, int len)
163{
164 struct mmc_cmd cmd;
165
166 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
167 cmd.resp_type = MMC_RSP_R1;
168 cmd.cmdarg = len;
169
170 udelay(1000);
171 return mmc_send_cmd(mmc, &cmd, NULL);
172}
173
174struct mmc *find_mmc_device(int dev_num)
175{
176 struct mmc *m;
177 struct list_head *entry;
178
179 list_for_each(entry, &mmc_devices) {
180 m = list_entry(entry, struct mmc, link);
181
182 if (m->block_dev.dev == dev_num)
183 return m;
184 }
185
186#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
187 printf("MMC Device %d not found\n", dev_num);
188#endif
189
190 return NULL;
191}
192
193static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
194 lbaint_t blkcnt)
195{
196 struct mmc_cmd cmd;
197 struct mmc_data data;
198
199 if (blkcnt > 1)
200 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
201 else
202 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
203
204 if (mmc->high_capacity)
205 cmd.cmdarg = start;
206 else
207 cmd.cmdarg = start * mmc->read_bl_len;
208
209 cmd.resp_type = MMC_RSP_R1;
210
211 data.dest = dst;
212 data.blocks = blkcnt;
213 data.blocksize = mmc->read_bl_len;
214 data.flags = MMC_DATA_READ;
215
216 if (mmc_send_cmd(mmc, &cmd, &data))
217 return 0;
218
219 if (blkcnt > 1) {
220 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
221 cmd.cmdarg = 0;
222 cmd.resp_type = MMC_RSP_R1b;
223 if (mmc_send_cmd(mmc, &cmd, NULL)) {
224#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
225 printf("mmc fail to send stop cmd\n");
226#endif
227 return 0;
228 }
229 }
230
231 return blkcnt;
232}
233
234static ulong mmc_bread(int dev_num, lbaint_t start, lbaint_t blkcnt, void *dst)
235{
236 lbaint_t cur, blocks_todo = blkcnt;
237
238 if (blkcnt == 0)
239 return 0;
240
241 struct mmc *mmc = find_mmc_device(dev_num);
242 if (!mmc)
243 return 0;
244
245 if ((start + blkcnt) > mmc->block_dev.lba) {
246#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
247 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
248 start + blkcnt, mmc->block_dev.lba);
249#endif
250 return 0;
251 }
252
253 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
254 return 0;
255
256 do {
257 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
258 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
259 return 0;
260 blocks_todo -= cur;
261 start += cur;
262 dst += cur * mmc->read_bl_len;
263 } while (blocks_todo > 0);
264
265 return blkcnt;
266}
267
268static int mmc_go_idle(struct mmc *mmc)
269{
270 struct mmc_cmd cmd;
271 int err;
272
273 udelay(1000);
274
275 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
276 cmd.cmdarg = 0;
277 cmd.resp_type = MMC_RSP_NONE;
278
279 err = mmc_send_cmd(mmc, &cmd, NULL);
280
281 if (err)
282 return err;
283
284 udelay(2000);
285
286 return 0;
287}
288
289static int sd_send_op_cond(struct mmc *mmc)
290{
291 int timeout = 1000;
292 int err;
293 struct mmc_cmd cmd;
294
295 do {
296 cmd.cmdidx = MMC_CMD_APP_CMD;
297 cmd.resp_type = MMC_RSP_R1;
298 cmd.cmdarg = 0;
299
300 err = mmc_send_cmd(mmc, &cmd, NULL);
301
302 if (err)
303 return err;
304
305 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
306 cmd.resp_type = MMC_RSP_R3;
307
308 /*
309 * Most cards do not answer if some reserved bits
310 * in the ocr are set. However, Some controller
311 * can set bit 7 (reserved for low voltages), but
312 * how to manage low voltages SD card is not yet
313 * specified.
314 */
315 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
316 (mmc->voltages & 0xff8000);
317
318 if (mmc->version == SD_VERSION_2)
319 cmd.cmdarg |= OCR_HCS;
320
321 err = mmc_send_cmd(mmc, &cmd, NULL);
322
323 if (err)
324 return err;
325
326 udelay(1000);
327 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
328
329 if (timeout <= 0)
330 return UNUSABLE_ERR;
331
332 if (mmc->version != SD_VERSION_2)
333 mmc->version = SD_VERSION_1_0;
334
335 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
336 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
337 cmd.resp_type = MMC_RSP_R3;
338 cmd.cmdarg = 0;
339
340 err = mmc_send_cmd(mmc, &cmd, NULL);
341
342 if (err)
343 return err;
344 }
345
346 mmc->ocr = cmd.response[0];
347
348 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
349 mmc->rca = 0;
350
351 return 0;
352}
353
354/* We pass in the cmd since otherwise the init seems to fail */
355static int mmc_send_op_cond_iter(struct mmc *mmc, struct mmc_cmd *cmd,
356 int use_arg)
357{
358 int err;
359
360 cmd->cmdidx = MMC_CMD_SEND_OP_COND;
361 cmd->resp_type = MMC_RSP_R3;
362 cmd->cmdarg = 0;
363 if (use_arg && !mmc_host_is_spi(mmc)) {
364 cmd->cmdarg =
365 (mmc->voltages &
366 (mmc->op_cond_response & OCR_VOLTAGE_MASK)) |
367 (mmc->op_cond_response & OCR_ACCESS_MODE);
368
369 if (mmc->host_caps & MMC_MODE_HC)
370 cmd->cmdarg |= OCR_HCS;
371 }
372 err = mmc_send_cmd(mmc, cmd, NULL);
373 if (err)
374 return err;
375 mmc->op_cond_response = cmd->response[0];
376 return 0;
377}
378
379int mmc_send_op_cond(struct mmc *mmc)
380{
381 struct mmc_cmd cmd;
382 int err, i;
383
384 /* Some cards seem to need this */
385 mmc_go_idle(mmc);
386
387 /* Asking to the card its capabilities */
388 mmc->op_cond_pending = 1;
389 for (i = 0; i < 2; i++) {
390 err = mmc_send_op_cond_iter(mmc, &cmd, i != 0);
391 if (err)
392 return err;
393
394 /* exit if not busy (flag seems to be inverted) */
395 if (mmc->op_cond_response & OCR_BUSY)
396 return 0;
397 }
398 return IN_PROGRESS;
399}
400
401int mmc_complete_op_cond(struct mmc *mmc)
402{
403 struct mmc_cmd cmd;
404 int timeout = 1000;
405 uint start;
406 int err;
407
408 mmc->op_cond_pending = 0;
409 start = get_timer(0);
410 do {
411 err = mmc_send_op_cond_iter(mmc, &cmd, 1);
412 if (err)
413 return err;
414 if (get_timer(start) > timeout)
415 return UNUSABLE_ERR;
416 udelay(100);
417 } while (!(mmc->op_cond_response & OCR_BUSY));
418
419 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
420 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
421 cmd.resp_type = MMC_RSP_R3;
422 cmd.cmdarg = 0;
423
424 err = mmc_send_cmd(mmc, &cmd, NULL);
425
426 if (err)
427 return err;
428 }
429
430 mmc->version = MMC_VERSION_UNKNOWN;
431 mmc->ocr = cmd.response[0];
432
433 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
434 mmc->rca = 0;
435
436 return 0;
437}
438
439
440static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
441{
442 struct mmc_cmd cmd;
443 struct mmc_data data;
444 int err;
445
446 /* Get the Card Status Register */
447 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
448 cmd.resp_type = MMC_RSP_R1;
449 cmd.cmdarg = 0;
450
451 data.dest = (char *)ext_csd;
452 data.blocks = 1;
453 data.blocksize = MMC_MAX_BLOCK_LEN;
454 data.flags = MMC_DATA_READ;
455
456 err = mmc_send_cmd(mmc, &cmd, &data);
457
458 return err;
459}
460
461
462static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
463{
464 struct mmc_cmd cmd;
465 int timeout = 1000;
466 int ret;
467
468 cmd.cmdidx = MMC_CMD_SWITCH;
469 cmd.resp_type = MMC_RSP_R1b;
470 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
471 (index << 16) |
472 (value << 8);
473
474 ret = mmc_send_cmd(mmc, &cmd, NULL);
475
476 /* Waiting for the ready status */
477 if (!ret)
478 ret = mmc_send_status(mmc, timeout);
479
480 return ret;
481
482}
483
484static int mmc_change_freq(struct mmc *mmc)
485{
486 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
487 char cardtype;
488 int err;
489
490 mmc->card_caps = 0;
491
492 if (mmc_host_is_spi(mmc))
493 return 0;
494
495 /* Only version 4 supports high-speed */
496 if (mmc->version < MMC_VERSION_4)
497 return 0;
498
499 err = mmc_send_ext_csd(mmc, ext_csd);
500
501 if (err)
502 return err;
503
504 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
505
506 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
507
508 if (err)
509 return err;
510
511 /* Now check to see that it worked */
512 err = mmc_send_ext_csd(mmc, ext_csd);
513
514 if (err)
515 return err;
516
517 /* No high-speed support */
518 if (!ext_csd[EXT_CSD_HS_TIMING])
519 return 0;
520
521 /* High Speed is set, there are two types: 52MHz and 26MHz */
522 if (cardtype & MMC_HS_52MHZ)
523 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
524 else
525 mmc->card_caps |= MMC_MODE_HS;
526
527 return 0;
528}
529
530static int mmc_set_capacity(struct mmc *mmc, int part_num)
531{
532 switch (part_num) {
533 case 0:
534 mmc->capacity = mmc->capacity_user;
535 break;
536 case 1:
537 case 2:
538 mmc->capacity = mmc->capacity_boot;
539 break;
540 case 3:
541 mmc->capacity = mmc->capacity_rpmb;
542 break;
543 case 4:
544 case 5:
545 case 6:
546 case 7:
547 mmc->capacity = mmc->capacity_gp[part_num - 4];
548 break;
549 default:
550 return -1;
551 }
552
553 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
554
555 return 0;
556}
557
558int mmc_switch_part(int dev_num, unsigned int part_num)
559{
560 struct mmc *mmc = find_mmc_device(dev_num);
561 int ret;
562
563 if (!mmc)
564 return -1;
565
566 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
567 (mmc->part_config & ~PART_ACCESS_MASK)
568 | (part_num & PART_ACCESS_MASK));
569 if (ret)
570 return ret;
571
572 return mmc_set_capacity(mmc, part_num);
573}
574
575int mmc_getcd(struct mmc *mmc)
576{
577 int cd;
578
579 cd = board_mmc_getcd(mmc);
580
581 if (cd < 0) {
582 if (mmc->getcd)
583 cd = mmc->getcd(mmc);
584 else
585 cd = 1;
586 }
587
588 return cd;
589}
590
591static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
592{
593 struct mmc_cmd cmd;
594 struct mmc_data data;
595
596 /* Switch the frequency */
597 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
598 cmd.resp_type = MMC_RSP_R1;
599 cmd.cmdarg = (mode << 31) | 0xffffff;
600 cmd.cmdarg &= ~(0xf << (group * 4));
601 cmd.cmdarg |= value << (group * 4);
602
603 data.dest = (char *)resp;
604 data.blocksize = 64;
605 data.blocks = 1;
606 data.flags = MMC_DATA_READ;
607
608 return mmc_send_cmd(mmc, &cmd, &data);
609}
610
611
612static int sd_change_freq(struct mmc *mmc)
613{
614 int err;
615 struct mmc_cmd cmd;
616 ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
617 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
618 struct mmc_data data;
619 int timeout;
620
621 mmc->card_caps = 0;
622
623 if (mmc_host_is_spi(mmc))
624 return 0;
625
626 /* Read the SCR to find out if this card supports higher speeds */
627 cmd.cmdidx = MMC_CMD_APP_CMD;
628 cmd.resp_type = MMC_RSP_R1;
629 cmd.cmdarg = mmc->rca << 16;
630
631 err = mmc_send_cmd(mmc, &cmd, NULL);
632
633 if (err)
634 return err;
635
636 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
637 cmd.resp_type = MMC_RSP_R1;
638 cmd.cmdarg = 0;
639
640 timeout = 3;
641
642retry_scr:
643 data.dest = (char *)scr;
644 data.blocksize = 8;
645 data.blocks = 1;
646 data.flags = MMC_DATA_READ;
647
648 err = mmc_send_cmd(mmc, &cmd, &data);
649
650 if (err) {
651 if (timeout--)
652 goto retry_scr;
653
654 return err;
655 }
656
657 mmc->scr[0] = __be32_to_cpu(scr[0]);
658 mmc->scr[1] = __be32_to_cpu(scr[1]);
659
660 switch ((mmc->scr[0] >> 24) & 0xf) {
661 case 0:
662 mmc->version = SD_VERSION_1_0;
663 break;
664 case 1:
665 mmc->version = SD_VERSION_1_10;
666 break;
667 case 2:
668 mmc->version = SD_VERSION_2;
669 if ((mmc->scr[0] >> 15) & 0x1)
670 mmc->version = SD_VERSION_3;
671 break;
672 default:
673 mmc->version = SD_VERSION_1_0;
674 break;
675 }
676
677 if (mmc->scr[0] & SD_DATA_4BIT)
678 mmc->card_caps |= MMC_MODE_4BIT;
679
680 /* Version 1.0 doesn't support switching */
681 if (mmc->version == SD_VERSION_1_0)
682 return 0;
683
684 timeout = 4;
685 while (timeout--) {
686 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
687 (u8 *)switch_status);
688
689 if (err)
690 return err;
691
692 /* The high-speed function is busy. Try again */
693 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
694 break;
695 }
696
697 /* If high-speed isn't supported, we return */
698 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
699 return 0;
700
701 /*
702 * If the host doesn't support SD_HIGHSPEED, do not switch card to
703 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
704 * This can avoid furthur problem when the card runs in different
705 * mode between the host.
706 */
707 if (!((mmc->host_caps & MMC_MODE_HS_52MHz) &&
708 (mmc->host_caps & MMC_MODE_HS)))
709 return 0;
710
711 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
712
713 if (err)
714 return err;
715
716 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
717 mmc->card_caps |= MMC_MODE_HS;
718
719 return 0;
720}
721
722/* frequency bases */
723/* divided by 10 to be nice to platforms without floating point */
724static const int fbase[] = {
725 10000,
726 100000,
727 1000000,
728 10000000,
729};
730
731/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
732 * to platforms without floating point.
733 */
734static const int multipliers[] = {
735 0, /* reserved */
736 10,
737 12,
738 13,
739 15,
740 20,
741 25,
742 30,
743 35,
744 40,
745 45,
746 50,
747 55,
748 60,
749 70,
750 80,
751};
752
753static void mmc_set_ios(struct mmc *mmc)
754{
755 mmc->set_ios(mmc);
756}
757
758static void mmc_set_74_clk(struct mmc *mmc)
759{
760 mmc->set_74_clk(mmc);
761}
762
763void mmc_set_clock(struct mmc *mmc, uint clock)
764{
765 if (clock > mmc->f_max)
766 clock = mmc->f_max;
767
768 if (clock < mmc->f_min)
769 clock = mmc->f_min;
770
771 mmc->clock = clock;
772
773 mmc_set_ios(mmc);
774}
775
776static void mmc_set_bus_width(struct mmc *mmc, uint width)
777{
778 mmc->bus_width = width;
779
780 mmc_set_ios(mmc);
781}
782
783static int mmc_startup(struct mmc *mmc)
784{
785 int err, i;
786 uint mult, freq;
787 u64 cmult, csize, capacity;
788 struct mmc_cmd cmd;
789 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
790 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
791 int timeout = 1000;
792
793#ifdef CONFIG_MMC_SPI_CRC_ON
794 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
795 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
796 cmd.resp_type = MMC_RSP_R1;
797 cmd.cmdarg = 1;
798 err = mmc_send_cmd(mmc, &cmd, NULL);
799
800 if (err)
801 return err;
802 }
803#endif
804
805 /* Put the Card in Identify Mode */
806 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
807 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
808 cmd.resp_type = MMC_RSP_R2;
809 cmd.cmdarg = 0;
810
811 err = mmc_send_cmd(mmc, &cmd, NULL);
812
813 if (err)
814 return err;
815
816 memcpy(mmc->cid, cmd.response, 16);
817
818 /*
819 * For MMC cards, set the Relative Address.
820 * For SD cards, get the Relatvie Address.
821 * This also puts the cards into Standby State
822 */
823 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
824 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
825 cmd.cmdarg = mmc->rca << 16;
826 cmd.resp_type = MMC_RSP_R6;
827
828 err = mmc_send_cmd(mmc, &cmd, NULL);
829
830 if (err)
831 return err;
832
833 if (IS_SD(mmc))
834 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
835 }
836
837 /* Get the Card-Specific Data */
838 cmd.cmdidx = MMC_CMD_SEND_CSD;
839 cmd.resp_type = MMC_RSP_R2;
840 cmd.cmdarg = mmc->rca << 16;
841
842 err = mmc_send_cmd(mmc, &cmd, NULL);
843
844 /* Waiting for the ready status */
845 mmc_send_status(mmc, timeout);
846
847 if (err)
848 return err;
849
850 mmc->csd[0] = cmd.response[0];
851 mmc->csd[1] = cmd.response[1];
852 mmc->csd[2] = cmd.response[2];
853 mmc->csd[3] = cmd.response[3];
854
855 if (mmc->version == MMC_VERSION_UNKNOWN) {
856 int version = (cmd.response[0] >> 26) & 0xf;
857
858 switch (version) {
859 case 0:
860 mmc->version = MMC_VERSION_1_2;
861 break;
862 case 1:
863 mmc->version = MMC_VERSION_1_4;
864 break;
865 case 2:
866 mmc->version = MMC_VERSION_2_2;
867 break;
868 case 3:
869 mmc->version = MMC_VERSION_3;
870 break;
871 case 4:
872 mmc->version = MMC_VERSION_4;
873 break;
874 default:
875 mmc->version = MMC_VERSION_1_2;
876 break;
877 }
878 }
879
880 /* divide frequency by 10, since the mults are 10x bigger */
881 freq = fbase[(cmd.response[0] & 0x7)];
882 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
883
884 mmc->tran_speed = freq * mult;
885
886 mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
887 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
888
889 if (IS_SD(mmc))
890 mmc->write_bl_len = mmc->read_bl_len;
891 else
892 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
893
894 if (mmc->high_capacity) {
895 csize = (mmc->csd[1] & 0x3f) << 16
896 | (mmc->csd[2] & 0xffff0000) >> 16;
897 cmult = 8;
898 } else {
899 csize = (mmc->csd[1] & 0x3ff) << 2
900 | (mmc->csd[2] & 0xc0000000) >> 30;
901 cmult = (mmc->csd[2] & 0x00038000) >> 15;
902 }
903
904 mmc->capacity_user = (csize + 1) << (cmult + 2);
905 mmc->capacity_user *= mmc->read_bl_len;
906 mmc->capacity_boot = 0;
907 mmc->capacity_rpmb = 0;
908 for (i = 0; i < 4; i++)
909 mmc->capacity_gp[i] = 0;
910
911 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
912 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
913
914 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
915 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
916
917 if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
918 cmd.cmdidx = MMC_CMD_SET_DSR;
919 cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
920 cmd.resp_type = MMC_RSP_NONE;
921 if (mmc_send_cmd(mmc, &cmd, NULL))
922 printf("MMC: SET_DSR failed\n");
923 }
924
925 /* Select the card, and put it into Transfer Mode */
926 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
927 cmd.cmdidx = MMC_CMD_SELECT_CARD;
928 cmd.resp_type = MMC_RSP_R1;
929 cmd.cmdarg = mmc->rca << 16;
930 err = mmc_send_cmd(mmc, &cmd, NULL);
931
932 if (err)
933 return err;
934 }
935
936 /*
937 * For SD, its erase group is always one sector
938 */
939 mmc->erase_grp_size = 1;
940 mmc->part_config = MMCPART_NOAVAILABLE;
941 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
942 /* check ext_csd version and capacity */
943 err = mmc_send_ext_csd(mmc, ext_csd);
944 if (!err && (ext_csd[EXT_CSD_REV] >= 2)) {
945 /*
946 * According to the JEDEC Standard, the value of
947 * ext_csd's capacity is valid if the value is more
948 * than 2GB
949 */
950 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
951 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
952 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
953 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
954 capacity *= MMC_MAX_BLOCK_LEN;
955 if ((capacity >> 20) > 2 * 1024)
956 mmc->capacity_user = capacity;
957 }
958
959 switch (ext_csd[EXT_CSD_REV]) {
960 case 1:
961 mmc->version = MMC_VERSION_4_1;
962 break;
963 case 2:
964 mmc->version = MMC_VERSION_4_2;
965 break;
966 case 3:
967 mmc->version = MMC_VERSION_4_3;
968 break;
969 case 5:
970 mmc->version = MMC_VERSION_4_41;
971 break;
972 case 6:
973 mmc->version = MMC_VERSION_4_5;
974 break;
975 }
976
977 /*
978 * Host needs to enable ERASE_GRP_DEF bit if device is
979 * partitioned. This bit will be lost every time after a reset
980 * or power off. This will affect erase size.
981 */
982 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
983 (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB)) {
984 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
985 EXT_CSD_ERASE_GROUP_DEF, 1);
986
987 if (err)
988 return err;
989
990 /* Read out group size from ext_csd */
991 mmc->erase_grp_size =
992 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
993 MMC_MAX_BLOCK_LEN * 1024;
994 } else {
995 /* Calculate the group size from the csd value. */
996 int erase_gsz, erase_gmul;
997 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
998 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
999 mmc->erase_grp_size = (erase_gsz + 1)
1000 * (erase_gmul + 1);
1001 }
1002
1003 /* store the partition info of emmc */
1004 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1005 ext_csd[EXT_CSD_BOOT_MULT])
1006 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
1007
1008 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
1009
1010 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
1011
1012 for (i = 0; i < 4; i++) {
1013 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
1014 mmc->capacity_gp[i] = (ext_csd[idx + 2] << 16) +
1015 (ext_csd[idx + 1] << 8) + ext_csd[idx];
1016 mmc->capacity_gp[i] *=
1017 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1018 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1019 }
1020 }
1021
1022 err = mmc_set_capacity(mmc, mmc->part_num);
1023 if (err)
1024 return err;
1025
1026 if (IS_SD(mmc))
1027 err = sd_change_freq(mmc);
1028 else
1029 err = mmc_change_freq(mmc);
1030
1031 if (err)
1032 return err;
1033
1034 /* Restrict card's capabilities by what the host can do */
1035 mmc->card_caps &= mmc->host_caps;
1036
1037 if (IS_SD(mmc)) {
1038 if (mmc->card_caps & MMC_MODE_4BIT) {
1039 cmd.cmdidx = MMC_CMD_APP_CMD;
1040 cmd.resp_type = MMC_RSP_R1;
1041 cmd.cmdarg = mmc->rca << 16;
1042
1043 err = mmc_send_cmd(mmc, &cmd, NULL);
1044 if (err)
1045 return err;
1046
1047 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1048 cmd.resp_type = MMC_RSP_R1;
1049 cmd.cmdarg = 2;
1050 err = mmc_send_cmd(mmc, &cmd, NULL);
1051 if (err)
1052 return err;
1053
1054 mmc_set_bus_width(mmc, 4);
1055 }
1056
1057 if (mmc->card_caps & MMC_MODE_HS)
1058 mmc->tran_speed = 50000000;
1059 else
1060 mmc->tran_speed = 25000000;
1061 } else {
1062 int idx;
1063
1064 /* An array of possible bus widths in order of preference */
1065 static unsigned ext_csd_bits[] = {
1066 EXT_CSD_BUS_WIDTH_8,
1067 EXT_CSD_BUS_WIDTH_4,
1068 EXT_CSD_BUS_WIDTH_1,
1069 };
1070
1071 /* An array to map CSD bus widths to host cap bits */
1072 static unsigned ext_to_hostcaps[] = {
1073 [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
1074 [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
1075 };
1076
1077 /* An array to map chosen bus width to an integer */
1078 static unsigned widths[] = {
1079 8, 4, 1,
1080 };
1081
1082 for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
1083 unsigned int extw = ext_csd_bits[idx];
1084
1085 /*
1086 * Check to make sure the controller supports
1087 * this bus width, if it's more than 1
1088 */
1089 if (extw != EXT_CSD_BUS_WIDTH_1 &&
1090 !(mmc->host_caps & ext_to_hostcaps[extw]))
1091 continue;
1092
1093 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1094 EXT_CSD_BUS_WIDTH, extw);
1095
1096 if (err)
1097 continue;
1098
1099 mmc_set_bus_width(mmc, widths[idx]);
1100
1101 err = mmc_send_ext_csd(mmc, test_csd);
1102 if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
1103 == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
1104 && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
1105 == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
1106 && ext_csd[EXT_CSD_REV] \
1107 == test_csd[EXT_CSD_REV]
1108 && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
1109 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1110 && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
1111 &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
1112
1113 mmc->card_caps |= ext_to_hostcaps[extw];
1114 break;
1115 }
1116 }
1117
1118 if (mmc->card_caps & MMC_MODE_HS) {
1119 if (mmc->card_caps & MMC_MODE_HS_52MHz)
1120 mmc->tran_speed = 52000000;
1121 else
1122 mmc->tran_speed = 26000000;
1123 }
1124 }
1125
1126 mmc_set_clock(mmc, mmc->tran_speed);
1127
1128 /* fill in device description */
1129 mmc->block_dev.lun = 0;
1130 mmc->block_dev.type = 0;
1131 mmc->block_dev.blksz = mmc->read_bl_len;
1132 mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz);
1133 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
1134#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1135 sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
1136 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1137 (mmc->cid[3] >> 16) & 0xffff);
1138 sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
1139 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1140 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1141 (mmc->cid[2] >> 24) & 0xff);
1142 sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
1143 (mmc->cid[2] >> 16) & 0xf);
1144#else
1145 mmc->block_dev.vendor[0] = 0;
1146 mmc->block_dev.product[0] = 0;
1147 mmc->block_dev.revision[0] = 0;
1148#endif
1149#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1150 init_part(&mmc->block_dev);
1151#endif
1152
1153 return 0;
1154}
1155
1156static int mmc_send_if_cond(struct mmc *mmc)
1157{
1158 struct mmc_cmd cmd;
1159 int err;
1160
1161 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1162 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1163 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
1164 cmd.resp_type = MMC_RSP_R7;
1165
1166 err = mmc_send_cmd(mmc, &cmd, NULL);
1167
1168 if (err)
1169 return err;
1170
1171 if ((cmd.response[0] & 0xff) != 0xaa)
1172 return UNUSABLE_ERR;
1173 else
1174 mmc->version = SD_VERSION_2;
1175
1176 return 0;
1177}
1178
1179static int mmc_dev_init(int dev_num);
1180int mmc_register(struct mmc *mmc)
1181{
1182 /* Setup dsr related values */
1183 mmc->dsr_imp = 0;
1184 mmc->dsr = 0xffffffff;
1185 /* Setup the universal parts of the block interface just once */
1186 mmc->block_dev.if_type = IF_TYPE_MMC;
1187 mmc->block_dev.dev = cur_dev_num++;
1188 mmc->block_dev.removable = 1;
1189 mmc->block_dev.block_read = mmc_bread;
1190 mmc->block_dev.block_write = mmc_bwrite;
1191 mmc->block_dev.block_erase = mmc_berase;
1192 mmc->block_dev.dev_init = mmc_dev_init;
1193 if (!mmc->b_max)
1194 mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1195
1196 INIT_LIST_HEAD (&mmc->link);
1197
1198 list_add_tail (&mmc->link, &mmc_devices);
1199
1200 return 0;
1201}
1202
1203#ifdef CONFIG_PARTITIONS
1204block_dev_desc_t *mmc_get_dev(int dev)
1205{
1206 struct mmc *mmc = find_mmc_device(dev);
1207 if (!mmc || mmc_init(mmc))
1208 return NULL;
1209
1210 return &mmc->block_dev;
1211}
1212#endif
1213
1214int mmc_start_init(struct mmc *mmc)
1215{
1216 int err;
1217
1218 if (mmc_getcd(mmc) == 0) {
1219 mmc->has_init = 0;
1220#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1221 printf("MMC: no card present\n");
1222#endif
1223 return NO_CARD_ERR;
1224 }
1225
1226 if (mmc->has_init)
1227 return 0;
1228
1229 err = mmc->init(mmc);
1230
1231 if (err)
1232 return err;
1233
1234 mmc_set_bus_width(mmc, 1);
1235 mmc_set_clock(mmc, 1);
1236 mmc_set_74_clk(mmc);
1237
1238 /* Reset the Card */
1239 err = mmc_go_idle(mmc);
1240
1241 if (err)
1242 return err;
1243
1244 /* The internal partition reset to user partition(0) at every CMD0*/
1245 mmc->part_num = 0;
1246
1247 /* Test for SD version 2 */
1248 err = mmc_send_if_cond(mmc);
1249
1250 /* Now try to get the SD card's operating condition */
1251 err = sd_send_op_cond(mmc);
1252
1253 /* If the command timed out, we check for an MMC card */
1254 if (err == TIMEOUT) {
1255 err = mmc_send_op_cond(mmc);
1256
1257 if (err && err != IN_PROGRESS) {
1258#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1259 printf("Card did not respond to voltage select!\n");
1260#endif
1261 return UNUSABLE_ERR;
1262 }
1263 }
1264
1265 if (err == IN_PROGRESS)
1266 mmc->init_in_progress = 1;
1267
1268 return err;
1269}
1270
1271static int mmc_complete_init(struct mmc *mmc)
1272{
1273 int err = 0;
1274
1275 if (mmc->op_cond_pending)
1276 err = mmc_complete_op_cond(mmc);
1277
1278 if (!err)
1279 err = mmc_startup(mmc);
1280 if (err)
1281 mmc->has_init = 0;
1282 else
1283 mmc->has_init = 1;
1284 mmc->init_in_progress = 0;
1285 return err;
1286}
1287
1288int mmc_init(struct mmc *mmc)
1289{
1290 int err = IN_PROGRESS;
1291 unsigned start = get_timer(0);
1292
1293 if (mmc->has_init)
1294 return 0;
1295 if (!mmc->init_in_progress)
1296 err = mmc_start_init(mmc);
1297
1298 if (!err || err == IN_PROGRESS)
1299 err = mmc_complete_init(mmc);
1300 debug("%s: %d, time %lu\n", __func__, err, get_timer(start));
1301 return err;
1302}
1303
1304int mmc_set_dsr(struct mmc *mmc, u16 val)
1305{
1306 mmc->dsr = val;
1307 return 0;
1308}
1309
1310static int mmc_dev_init(int dev_num)
1311{
1312 struct mmc *mmc = find_mmc_device(dev_num);
1313 if (!mmc)
1314 return -1;
1315
1316 return mmc_init(mmc);
1317}
1318
1319/*
1320 * CPU and board-specific MMC initializations. Aliased function
1321 * signals caller to move on
1322 */
1323static int __def_mmc_init(bd_t *bis)
1324{
1325 return -1;
1326}
1327
1328int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1329int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1330
1331#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1332
1333void print_mmc_devices(char separator)
1334{
1335 struct mmc *m;
1336 struct list_head *entry;
1337
1338 list_for_each(entry, &mmc_devices) {
1339 m = list_entry(entry, struct mmc, link);
1340
1341 printf("%s: %d", m->name, m->block_dev.dev);
1342
1343 if (entry->next != &mmc_devices)
1344 printf("%c ", separator);
1345 }
1346
1347 printf("\n");
1348}
1349
1350#else
1351void print_mmc_devices(char separator) { }
1352#endif
1353
1354int get_mmc_num(void)
1355{
1356 return cur_dev_num;
1357}
1358
1359void mmc_set_preinit(struct mmc *mmc, int preinit)
1360{
1361 mmc->preinit = preinit;
1362}
1363
1364static void do_preinit(void)
1365{
1366 struct mmc *m;
1367 struct list_head *entry;
1368
1369 list_for_each(entry, &mmc_devices) {
1370 m = list_entry(entry, struct mmc, link);
1371
1372 if (m->preinit)
1373 mmc_start_init(m);
1374 }
1375}
1376
1377
1378int mmc_initialize(bd_t *bis)
1379{
1380 INIT_LIST_HEAD (&mmc_devices);
1381 cur_dev_num = 0;
1382
1383 if (board_mmc_init(bis) < 0)
1384 cpu_mmc_init(bis);
1385
1386#ifndef CONFIG_SPL_BUILD
1387 print_mmc_devices(',');
1388#endif
1389
1390 do_preinit();
1391 return 0;
1392}
1393
1394#ifdef CONFIG_SUPPORT_EMMC_BOOT
1395/*
1396 * This function changes the size of boot partition and the size of rpmb
1397 * partition present on EMMC devices.
1398 *
1399 * Input Parameters:
1400 * struct *mmc: pointer for the mmc device strcuture
1401 * bootsize: size of boot partition
1402 * rpmbsize: size of rpmb partition
1403 *
1404 * Returns 0 on success.
1405 */
1406
1407int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize,
1408 unsigned long rpmbsize)
1409{
1410 int err;
1411 struct mmc_cmd cmd;
1412
1413 /* Only use this command for raw EMMC moviNAND. Enter backdoor mode */
1414 cmd.cmdidx = MMC_CMD_RES_MAN;
1415 cmd.resp_type = MMC_RSP_R1b;
1416 cmd.cmdarg = MMC_CMD62_ARG1;
1417
1418 err = mmc_send_cmd(mmc, &cmd, NULL);
1419 if (err) {
1420 debug("mmc_boot_partition_size_change: Error1 = %d\n", err);
1421 return err;
1422 }
1423
1424 /* Boot partition changing mode */
1425 cmd.cmdidx = MMC_CMD_RES_MAN;
1426 cmd.resp_type = MMC_RSP_R1b;
1427 cmd.cmdarg = MMC_CMD62_ARG2;
1428
1429 err = mmc_send_cmd(mmc, &cmd, NULL);
1430 if (err) {
1431 debug("mmc_boot_partition_size_change: Error2 = %d\n", err);
1432 return err;
1433 }
1434 /* boot partition size is multiple of 128KB */
1435 bootsize = (bootsize * 1024) / 128;
1436
1437 /* Arg: boot partition size */
1438 cmd.cmdidx = MMC_CMD_RES_MAN;
1439 cmd.resp_type = MMC_RSP_R1b;
1440 cmd.cmdarg = bootsize;
1441
1442 err = mmc_send_cmd(mmc, &cmd, NULL);
1443 if (err) {
1444 debug("mmc_boot_partition_size_change: Error3 = %d\n", err);
1445 return err;
1446 }
1447 /* RPMB partition size is multiple of 128KB */
1448 rpmbsize = (rpmbsize * 1024) / 128;
1449 /* Arg: RPMB partition size */
1450 cmd.cmdidx = MMC_CMD_RES_MAN;
1451 cmd.resp_type = MMC_RSP_R1b;
1452 cmd.cmdarg = rpmbsize;
1453
1454 err = mmc_send_cmd(mmc, &cmd, NULL);
1455 if (err) {
1456 debug("mmc_boot_partition_size_change: Error4 = %d\n", err);
1457 return err;
1458 }
1459 return 0;
1460}
1461
1462/*
1463 * This function shall form and send the commands to open / close the
1464 * boot partition specified by user.
1465 *
1466 * Input Parameters:
1467 * ack: 0x0 - No boot acknowledge sent (default)
1468 * 0x1 - Boot acknowledge sent during boot operation
1469 * part_num: User selects boot data that will be sent to master
1470 * 0x0 - Device not boot enabled (default)
1471 * 0x1 - Boot partition 1 enabled for boot
1472 * 0x2 - Boot partition 2 enabled for boot
1473 * access: User selects partitions to access
1474 * 0x0 : No access to boot partition (default)
1475 * 0x1 : R/W boot partition 1
1476 * 0x2 : R/W boot partition 2
1477 * 0x3 : R/W Replay Protected Memory Block (RPMB)
1478 *
1479 * Returns 0 on success.
1480 */
1481int mmc_boot_part_access(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
1482{
1483 int err;
1484 struct mmc_cmd cmd;
1485
1486 /* Boot ack enable, boot partition enable , boot partition access */
1487 cmd.cmdidx = MMC_CMD_SWITCH;
1488 cmd.resp_type = MMC_RSP_R1b;
1489
1490 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1491 (EXT_CSD_PART_CONF << 16) |
1492 ((EXT_CSD_BOOT_ACK(ack) |
1493 EXT_CSD_BOOT_PART_NUM(part_num) |
1494 EXT_CSD_PARTITION_ACCESS(access)) << 8);
1495
1496 err = mmc_send_cmd(mmc, &cmd, NULL);
1497 if (err) {
1498 if (access) {
1499 debug("mmc boot partition#%d open fail:Error1 = %d\n",
1500 part_num, err);
1501 } else {
1502 debug("mmc boot partition#%d close fail:Error = %d\n",
1503 part_num, err);
1504 }
1505 return err;
1506 }
1507
1508 if (access) {
1509 /* 4bit transfer mode at booting time. */
1510 cmd.cmdidx = MMC_CMD_SWITCH;
1511 cmd.resp_type = MMC_RSP_R1b;
1512
1513 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1514 (EXT_CSD_BOOT_BUS_WIDTH << 16) |
1515 ((1 << 0) << 8);
1516
1517 err = mmc_send_cmd(mmc, &cmd, NULL);
1518 if (err) {
1519 debug("mmc boot partition#%d open fail:Error2 = %d\n",
1520 part_num, err);
1521 return err;
1522 }
1523 }
1524 return 0;
1525}
1526#endif