mmc_write.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  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. #include <config.h>
  10. #include <common.h>
  11. #include <part.h>
  12. #include "mmc_private.h"
  13. static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
  14. {
  15. struct mmc_cmd cmd;
  16. ulong end;
  17. int err, start_cmd, end_cmd;
  18. if (mmc->high_capacity) {
  19. end = start + blkcnt - 1;
  20. } else {
  21. end = (start + blkcnt - 1) * mmc->write_bl_len;
  22. start *= mmc->write_bl_len;
  23. }
  24. if (IS_SD(mmc)) {
  25. start_cmd = SD_CMD_ERASE_WR_BLK_START;
  26. end_cmd = SD_CMD_ERASE_WR_BLK_END;
  27. } else {
  28. start_cmd = MMC_CMD_ERASE_GROUP_START;
  29. end_cmd = MMC_CMD_ERASE_GROUP_END;
  30. }
  31. cmd.cmdidx = start_cmd;
  32. cmd.cmdarg = start;
  33. cmd.resp_type = MMC_RSP_R1;
  34. err = mmc_send_cmd(mmc, &cmd, NULL);
  35. if (err)
  36. goto err_out;
  37. cmd.cmdidx = end_cmd;
  38. cmd.cmdarg = end;
  39. err = mmc_send_cmd(mmc, &cmd, NULL);
  40. if (err)
  41. goto err_out;
  42. cmd.cmdidx = MMC_CMD_ERASE;
  43. cmd.cmdarg = SECURE_ERASE;
  44. cmd.resp_type = MMC_RSP_R1b;
  45. err = mmc_send_cmd(mmc, &cmd, NULL);
  46. if (err)
  47. goto err_out;
  48. return 0;
  49. err_out:
  50. puts("mmc erase failed\n");
  51. return err;
  52. }
  53. unsigned long mmc_berase(int dev_num, lbaint_t start, lbaint_t blkcnt)
  54. {
  55. int err = 0;
  56. struct mmc *mmc = find_mmc_device(dev_num);
  57. lbaint_t blk = 0, blk_r = 0;
  58. int timeout = 1000;
  59. if (!mmc)
  60. return -1;
  61. if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size))
  62. printf("\n\nCaution! Your devices Erase group is 0x%x\n"
  63. "The erase range would be change to "
  64. "0x" LBAF "~0x" LBAF "\n\n",
  65. mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
  66. ((start + blkcnt + mmc->erase_grp_size)
  67. & ~(mmc->erase_grp_size - 1)) - 1);
  68. while (blk < blkcnt) {
  69. blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
  70. mmc->erase_grp_size : (blkcnt - blk);
  71. err = mmc_erase_t(mmc, start + blk, blk_r);
  72. if (err)
  73. break;
  74. blk += blk_r;
  75. /* Waiting for the ready status */
  76. if (mmc_send_status(mmc, timeout))
  77. return 0;
  78. }
  79. return blk;
  80. }
  81. static ulong mmc_write_blocks(struct mmc *mmc, lbaint_t start,
  82. lbaint_t blkcnt, const void *src)
  83. {
  84. struct mmc_cmd cmd;
  85. struct mmc_data data;
  86. int timeout = 1000;
  87. if ((start + blkcnt) > mmc->block_dev.lba) {
  88. printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
  89. start + blkcnt, mmc->block_dev.lba);
  90. return 0;
  91. }
  92. if (blkcnt == 0)
  93. return 0;
  94. else if (blkcnt == 1)
  95. cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
  96. else
  97. cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
  98. if (mmc->high_capacity)
  99. cmd.cmdarg = start;
  100. else
  101. cmd.cmdarg = start * mmc->write_bl_len;
  102. cmd.resp_type = MMC_RSP_R1;
  103. data.src = src;
  104. data.blocks = blkcnt;
  105. data.blocksize = mmc->write_bl_len;
  106. data.flags = MMC_DATA_WRITE;
  107. if (mmc_send_cmd(mmc, &cmd, &data)) {
  108. printf("mmc write failed\n");
  109. return 0;
  110. }
  111. /* SPI multiblock writes terminate using a special
  112. * token, not a STOP_TRANSMISSION request.
  113. */
  114. if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
  115. cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
  116. cmd.cmdarg = 0;
  117. cmd.resp_type = MMC_RSP_R1b;
  118. if (mmc_send_cmd(mmc, &cmd, NULL)) {
  119. printf("mmc fail to send stop cmd\n");
  120. return 0;
  121. }
  122. }
  123. /* Waiting for the ready status */
  124. if (mmc_send_status(mmc, timeout))
  125. return 0;
  126. return blkcnt;
  127. }
  128. ulong mmc_bwrite(int dev_num, lbaint_t start, lbaint_t blkcnt, const void *src)
  129. {
  130. lbaint_t cur, blocks_todo = blkcnt;
  131. struct mmc *mmc = find_mmc_device(dev_num);
  132. if (!mmc)
  133. return 0;
  134. if (mmc_set_blocklen(mmc, mmc->write_bl_len))
  135. return 0;
  136. do {
  137. cur = (blocks_todo > mmc->cfg->b_max) ?
  138. mmc->cfg->b_max : blocks_todo;
  139. if (mmc_write_blocks(mmc, start, cur, src) != cur)
  140. return 0;
  141. blocks_todo -= cur;
  142. start += cur;
  143. src += cur * mmc->write_bl_len;
  144. } while (blocks_todo > 0);
  145. return blkcnt;
  146. }