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