Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Paul Burton | da61fa5 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2008, Freescale Semiconductor, Inc |
| 4 | * Andy Fleming |
| 5 | * |
| 6 | * Based vaguely on the Linux code |
Paul Burton | da61fa5 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <config.h> |
Simon Glass | e6f6f9e | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 10 | #include <blk.h> |
Simon Glass | 33fb211 | 2016-05-01 13:52:41 -0600 | [diff] [blame] | 11 | #include <dm.h> |
Paul Burton | da61fa5 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 12 | #include <part.h> |
Tom Rini | 180f87f | 2015-06-11 20:53:31 -0400 | [diff] [blame] | 13 | #include <div64.h> |
| 14 | #include <linux/math64.h> |
Paul Burton | da61fa5 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 15 | #include "mmc_private.h" |
| 16 | |
Loic Poulain | 67642c1 | 2023-01-26 10:24:18 +0100 | [diff] [blame] | 17 | static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt, u32 args) |
Paul Burton | da61fa5 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 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; |
Loic Poulain | 67642c1 | 2023-01-26 10:24:18 +0100 | [diff] [blame] | 54 | cmd.cmdarg = args ? args : MMC_ERASE_ARG; |
Paul Burton | da61fa5 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 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 | |
Ezequiel Garcia | 2a4bb3d | 2019-01-07 18:13:24 -0300 | [diff] [blame] | 68 | #if CONFIG_IS_ENABLED(BLK) |
Simon Glass | 561e624 | 2016-10-01 14:43:17 -0600 | [diff] [blame] | 69 | ulong mmc_berase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt) |
| 70 | #else |
| 71 | ulong mmc_berase(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt) |
| 72 | #endif |
Paul Burton | da61fa5 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 73 | { |
Ezequiel Garcia | 2a4bb3d | 2019-01-07 18:13:24 -0300 | [diff] [blame] | 74 | #if CONFIG_IS_ENABLED(BLK) |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 75 | struct blk_desc *block_dev = dev_get_uclass_plat(dev); |
Simon Glass | 561e624 | 2016-10-01 14:43:17 -0600 | [diff] [blame] | 76 | #endif |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 77 | int dev_num = block_dev->devnum; |
Paul Burton | da61fa5 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 78 | int err = 0; |
Loic Poulain | 67642c1 | 2023-01-26 10:24:18 +0100 | [diff] [blame] | 79 | u32 start_rem, blkcnt_rem, erase_args = 0; |
Paul Burton | da61fa5 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 80 | struct mmc *mmc = find_mmc_device(dev_num); |
| 81 | lbaint_t blk = 0, blk_r = 0; |
Sam Protsenko | 6cf8a90 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 82 | int timeout_ms = 1000; |
Paul Burton | da61fa5 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 83 | |
| 84 | if (!mmc) |
| 85 | return -1; |
| 86 | |
Simon Glass | e33a5c6 | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 87 | err = blk_select_hwpart_devnum(UCLASS_MMC, dev_num, |
Simon Glass | 69f45cd | 2016-05-01 13:52:29 -0600 | [diff] [blame] | 88 | block_dev->hwpart); |
Stephen Warren | 873cc1d | 2015-12-07 11:38:49 -0700 | [diff] [blame] | 89 | if (err < 0) |
| 90 | return -1; |
| 91 | |
Tom Rini | 180f87f | 2015-06-11 20:53:31 -0400 | [diff] [blame] | 92 | /* |
| 93 | * We want to see if the requested start or total block count are |
| 94 | * unaligned. We discard the whole numbers and only care about the |
| 95 | * remainder. |
| 96 | */ |
| 97 | err = div_u64_rem(start, mmc->erase_grp_size, &start_rem); |
| 98 | err = div_u64_rem(blkcnt, mmc->erase_grp_size, &blkcnt_rem); |
Loic Poulain | 67642c1 | 2023-01-26 10:24:18 +0100 | [diff] [blame] | 99 | if (start_rem || blkcnt_rem) { |
| 100 | if (mmc->can_trim) { |
| 101 | /* Trim function applies the erase operation to write |
| 102 | * blocks instead of erase groups. |
| 103 | */ |
| 104 | erase_args = MMC_TRIM_ARG; |
| 105 | } else { |
| 106 | /* The card ignores all LSB's below the erase group |
| 107 | * size, rounding down the addess to a erase group |
| 108 | * boundary. |
| 109 | */ |
| 110 | printf("\n\nCaution! Your devices Erase group is 0x%x\n" |
| 111 | "The erase range would be change to " |
| 112 | "0x" LBAF "~0x" LBAF "\n\n", |
| 113 | mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1), |
| 114 | ((start + blkcnt + mmc->erase_grp_size - 1) |
| 115 | & ~(mmc->erase_grp_size - 1)) - 1); |
| 116 | } |
| 117 | } |
Paul Burton | da61fa5 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 118 | |
| 119 | while (blk < blkcnt) { |
Peng Fan | e492dbb | 2016-09-01 11:13:39 +0800 | [diff] [blame] | 120 | if (IS_SD(mmc) && mmc->ssr.au) { |
| 121 | blk_r = ((blkcnt - blk) > mmc->ssr.au) ? |
| 122 | mmc->ssr.au : (blkcnt - blk); |
| 123 | } else { |
| 124 | blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ? |
| 125 | mmc->erase_grp_size : (blkcnt - blk); |
| 126 | } |
Loic Poulain | 67642c1 | 2023-01-26 10:24:18 +0100 | [diff] [blame] | 127 | err = mmc_erase_t(mmc, start + blk, blk_r, erase_args); |
Paul Burton | da61fa5 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 128 | if (err) |
| 129 | break; |
| 130 | |
| 131 | blk += blk_r; |
| 132 | |
| 133 | /* Waiting for the ready status */ |
Sam Protsenko | 6cf8a90 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 134 | if (mmc_poll_for_busy(mmc, timeout_ms)) |
Paul Burton | da61fa5 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | return blk; |
| 139 | } |
| 140 | |
| 141 | static ulong mmc_write_blocks(struct mmc *mmc, lbaint_t start, |
| 142 | lbaint_t blkcnt, const void *src) |
| 143 | { |
| 144 | struct mmc_cmd cmd; |
| 145 | struct mmc_data data; |
Sam Protsenko | 6cf8a90 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 146 | int timeout_ms = 1000; |
Paul Burton | da61fa5 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 147 | |
Simon Glass | c40fdca | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 148 | if ((start + blkcnt) > mmc_get_blk_desc(mmc)->lba) { |
Paul Burton | da61fa5 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 149 | printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n", |
Simon Glass | c40fdca | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 150 | start + blkcnt, mmc_get_blk_desc(mmc)->lba); |
Paul Burton | da61fa5 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | if (blkcnt == 0) |
| 155 | return 0; |
| 156 | else if (blkcnt == 1) |
| 157 | cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK; |
| 158 | else |
| 159 | cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK; |
| 160 | |
| 161 | if (mmc->high_capacity) |
| 162 | cmd.cmdarg = start; |
| 163 | else |
| 164 | cmd.cmdarg = start * mmc->write_bl_len; |
| 165 | |
| 166 | cmd.resp_type = MMC_RSP_R1; |
| 167 | |
| 168 | data.src = src; |
| 169 | data.blocks = blkcnt; |
| 170 | data.blocksize = mmc->write_bl_len; |
| 171 | data.flags = MMC_DATA_WRITE; |
| 172 | |
| 173 | if (mmc_send_cmd(mmc, &cmd, &data)) { |
| 174 | printf("mmc write failed\n"); |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | /* SPI multiblock writes terminate using a special |
| 179 | * token, not a STOP_TRANSMISSION request. |
| 180 | */ |
| 181 | if (!mmc_host_is_spi(mmc) && blkcnt > 1) { |
| 182 | cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; |
| 183 | cmd.cmdarg = 0; |
| 184 | cmd.resp_type = MMC_RSP_R1b; |
| 185 | if (mmc_send_cmd(mmc, &cmd, NULL)) { |
| 186 | printf("mmc fail to send stop cmd\n"); |
| 187 | return 0; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /* Waiting for the ready status */ |
Sam Protsenko | 6cf8a90 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 192 | if (mmc_poll_for_busy(mmc, timeout_ms)) |
Paul Burton | da61fa5 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 193 | return 0; |
| 194 | |
| 195 | return blkcnt; |
| 196 | } |
| 197 | |
Ezequiel Garcia | 2a4bb3d | 2019-01-07 18:13:24 -0300 | [diff] [blame] | 198 | #if CONFIG_IS_ENABLED(BLK) |
Simon Glass | 33fb211 | 2016-05-01 13:52:41 -0600 | [diff] [blame] | 199 | ulong mmc_bwrite(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, |
| 200 | const void *src) |
| 201 | #else |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 202 | ulong mmc_bwrite(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt, |
Stephen Warren | 7c4213f | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 203 | const void *src) |
Simon Glass | 33fb211 | 2016-05-01 13:52:41 -0600 | [diff] [blame] | 204 | #endif |
Paul Burton | da61fa5 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 205 | { |
Ezequiel Garcia | 2a4bb3d | 2019-01-07 18:13:24 -0300 | [diff] [blame] | 206 | #if CONFIG_IS_ENABLED(BLK) |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 207 | struct blk_desc *block_dev = dev_get_uclass_plat(dev); |
Simon Glass | 33fb211 | 2016-05-01 13:52:41 -0600 | [diff] [blame] | 208 | #endif |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 209 | int dev_num = block_dev->devnum; |
Paul Burton | da61fa5 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 210 | lbaint_t cur, blocks_todo = blkcnt; |
Stephen Warren | 873cc1d | 2015-12-07 11:38:49 -0700 | [diff] [blame] | 211 | int err; |
Paul Burton | da61fa5 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 212 | |
| 213 | struct mmc *mmc = find_mmc_device(dev_num); |
| 214 | if (!mmc) |
| 215 | return 0; |
| 216 | |
Simon Glass | e33a5c6 | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 217 | err = blk_select_hwpart_devnum(UCLASS_MMC, dev_num, block_dev->hwpart); |
Stephen Warren | 873cc1d | 2015-12-07 11:38:49 -0700 | [diff] [blame] | 218 | if (err < 0) |
| 219 | return 0; |
| 220 | |
Paul Burton | da61fa5 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 221 | if (mmc_set_blocklen(mmc, mmc->write_bl_len)) |
| 222 | return 0; |
| 223 | |
| 224 | do { |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 225 | cur = (blocks_todo > mmc->cfg->b_max) ? |
| 226 | mmc->cfg->b_max : blocks_todo; |
Paul Burton | da61fa5 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 227 | if (mmc_write_blocks(mmc, start, cur, src) != cur) |
| 228 | return 0; |
| 229 | blocks_todo -= cur; |
| 230 | start += cur; |
| 231 | src += cur * mmc->write_bl_len; |
| 232 | } while (blocks_todo > 0); |
| 233 | |
| 234 | return blkcnt; |
| 235 | } |