hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/mmc/core/mmc.c
....@@ -1,13 +1,10 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * linux/drivers/mmc/core/mmc.c
34 *
45 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
56 * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
67 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
7
- *
8
- * This program is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License version 2 as
10
- * published by the Free Software Foundation.
118 */
129
1310 #include <linux/err.h>
....@@ -661,6 +658,9 @@
661658 mmc_hostname(card->host),
662659 card->ext_csd.cmdq_depth);
663660 }
661
+ card->ext_csd.enhanced_rpmb_supported =
662
+ (card->ext_csd.rel_param &
663
+ EXT_CSD_WR_REL_PARAM_EN_RPMB_REL_WR);
664664 }
665665 out:
666666 return err;
....@@ -697,7 +697,7 @@
697697 u8 *ext_csd;
698698 int err;
699699 #ifdef CONFIG_ROCKCHIP_THUNDER_BOOT_MMC
700
- void *ecsd = NULL;
700
+ void *ecsd;
701701 bool valid_ecsd = false;
702702 bool valid_reserved = false;
703703 struct device_node *mem;
....@@ -875,6 +875,8 @@
875875 card->ext_csd.enhanced_area_offset);
876876 MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
877877 MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
878
+MMC_DEV_ATTR(enhanced_rpmb_supported, "%#x\n",
879
+ card->ext_csd.enhanced_rpmb_supported);
878880 MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
879881 MMC_DEV_ATTR(ocr, "0x%08x\n", card->ocr);
880882 MMC_DEV_ATTR(rca, "0x%04x\n", card->rca);
....@@ -932,6 +934,7 @@
932934 &dev_attr_enhanced_area_offset.attr,
933935 &dev_attr_enhanced_area_size.attr,
934936 &dev_attr_raw_rpmb_size_mult.attr,
937
+ &dev_attr_enhanced_rpmb_supported.attr,
935938 &dev_attr_rel_sectors.attr,
936939 &dev_attr_ocr.attr,
937940 &dev_attr_rca.attr,
....@@ -1048,7 +1051,7 @@
10481051 /*
10491052 * Set the bus speed for the selected speed mode.
10501053 */
1051
-static void mmc_set_bus_speed(struct mmc_card *card)
1054
+void mmc_set_bus_speed(struct mmc_card *card)
10521055 {
10531056 unsigned int max_dtr = (unsigned int)-1;
10541057
....@@ -1068,7 +1071,7 @@
10681071 * If the bus width is changed successfully, return the selected width value.
10691072 * Zero is returned instead of error value if the wide width is not supported.
10701073 */
1071
-static int mmc_select_bus_width(struct mmc_card *card)
1074
+int mmc_select_bus_width(struct mmc_card *card)
10721075 {
10731076 static unsigned ext_csd_bits[] = {
10741077 EXT_CSD_BUS_WIDTH_8,
....@@ -1134,29 +1137,31 @@
11341137
11351138 return err;
11361139 }
1140
+EXPORT_SYMBOL_GPL(mmc_select_bus_width);
11371141
11381142 /*
11391143 * Switch to the high-speed mode
11401144 */
1141
-static int mmc_select_hs(struct mmc_card *card)
1145
+int mmc_select_hs(struct mmc_card *card)
11421146 {
11431147 int err;
11441148
11451149 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
11461150 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
11471151 card->ext_csd.generic_cmd6_time, MMC_TIMING_MMC_HS,
1148
- true, true, true);
1152
+ true, true);
11491153 if (err)
11501154 pr_warn("%s: switch to high-speed failed, err:%d\n",
11511155 mmc_hostname(card->host), err);
11521156
11531157 return err;
11541158 }
1159
+EXPORT_SYMBOL_GPL(mmc_select_hs);
11551160
11561161 /*
11571162 * Activate wide bus and DDR if supported.
11581163 */
1159
-static int mmc_select_hs_ddr(struct mmc_card *card)
1164
+int mmc_select_hs_ddr(struct mmc_card *card)
11601165 {
11611166 struct mmc_host *host = card->host;
11621167 u32 bus_width, ext_csd_bits;
....@@ -1177,7 +1182,7 @@
11771182 ext_csd_bits,
11781183 card->ext_csd.generic_cmd6_time,
11791184 MMC_TIMING_MMC_DDR52,
1180
- true, true, true);
1185
+ true, true);
11811186 if (err) {
11821187 pr_err("%s: switch to bus width %d ddr failed\n",
11831188 mmc_hostname(host), 1 << bus_width);
....@@ -1225,8 +1230,9 @@
12251230
12261231 return err;
12271232 }
1233
+EXPORT_SYMBOL_GPL(mmc_select_hs_ddr);
12281234
1229
-static int mmc_select_hs400(struct mmc_card *card)
1235
+int mmc_select_hs400(struct mmc_card *card)
12301236 {
12311237 struct mmc_host *host = card->host;
12321238 unsigned int max_dtr;
....@@ -1245,27 +1251,30 @@
12451251 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
12461252 EXT_CSD_HS_TIMING, val,
12471253 card->ext_csd.generic_cmd6_time, 0,
1248
- true, false, true);
1254
+ false, true);
12491255 if (err) {
12501256 pr_err("%s: switch to high-speed from hs200 failed, err:%d\n",
12511257 mmc_hostname(host), err);
12521258 return err;
12531259 }
12541260
1255
- /* Set host controller to HS timing */
1256
- mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1257
-
12581261 /* Prepare host to downgrade to HS timing */
12591262 if (host->ops->hs400_downgrade)
12601263 host->ops->hs400_downgrade(host);
1264
+
1265
+ /* Set host controller to HS timing */
1266
+ mmc_set_timing(host, MMC_TIMING_MMC_HS);
12611267
12621268 /* Reduce frequency to HS frequency */
12631269 max_dtr = card->ext_csd.hs_max_dtr;
12641270 mmc_set_clock(host, max_dtr);
12651271
1266
- err = mmc_switch_status(card);
1272
+ err = mmc_switch_status(card, true);
12671273 if (err)
12681274 goto out_err;
1275
+
1276
+ if (host->ops->hs400_prepare_ddr)
1277
+ host->ops->hs400_prepare_ddr(host);
12691278
12701279 /* Switch card to DDR */
12711280 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
....@@ -1284,7 +1293,7 @@
12841293 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
12851294 EXT_CSD_HS_TIMING, val,
12861295 card->ext_csd.generic_cmd6_time, 0,
1287
- true, false, true);
1296
+ false, true);
12881297 if (err) {
12891298 pr_err("%s: switch to hs400 failed, err:%d\n",
12901299 mmc_hostname(host), err);
....@@ -1298,7 +1307,7 @@
12981307 if (host->ops->hs400_complete)
12991308 host->ops->hs400_complete(host);
13001309
1301
- err = mmc_switch_status(card);
1310
+ err = mmc_switch_status(card, true);
13021311 if (err)
13031312 goto out_err;
13041313
....@@ -1309,6 +1318,7 @@
13091318 __func__, err);
13101319 return err;
13111320 }
1321
+EXPORT_SYMBOL_GPL(mmc_select_hs400);
13121322
13131323 int mmc_hs200_to_hs400(struct mmc_card *card)
13141324 {
....@@ -1330,29 +1340,29 @@
13301340 val = EXT_CSD_TIMING_HS;
13311341 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
13321342 val, card->ext_csd.generic_cmd6_time, 0,
1333
- true, false, true);
1343
+ false, true);
13341344 if (err)
13351345 goto out_err;
13361346
1347
+ if (host->ops->hs400_downgrade)
1348
+ host->ops->hs400_downgrade(host);
1349
+
13371350 mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
13381351
1339
- err = mmc_switch_status(card);
1352
+ err = mmc_switch_status(card, true);
13401353 if (err)
13411354 goto out_err;
13421355
13431356 /* Switch HS DDR to HS */
13441357 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
13451358 EXT_CSD_BUS_WIDTH_8, card->ext_csd.generic_cmd6_time,
1346
- 0, true, false, true);
1359
+ 0, false, true);
13471360 if (err)
13481361 goto out_err;
13491362
13501363 mmc_set_timing(host, MMC_TIMING_MMC_HS);
13511364
1352
- if (host->ops->hs400_downgrade)
1353
- host->ops->hs400_downgrade(host);
1354
-
1355
- err = mmc_switch_status(card);
1365
+ err = mmc_switch_status(card, true);
13561366 if (err)
13571367 goto out_err;
13581368
....@@ -1361,7 +1371,7 @@
13611371 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
13621372 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
13631373 val, card->ext_csd.generic_cmd6_time, 0,
1364
- true, false, true);
1374
+ false, true);
13651375 if (err)
13661376 goto out_err;
13671377
....@@ -1372,7 +1382,7 @@
13721382 * failed. If there really is a problem, we would expect tuning will
13731383 * fail and the result ends up the same.
13741384 */
1375
- err = __mmc_switch_status(card, false);
1385
+ err = mmc_switch_status(card, false);
13761386 if (err)
13771387 goto out_err;
13781388
....@@ -1445,18 +1455,22 @@
14451455 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
14461456 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
14471457 card->ext_csd.generic_cmd6_time, 0,
1448
- true, false, true);
1458
+ false, true);
14491459 if (err) {
14501460 pr_err("%s: switch to hs for hs400es failed, err:%d\n",
14511461 mmc_hostname(host), err);
14521462 goto out_err;
14531463 }
14541464
1465
+ /*
1466
+ * Bump to HS timing and frequency. Some cards don't handle
1467
+ * SEND_STATUS reliably at the initial frequency.
1468
+ */
14551469 mmc_set_timing(host, MMC_TIMING_MMC_HS);
14561470 /* Set clock immediately after changing timing */
1457
- mmc_set_clock(host, card->ext_csd.hs_max_dtr);
1471
+ mmc_set_bus_speed(card);
14581472
1459
- err = mmc_switch_status(card);
1473
+ err = mmc_switch_status(card, true);
14601474 if (err)
14611475 goto out_err;
14621476
....@@ -1480,7 +1494,7 @@
14801494 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
14811495 EXT_CSD_HS_TIMING, val,
14821496 card->ext_csd.generic_cmd6_time, 0,
1483
- true, false, true);
1497
+ false, true);
14841498 if (err) {
14851499 pr_err("%s: switch to hs400es failed, err:%d\n",
14861500 mmc_hostname(host), err);
....@@ -1495,10 +1509,7 @@
14951509 if (host->ops->hs400_enhanced_strobe)
14961510 host->ops->hs400_enhanced_strobe(host, &host->ios);
14971511
1498
- /* some emmc device need a delay before read status */
1499
- usleep_range(100, 200);
1500
-
1501
- err = mmc_switch_status(card);
1512
+ err = mmc_switch_status(card, true);
15021513 if (err)
15031514 goto out_err;
15041515
....@@ -1520,7 +1531,7 @@
15201531 static int mmc_select_hs200(struct mmc_card *card)
15211532 {
15221533 struct mmc_host *host = card->host;
1523
- unsigned int old_timing, old_signal_voltage;
1534
+ unsigned int old_timing, old_signal_voltage, old_clock;
15241535 int err = -EINVAL;
15251536 u8 val;
15261537
....@@ -1548,25 +1559,36 @@
15481559 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
15491560 EXT_CSD_HS_TIMING, val,
15501561 card->ext_csd.generic_cmd6_time, 0,
1551
- true, false, true);
1562
+ false, true);
15521563 if (err)
15531564 goto err;
1565
+
1566
+ /*
1567
+ * Bump to HS timing and frequency. Some cards don't handle
1568
+ * SEND_STATUS reliably at the initial frequency.
1569
+ * NB: We can't move to full (HS200) speeds until after we've
1570
+ * successfully switched over.
1571
+ */
15541572 old_timing = host->ios.timing;
1573
+ old_clock = host->ios.clock;
15551574 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1575
+ mmc_set_clock(card->host, card->ext_csd.hs_max_dtr);
15561576
15571577 /*
15581578 * For HS200, CRC errors are not a reliable way to know the
15591579 * switch failed. If there really is a problem, we would expect
15601580 * tuning will fail and the result ends up the same.
15611581 */
1562
- err = __mmc_switch_status(card, false);
1582
+ err = mmc_switch_status(card, false);
15631583
15641584 /*
15651585 * mmc_select_timing() assumes timing has not changed if
15661586 * it is a switch error.
15671587 */
1568
- if (err == -EBADMSG)
1588
+ if (err == -EBADMSG) {
1589
+ mmc_set_clock(host, old_clock);
15691590 mmc_set_timing(host, old_timing);
1591
+ }
15701592 }
15711593 err:
15721594 if (err) {
....@@ -1583,7 +1605,7 @@
15831605 /*
15841606 * Activate High Speed, HS200 or HS400ES mode if supported.
15851607 */
1586
-static int mmc_select_timing(struct mmc_card *card)
1608
+int mmc_select_timing(struct mmc_card *card)
15871609 {
15881610 int err = 0;
15891611
....@@ -1608,12 +1630,13 @@
16081630 mmc_set_bus_speed(card);
16091631 return 0;
16101632 }
1633
+EXPORT_SYMBOL_GPL(mmc_select_timing);
16111634
16121635 /*
16131636 * Execute tuning sequence to seek the proper bus operating
16141637 * conditions for HS200 and HS400, which sends CMD21 to the device.
16151638 */
1616
-static int mmc_hs200_tuning(struct mmc_card *card)
1639
+int mmc_hs200_tuning(struct mmc_card *card)
16171640 {
16181641 struct mmc_host *host = card->host;
16191642
....@@ -1628,6 +1651,7 @@
16281651
16291652 return mmc_execute_tuning(card);
16301653 }
1654
+EXPORT_SYMBOL_GPL(mmc_hs200_tuning);
16311655
16321656 /*
16331657 * Handle the detection and initialisation of a card.
....@@ -1683,6 +1707,8 @@
16831707
16841708 if (oldcard) {
16851709 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1710
+ pr_debug("%s: Perhaps the card was replaced\n",
1711
+ mmc_hostname(host));
16861712 err = -ENOENT;
16871713 goto err;
16881714 }
....@@ -1836,6 +1862,14 @@
18361862 card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
18371863 }
18381864
1865
+ /* set erase_arg */
1866
+ if (mmc_can_discard(card))
1867
+ card->erase_arg = MMC_DISCARD_ARG;
1868
+ else if (mmc_can_trim(card))
1869
+ card->erase_arg = MMC_TRIM_ARG;
1870
+ else
1871
+ card->erase_arg = MMC_ERASE_ARG;
1872
+
18391873 /*
18401874 * Select timing interface
18411875 */
....@@ -1844,13 +1878,17 @@
18441878 goto free_card;
18451879
18461880 if (mmc_card_hs200(card)) {
1881
+ host->doing_init_tune = 1;
1882
+
18471883 err = mmc_hs200_tuning(card);
1884
+ if (!err)
1885
+ err = mmc_select_hs400(card);
1886
+
1887
+ host->doing_init_tune = 0;
1888
+
18481889 if (err)
18491890 goto free_card;
18501891
1851
- err = mmc_select_hs400(card);
1852
- if (err)
1853
- goto free_card;
18541892 } else if (!mmc_card_hs400es(card)) {
18551893 /* Select the desired bus width optionally */
18561894 err = mmc_select_bus_width(card);
....@@ -2050,7 +2088,7 @@
20502088
20512089 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
20522090 EXT_CSD_POWER_OFF_NOTIFICATION,
2053
- notify_type, timeout, 0, true, false, false);
2091
+ notify_type, timeout, 0, false, false);
20542092 if (err)
20552093 pr_err("%s: Power Off Notification timed out, %u\n",
20562094 mmc_hostname(card->host), timeout);
....@@ -2121,18 +2159,13 @@
21212159 if (mmc_card_suspended(host->card))
21222160 goto out;
21232161
2124
- if (mmc_card_doing_bkops(host->card)) {
2125
- err = mmc_stop_bkops(host->card);
2126
- if (err)
2127
- goto out;
2128
- }
2129
-
21302162 err = mmc_flush_cache(host->card);
21312163 if (err)
21322164 goto out;
21332165
21342166 if (mmc_can_poweroff_notify(host->card) &&
2135
- ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend))
2167
+ ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend ||
2168
+ (host->caps2 & MMC_CAP2_FULL_PWR_CYCLE_IN_SUSPEND)))
21362169 err = mmc_poweroff_notify(host->card, notify_type);
21372170 else if (mmc_can_sleep(host->card))
21382171 err = mmc_sleep(host);