.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * linux/drivers/mmc/core/core.c |
---|
3 | 4 | * |
---|
.. | .. |
---|
5 | 6 | * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved. |
---|
6 | 7 | * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved. |
---|
7 | 8 | * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved. |
---|
8 | | - * |
---|
9 | | - * This program is free software; you can redistribute it and/or modify |
---|
10 | | - * it under the terms of the GNU General Public License version 2 as |
---|
11 | | - * published by the Free Software Foundation. |
---|
12 | 9 | */ |
---|
13 | 10 | #include <linux/module.h> |
---|
14 | 11 | #include <linux/init.h> |
---|
.. | .. |
---|
21 | 18 | #include <linux/leds.h> |
---|
22 | 19 | #include <linux/scatterlist.h> |
---|
23 | 20 | #include <linux/log2.h> |
---|
24 | | -#include <linux/regulator/consumer.h> |
---|
25 | 21 | #include <linux/pm_runtime.h> |
---|
26 | 22 | #include <linux/pm_wakeup.h> |
---|
27 | 23 | #include <linux/suspend.h> |
---|
.. | .. |
---|
41 | 37 | |
---|
42 | 38 | #include "core.h" |
---|
43 | 39 | #include "card.h" |
---|
| 40 | +#include "crypto.h" |
---|
44 | 41 | #include "bus.h" |
---|
45 | 42 | #include "host.h" |
---|
46 | 43 | #include "sdio_bus.h" |
---|
.. | .. |
---|
52 | 49 | |
---|
53 | 50 | /* The max erase timeout, used when host->max_busy_timeout isn't specified */ |
---|
54 | 51 | #define MMC_ERASE_TIMEOUT_MS (60 * 1000) /* 60 s */ |
---|
| 52 | +#define SD_DISCARD_TIMEOUT_MS (250) |
---|
55 | 53 | |
---|
56 | 54 | /* |
---|
57 | 55 | * Enabling software CRCs on the data blocks can be a significant (30%) |
---|
.. | .. |
---|
404 | 402 | |
---|
405 | 403 | cmd = mrq->cmd; |
---|
406 | 404 | |
---|
407 | | - /* |
---|
408 | | - * If host has timed out waiting for the sanitize |
---|
409 | | - * to complete, card might be still in programming state |
---|
410 | | - * so let's try to bring the card out of programming |
---|
411 | | - * state. |
---|
412 | | - */ |
---|
413 | | - if (cmd->sanitize_busy && cmd->error == -ETIMEDOUT) { |
---|
414 | | - if (!mmc_interrupt_hpi(host->card)) { |
---|
415 | | - pr_warn("%s: %s: Interrupted sanitize\n", |
---|
416 | | - mmc_hostname(host), __func__); |
---|
417 | | - cmd->error = 0; |
---|
418 | | - break; |
---|
419 | | - } else { |
---|
420 | | - pr_err("%s: %s: Failed to interrupt sanitize\n", |
---|
421 | | - mmc_hostname(host), __func__); |
---|
422 | | - } |
---|
423 | | - } |
---|
424 | 405 | if (!cmd->error || !cmd->retries || |
---|
425 | 406 | mmc_card_removed(host->card)) |
---|
426 | 407 | break; |
---|
.. | .. |
---|
757 | 738 | } |
---|
758 | 739 | EXPORT_SYMBOL(mmc_set_data_timeout); |
---|
759 | 740 | |
---|
760 | | -/** |
---|
761 | | - * mmc_align_data_size - pads a transfer size to a more optimal value |
---|
762 | | - * @card: the MMC card associated with the data transfer |
---|
763 | | - * @sz: original transfer size |
---|
764 | | - * |
---|
765 | | - * Pads the original data size with a number of extra bytes in |
---|
766 | | - * order to avoid controller bugs and/or performance hits |
---|
767 | | - * (e.g. some controllers revert to PIO for certain sizes). |
---|
768 | | - * |
---|
769 | | - * Returns the improved size, which might be unmodified. |
---|
770 | | - * |
---|
771 | | - * Note that this function is only relevant when issuing a |
---|
772 | | - * single scatter gather entry. |
---|
773 | | - */ |
---|
774 | | -unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz) |
---|
775 | | -{ |
---|
776 | | - /* |
---|
777 | | - * FIXME: We don't have a system for the controller to tell |
---|
778 | | - * the core about its problems yet, so for now we just 32-bit |
---|
779 | | - * align the size. |
---|
780 | | - */ |
---|
781 | | - sz = ((sz + 3) / 4) * 4; |
---|
782 | | - |
---|
783 | | - return sz; |
---|
784 | | -} |
---|
785 | | -EXPORT_SYMBOL(mmc_align_data_size); |
---|
786 | | - |
---|
787 | 741 | /* |
---|
788 | 742 | * Allow claiming an already claimed host if the context is the same or there is |
---|
789 | 743 | * no context but the task is the same. |
---|
.. | .. |
---|
886 | 840 | spin_unlock_irqrestore(&host->lock, flags); |
---|
887 | 841 | wake_up(&host->wq); |
---|
888 | 842 | pm_runtime_mark_last_busy(mmc_dev(host)); |
---|
889 | | - pm_runtime_put_autosuspend(mmc_dev(host)); |
---|
| 843 | + if (host->caps & MMC_CAP_SYNC_RUNTIME_PM) |
---|
| 844 | + pm_runtime_put_sync_suspend(mmc_dev(host)); |
---|
| 845 | + else |
---|
| 846 | + pm_runtime_put_autosuspend(mmc_dev(host)); |
---|
890 | 847 | } |
---|
891 | 848 | } |
---|
892 | 849 | EXPORT_SYMBOL(mmc_release_host); |
---|
.. | .. |
---|
958 | 915 | host->ios.clock = hz; |
---|
959 | 916 | mmc_set_ios(host); |
---|
960 | 917 | } |
---|
| 918 | +EXPORT_SYMBOL_GPL(mmc_set_clock); |
---|
961 | 919 | |
---|
962 | 920 | int mmc_execute_tuning(struct mmc_card *card) |
---|
963 | 921 | { |
---|
.. | .. |
---|
1037 | 995 | host->ops->hs400_enhanced_strobe(host, &host->ios); |
---|
1038 | 996 | |
---|
1039 | 997 | mmc_set_ios(host); |
---|
| 998 | + |
---|
| 999 | + mmc_crypto_set_initial_state(host); |
---|
1040 | 1000 | } |
---|
| 1001 | +EXPORT_SYMBOL_GPL(mmc_set_initial_state); |
---|
1041 | 1002 | |
---|
1042 | 1003 | /** |
---|
1043 | 1004 | * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number |
---|
.. | .. |
---|
1111 | 1072 | |
---|
1112 | 1073 | return mask; |
---|
1113 | 1074 | } |
---|
1114 | | -EXPORT_SYMBOL(mmc_vddrange_to_ocrmask); |
---|
1115 | | - |
---|
1116 | | -#ifdef CONFIG_OF |
---|
1117 | | - |
---|
1118 | | -/** |
---|
1119 | | - * mmc_of_parse_voltage - return mask of supported voltages |
---|
1120 | | - * @np: The device node need to be parsed. |
---|
1121 | | - * @mask: mask of voltages available for MMC/SD/SDIO |
---|
1122 | | - * |
---|
1123 | | - * Parse the "voltage-ranges" DT property, returning zero if it is not |
---|
1124 | | - * found, negative errno if the voltage-range specification is invalid, |
---|
1125 | | - * or one if the voltage-range is specified and successfully parsed. |
---|
1126 | | - */ |
---|
1127 | | -int mmc_of_parse_voltage(struct device_node *np, u32 *mask) |
---|
1128 | | -{ |
---|
1129 | | - const u32 *voltage_ranges; |
---|
1130 | | - int num_ranges, i; |
---|
1131 | | - |
---|
1132 | | - voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges); |
---|
1133 | | - num_ranges = num_ranges / sizeof(*voltage_ranges) / 2; |
---|
1134 | | - if (!voltage_ranges) { |
---|
1135 | | - pr_debug("%pOF: voltage-ranges unspecified\n", np); |
---|
1136 | | - return 0; |
---|
1137 | | - } |
---|
1138 | | - if (!num_ranges) { |
---|
1139 | | - pr_err("%pOF: voltage-ranges empty\n", np); |
---|
1140 | | - return -EINVAL; |
---|
1141 | | - } |
---|
1142 | | - |
---|
1143 | | - for (i = 0; i < num_ranges; i++) { |
---|
1144 | | - const int j = i * 2; |
---|
1145 | | - u32 ocr_mask; |
---|
1146 | | - |
---|
1147 | | - ocr_mask = mmc_vddrange_to_ocrmask( |
---|
1148 | | - be32_to_cpu(voltage_ranges[j]), |
---|
1149 | | - be32_to_cpu(voltage_ranges[j + 1])); |
---|
1150 | | - if (!ocr_mask) { |
---|
1151 | | - pr_err("%pOF: voltage-range #%d is invalid\n", |
---|
1152 | | - np, i); |
---|
1153 | | - return -EINVAL; |
---|
1154 | | - } |
---|
1155 | | - *mask |= ocr_mask; |
---|
1156 | | - } |
---|
1157 | | - |
---|
1158 | | - return 1; |
---|
1159 | | -} |
---|
1160 | | -EXPORT_SYMBOL(mmc_of_parse_voltage); |
---|
1161 | | - |
---|
1162 | | -#endif /* CONFIG_OF */ |
---|
1163 | 1075 | |
---|
1164 | 1076 | static int mmc_of_get_func_num(struct device_node *node) |
---|
1165 | 1077 | { |
---|
.. | .. |
---|
1188 | 1100 | |
---|
1189 | 1101 | return NULL; |
---|
1190 | 1102 | } |
---|
1191 | | - |
---|
1192 | | -#ifdef CONFIG_REGULATOR |
---|
1193 | | - |
---|
1194 | | -/** |
---|
1195 | | - * mmc_ocrbitnum_to_vdd - Convert a OCR bit number to its voltage |
---|
1196 | | - * @vdd_bit: OCR bit number |
---|
1197 | | - * @min_uV: minimum voltage value (mV) |
---|
1198 | | - * @max_uV: maximum voltage value (mV) |
---|
1199 | | - * |
---|
1200 | | - * This function returns the voltage range according to the provided OCR |
---|
1201 | | - * bit number. If conversion is not possible a negative errno value returned. |
---|
1202 | | - */ |
---|
1203 | | -static int mmc_ocrbitnum_to_vdd(int vdd_bit, int *min_uV, int *max_uV) |
---|
1204 | | -{ |
---|
1205 | | - int tmp; |
---|
1206 | | - |
---|
1207 | | - if (!vdd_bit) |
---|
1208 | | - return -EINVAL; |
---|
1209 | | - |
---|
1210 | | - /* |
---|
1211 | | - * REVISIT mmc_vddrange_to_ocrmask() may have set some |
---|
1212 | | - * bits this regulator doesn't quite support ... don't |
---|
1213 | | - * be too picky, most cards and regulators are OK with |
---|
1214 | | - * a 0.1V range goof (it's a small error percentage). |
---|
1215 | | - */ |
---|
1216 | | - tmp = vdd_bit - ilog2(MMC_VDD_165_195); |
---|
1217 | | - if (tmp == 0) { |
---|
1218 | | - *min_uV = 1650 * 1000; |
---|
1219 | | - *max_uV = 1950 * 1000; |
---|
1220 | | - } else { |
---|
1221 | | - *min_uV = 1900 * 1000 + tmp * 100 * 1000; |
---|
1222 | | - *max_uV = *min_uV + 100 * 1000; |
---|
1223 | | - } |
---|
1224 | | - |
---|
1225 | | - return 0; |
---|
1226 | | -} |
---|
1227 | | - |
---|
1228 | | -/** |
---|
1229 | | - * mmc_regulator_get_ocrmask - return mask of supported voltages |
---|
1230 | | - * @supply: regulator to use |
---|
1231 | | - * |
---|
1232 | | - * This returns either a negative errno, or a mask of voltages that |
---|
1233 | | - * can be provided to MMC/SD/SDIO devices using the specified voltage |
---|
1234 | | - * regulator. This would normally be called before registering the |
---|
1235 | | - * MMC host adapter. |
---|
1236 | | - */ |
---|
1237 | | -int mmc_regulator_get_ocrmask(struct regulator *supply) |
---|
1238 | | -{ |
---|
1239 | | - int result = 0; |
---|
1240 | | - int count; |
---|
1241 | | - int i; |
---|
1242 | | - int vdd_uV; |
---|
1243 | | - int vdd_mV; |
---|
1244 | | - |
---|
1245 | | - count = regulator_count_voltages(supply); |
---|
1246 | | - if (count < 0) |
---|
1247 | | - return count; |
---|
1248 | | - |
---|
1249 | | - for (i = 0; i < count; i++) { |
---|
1250 | | - vdd_uV = regulator_list_voltage(supply, i); |
---|
1251 | | - if (vdd_uV <= 0) |
---|
1252 | | - continue; |
---|
1253 | | - |
---|
1254 | | - vdd_mV = vdd_uV / 1000; |
---|
1255 | | - result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV); |
---|
1256 | | - } |
---|
1257 | | - |
---|
1258 | | - if (!result) { |
---|
1259 | | - vdd_uV = regulator_get_voltage(supply); |
---|
1260 | | - if (vdd_uV <= 0) |
---|
1261 | | - return vdd_uV; |
---|
1262 | | - |
---|
1263 | | - vdd_mV = vdd_uV / 1000; |
---|
1264 | | - result = mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV); |
---|
1265 | | - } |
---|
1266 | | - |
---|
1267 | | - return result; |
---|
1268 | | -} |
---|
1269 | | -EXPORT_SYMBOL_GPL(mmc_regulator_get_ocrmask); |
---|
1270 | | - |
---|
1271 | | -/** |
---|
1272 | | - * mmc_regulator_set_ocr - set regulator to match host->ios voltage |
---|
1273 | | - * @mmc: the host to regulate |
---|
1274 | | - * @supply: regulator to use |
---|
1275 | | - * @vdd_bit: zero for power off, else a bit number (host->ios.vdd) |
---|
1276 | | - * |
---|
1277 | | - * Returns zero on success, else negative errno. |
---|
1278 | | - * |
---|
1279 | | - * MMC host drivers may use this to enable or disable a regulator using |
---|
1280 | | - * a particular supply voltage. This would normally be called from the |
---|
1281 | | - * set_ios() method. |
---|
1282 | | - */ |
---|
1283 | | -int mmc_regulator_set_ocr(struct mmc_host *mmc, |
---|
1284 | | - struct regulator *supply, |
---|
1285 | | - unsigned short vdd_bit) |
---|
1286 | | -{ |
---|
1287 | | - int result = 0; |
---|
1288 | | - int min_uV, max_uV; |
---|
1289 | | - |
---|
1290 | | - if (vdd_bit) { |
---|
1291 | | - mmc_ocrbitnum_to_vdd(vdd_bit, &min_uV, &max_uV); |
---|
1292 | | - |
---|
1293 | | - result = regulator_set_voltage(supply, min_uV, max_uV); |
---|
1294 | | - if (result == 0 && !mmc->regulator_enabled) { |
---|
1295 | | - result = regulator_enable(supply); |
---|
1296 | | - if (!result) |
---|
1297 | | - mmc->regulator_enabled = true; |
---|
1298 | | - } |
---|
1299 | | - } else if (mmc->regulator_enabled) { |
---|
1300 | | - result = regulator_disable(supply); |
---|
1301 | | - if (result == 0) |
---|
1302 | | - mmc->regulator_enabled = false; |
---|
1303 | | - } |
---|
1304 | | - |
---|
1305 | | - if (result) |
---|
1306 | | - dev_err(mmc_dev(mmc), |
---|
1307 | | - "could not set regulator OCR (%d)\n", result); |
---|
1308 | | - return result; |
---|
1309 | | -} |
---|
1310 | | -EXPORT_SYMBOL_GPL(mmc_regulator_set_ocr); |
---|
1311 | | - |
---|
1312 | | -static int mmc_regulator_set_voltage_if_supported(struct regulator *regulator, |
---|
1313 | | - int min_uV, int target_uV, |
---|
1314 | | - int max_uV) |
---|
1315 | | -{ |
---|
1316 | | - /* |
---|
1317 | | - * Check if supported first to avoid errors since we may try several |
---|
1318 | | - * signal levels during power up and don't want to show errors. |
---|
1319 | | - */ |
---|
1320 | | - if (!regulator_is_supported_voltage(regulator, min_uV, max_uV)) |
---|
1321 | | - return -EINVAL; |
---|
1322 | | - |
---|
1323 | | - return regulator_set_voltage_triplet(regulator, min_uV, target_uV, |
---|
1324 | | - max_uV); |
---|
1325 | | -} |
---|
1326 | | - |
---|
1327 | | -/** |
---|
1328 | | - * mmc_regulator_set_vqmmc - Set VQMMC as per the ios |
---|
1329 | | - * |
---|
1330 | | - * For 3.3V signaling, we try to match VQMMC to VMMC as closely as possible. |
---|
1331 | | - * That will match the behavior of old boards where VQMMC and VMMC were supplied |
---|
1332 | | - * by the same supply. The Bus Operating conditions for 3.3V signaling in the |
---|
1333 | | - * SD card spec also define VQMMC in terms of VMMC. |
---|
1334 | | - * If this is not possible we'll try the full 2.7-3.6V of the spec. |
---|
1335 | | - * |
---|
1336 | | - * For 1.2V and 1.8V signaling we'll try to get as close as possible to the |
---|
1337 | | - * requested voltage. This is definitely a good idea for UHS where there's a |
---|
1338 | | - * separate regulator on the card that's trying to make 1.8V and it's best if |
---|
1339 | | - * we match. |
---|
1340 | | - * |
---|
1341 | | - * This function is expected to be used by a controller's |
---|
1342 | | - * start_signal_voltage_switch() function. |
---|
1343 | | - */ |
---|
1344 | | -int mmc_regulator_set_vqmmc(struct mmc_host *mmc, struct mmc_ios *ios) |
---|
1345 | | -{ |
---|
1346 | | - struct device *dev = mmc_dev(mmc); |
---|
1347 | | - int ret, volt, min_uV, max_uV; |
---|
1348 | | - |
---|
1349 | | - /* If no vqmmc supply then we can't change the voltage */ |
---|
1350 | | - if (IS_ERR(mmc->supply.vqmmc)) |
---|
1351 | | - return -EINVAL; |
---|
1352 | | - |
---|
1353 | | - switch (ios->signal_voltage) { |
---|
1354 | | - case MMC_SIGNAL_VOLTAGE_120: |
---|
1355 | | - return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc, |
---|
1356 | | - 1100000, 1200000, 1300000); |
---|
1357 | | - case MMC_SIGNAL_VOLTAGE_180: |
---|
1358 | | - return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc, |
---|
1359 | | - 1700000, 1800000, 1950000); |
---|
1360 | | - case MMC_SIGNAL_VOLTAGE_330: |
---|
1361 | | - ret = mmc_ocrbitnum_to_vdd(mmc->ios.vdd, &volt, &max_uV); |
---|
1362 | | - if (ret < 0) |
---|
1363 | | - return ret; |
---|
1364 | | - |
---|
1365 | | - dev_dbg(dev, "%s: found vmmc voltage range of %d-%duV\n", |
---|
1366 | | - __func__, volt, max_uV); |
---|
1367 | | - |
---|
1368 | | - min_uV = max(volt - 300000, 2700000); |
---|
1369 | | - max_uV = min(max_uV + 200000, 3600000); |
---|
1370 | | - |
---|
1371 | | - /* |
---|
1372 | | - * Due to a limitation in the current implementation of |
---|
1373 | | - * regulator_set_voltage_triplet() which is taking the lowest |
---|
1374 | | - * voltage possible if below the target, search for a suitable |
---|
1375 | | - * voltage in two steps and try to stay close to vmmc |
---|
1376 | | - * with a 0.3V tolerance at first. |
---|
1377 | | - */ |
---|
1378 | | - if (!mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc, |
---|
1379 | | - min_uV, volt, max_uV)) |
---|
1380 | | - return 0; |
---|
1381 | | - |
---|
1382 | | - return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc, |
---|
1383 | | - 2700000, volt, 3600000); |
---|
1384 | | - default: |
---|
1385 | | - return -EINVAL; |
---|
1386 | | - } |
---|
1387 | | -} |
---|
1388 | | -EXPORT_SYMBOL_GPL(mmc_regulator_set_vqmmc); |
---|
1389 | | - |
---|
1390 | | -#endif /* CONFIG_REGULATOR */ |
---|
1391 | | - |
---|
1392 | | -/** |
---|
1393 | | - * mmc_regulator_get_supply - try to get VMMC and VQMMC regulators for a host |
---|
1394 | | - * @mmc: the host to regulate |
---|
1395 | | - * |
---|
1396 | | - * Returns 0 or errno. errno should be handled, it is either a critical error |
---|
1397 | | - * or -EPROBE_DEFER. 0 means no critical error but it does not mean all |
---|
1398 | | - * regulators have been found because they all are optional. If you require |
---|
1399 | | - * certain regulators, you need to check separately in your driver if they got |
---|
1400 | | - * populated after calling this function. |
---|
1401 | | - */ |
---|
1402 | | -int mmc_regulator_get_supply(struct mmc_host *mmc) |
---|
1403 | | -{ |
---|
1404 | | - struct device *dev = mmc_dev(mmc); |
---|
1405 | | - int ret; |
---|
1406 | | - |
---|
1407 | | - mmc->supply.vmmc = devm_regulator_get_optional(dev, "vmmc"); |
---|
1408 | | - mmc->supply.vqmmc = devm_regulator_get_optional(dev, "vqmmc"); |
---|
1409 | | - |
---|
1410 | | - if (IS_ERR(mmc->supply.vmmc)) { |
---|
1411 | | - if (PTR_ERR(mmc->supply.vmmc) == -EPROBE_DEFER) |
---|
1412 | | - return -EPROBE_DEFER; |
---|
1413 | | - dev_dbg(dev, "No vmmc regulator found\n"); |
---|
1414 | | - } else { |
---|
1415 | | - ret = mmc_regulator_get_ocrmask(mmc->supply.vmmc); |
---|
1416 | | - if (ret > 0) |
---|
1417 | | - mmc->ocr_avail = ret; |
---|
1418 | | - else |
---|
1419 | | - dev_warn(dev, "Failed getting OCR mask: %d\n", ret); |
---|
1420 | | - } |
---|
1421 | | - |
---|
1422 | | - if (IS_ERR(mmc->supply.vqmmc)) { |
---|
1423 | | - if (PTR_ERR(mmc->supply.vqmmc) == -EPROBE_DEFER) |
---|
1424 | | - return -EPROBE_DEFER; |
---|
1425 | | - dev_dbg(dev, "No vqmmc regulator found\n"); |
---|
1426 | | - } |
---|
1427 | | - |
---|
1428 | | - return 0; |
---|
1429 | | -} |
---|
1430 | | -EXPORT_SYMBOL_GPL(mmc_regulator_get_supply); |
---|
1431 | 1103 | |
---|
1432 | 1104 | /* |
---|
1433 | 1105 | * Mask off any voltages we don't support and select |
---|
.. | .. |
---|
1459 | 1131 | mmc_power_cycle(host, ocr); |
---|
1460 | 1132 | } else { |
---|
1461 | 1133 | bit = fls(ocr) - 1; |
---|
1462 | | - ocr &= 3 << bit; |
---|
| 1134 | + /* |
---|
| 1135 | + * The bit variable represents the highest voltage bit set in |
---|
| 1136 | + * the OCR register. |
---|
| 1137 | + * To keep a range of 2 values (e.g. 3.2V/3.3V and 3.3V/3.4V), |
---|
| 1138 | + * we must shift the mask '3' with (bit - 1). |
---|
| 1139 | + */ |
---|
| 1140 | + ocr &= 3 << (bit - 1); |
---|
1463 | 1141 | if (bit != host->ios.vdd) |
---|
1464 | 1142 | dev_warn(mmc_dev(host), "exceeding card's volts\n"); |
---|
1465 | 1143 | } |
---|
.. | .. |
---|
1590 | 1268 | host->ios.timing = timing; |
---|
1591 | 1269 | mmc_set_ios(host); |
---|
1592 | 1270 | } |
---|
| 1271 | +EXPORT_SYMBOL_GPL(mmc_set_timing); |
---|
1593 | 1272 | |
---|
1594 | 1273 | /* |
---|
1595 | 1274 | * Select appropriate driver type for host. |
---|
.. | .. |
---|
1786 | 1465 | mmc_bus_put(host); |
---|
1787 | 1466 | } |
---|
1788 | 1467 | |
---|
1789 | | -static void _mmc_detect_change(struct mmc_host *host, unsigned long delay, |
---|
1790 | | - bool cd_irq) |
---|
| 1468 | +void _mmc_detect_change(struct mmc_host *host, unsigned long delay, bool cd_irq) |
---|
1791 | 1469 | { |
---|
1792 | 1470 | /* |
---|
1793 | | - * If the device is configured as wakeup, we prevent a new sleep for |
---|
1794 | | - * 5 s to give provision for user space to consume the event. |
---|
| 1471 | + * Prevent system sleep for 5s to allow user space to consume the |
---|
| 1472 | + * corresponding uevent. This is especially useful, when CD irq is used |
---|
| 1473 | + * as a system wakeup, but doesn't hurt in other cases. |
---|
1795 | 1474 | */ |
---|
1796 | | - if (cd_irq && !(host->caps & MMC_CAP_NEEDS_POLL) && |
---|
1797 | | - device_can_wakeup(mmc_dev(host))) |
---|
1798 | | - pm_wakeup_event(mmc_dev(host), 5000); |
---|
| 1475 | + if (cd_irq && !(host->caps & MMC_CAP_NEEDS_POLL)) |
---|
| 1476 | + __pm_wakeup_event(host->ws, 5000); |
---|
1799 | 1477 | |
---|
1800 | 1478 | host->detect_change = 1; |
---|
1801 | 1479 | mmc_schedule_delayed_work(&host->detect, delay); |
---|
.. | .. |
---|
1863 | 1541 | } |
---|
1864 | 1542 | } else |
---|
1865 | 1543 | card->pref_erase = 0; |
---|
| 1544 | +} |
---|
| 1545 | + |
---|
| 1546 | +static bool is_trim_arg(unsigned int arg) |
---|
| 1547 | +{ |
---|
| 1548 | + return (arg & MMC_TRIM_OR_DISCARD_ARGS) && arg != MMC_DISCARD_ARG; |
---|
1866 | 1549 | } |
---|
1867 | 1550 | |
---|
1868 | 1551 | static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card, |
---|
.. | .. |
---|
1935 | 1618 | { |
---|
1936 | 1619 | unsigned int erase_timeout; |
---|
1937 | 1620 | |
---|
| 1621 | + /* for DISCARD none of the below calculation applies. |
---|
| 1622 | + * the busy timeout is 250msec per discard command. |
---|
| 1623 | + */ |
---|
| 1624 | + if (arg == SD_DISCARD_ARG) |
---|
| 1625 | + return SD_DISCARD_TIMEOUT_MS; |
---|
| 1626 | + |
---|
1938 | 1627 | if (card->ssr.erase_timeout) { |
---|
1939 | 1628 | /* Erase timeout specified in SD Status Register (SSR) */ |
---|
1940 | 1629 | erase_timeout = card->ssr.erase_timeout * qty + |
---|
.. | .. |
---|
1970 | 1659 | struct mmc_command cmd = {}; |
---|
1971 | 1660 | unsigned int qty = 0, busy_timeout = 0; |
---|
1972 | 1661 | bool use_r1b_resp = false; |
---|
1973 | | - unsigned long timeout; |
---|
1974 | | - int loop_udelay=64, udelay_max=32768; |
---|
1975 | 1662 | int err; |
---|
1976 | 1663 | |
---|
1977 | 1664 | mmc_retune_hold(card->host); |
---|
.. | .. |
---|
2075 | 1762 | if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp) |
---|
2076 | 1763 | goto out; |
---|
2077 | 1764 | |
---|
2078 | | - timeout = jiffies + msecs_to_jiffies(busy_timeout); |
---|
2079 | | - do { |
---|
2080 | | - memset(&cmd, 0, sizeof(struct mmc_command)); |
---|
2081 | | - cmd.opcode = MMC_SEND_STATUS; |
---|
2082 | | - cmd.arg = card->rca << 16; |
---|
2083 | | - cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
---|
2084 | | - /* Do not retry else we can't see errors */ |
---|
2085 | | - err = mmc_wait_for_cmd(card->host, &cmd, 0); |
---|
2086 | | - if (err || R1_STATUS(cmd.resp[0])) { |
---|
2087 | | - pr_err("error %d requesting status %#x\n", |
---|
2088 | | - err, cmd.resp[0]); |
---|
2089 | | - err = -EIO; |
---|
2090 | | - goto out; |
---|
2091 | | - } |
---|
2092 | | - |
---|
2093 | | - /* Timeout if the device never becomes ready for data and |
---|
2094 | | - * never leaves the program state. |
---|
2095 | | - */ |
---|
2096 | | - if (time_after(jiffies, timeout)) { |
---|
2097 | | - pr_err("%s: Card stuck in programming state! %s\n", |
---|
2098 | | - mmc_hostname(card->host), __func__); |
---|
2099 | | - err = -EIO; |
---|
2100 | | - goto out; |
---|
2101 | | - } |
---|
2102 | | - if ((cmd.resp[0] & R1_READY_FOR_DATA) && |
---|
2103 | | - R1_CURRENT_STATE(cmd.resp[0]) != R1_STATE_PRG) |
---|
2104 | | - break; |
---|
2105 | | - |
---|
2106 | | - usleep_range(loop_udelay, loop_udelay*2); |
---|
2107 | | - if (loop_udelay < udelay_max) |
---|
2108 | | - loop_udelay *= 2; |
---|
2109 | | - } while (1); |
---|
| 1765 | + /* Let's poll to find out when the erase operation completes. */ |
---|
| 1766 | + err = mmc_poll_for_busy(card, busy_timeout, MMC_BUSY_ERASE); |
---|
2110 | 1767 | |
---|
2111 | 1768 | out: |
---|
2112 | 1769 | mmc_retune_release(card->host); |
---|
.. | .. |
---|
2166 | 1823 | * @card: card to erase |
---|
2167 | 1824 | * @from: first sector to erase |
---|
2168 | 1825 | * @nr: number of sectors to erase |
---|
2169 | | - * @arg: erase command argument (SD supports only %MMC_ERASE_ARG) |
---|
| 1826 | + * @arg: erase command argument |
---|
2170 | 1827 | * |
---|
2171 | 1828 | * Caller must claim host before calling this function. |
---|
2172 | 1829 | */ |
---|
.. | .. |
---|
2176 | 1833 | unsigned int rem, to = from + nr; |
---|
2177 | 1834 | int err; |
---|
2178 | 1835 | |
---|
2179 | | - if (!(card->host->caps & MMC_CAP_ERASE) || |
---|
2180 | | - !(card->csd.cmdclass & CCC_ERASE)) |
---|
| 1836 | + if (!(card->csd.cmdclass & CCC_ERASE)) |
---|
2181 | 1837 | return -EOPNOTSUPP; |
---|
2182 | 1838 | |
---|
2183 | 1839 | if (!card->erase_size) |
---|
2184 | 1840 | return -EOPNOTSUPP; |
---|
2185 | 1841 | |
---|
2186 | | - if (mmc_card_sd(card) && arg != MMC_ERASE_ARG) |
---|
| 1842 | + if (mmc_card_sd(card) && arg != SD_ERASE_ARG && arg != SD_DISCARD_ARG) |
---|
2187 | 1843 | return -EOPNOTSUPP; |
---|
2188 | 1844 | |
---|
2189 | | - if ((arg & MMC_SECURE_ARGS) && |
---|
| 1845 | + if (mmc_card_mmc(card) && (arg & MMC_SECURE_ARGS) && |
---|
2190 | 1846 | !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)) |
---|
2191 | 1847 | return -EOPNOTSUPP; |
---|
2192 | 1848 | |
---|
2193 | | - if ((arg & MMC_TRIM_ARGS) && |
---|
| 1849 | + if (mmc_card_mmc(card) && is_trim_arg(arg) && |
---|
2194 | 1850 | !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)) |
---|
2195 | 1851 | return -EOPNOTSUPP; |
---|
2196 | 1852 | |
---|
.. | .. |
---|
2220 | 1876 | * identified by the card->eg_boundary flag. |
---|
2221 | 1877 | */ |
---|
2222 | 1878 | rem = card->erase_size - (from % card->erase_size); |
---|
2223 | | - if ((arg & MMC_TRIM_ARGS) && (card->eg_boundary) && (nr > rem)) { |
---|
| 1879 | + if ((arg & MMC_TRIM_OR_DISCARD_ARGS) && card->eg_boundary && nr > rem) { |
---|
2224 | 1880 | err = mmc_do_erase(card, from, from + rem - 1, arg); |
---|
2225 | 1881 | from += rem; |
---|
2226 | 1882 | if ((err) || (to <= from)) |
---|
.. | .. |
---|
2233 | 1889 | |
---|
2234 | 1890 | int mmc_can_erase(struct mmc_card *card) |
---|
2235 | 1891 | { |
---|
2236 | | - if ((card->host->caps & MMC_CAP_ERASE) && |
---|
2237 | | - (card->csd.cmdclass & CCC_ERASE) && card->erase_size) |
---|
| 1892 | + if (card->csd.cmdclass & CCC_ERASE && card->erase_size) |
---|
2238 | 1893 | return 1; |
---|
2239 | 1894 | return 0; |
---|
2240 | 1895 | } |
---|
.. | .. |
---|
2269 | 1924 | return 1; |
---|
2270 | 1925 | return 0; |
---|
2271 | 1926 | } |
---|
2272 | | -EXPORT_SYMBOL(mmc_can_sanitize); |
---|
2273 | 1927 | |
---|
2274 | 1928 | int mmc_can_secure_erase_trim(struct mmc_card *card) |
---|
2275 | 1929 | { |
---|
.. | .. |
---|
2418 | 2072 | } |
---|
2419 | 2073 | EXPORT_SYMBOL(mmc_set_blocklen); |
---|
2420 | 2074 | |
---|
2421 | | -int mmc_set_blockcount(struct mmc_card *card, unsigned int blockcount, |
---|
2422 | | - bool is_rel_write) |
---|
2423 | | -{ |
---|
2424 | | - struct mmc_command cmd = {}; |
---|
2425 | | - |
---|
2426 | | - cmd.opcode = MMC_SET_BLOCK_COUNT; |
---|
2427 | | - cmd.arg = blockcount & 0x0000FFFF; |
---|
2428 | | - if (is_rel_write) |
---|
2429 | | - cmd.arg |= 1 << 31; |
---|
2430 | | - cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
---|
2431 | | - return mmc_wait_for_cmd(card->host, &cmd, 5); |
---|
2432 | | -} |
---|
2433 | | -EXPORT_SYMBOL(mmc_set_blockcount); |
---|
2434 | | - |
---|
2435 | 2075 | #ifndef CONFIG_ROCKCHIP_THUNDER_BOOT_MMC |
---|
2436 | 2076 | static void mmc_hw_reset_for_init(struct mmc_host *host) |
---|
2437 | 2077 | { |
---|
.. | .. |
---|
2443 | 2083 | } |
---|
2444 | 2084 | #endif |
---|
2445 | 2085 | |
---|
| 2086 | +/** |
---|
| 2087 | + * mmc_hw_reset - reset the card in hardware |
---|
| 2088 | + * @host: MMC host to which the card is attached |
---|
| 2089 | + * |
---|
| 2090 | + * Hard reset the card. This function is only for upper layers, like the |
---|
| 2091 | + * block layer or card drivers. You cannot use it in host drivers (struct |
---|
| 2092 | + * mmc_card might be gone then). |
---|
| 2093 | + * |
---|
| 2094 | + * Return: 0 on success, -errno on failure |
---|
| 2095 | + */ |
---|
2446 | 2096 | int mmc_hw_reset(struct mmc_host *host) |
---|
2447 | 2097 | { |
---|
2448 | 2098 | int ret; |
---|
.. | .. |
---|
2459 | 2109 | ret = host->bus_ops->hw_reset(host); |
---|
2460 | 2110 | mmc_bus_put(host); |
---|
2461 | 2111 | |
---|
2462 | | - if (ret) |
---|
| 2112 | + if (ret < 0) |
---|
2463 | 2113 | pr_warn("%s: tried to HW reset card, got error %d\n", |
---|
2464 | 2114 | mmc_hostname(host), ret); |
---|
2465 | 2115 | |
---|
.. | .. |
---|
2508 | 2158 | mmc_hw_reset_for_init(host); |
---|
2509 | 2159 | #endif |
---|
2510 | 2160 | |
---|
2511 | | -#ifdef CONFIG_SDIO_KEEPALIVE |
---|
2512 | | - if (host->support_chip_alive) { |
---|
2513 | | - host->chip_alive = 1; |
---|
2514 | | - if (!mmc_attach_sdio(host)) { |
---|
2515 | | - return 0; |
---|
2516 | | - } else { |
---|
2517 | | - pr_err("%s: chip_alive attach sdio failed.\n", mmc_hostname(host)); |
---|
2518 | | - host->chip_alive = 0; |
---|
2519 | | - } |
---|
2520 | | - } else { |
---|
2521 | | - host->chip_alive = 0; |
---|
2522 | | - } |
---|
2523 | | -#endif |
---|
2524 | | - |
---|
2525 | 2161 | /* |
---|
2526 | 2162 | * sdio_reset sends CMD52 to reset card. Since we do not know |
---|
2527 | 2163 | * if the card is being re-initialized, just send it. CMD52 |
---|
2528 | 2164 | * should be ignored by SD/eMMC cards. |
---|
2529 | 2165 | * Skip it if we already know that we do not support SDIO commands |
---|
2530 | 2166 | */ |
---|
2531 | | -#ifdef MMC_STANDARD_PROBE |
---|
2532 | 2167 | if (!(host->caps2 & MMC_CAP2_NO_SDIO)) |
---|
2533 | 2168 | sdio_reset(host); |
---|
2534 | 2169 | |
---|
.. | .. |
---|
2549 | 2184 | if (!(host->caps2 & MMC_CAP2_NO_MMC)) |
---|
2550 | 2185 | if (!mmc_attach_mmc(host)) |
---|
2551 | 2186 | return 0; |
---|
2552 | | -#else |
---|
2553 | | -#ifdef CONFIG_SDIO_KEEPALIVE |
---|
2554 | | - if ((!(host->chip_alive)) && (host->restrict_caps & RESTRICT_CARD_TYPE_SDIO)) |
---|
2555 | | - sdio_reset(host); |
---|
2556 | | -#else |
---|
2557 | | - if (host->restrict_caps & RESTRICT_CARD_TYPE_SDIO) |
---|
2558 | | - sdio_reset(host); |
---|
2559 | | -#endif |
---|
2560 | 2187 | |
---|
2561 | | - mmc_go_idle(host); |
---|
2562 | | - |
---|
2563 | | - if (host->restrict_caps & |
---|
2564 | | - (RESTRICT_CARD_TYPE_SDIO | RESTRICT_CARD_TYPE_SD)) |
---|
2565 | | - mmc_send_if_cond(host, host->ocr_avail); |
---|
2566 | | - /* Order's important: probe SDIO, then SD, then MMC */ |
---|
2567 | | - if ((host->restrict_caps & RESTRICT_CARD_TYPE_SDIO) && |
---|
2568 | | - !mmc_attach_sdio(host)) |
---|
2569 | | - return 0; |
---|
2570 | | - if ((host->restrict_caps & RESTRICT_CARD_TYPE_SD) && |
---|
2571 | | - !mmc_attach_sd(host)) |
---|
2572 | | - return 0; |
---|
2573 | | - if ((host->restrict_caps & RESTRICT_CARD_TYPE_EMMC) && |
---|
2574 | | - !mmc_attach_mmc(host)) |
---|
2575 | | - return 0; |
---|
2576 | | -#endif |
---|
2577 | 2188 | mmc_power_off(host); |
---|
2578 | 2189 | return -EIO; |
---|
2579 | 2190 | } |
---|
.. | .. |
---|
2668 | 2279 | |
---|
2669 | 2280 | mmc_bus_get(host); |
---|
2670 | 2281 | |
---|
2671 | | - /* |
---|
2672 | | - * if there is a _removable_ card registered, check whether it is |
---|
2673 | | - * still present |
---|
2674 | | - */ |
---|
2675 | | - if (host->bus_ops && !host->bus_dead && mmc_card_is_removable(host)) |
---|
| 2282 | + /* Verify a registered card to be functional, else remove it. */ |
---|
| 2283 | + if (host->bus_ops && !host->bus_dead) |
---|
2676 | 2284 | host->bus_ops->detect(host); |
---|
2677 | 2285 | |
---|
2678 | 2286 | host->detect_change = 0; |
---|
.. | .. |
---|
2705 | 2313 | } |
---|
2706 | 2314 | |
---|
2707 | 2315 | for (i = 0; i < ARRAY_SIZE(freqs); i++) { |
---|
2708 | | - if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min))) |
---|
| 2316 | + unsigned int freq = freqs[i]; |
---|
| 2317 | + if (freq > host->f_max) { |
---|
| 2318 | + if (i + 1 < ARRAY_SIZE(freqs)) |
---|
| 2319 | + continue; |
---|
| 2320 | + freq = host->f_max; |
---|
| 2321 | + } |
---|
| 2322 | + if (!mmc_rescan_try_freq(host, max(freq, host->f_min))) |
---|
2709 | 2323 | break; |
---|
2710 | 2324 | if (freqs[i] <= host->f_min) |
---|
2711 | 2325 | break; |
---|
.. | .. |
---|
2719 | 2333 | |
---|
2720 | 2334 | void mmc_start_host(struct mmc_host *host) |
---|
2721 | 2335 | { |
---|
2722 | | - host->f_init = max(freqs[0], host->f_min); |
---|
| 2336 | + host->f_init = max(min(freqs[0], host->f_max), host->f_min); |
---|
2723 | 2337 | host->rescan_disable = 0; |
---|
2724 | | - host->ios.power_mode = MMC_POWER_UNDEFINED; |
---|
2725 | 2338 | |
---|
2726 | 2339 | if (!(host->caps2 & MMC_CAP2_NO_PRESCAN_POWERUP)) { |
---|
2727 | 2340 | mmc_claim_host(host); |
---|
.. | .. |
---|
2733 | 2346 | _mmc_detect_change(host, 0, false); |
---|
2734 | 2347 | } |
---|
2735 | 2348 | |
---|
2736 | | -void mmc_stop_host(struct mmc_host *host) |
---|
| 2349 | +void __mmc_stop_host(struct mmc_host *host) |
---|
2737 | 2350 | { |
---|
2738 | 2351 | if (host->slot.cd_irq >= 0) { |
---|
2739 | 2352 | mmc_gpio_set_cd_wake(host, false); |
---|
.. | .. |
---|
2742 | 2355 | |
---|
2743 | 2356 | host->rescan_disable = 1; |
---|
2744 | 2357 | cancel_delayed_work_sync(&host->detect); |
---|
| 2358 | +} |
---|
| 2359 | + |
---|
| 2360 | +void mmc_stop_host(struct mmc_host *host) |
---|
| 2361 | +{ |
---|
| 2362 | + __mmc_stop_host(host); |
---|
2745 | 2363 | |
---|
2746 | 2364 | /* clear pm flags now and let card drivers set them as needed */ |
---|
2747 | 2365 | host->pm_flags = 0; |
---|
.. | .. |
---|
2763 | 2381 | mmc_power_off(host); |
---|
2764 | 2382 | mmc_release_host(host); |
---|
2765 | 2383 | } |
---|
2766 | | - |
---|
2767 | | -#ifdef CONFIG_PM_SLEEP |
---|
2768 | | -/* Do the card removal on suspend if card is assumed removeable |
---|
2769 | | - * Do that in pm notifier while userspace isn't yet frozen, so we will be able |
---|
2770 | | - to sync the card. |
---|
2771 | | -*/ |
---|
2772 | | -static int mmc_pm_notify(struct notifier_block *notify_block, |
---|
2773 | | - unsigned long mode, void *unused) |
---|
2774 | | -{ |
---|
2775 | | - struct mmc_host *host = container_of( |
---|
2776 | | - notify_block, struct mmc_host, pm_notify); |
---|
2777 | | - unsigned long flags; |
---|
2778 | | - int err = 0; |
---|
2779 | | - |
---|
2780 | | - switch (mode) { |
---|
2781 | | - case PM_HIBERNATION_PREPARE: |
---|
2782 | | - case PM_SUSPEND_PREPARE: |
---|
2783 | | - case PM_RESTORE_PREPARE: |
---|
2784 | | - spin_lock_irqsave(&host->lock, flags); |
---|
2785 | | - host->rescan_disable = 1; |
---|
2786 | | - spin_unlock_irqrestore(&host->lock, flags); |
---|
2787 | | - cancel_delayed_work_sync(&host->detect); |
---|
2788 | | - |
---|
2789 | | - if (!host->bus_ops) |
---|
2790 | | - break; |
---|
2791 | | - |
---|
2792 | | - /* Validate prerequisites for suspend */ |
---|
2793 | | - if (host->bus_ops->pre_suspend) |
---|
2794 | | - err = host->bus_ops->pre_suspend(host); |
---|
2795 | | - if (!err) |
---|
2796 | | - break; |
---|
2797 | | - |
---|
2798 | | - if (!mmc_card_is_removable(host)) { |
---|
2799 | | - dev_warn(mmc_dev(host), |
---|
2800 | | - "pre_suspend failed for non-removable host: " |
---|
2801 | | - "%d\n", err); |
---|
2802 | | - /* Avoid removing non-removable hosts */ |
---|
2803 | | - break; |
---|
2804 | | - } |
---|
2805 | | - |
---|
2806 | | - /* Calling bus_ops->remove() with a claimed host can deadlock */ |
---|
2807 | | - host->bus_ops->remove(host); |
---|
2808 | | - mmc_claim_host(host); |
---|
2809 | | - mmc_detach_bus(host); |
---|
2810 | | - mmc_power_off(host); |
---|
2811 | | - mmc_release_host(host); |
---|
2812 | | - host->pm_flags = 0; |
---|
2813 | | - break; |
---|
2814 | | - |
---|
2815 | | - case PM_POST_SUSPEND: |
---|
2816 | | - case PM_POST_HIBERNATION: |
---|
2817 | | - case PM_POST_RESTORE: |
---|
2818 | | - |
---|
2819 | | - spin_lock_irqsave(&host->lock, flags); |
---|
2820 | | - host->rescan_disable = 0; |
---|
2821 | | - spin_unlock_irqrestore(&host->lock, flags); |
---|
2822 | | - _mmc_detect_change(host, 0, false); |
---|
2823 | | - |
---|
2824 | | - } |
---|
2825 | | - |
---|
2826 | | - return 0; |
---|
2827 | | -} |
---|
2828 | | - |
---|
2829 | | -void mmc_register_pm_notifier(struct mmc_host *host) |
---|
2830 | | -{ |
---|
2831 | | - host->pm_notify.notifier_call = mmc_pm_notify; |
---|
2832 | | - register_pm_notifier(&host->pm_notify); |
---|
2833 | | -} |
---|
2834 | | - |
---|
2835 | | -void mmc_unregister_pm_notifier(struct mmc_host *host) |
---|
2836 | | -{ |
---|
2837 | | - unregister_pm_notifier(&host->pm_notify); |
---|
2838 | | -} |
---|
2839 | | -#endif |
---|
2840 | 2384 | |
---|
2841 | 2385 | static int __init mmc_init(void) |
---|
2842 | 2386 | { |
---|