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 10 #include <config.h> 11 #include <common.h> 12 #include <part.h> 13 #include <div64.h> 14 #include <linux/math64.h> 15 #include "mmc_private.h" 16 17 static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt) 18 { 19 struct mmc_cmd cmd; 20 ulong end; 21 int err, start_cmd, end_cmd; 22 23 if (mmc->high_capacity) { 24 end = start + blkcnt - 1; 25 } else { 26 end = (start + blkcnt - 1) * mmc->write_bl_len; 27 start *= mmc->write_bl_len; 28 } 29 30 if (IS_SD(mmc)) { 31 start_cmd = SD_CMD_ERASE_WR_BLK_START; 32 end_cmd = SD_CMD_ERASE_WR_BLK_END; 33 } else { 34 start_cmd = MMC_CMD_ERASE_GROUP_START; 35 end_cmd = MMC_CMD_ERASE_GROUP_END; 36 } 37 38 cmd.cmdidx = start_cmd; 39 cmd.cmdarg = start; 40 cmd.resp_type = MMC_RSP_R1; 41 42 err = mmc_send_cmd(mmc, &cmd, NULL); 43 if (err) 44 goto err_out; 45 46 cmd.cmdidx = end_cmd; 47 cmd.cmdarg = end; 48 49 err = mmc_send_cmd(mmc, &cmd, NULL); 50 if (err) 51 goto err_out; 52 53 cmd.cmdidx = MMC_CMD_ERASE; 54 cmd.cmdarg = MMC_ERASE_ARG; 55 cmd.resp_type = MMC_RSP_R1b; 56 57 err = mmc_send_cmd(mmc, &cmd, NULL); 58 if (err) 59 goto err_out; 60 61 return 0; 62 63 err_out: 64 puts("mmc erase failed\n"); 65 return err; 66 } 67 68 unsigned long mmc_berase(struct blk_desc *block_dev, lbaint_t start, 69 lbaint_t blkcnt) 70 { 71 int dev_num = block_dev->devnum; 72 int err = 0; 73 u32 start_rem, blkcnt_rem; 74 struct mmc *mmc = find_mmc_device(dev_num); 75 lbaint_t blk = 0, blk_r = 0; 76 int timeout = 1000; 77 78 if (!mmc) 79 return -1; 80 81 err = mmc_select_hwpart(dev_num, block_dev->hwpart); 82 if (err < 0) 83 return -1; 84 85 /* 86 * We want to see if the requested start or total block count are 87 * unaligned. We discard the whole numbers and only care about the 88 * remainder. 89 */ 90 err = div_u64_rem(start, mmc->erase_grp_size, &start_rem); 91 err = div_u64_rem(blkcnt, mmc->erase_grp_size, &blkcnt_rem); 92 if (start_rem || blkcnt_rem) 93 printf("\n\nCaution! Your devices Erase group is 0x%x\n" 94 "The erase range would be change to " 95 "0x" LBAF "~0x" LBAF "\n\n", 96 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1), 97 ((start + blkcnt + mmc->erase_grp_size) 98 & ~(mmc->erase_grp_size - 1)) - 1); 99 100 while (blk < blkcnt) { 101 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ? 102 mmc->erase_grp_size : (blkcnt - blk); 103 err = mmc_erase_t(mmc, start + blk, blk_r); 104 if (err) 105 break; 106 107 blk += blk_r; 108 109 /* Waiting for the ready status */ 110 if (mmc_send_status(mmc, timeout)) 111 return 0; 112 } 113 114 return blk; 115 } 116 117 static ulong mmc_write_blocks(struct mmc *mmc, lbaint_t start, 118 lbaint_t blkcnt, const void *src) 119 { 120 struct mmc_cmd cmd; 121 struct mmc_data data; 122 int timeout = 1000; 123 124 if ((start + blkcnt) > mmc->block_dev.lba) { 125 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n", 126 start + blkcnt, mmc->block_dev.lba); 127 return 0; 128 } 129 130 if (blkcnt == 0) 131 return 0; 132 else if (blkcnt == 1) 133 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK; 134 else 135 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK; 136 137 if (mmc->high_capacity) 138 cmd.cmdarg = start; 139 else 140 cmd.cmdarg = start * mmc->write_bl_len; 141 142 cmd.resp_type = MMC_RSP_R1; 143 144 data.src = src; 145 data.blocks = blkcnt; 146 data.blocksize = mmc->write_bl_len; 147 data.flags = MMC_DATA_WRITE; 148 149 if (mmc_send_cmd(mmc, &cmd, &data)) { 150 printf("mmc write failed\n"); 151 return 0; 152 } 153 154 /* SPI multiblock writes terminate using a special 155 * token, not a STOP_TRANSMISSION request. 156 */ 157 if (!mmc_host_is_spi(mmc) && blkcnt > 1) { 158 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; 159 cmd.cmdarg = 0; 160 cmd.resp_type = MMC_RSP_R1b; 161 if (mmc_send_cmd(mmc, &cmd, NULL)) { 162 printf("mmc fail to send stop cmd\n"); 163 return 0; 164 } 165 } 166 167 /* Waiting for the ready status */ 168 if (mmc_send_status(mmc, timeout)) 169 return 0; 170 171 return blkcnt; 172 } 173 174 ulong mmc_bwrite(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt, 175 const void *src) 176 { 177 int dev_num = block_dev->devnum; 178 lbaint_t cur, blocks_todo = blkcnt; 179 int err; 180 181 struct mmc *mmc = find_mmc_device(dev_num); 182 if (!mmc) 183 return 0; 184 185 err = mmc_select_hwpart(dev_num, block_dev->hwpart); 186 if (err < 0) 187 return 0; 188 189 if (mmc_set_blocklen(mmc, mmc->write_bl_len)) 190 return 0; 191 192 do { 193 cur = (blocks_todo > mmc->cfg->b_max) ? 194 mmc->cfg->b_max : blocks_todo; 195 if (mmc_write_blocks(mmc, start, cur, src) != cur) 196 return 0; 197 blocks_todo -= cur; 198 start += cur; 199 src += cur * mmc->write_bl_len; 200 } while (blocks_todo > 0); 201 202 return blkcnt; 203 } 204