diff options
Diffstat (limited to 'drivers/mmc/core/core.c')
| -rw-r--r-- | drivers/mmc/core/core.c | 346 |
1 files changed, 346 insertions, 0 deletions
diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c index 83240faa1dc8..5db49b124ffa 100644 --- a/drivers/mmc/core/core.c +++ b/drivers/mmc/core/core.c | |||
| @@ -1050,6 +1050,352 @@ void mmc_detect_change(struct mmc_host *host, unsigned long delay) | |||
| 1050 | 1050 | ||
| 1051 | EXPORT_SYMBOL(mmc_detect_change); | 1051 | EXPORT_SYMBOL(mmc_detect_change); |
| 1052 | 1052 | ||
| 1053 | void mmc_init_erase(struct mmc_card *card) | ||
| 1054 | { | ||
| 1055 | unsigned int sz; | ||
| 1056 | |||
| 1057 | if (is_power_of_2(card->erase_size)) | ||
| 1058 | card->erase_shift = ffs(card->erase_size) - 1; | ||
| 1059 | else | ||
| 1060 | card->erase_shift = 0; | ||
| 1061 | |||
| 1062 | /* | ||
| 1063 | * It is possible to erase an arbitrarily large area of an SD or MMC | ||
| 1064 | * card. That is not desirable because it can take a long time | ||
| 1065 | * (minutes) potentially delaying more important I/O, and also the | ||
| 1066 | * timeout calculations become increasingly hugely over-estimated. | ||
| 1067 | * Consequently, 'pref_erase' is defined as a guide to limit erases | ||
| 1068 | * to that size and alignment. | ||
| 1069 | * | ||
| 1070 | * For SD cards that define Allocation Unit size, limit erases to one | ||
| 1071 | * Allocation Unit at a time. For MMC cards that define High Capacity | ||
| 1072 | * Erase Size, whether it is switched on or not, limit to that size. | ||
| 1073 | * Otherwise just have a stab at a good value. For modern cards it | ||
| 1074 | * will end up being 4MiB. Note that if the value is too small, it | ||
| 1075 | * can end up taking longer to erase. | ||
| 1076 | */ | ||
| 1077 | if (mmc_card_sd(card) && card->ssr.au) { | ||
| 1078 | card->pref_erase = card->ssr.au; | ||
| 1079 | card->erase_shift = ffs(card->ssr.au) - 1; | ||
| 1080 | } else if (card->ext_csd.hc_erase_size) { | ||
| 1081 | card->pref_erase = card->ext_csd.hc_erase_size; | ||
| 1082 | } else { | ||
| 1083 | sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11; | ||
| 1084 | if (sz < 128) | ||
| 1085 | card->pref_erase = 512 * 1024 / 512; | ||
| 1086 | else if (sz < 512) | ||
| 1087 | card->pref_erase = 1024 * 1024 / 512; | ||
| 1088 | else if (sz < 1024) | ||
| 1089 | card->pref_erase = 2 * 1024 * 1024 / 512; | ||
| 1090 | else | ||
| 1091 | card->pref_erase = 4 * 1024 * 1024 / 512; | ||
| 1092 | if (card->pref_erase < card->erase_size) | ||
| 1093 | card->pref_erase = card->erase_size; | ||
| 1094 | else { | ||
| 1095 | sz = card->pref_erase % card->erase_size; | ||
| 1096 | if (sz) | ||
| 1097 | card->pref_erase += card->erase_size - sz; | ||
| 1098 | } | ||
| 1099 | } | ||
| 1100 | } | ||
| 1101 | |||
| 1102 | static void mmc_set_mmc_erase_timeout(struct mmc_card *card, | ||
| 1103 | struct mmc_command *cmd, | ||
| 1104 | unsigned int arg, unsigned int qty) | ||
| 1105 | { | ||
| 1106 | unsigned int erase_timeout; | ||
| 1107 | |||
| 1108 | if (card->ext_csd.erase_group_def & 1) { | ||
| 1109 | /* High Capacity Erase Group Size uses HC timeouts */ | ||
| 1110 | if (arg == MMC_TRIM_ARG) | ||
| 1111 | erase_timeout = card->ext_csd.trim_timeout; | ||
| 1112 | else | ||
| 1113 | erase_timeout = card->ext_csd.hc_erase_timeout; | ||
| 1114 | } else { | ||
| 1115 | /* CSD Erase Group Size uses write timeout */ | ||
| 1116 | unsigned int mult = (10 << card->csd.r2w_factor); | ||
| 1117 | unsigned int timeout_clks = card->csd.tacc_clks * mult; | ||
| 1118 | unsigned int timeout_us; | ||
| 1119 | |||
| 1120 | /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */ | ||
| 1121 | if (card->csd.tacc_ns < 1000000) | ||
| 1122 | timeout_us = (card->csd.tacc_ns * mult) / 1000; | ||
| 1123 | else | ||
| 1124 | timeout_us = (card->csd.tacc_ns / 1000) * mult; | ||
| 1125 | |||
| 1126 | /* | ||
| 1127 | * ios.clock is only a target. The real clock rate might be | ||
| 1128 | * less but not that much less, so fudge it by multiplying by 2. | ||
| 1129 | */ | ||
| 1130 | timeout_clks <<= 1; | ||
| 1131 | timeout_us += (timeout_clks * 1000) / | ||
| 1132 | (card->host->ios.clock / 1000); | ||
| 1133 | |||
| 1134 | erase_timeout = timeout_us / 1000; | ||
| 1135 | |||
| 1136 | /* | ||
| 1137 | * Theoretically, the calculation could underflow so round up | ||
| 1138 | * to 1ms in that case. | ||
| 1139 | */ | ||
| 1140 | if (!erase_timeout) | ||
| 1141 | erase_timeout = 1; | ||
| 1142 | } | ||
| 1143 | |||
| 1144 | /* Multiplier for secure operations */ | ||
| 1145 | if (arg & MMC_SECURE_ARGS) { | ||
| 1146 | if (arg == MMC_SECURE_ERASE_ARG) | ||
| 1147 | erase_timeout *= card->ext_csd.sec_erase_mult; | ||
| 1148 | else | ||
| 1149 | erase_timeout *= card->ext_csd.sec_trim_mult; | ||
| 1150 | } | ||
| 1151 | |||
| 1152 | erase_timeout *= qty; | ||
| 1153 | |||
| 1154 | /* | ||
| 1155 | * Ensure at least a 1 second timeout for SPI as per | ||
| 1156 | * 'mmc_set_data_timeout()' | ||
| 1157 | */ | ||
| 1158 | if (mmc_host_is_spi(card->host) && erase_timeout < 1000) | ||
| 1159 | erase_timeout = 1000; | ||
| 1160 | |||
| 1161 | cmd->erase_timeout = erase_timeout; | ||
| 1162 | } | ||
| 1163 | |||
| 1164 | static void mmc_set_sd_erase_timeout(struct mmc_card *card, | ||
| 1165 | struct mmc_command *cmd, unsigned int arg, | ||
| 1166 | unsigned int qty) | ||
| 1167 | { | ||
| 1168 | if (card->ssr.erase_timeout) { | ||
| 1169 | /* Erase timeout specified in SD Status Register (SSR) */ | ||
| 1170 | cmd->erase_timeout = card->ssr.erase_timeout * qty + | ||
| 1171 | card->ssr.erase_offset; | ||
| 1172 | } else { | ||
| 1173 | /* | ||
| 1174 | * Erase timeout not specified in SD Status Register (SSR) so | ||
| 1175 | * use 250ms per write block. | ||
| 1176 | */ | ||
| 1177 | cmd->erase_timeout = 250 * qty; | ||
| 1178 | } | ||
| 1179 | |||
| 1180 | /* Must not be less than 1 second */ | ||
| 1181 | if (cmd->erase_timeout < 1000) | ||
| 1182 | cmd->erase_timeout = 1000; | ||
| 1183 | } | ||
| 1184 | |||
| 1185 | static void mmc_set_erase_timeout(struct mmc_card *card, | ||
| 1186 | struct mmc_command *cmd, unsigned int arg, | ||
| 1187 | unsigned int qty) | ||
| 1188 | { | ||
| 1189 | if (mmc_card_sd(card)) | ||
| 1190 | mmc_set_sd_erase_timeout(card, cmd, arg, qty); | ||
| 1191 | else | ||
| 1192 | mmc_set_mmc_erase_timeout(card, cmd, arg, qty); | ||
| 1193 | } | ||
| 1194 | |||
| 1195 | static int mmc_do_erase(struct mmc_card *card, unsigned int from, | ||
| 1196 | unsigned int to, unsigned int arg) | ||
| 1197 | { | ||
| 1198 | struct mmc_command cmd; | ||
| 1199 | unsigned int qty = 0; | ||
| 1200 | int err; | ||
| 1201 | |||
| 1202 | /* | ||
| 1203 | * qty is used to calculate the erase timeout which depends on how many | ||
| 1204 | * erase groups (or allocation units in SD terminology) are affected. | ||
| 1205 | * We count erasing part of an erase group as one erase group. | ||
| 1206 | * For SD, the allocation units are always a power of 2. For MMC, the | ||
| 1207 | * erase group size is almost certainly also power of 2, but it does not | ||
| 1208 | * seem to insist on that in the JEDEC standard, so we fall back to | ||
| 1209 | * division in that case. SD may not specify an allocation unit size, | ||
| 1210 | * in which case the timeout is based on the number of write blocks. | ||
| 1211 | * | ||
| 1212 | * Note that the timeout for secure trim 2 will only be correct if the | ||
| 1213 | * number of erase groups specified is the same as the total of all | ||
| 1214 | * preceding secure trim 1 commands. Since the power may have been | ||
| 1215 | * lost since the secure trim 1 commands occurred, it is generally | ||
| 1216 | * impossible to calculate the secure trim 2 timeout correctly. | ||
| 1217 | */ | ||
| 1218 | if (card->erase_shift) | ||
| 1219 | qty += ((to >> card->erase_shift) - | ||
| 1220 | (from >> card->erase_shift)) + 1; | ||
| 1221 | else if (mmc_card_sd(card)) | ||
| 1222 | qty += to - from + 1; | ||
| 1223 | else | ||
| 1224 | qty += ((to / card->erase_size) - | ||
| 1225 | (from / card->erase_size)) + 1; | ||
| 1226 | |||
| 1227 | if (!mmc_card_blockaddr(card)) { | ||
| 1228 | from <<= 9; | ||
| 1229 | to <<= 9; | ||
| 1230 | } | ||
| 1231 | |||
| 1232 | memset(&cmd, 0, sizeof(struct mmc_command)); | ||
| 1233 | if (mmc_card_sd(card)) | ||
| 1234 | cmd.opcode = SD_ERASE_WR_BLK_START; | ||
| 1235 | else | ||
| 1236 | cmd.opcode = MMC_ERASE_GROUP_START; | ||
| 1237 | cmd.arg = from; | ||
| 1238 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; | ||
| 1239 | err = mmc_wait_for_cmd(card->host, &cmd, 0); | ||
| 1240 | if (err) { | ||
| 1241 | printk(KERN_ERR "mmc_erase: group start error %d, " | ||
| 1242 | "status %#x\n", err, cmd.resp[0]); | ||
| 1243 | err = -EINVAL; | ||
| 1244 | goto out; | ||
| 1245 | } | ||
| 1246 | |||
| 1247 | memset(&cmd, 0, sizeof(struct mmc_command)); | ||
| 1248 | if (mmc_card_sd(card)) | ||
| 1249 | cmd.opcode = SD_ERASE_WR_BLK_END; | ||
| 1250 | else | ||
| 1251 | cmd.opcode = MMC_ERASE_GROUP_END; | ||
| 1252 | cmd.arg = to; | ||
| 1253 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; | ||
| 1254 | err = mmc_wait_for_cmd(card->host, &cmd, 0); | ||
| 1255 | if (err) { | ||
| 1256 | printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n", | ||
| 1257 | err, cmd.resp[0]); | ||
| 1258 | err = -EINVAL; | ||
| 1259 | goto out; | ||
| 1260 | } | ||
| 1261 | |||
| 1262 | memset(&cmd, 0, sizeof(struct mmc_command)); | ||
| 1263 | cmd.opcode = MMC_ERASE; | ||
| 1264 | cmd.arg = arg; | ||
| 1265 | cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; | ||
| 1266 | mmc_set_erase_timeout(card, &cmd, arg, qty); | ||
| 1267 | err = mmc_wait_for_cmd(card->host, &cmd, 0); | ||
| 1268 | if (err) { | ||
| 1269 | printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n", | ||
| 1270 | err, cmd.resp[0]); | ||
| 1271 | err = -EIO; | ||
| 1272 | goto out; | ||
| 1273 | } | ||
| 1274 | |||
| 1275 | if (mmc_host_is_spi(card->host)) | ||
| 1276 | goto out; | ||
| 1277 | |||
| 1278 | do { | ||
| 1279 | memset(&cmd, 0, sizeof(struct mmc_command)); | ||
| 1280 | cmd.opcode = MMC_SEND_STATUS; | ||
| 1281 | cmd.arg = card->rca << 16; | ||
| 1282 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; | ||
| 1283 | /* Do not retry else we can't see errors */ | ||
| 1284 | err = mmc_wait_for_cmd(card->host, &cmd, 0); | ||
| 1285 | if (err || (cmd.resp[0] & 0xFDF92000)) { | ||
| 1286 | printk(KERN_ERR "error %d requesting status %#x\n", | ||
| 1287 | err, cmd.resp[0]); | ||
| 1288 | err = -EIO; | ||
| 1289 | goto out; | ||
| 1290 | } | ||
| 1291 | } while (!(cmd.resp[0] & R1_READY_FOR_DATA) || | ||
| 1292 | R1_CURRENT_STATE(cmd.resp[0]) == 7); | ||
| 1293 | out: | ||
| 1294 | return err; | ||
| 1295 | } | ||
| 1296 | |||
| 1297 | /** | ||
| 1298 | * mmc_erase - erase sectors. | ||
| 1299 | * @card: card to erase | ||
| 1300 | * @from: first sector to erase | ||
| 1301 | * @nr: number of sectors to erase | ||
| 1302 | * @arg: erase command argument (SD supports only %MMC_ERASE_ARG) | ||
| 1303 | * | ||
| 1304 | * Caller must claim host before calling this function. | ||
| 1305 | */ | ||
| 1306 | int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr, | ||
| 1307 | unsigned int arg) | ||
| 1308 | { | ||
| 1309 | unsigned int rem, to = from + nr; | ||
| 1310 | |||
| 1311 | if (!(card->host->caps & MMC_CAP_ERASE) || | ||
| 1312 | !(card->csd.cmdclass & CCC_ERASE)) | ||
| 1313 | return -EOPNOTSUPP; | ||
| 1314 | |||
| 1315 | if (!card->erase_size) | ||
| 1316 | return -EOPNOTSUPP; | ||
| 1317 | |||
| 1318 | if (mmc_card_sd(card) && arg != MMC_ERASE_ARG) | ||
| 1319 | return -EOPNOTSUPP; | ||
| 1320 | |||
| 1321 | if ((arg & MMC_SECURE_ARGS) && | ||
| 1322 | !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)) | ||
| 1323 | return -EOPNOTSUPP; | ||
| 1324 | |||
| 1325 | if ((arg & MMC_TRIM_ARGS) && | ||
| 1326 | !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)) | ||
| 1327 | return -EOPNOTSUPP; | ||
| 1328 | |||
| 1329 | if (arg == MMC_SECURE_ERASE_ARG) { | ||
| 1330 | if (from % card->erase_size || nr % card->erase_size) | ||
| 1331 | return -EINVAL; | ||
| 1332 | } | ||
| 1333 | |||
| 1334 | if (arg == MMC_ERASE_ARG) { | ||
| 1335 | rem = from % card->erase_size; | ||
| 1336 | if (rem) { | ||
| 1337 | rem = card->erase_size - rem; | ||
| 1338 | from += rem; | ||
| 1339 | if (nr > rem) | ||
| 1340 | nr -= rem; | ||
| 1341 | else | ||
| 1342 | return 0; | ||
| 1343 | } | ||
| 1344 | rem = nr % card->erase_size; | ||
| 1345 | if (rem) | ||
| 1346 | nr -= rem; | ||
| 1347 | } | ||
| 1348 | |||
| 1349 | if (nr == 0) | ||
| 1350 | return 0; | ||
| 1351 | |||
| 1352 | to = from + nr; | ||
| 1353 | |||
| 1354 | if (to <= from) | ||
| 1355 | return -EINVAL; | ||
| 1356 | |||
| 1357 | /* 'from' and 'to' are inclusive */ | ||
| 1358 | to -= 1; | ||
| 1359 | |||
| 1360 | return mmc_do_erase(card, from, to, arg); | ||
| 1361 | } | ||
| 1362 | EXPORT_SYMBOL(mmc_erase); | ||
| 1363 | |||
| 1364 | int mmc_can_erase(struct mmc_card *card) | ||
| 1365 | { | ||
| 1366 | if ((card->host->caps & MMC_CAP_ERASE) && | ||
| 1367 | (card->csd.cmdclass & CCC_ERASE) && card->erase_size) | ||
| 1368 | return 1; | ||
| 1369 | return 0; | ||
| 1370 | } | ||
| 1371 | EXPORT_SYMBOL(mmc_can_erase); | ||
| 1372 | |||
| 1373 | int mmc_can_trim(struct mmc_card *card) | ||
| 1374 | { | ||
| 1375 | if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN) | ||
| 1376 | return 1; | ||
| 1377 | return 0; | ||
| 1378 | } | ||
| 1379 | EXPORT_SYMBOL(mmc_can_trim); | ||
| 1380 | |||
| 1381 | int mmc_can_secure_erase_trim(struct mmc_card *card) | ||
| 1382 | { | ||
| 1383 | if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN) | ||
| 1384 | return 1; | ||
| 1385 | return 0; | ||
| 1386 | } | ||
| 1387 | EXPORT_SYMBOL(mmc_can_secure_erase_trim); | ||
| 1388 | |||
| 1389 | int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from, | ||
| 1390 | unsigned int nr) | ||
| 1391 | { | ||
| 1392 | if (!card->erase_size) | ||
| 1393 | return 0; | ||
| 1394 | if (from % card->erase_size || nr % card->erase_size) | ||
| 1395 | return 0; | ||
| 1396 | return 1; | ||
| 1397 | } | ||
| 1398 | EXPORT_SYMBOL(mmc_erase_group_aligned); | ||
| 1053 | 1399 | ||
| 1054 | void mmc_rescan(struct work_struct *work) | 1400 | void mmc_rescan(struct work_struct *work) |
| 1055 | { | 1401 | { |
