forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-02-20 ea08eeccae9297f7aabd2ef7f0c2517ac4549acc
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>
....@@ -70,7 +67,7 @@
7067 /*
7168 * Given the decoded CSD structure, decode the raw CID to our CID structure.
7269 */
73
-#ifndef CONFIG_ROCKCHIP_THUNDER_BOOT
70
+#ifndef CONFIG_ROCKCHIP_THUNDER_BOOT_MMC
7471 static int mmc_decode_cid(struct mmc_card *card)
7572 {
7673 u32 *resp = card->raw_cid;
....@@ -661,12 +658,15 @@
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;
667667 }
668668
669
-#ifdef CONFIG_ROCKCHIP_THUNDER_BOOT
669
+#ifdef CONFIG_ROCKCHIP_THUNDER_BOOT_MMC
670670 static void *mmc_tb_map_ecsd(phys_addr_t start, size_t len)
671671 {
672672 int i;
....@@ -696,8 +696,8 @@
696696 {
697697 u8 *ext_csd;
698698 int err;
699
-#ifdef CONFIG_ROCKCHIP_THUNDER_BOOT
700
- void *ecsd = NULL;
699
+#ifdef CONFIG_ROCKCHIP_THUNDER_BOOT_MMC
700
+ void *ecsd;
701701 bool valid_ecsd = false;
702702 bool valid_reserved = false;
703703 struct device_node *mem;
....@@ -707,7 +707,7 @@
707707 if (!mmc_can_ext_csd(card))
708708 return 0;
709709
710
-#ifdef CONFIG_ROCKCHIP_THUNDER_BOOT
710
+#ifdef CONFIG_ROCKCHIP_THUNDER_BOOT_MMC
711711 mem = of_parse_phandle(dev->of_node, "memory-region-ecsd", 0);
712712 if (mem) {
713713 err = of_address_to_resource(mem, 0, &reg);
....@@ -758,11 +758,11 @@
758758
759759 return err;
760760 }
761
-#ifdef CONFIG_ROCKCHIP_THUNDER_BOOT
761
+#ifdef CONFIG_ROCKCHIP_THUNDER_BOOT_MMC
762762 decode:
763763 #endif
764764 err = mmc_decode_ext_csd(card, ext_csd);
765
-#ifdef CONFIG_ROCKCHIP_THUNDER_BOOT
765
+#ifdef CONFIG_ROCKCHIP_THUNDER_BOOT_MMC
766766 if (!valid_ecsd)
767767 kfree(ext_csd);
768768 else
....@@ -777,7 +777,7 @@
777777 return err;
778778 }
779779
780
-#ifndef CONFIG_ROCKCHIP_THUNDER_BOOT
780
+#ifndef CONFIG_ROCKCHIP_THUNDER_BOOT_MMC
781781 static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
782782 {
783783 u8 *bw_ext_csd;
....@@ -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,
....@@ -1117,7 +1120,7 @@
11171120 * compare ext_csd previously read in 1 bit mode
11181121 * against ext_csd at new bus width
11191122 */
1120
-#ifndef CONFIG_ROCKCHIP_THUNDER_BOOT
1123
+#ifndef CONFIG_ROCKCHIP_THUNDER_BOOT_MMC
11211124 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
11221125 err = mmc_compare_ext_csds(card, bus_width);
11231126 else
....@@ -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,7 +1509,7 @@
14951509 if (host->ops->hs400_enhanced_strobe)
14961510 host->ops->hs400_enhanced_strobe(host, &host->ios);
14971511
1498
- err = mmc_switch_status(card);
1512
+ err = mmc_switch_status(card, true);
14991513 if (err)
15001514 goto out_err;
15011515
....@@ -1517,7 +1531,7 @@
15171531 static int mmc_select_hs200(struct mmc_card *card)
15181532 {
15191533 struct mmc_host *host = card->host;
1520
- unsigned int old_timing, old_signal_voltage;
1534
+ unsigned int old_timing, old_signal_voltage, old_clock;
15211535 int err = -EINVAL;
15221536 u8 val;
15231537
....@@ -1545,25 +1559,36 @@
15451559 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
15461560 EXT_CSD_HS_TIMING, val,
15471561 card->ext_csd.generic_cmd6_time, 0,
1548
- true, false, true);
1562
+ false, true);
15491563 if (err)
15501564 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
+ */
15511572 old_timing = host->ios.timing;
1573
+ old_clock = host->ios.clock;
15521574 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1575
+ mmc_set_clock(card->host, card->ext_csd.hs_max_dtr);
15531576
15541577 /*
15551578 * For HS200, CRC errors are not a reliable way to know the
15561579 * switch failed. If there really is a problem, we would expect
15571580 * tuning will fail and the result ends up the same.
15581581 */
1559
- err = __mmc_switch_status(card, false);
1582
+ err = mmc_switch_status(card, false);
15601583
15611584 /*
15621585 * mmc_select_timing() assumes timing has not changed if
15631586 * it is a switch error.
15641587 */
1565
- if (err == -EBADMSG)
1588
+ if (err == -EBADMSG) {
1589
+ mmc_set_clock(host, old_clock);
15661590 mmc_set_timing(host, old_timing);
1591
+ }
15671592 }
15681593 err:
15691594 if (err) {
....@@ -1580,7 +1605,7 @@
15801605 /*
15811606 * Activate High Speed, HS200 or HS400ES mode if supported.
15821607 */
1583
-static int mmc_select_timing(struct mmc_card *card)
1608
+int mmc_select_timing(struct mmc_card *card)
15841609 {
15851610 int err = 0;
15861611
....@@ -1605,12 +1630,13 @@
16051630 mmc_set_bus_speed(card);
16061631 return 0;
16071632 }
1633
+EXPORT_SYMBOL_GPL(mmc_select_timing);
16081634
16091635 /*
16101636 * Execute tuning sequence to seek the proper bus operating
16111637 * conditions for HS200 and HS400, which sends CMD21 to the device.
16121638 */
1613
-static int mmc_hs200_tuning(struct mmc_card *card)
1639
+int mmc_hs200_tuning(struct mmc_card *card)
16141640 {
16151641 struct mmc_host *host = card->host;
16161642
....@@ -1625,6 +1651,7 @@
16251651
16261652 return mmc_execute_tuning(card);
16271653 }
1654
+EXPORT_SYMBOL_GPL(mmc_hs200_tuning);
16281655
16291656 /*
16301657 * Handle the detection and initialisation of a card.
....@@ -1653,7 +1680,7 @@
16531680 * respond.
16541681 * mmc_go_idle is needed for eMMC that are asleep
16551682 */
1656
-#ifndef CONFIG_ROCKCHIP_THUNDER_BOOT
1683
+#ifndef CONFIG_ROCKCHIP_THUNDER_BOOT_MMC
16571684 mmc_go_idle(host);
16581685 #endif
16591686
....@@ -1680,6 +1707,8 @@
16801707
16811708 if (oldcard) {
16821709 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1710
+ pr_debug("%s: Perhaps the card was replaced\n",
1711
+ mmc_hostname(host));
16831712 err = -ENOENT;
16841713 goto err;
16851714 }
....@@ -1698,7 +1727,7 @@
16981727 card->ocr = ocr;
16991728 card->type = MMC_TYPE_MMC;
17001729 card->rca = 1;
1701
-#ifndef CONFIG_ROCKCHIP_THUNDER_BOOT
1730
+#ifndef CONFIG_ROCKCHIP_THUNDER_BOOT_MMC
17021731 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
17031732 #endif
17041733 }
....@@ -1731,7 +1760,7 @@
17311760 err = mmc_decode_csd(card);
17321761 if (err)
17331762 goto free_card;
1734
-#ifndef CONFIG_ROCKCHIP_THUNDER_BOOT
1763
+#ifndef CONFIG_ROCKCHIP_THUNDER_BOOT_MMC
17351764 err = mmc_decode_cid(card);
17361765 if (err)
17371766 goto free_card;
....@@ -1833,6 +1862,14 @@
18331862 card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
18341863 }
18351864
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
+
18361873 /*
18371874 * Select timing interface
18381875 */
....@@ -1841,13 +1878,17 @@
18411878 goto free_card;
18421879
18431880 if (mmc_card_hs200(card)) {
1881
+ host->doing_init_tune = 1;
1882
+
18441883 err = mmc_hs200_tuning(card);
1884
+ if (!err)
1885
+ err = mmc_select_hs400(card);
1886
+
1887
+ host->doing_init_tune = 0;
1888
+
18451889 if (err)
18461890 goto free_card;
18471891
1848
- err = mmc_select_hs400(card);
1849
- if (err)
1850
- goto free_card;
18511892 } else if (!mmc_card_hs400es(card)) {
18521893 /* Select the desired bus width optionally */
18531894 err = mmc_select_bus_width(card);
....@@ -1866,7 +1907,7 @@
18661907 /*
18671908 * Enable HPI feature (if supported)
18681909 */
1869
-#ifndef CONFIG_ROCKCHIP_THUNDER_BOOT
1910
+#ifndef CONFIG_ROCKCHIP_THUNDER_BOOT_MMC
18701911 if (card->ext_csd.hpi) {
18711912 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
18721913 EXT_CSD_HPI_MGMT, 1,
....@@ -2047,7 +2088,7 @@
20472088
20482089 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
20492090 EXT_CSD_POWER_OFF_NOTIFICATION,
2050
- notify_type, timeout, 0, true, false, false);
2091
+ notify_type, timeout, 0, false, false);
20512092 if (err)
20522093 pr_err("%s: Power Off Notification timed out, %u\n",
20532094 mmc_hostname(card->host), timeout);
....@@ -2118,18 +2159,13 @@
21182159 if (mmc_card_suspended(host->card))
21192160 goto out;
21202161
2121
- if (mmc_card_doing_bkops(host->card)) {
2122
- err = mmc_stop_bkops(host->card);
2123
- if (err)
2124
- goto out;
2125
- }
2126
-
21272162 err = mmc_flush_cache(host->card);
21282163 if (err)
21292164 goto out;
21302165
21312166 if (mmc_can_poweroff_notify(host->card) &&
2132
- ((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)))
21332169 err = mmc_poweroff_notify(host->card, notify_type);
21342170 else if (mmc_can_sleep(host->card))
21352171 err = mmc_sleep(host);