hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/mmc/core/sd.c
....@@ -1,13 +1,10 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * linux/drivers/mmc/core/sd.c
34 *
45 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
56 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
67 * Copyright (C) 2005-2007 Pierre Ossman, 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>
....@@ -20,6 +17,8 @@
2017 #include <linux/mmc/card.h>
2118 #include <linux/mmc/mmc.h>
2219 #include <linux/mmc/sd.h>
20
+
21
+#include <trace/hooks/mmc_core.h>
2322
2423 #include "core.h"
2524 #include "card.h"
....@@ -215,6 +214,11 @@
215214 /* Check if Physical Layer Spec v3.0 is supported */
216215 scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1);
217216
217
+ if (scr->sda_spec3) {
218
+ scr->sda_spec4 = UNSTUFF_BITS(resp, 42, 1);
219
+ scr->sda_specx = UNSTUFF_BITS(resp, 38, 4);
220
+ }
221
+
218222 if (UNSTUFF_BITS(resp, 55, 1))
219223 card->erased_byte = 0xFF;
220224 else
....@@ -240,6 +244,8 @@
240244 {
241245 unsigned int au, es, et, eo;
242246 __be32 *raw_ssr;
247
+ u32 resp[4] = {};
248
+ u8 discard_support;
243249 int i;
244250
245251 if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
....@@ -284,6 +290,14 @@
284290 mmc_hostname(card->host));
285291 }
286292 }
293
+
294
+ /*
295
+ * starting SD5.1 discard is supported if DISCARD_SUPPORT (b313) is set
296
+ */
297
+ resp[3] = card->raw_ssr[6];
298
+ discard_support = UNSTUFF_BITS(resp, 313 - 288, 1);
299
+ card->erase_arg = (card->scr.sda_specx && discard_support) ?
300
+ SD_DISCARD_ARG : SD_ERASE_ARG;
287301
288302 return 0;
289303 }
....@@ -370,11 +384,11 @@
370384 if (!status)
371385 return -ENOMEM;
372386
373
- err = mmc_sd_switch(card, 1, 0, 1, status);
387
+ err = mmc_sd_switch(card, 1, 0, HIGH_SPEED_BUS_SPEED, status);
374388 if (err)
375389 goto out;
376390
377
- if ((status[16] & 0xF) != 1) {
391
+ if ((status[16] & 0xF) != HIGH_SPEED_BUS_SPEED) {
378392 pr_warn("%s: Problem switching card into high-speed mode!\n",
379393 mmc_hostname(card->host));
380394 err = 0;
....@@ -450,6 +464,8 @@
450464 SD_MODE_UHS_SDR12)) {
451465 card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
452466 }
467
+
468
+ trace_android_vh_sd_update_bus_speed_mode(card);
453469 }
454470
455471 static int sd_set_bus_speed_mode(struct mmc_card *card, u8 *status)
....@@ -701,7 +717,36 @@
701717
702718 static DEVICE_ATTR(dsr, S_IRUGO, mmc_dsr_show, NULL);
703719
720
+MMC_DEV_ATTR(vendor, "0x%04x\n", card->cis.vendor);
721
+MMC_DEV_ATTR(device, "0x%04x\n", card->cis.device);
722
+MMC_DEV_ATTR(revision, "%u.%u\n", card->major_rev, card->minor_rev);
723
+
724
+#define sdio_info_attr(num) \
725
+static ssize_t info##num##_show(struct device *dev, struct device_attribute *attr, char *buf) \
726
+{ \
727
+ struct mmc_card *card = mmc_dev_to_card(dev); \
728
+ \
729
+ if (num > card->num_info) \
730
+ return -ENODATA; \
731
+ if (!card->info[num-1][0]) \
732
+ return 0; \
733
+ return sprintf(buf, "%s\n", card->info[num-1]); \
734
+} \
735
+static DEVICE_ATTR_RO(info##num)
736
+
737
+sdio_info_attr(1);
738
+sdio_info_attr(2);
739
+sdio_info_attr(3);
740
+sdio_info_attr(4);
741
+
704742 static struct attribute *sd_std_attrs[] = {
743
+ &dev_attr_vendor.attr,
744
+ &dev_attr_device.attr,
745
+ &dev_attr_revision.attr,
746
+ &dev_attr_info1.attr,
747
+ &dev_attr_info2.attr,
748
+ &dev_attr_info3.attr,
749
+ &dev_attr_info4.attr,
705750 &dev_attr_cid.attr,
706751 &dev_attr_csd.attr,
707752 &dev_attr_scr.attr,
....@@ -720,7 +765,32 @@
720765 &dev_attr_dsr.attr,
721766 NULL,
722767 };
723
-ATTRIBUTE_GROUPS(sd_std);
768
+
769
+static umode_t sd_std_is_visible(struct kobject *kobj, struct attribute *attr,
770
+ int index)
771
+{
772
+ struct device *dev = kobj_to_dev(kobj);
773
+ struct mmc_card *card = mmc_dev_to_card(dev);
774
+
775
+ /* CIS vendor and device ids, revision and info string are available only for Combo cards */
776
+ if ((attr == &dev_attr_vendor.attr ||
777
+ attr == &dev_attr_device.attr ||
778
+ attr == &dev_attr_revision.attr ||
779
+ attr == &dev_attr_info1.attr ||
780
+ attr == &dev_attr_info2.attr ||
781
+ attr == &dev_attr_info3.attr ||
782
+ attr == &dev_attr_info4.attr
783
+ ) && card->type != MMC_TYPE_SD_COMBO)
784
+ return 0;
785
+
786
+ return attr->mode;
787
+}
788
+
789
+static const struct attribute_group sd_std_group = {
790
+ .attrs = sd_std_attrs,
791
+ .is_visible = sd_std_is_visible,
792
+};
793
+__ATTRIBUTE_GROUPS(sd_std);
724794
725795 struct device_type sd_type = {
726796 .groups = sd_std_groups,
....@@ -787,7 +857,8 @@
787857 * the CCS bit is set as well. We deliberately deviate from the spec in
788858 * regards to this, which allows UHS-I to be supported for SDSC cards.
789859 */
790
- if (!mmc_host_is_spi(host) && rocr && (*rocr & 0x01000000)) {
860
+ if (!mmc_host_is_spi(host) && (ocr & SD_OCR_S18R) &&
861
+ rocr && (*rocr & SD_ROCR_S18A)) {
791862 err = mmc_set_uhs_voltage(host, pocr);
792863 if (err == -EAGAIN) {
793864 retries--;
....@@ -866,14 +937,15 @@
866937
867938 /* Erase init depends on CSD and SSR */
868939 mmc_init_erase(card);
869
-
870
- /*
871
- * Fetch switch information from card.
872
- */
873
- err = mmc_read_switch(card);
874
- if (err)
875
- return err;
876940 }
941
+
942
+ /*
943
+ * Fetch switch information from card. Note, sd3_bus_mode can change if
944
+ * voltage switch outcome changes, so do this always.
945
+ */
946
+ err = mmc_read_switch(card);
947
+ if (err)
948
+ return err;
877949
878950 /*
879951 * For SPI, enable CRC as appropriate.
....@@ -952,8 +1024,11 @@
9521024 return err;
9531025
9541026 if (oldcard) {
955
- if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
1027
+ if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1028
+ pr_debug("%s: Perhaps the card was replaced\n",
1029
+ mmc_hostname(host));
9561030 return -ENOENT;
1031
+ }
9571032
9581033 card = oldcard;
9591034 } else {
....@@ -1020,26 +1095,15 @@
10201095 if (!v18_fixup_failed && !mmc_host_is_spi(host) && mmc_host_uhs(host) &&
10211096 mmc_sd_card_using_v18(card) &&
10221097 host->ios.signal_voltage != MMC_SIGNAL_VOLTAGE_180) {
1023
- /*
1024
- * Re-read switch information in case it has changed since
1025
- * oldcard was initialized.
1026
- */
1027
- if (oldcard) {
1028
- err = mmc_read_switch(card);
1029
- if (err)
1030
- goto free_card;
1098
+ if (mmc_host_set_uhs_voltage(host) ||
1099
+ mmc_sd_init_uhs_card(card)) {
1100
+ v18_fixup_failed = true;
1101
+ mmc_power_cycle(host, ocr);
1102
+ if (!oldcard)
1103
+ mmc_remove_card(card);
1104
+ goto retry;
10311105 }
1032
- if (mmc_sd_card_using_v18(card)) {
1033
- if (mmc_host_set_uhs_voltage(host) ||
1034
- mmc_sd_init_uhs_card(card)) {
1035
- v18_fixup_failed = true;
1036
- mmc_power_cycle(host, ocr);
1037
- if (!oldcard)
1038
- mmc_remove_card(card);
1039
- goto retry;
1040
- }
1041
- goto done;
1042
- }
1106
+ goto cont;
10431107 }
10441108
10451109 /* Initialization sequence for UHS-I cards */
....@@ -1074,6 +1138,16 @@
10741138 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
10751139 }
10761140 }
1141
+cont:
1142
+ if (host->cqe_ops && !host->cqe_enabled) {
1143
+ err = host->cqe_ops->cqe_enable(host, card);
1144
+ if (!err) {
1145
+ host->cqe_enabled = true;
1146
+ host->hsq_enabled = true;
1147
+ pr_info("%s: Host Software Queue enabled\n",
1148
+ mmc_hostname(host));
1149
+ }
1150
+ }
10771151
10781152 if (host->caps2 & MMC_CAP2_AVOID_3_3V &&
10791153 host->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
....@@ -1082,7 +1156,7 @@
10821156 err = -EINVAL;
10831157 goto free_card;
10841158 }
1085
-done:
1159
+
10861160 host->card = card;
10871161 return 0;
10881162
....@@ -1162,8 +1236,8 @@
11621236 {
11631237 int err = 0;
11641238
1165
- BUG_ON(!host);
1166
- BUG_ON(!host->card);
1239
+ if (WARN_ON(!host) || WARN_ON(!host->card))
1240
+ return 0;
11671241
11681242 mmc_claim_host(host);
11691243
....@@ -1178,19 +1252,10 @@
11781252 mmc_card_set_suspended(host->card);
11791253 }
11801254
1181
- /*
1182
- * mmc_sd_shutdown is used for SD card, so what we need
1183
- * is to make sure we set signal voltage to initial state
1184
- * if it's used as main disk. RESTRICT_CARD_TYPE_MMC is
1185
- * combined into host->restrict_caps via DT for SD cards
1186
- * running system image.
1187
- */
1188
- if (host->restrict_caps & RESTRICT_CARD_TYPE_EMMC) {
1189
- host->ios.signal_voltage = MMC_SIGNAL_VOLTAGE_330;
1190
- host->ios.vdd = fls(host->ocr_avail) - 1;
1191
- mmc_regulator_set_vqmmc(host, &host->ios);
1192
- pr_info("Set signal voltage to initial state\n");
1193
- }
1255
+ host->ios.signal_voltage = MMC_SIGNAL_VOLTAGE_330;
1256
+ host->ios.vdd = fls(host->ocr_avail) - 1;
1257
+ mmc_regulator_set_vqmmc(host, &host->ios);
1258
+ pr_info("Set signal voltage to initial state\n");
11941259
11951260 out:
11961261 mmc_release_host(host);
....@@ -1378,5 +1443,7 @@
13781443 pr_err("%s: error %d whilst initialising SD card\n",
13791444 mmc_hostname(host), err);
13801445
1446
+ trace_android_vh_mmc_attach_sd(host, ocr, err);
1447
+
13811448 return err;
13821449 }