hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/mmc/core/core.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * linux/drivers/mmc/core/core.c
34 *
....@@ -5,10 +6,6 @@
56 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
67 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
78 * 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.
129 */
1310 #include <linux/module.h>
1411 #include <linux/init.h>
....@@ -21,7 +18,6 @@
2118 #include <linux/leds.h>
2219 #include <linux/scatterlist.h>
2320 #include <linux/log2.h>
24
-#include <linux/regulator/consumer.h>
2521 #include <linux/pm_runtime.h>
2622 #include <linux/pm_wakeup.h>
2723 #include <linux/suspend.h>
....@@ -41,6 +37,7 @@
4137
4238 #include "core.h"
4339 #include "card.h"
40
+#include "crypto.h"
4441 #include "bus.h"
4542 #include "host.h"
4643 #include "sdio_bus.h"
....@@ -52,6 +49,7 @@
5249
5350 /* The max erase timeout, used when host->max_busy_timeout isn't specified */
5451 #define MMC_ERASE_TIMEOUT_MS (60 * 1000) /* 60 s */
52
+#define SD_DISCARD_TIMEOUT_MS (250)
5553
5654 /*
5755 * Enabling software CRCs on the data blocks can be a significant (30%)
....@@ -404,23 +402,6 @@
404402
405403 cmd = mrq->cmd;
406404
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
- }
424405 if (!cmd->error || !cmd->retries ||
425406 mmc_card_removed(host->card))
426407 break;
....@@ -757,33 +738,6 @@
757738 }
758739 EXPORT_SYMBOL(mmc_set_data_timeout);
759740
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
-
787741 /*
788742 * Allow claiming an already claimed host if the context is the same or there is
789743 * no context but the task is the same.
....@@ -886,7 +840,10 @@
886840 spin_unlock_irqrestore(&host->lock, flags);
887841 wake_up(&host->wq);
888842 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));
890847 }
891848 }
892849 EXPORT_SYMBOL(mmc_release_host);
....@@ -958,6 +915,7 @@
958915 host->ios.clock = hz;
959916 mmc_set_ios(host);
960917 }
918
+EXPORT_SYMBOL_GPL(mmc_set_clock);
961919
962920 int mmc_execute_tuning(struct mmc_card *card)
963921 {
....@@ -1037,7 +995,10 @@
1037995 host->ops->hs400_enhanced_strobe(host, &host->ios);
1038996
1039997 mmc_set_ios(host);
998
+
999
+ mmc_crypto_set_initial_state(host);
10401000 }
1001
+EXPORT_SYMBOL_GPL(mmc_set_initial_state);
10411002
10421003 /**
10431004 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
....@@ -1111,55 +1072,6 @@
11111072
11121073 return mask;
11131074 }
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 */
11631075
11641076 static int mmc_of_get_func_num(struct device_node *node)
11651077 {
....@@ -1188,246 +1100,6 @@
11881100
11891101 return NULL;
11901102 }
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);
14311103
14321104 /*
14331105 * Mask off any voltages we don't support and select
....@@ -1459,7 +1131,13 @@
14591131 mmc_power_cycle(host, ocr);
14601132 } else {
14611133 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);
14631141 if (bit != host->ios.vdd)
14641142 dev_warn(mmc_dev(host), "exceeding card's volts\n");
14651143 }
....@@ -1590,6 +1268,7 @@
15901268 host->ios.timing = timing;
15911269 mmc_set_ios(host);
15921270 }
1271
+EXPORT_SYMBOL_GPL(mmc_set_timing);
15931272
15941273 /*
15951274 * Select appropriate driver type for host.
....@@ -1786,16 +1465,15 @@
17861465 mmc_bus_put(host);
17871466 }
17881467
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)
17911469 {
17921470 /*
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.
17951474 */
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);
17991477
18001478 host->detect_change = 1;
18011479 mmc_schedule_delayed_work(&host->detect, delay);
....@@ -1863,6 +1541,11 @@
18631541 }
18641542 } else
18651543 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;
18661549 }
18671550
18681551 static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
....@@ -1935,6 +1618,12 @@
19351618 {
19361619 unsigned int erase_timeout;
19371620
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
+
19381627 if (card->ssr.erase_timeout) {
19391628 /* Erase timeout specified in SD Status Register (SSR) */
19401629 erase_timeout = card->ssr.erase_timeout * qty +
....@@ -1970,8 +1659,6 @@
19701659 struct mmc_command cmd = {};
19711660 unsigned int qty = 0, busy_timeout = 0;
19721661 bool use_r1b_resp = false;
1973
- unsigned long timeout;
1974
- int loop_udelay=64, udelay_max=32768;
19751662 int err;
19761663
19771664 mmc_retune_hold(card->host);
....@@ -2075,38 +1762,8 @@
20751762 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp)
20761763 goto out;
20771764
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);
21101767
21111768 out:
21121769 mmc_retune_release(card->host);
....@@ -2166,7 +1823,7 @@
21661823 * @card: card to erase
21671824 * @from: first sector to erase
21681825 * @nr: number of sectors to erase
2169
- * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1826
+ * @arg: erase command argument
21701827 *
21711828 * Caller must claim host before calling this function.
21721829 */
....@@ -2176,21 +1833,20 @@
21761833 unsigned int rem, to = from + nr;
21771834 int err;
21781835
2179
- if (!(card->host->caps & MMC_CAP_ERASE) ||
2180
- !(card->csd.cmdclass & CCC_ERASE))
1836
+ if (!(card->csd.cmdclass & CCC_ERASE))
21811837 return -EOPNOTSUPP;
21821838
21831839 if (!card->erase_size)
21841840 return -EOPNOTSUPP;
21851841
2186
- if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1842
+ if (mmc_card_sd(card) && arg != SD_ERASE_ARG && arg != SD_DISCARD_ARG)
21871843 return -EOPNOTSUPP;
21881844
2189
- if ((arg & MMC_SECURE_ARGS) &&
1845
+ if (mmc_card_mmc(card) && (arg & MMC_SECURE_ARGS) &&
21901846 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
21911847 return -EOPNOTSUPP;
21921848
2193
- if ((arg & MMC_TRIM_ARGS) &&
1849
+ if (mmc_card_mmc(card) && is_trim_arg(arg) &&
21941850 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
21951851 return -EOPNOTSUPP;
21961852
....@@ -2220,7 +1876,7 @@
22201876 * identified by the card->eg_boundary flag.
22211877 */
22221878 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) {
22241880 err = mmc_do_erase(card, from, from + rem - 1, arg);
22251881 from += rem;
22261882 if ((err) || (to <= from))
....@@ -2233,8 +1889,7 @@
22331889
22341890 int mmc_can_erase(struct mmc_card *card)
22351891 {
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)
22381893 return 1;
22391894 return 0;
22401895 }
....@@ -2269,7 +1924,6 @@
22691924 return 1;
22701925 return 0;
22711926 }
2272
-EXPORT_SYMBOL(mmc_can_sanitize);
22731927
22741928 int mmc_can_secure_erase_trim(struct mmc_card *card)
22751929 {
....@@ -2418,20 +2072,6 @@
24182072 }
24192073 EXPORT_SYMBOL(mmc_set_blocklen);
24202074
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
-
24352075 #ifndef CONFIG_ROCKCHIP_THUNDER_BOOT_MMC
24362076 static void mmc_hw_reset_for_init(struct mmc_host *host)
24372077 {
....@@ -2443,6 +2083,16 @@
24432083 }
24442084 #endif
24452085
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
+ */
24462096 int mmc_hw_reset(struct mmc_host *host)
24472097 {
24482098 int ret;
....@@ -2459,7 +2109,7 @@
24592109 ret = host->bus_ops->hw_reset(host);
24602110 mmc_bus_put(host);
24612111
2462
- if (ret)
2112
+ if (ret < 0)
24632113 pr_warn("%s: tried to HW reset card, got error %d\n",
24642114 mmc_hostname(host), ret);
24652115
....@@ -2508,27 +2158,12 @@
25082158 mmc_hw_reset_for_init(host);
25092159 #endif
25102160
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
-
25252161 /*
25262162 * sdio_reset sends CMD52 to reset card. Since we do not know
25272163 * if the card is being re-initialized, just send it. CMD52
25282164 * should be ignored by SD/eMMC cards.
25292165 * Skip it if we already know that we do not support SDIO commands
25302166 */
2531
-#ifdef MMC_STANDARD_PROBE
25322167 if (!(host->caps2 & MMC_CAP2_NO_SDIO))
25332168 sdio_reset(host);
25342169
....@@ -2549,31 +2184,7 @@
25492184 if (!(host->caps2 & MMC_CAP2_NO_MMC))
25502185 if (!mmc_attach_mmc(host))
25512186 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
25602187
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
25772188 mmc_power_off(host);
25782189 return -EIO;
25792190 }
....@@ -2668,11 +2279,8 @@
26682279
26692280 mmc_bus_get(host);
26702281
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)
26762284 host->bus_ops->detect(host);
26772285
26782286 host->detect_change = 0;
....@@ -2705,7 +2313,13 @@
27052313 }
27062314
27072315 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)))
27092323 break;
27102324 if (freqs[i] <= host->f_min)
27112325 break;
....@@ -2719,9 +2333,8 @@
27192333
27202334 void mmc_start_host(struct mmc_host *host)
27212335 {
2722
- host->f_init = max(freqs[0], host->f_min);
2336
+ host->f_init = max(min(freqs[0], host->f_max), host->f_min);
27232337 host->rescan_disable = 0;
2724
- host->ios.power_mode = MMC_POWER_UNDEFINED;
27252338
27262339 if (!(host->caps2 & MMC_CAP2_NO_PRESCAN_POWERUP)) {
27272340 mmc_claim_host(host);
....@@ -2733,7 +2346,7 @@
27332346 _mmc_detect_change(host, 0, false);
27342347 }
27352348
2736
-void mmc_stop_host(struct mmc_host *host)
2349
+void __mmc_stop_host(struct mmc_host *host)
27372350 {
27382351 if (host->slot.cd_irq >= 0) {
27392352 mmc_gpio_set_cd_wake(host, false);
....@@ -2742,6 +2355,11 @@
27422355
27432356 host->rescan_disable = 1;
27442357 cancel_delayed_work_sync(&host->detect);
2358
+}
2359
+
2360
+void mmc_stop_host(struct mmc_host *host)
2361
+{
2362
+ __mmc_stop_host(host);
27452363
27462364 /* clear pm flags now and let card drivers set them as needed */
27472365 host->pm_flags = 0;
....@@ -2763,80 +2381,6 @@
27632381 mmc_power_off(host);
27642382 mmc_release_host(host);
27652383 }
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
28402384
28412385 static int __init mmc_init(void)
28422386 {