14 #include <linux/slab.h>
15 #include <linux/stat.h>
28 static const unsigned int tran_exp[] = {
29 10000, 100000, 1000000, 10000000,
33 static const unsigned char tran_mant[] = {
34 0, 10, 12, 13, 15, 20, 25, 30,
35 35, 40, 45, 50, 55, 60, 70, 80,
38 static const unsigned int tacc_exp[] = {
39 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
42 static const unsigned int tacc_mant[] = {
43 0, 10, 12, 13, 15, 20, 25, 30,
44 35, 40, 45, 50, 55, 60, 70, 80,
47 #define UNSTUFF_BITS(resp,start,size) \
49 const int __size = size; \
50 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
51 const int __off = 3 - ((start) / 32); \
52 const int __shft = (start) & 31; \
55 __res = resp[__off] >> __shft; \
56 if (__size + __shft > 32) \
57 __res |= resp[__off-1] << ((32 - __shft) % 32); \
87 card->
cid.year += 2000;
96 unsigned int e,
m, csd_struct;
101 switch (csd_struct) {
105 csd->
tacc_ns = (tacc_exp[
e] * tacc_mant[
m] + 9) / 10;
110 csd->
max_dtr = tran_exp[
e] * tran_mant[
m];
146 csd->
max_dtr = tran_exp[
e] * tran_mant[
m];
151 if (csd->
c_size >= 0xFFFF)
167 pr_err(
"%s: unrecognised CSD structure version %d\n",
180 static int mmc_decode_scr(
struct mmc_card *card)
183 unsigned int scr_struct;
190 if (scr_struct != 0) {
191 pr_err(
"%s: unrecognised SCR structure version %d\n",
215 static int mmc_read_ssr(
struct mmc_card *card)
217 unsigned int au,
es,
et,
eo;
222 pr_warning(
"%s: card lacks mandatory SD Status "
239 for (i = 0; i < 16; i++)
247 if (au > 0 && au <= 9) {
248 card->
ssr.au = 1 << (au + 4);
253 card->
ssr.erase_timeout = (et * 1000) / es;
254 card->
ssr.erase_offset = eo * 1000;
257 pr_warning(
"%s: SD Status: Invalid Allocation Unit "
268 static int mmc_read_switch(
struct mmc_card *card)
278 "function, performance might suffer.\n",
287 pr_err(
"%s: could not allocate a buffer for "
288 "switch capabilities.\n",
307 pr_warning(
"%s: problem reading Bus Speed modes.\n",
317 if (card->
scr.sda_spec3) {
318 card->
sw_caps.sd3_bus_mode = status[13];
320 card->
sw_caps.sd3_drv_type = status[9];
346 if (card->
sw_caps.hs_max_dtr == 0)
353 pr_err(
"%s: could not allocate a buffer for "
362 if ((status[16] & 0xF) != 1) {
364 "into high-speed mode!\n",
377 static int sd_select_driver_type(
struct mmc_card *card,
u8 *status)
393 if (!card->
host->ops->select_drive_strength)
420 mmc_host_clk_hold(card->
host);
421 drive_strength = card->
host->ops->select_drive_strength(
423 host_drv_type, card_drv_type);
424 mmc_host_clk_release(card->
host);
430 if ((status[15] & 0xF) != drive_strength) {
431 pr_warning(
"%s: Problem setting drive strength!\n",
441 static void sd_update_bus_speed_mode(
struct mmc_card *card)
475 static int sd_set_bus_speed_mode(
struct mmc_card *card,
u8 *status)
510 pr_warning(
"%s: Problem setting bus speed mode!\n",
523 u32 voltage, max_current;
525 voltage = 1 << host->
ios.vdd;
545 static int sd_set_current_limit(
struct mmc_card *card,
u8 *status)
565 max_current = sd_get_host_max_current(card->
host);
575 if (max_current >= 800)
577 else if (max_current >= 600)
579 else if (max_current >= 400)
581 else if (max_current >= 200)
589 if (((status[15] >> 4) & 0x0F) != current_limit)
590 pr_warning(
"%s: Problem setting current limit!\n",
601 static int mmc_sd_init_uhs_card(
struct mmc_card *card)
606 if (!card->
scr.sda_spec3)
614 pr_err(
"%s: could not allocate a buffer for "
633 sd_update_bus_speed_mode(card);
636 err = sd_select_driver_type(card, status);
641 err = sd_set_current_limit(card, status);
646 err = sd_set_bus_speed_mode(card, status);
652 mmc_host_clk_hold(card->
host);
653 err = card->
host->ops->execute_tuning(card->
host,
655 mmc_host_clk_release(card->
host);
680 static struct attribute *sd_std_attrs[] = {
685 &dev_attr_erase_size.attr,
686 &dev_attr_preferred_erase_size.attr,
687 &dev_attr_fwrev.attr,
688 &dev_attr_hwrev.attr,
689 &dev_attr_manfid.attr,
691 &dev_attr_oemid.attr,
692 &dev_attr_serial.attr,
697 .attrs = sd_std_attrs,
706 .groups = sd_attr_groups,
747 max_current = sd_get_host_max_current(host);
748 if (max_current > 150)
761 ((*rocr & 0x41000000) == 0x41000000)) {
788 err = mmc_decode_csd(card);
808 err = mmc_decode_scr(card);
815 err = mmc_read_ssr(card);
825 err = mmc_read_switch(card);
848 if (host->
ops->get_ro) {
849 mmc_host_clk_hold(card->
host);
850 ro = host->
ops->get_ro(host);
851 mmc_host_clk_release(card->
host);
856 "support reading read-only "
857 "switch. assuming write-enable.\n",
869 unsigned max_dtr = (
unsigned int)-1;
872 if (max_dtr > card->
sw_caps.hs_max_dtr)
873 max_dtr = card->
sw_caps.hs_max_dtr;
874 }
else if (max_dtr > card->
csd.max_dtr) {
875 max_dtr = card->
csd.max_dtr;
893 static int mmc_sd_init_card(
struct mmc_host *host,
u32 ocr,
919 return PTR_ERR(card);
957 err = mmc_sd_init_uhs_card(card);
968 if (host->
ops->enable_preset_value) {
969 mmc_host_clk_hold(card->
host);
970 host->
ops->enable_preset_value(host,
true);
971 mmc_host_clk_release(card->
host);
1014 static void mmc_sd_remove(
struct mmc_host *host)
1026 static int mmc_sd_alive(
struct mmc_host *host)
1034 static void mmc_sd_detect(
struct mmc_host *host)
1041 mmc_claim_host(host);
1051 mmc_sd_remove(host);
1053 mmc_claim_host(host);
1063 static int mmc_sd_suspend(
struct mmc_host *host)
1070 mmc_claim_host(host);
1085 static int mmc_sd_resume(
struct mmc_host *host)
1092 mmc_claim_host(host);
1093 err = mmc_sd_init_card(host, host->
ocr, host->
card);
1099 static int mmc_sd_power_restore(
struct mmc_host *host)
1104 mmc_claim_host(host);
1105 ret = mmc_sd_init_card(host, host->
ocr, host->
card);
1112 .remove = mmc_sd_remove,
1113 .detect = mmc_sd_detect,
1116 .power_restore = mmc_sd_power_restore,
1117 .alive = mmc_sd_alive,
1120 static const struct mmc_bus_ops mmc_sd_ops_unsafe = {
1121 .remove = mmc_sd_remove,
1122 .detect = mmc_sd_detect,
1123 .suspend = mmc_sd_suspend,
1124 .resume = mmc_sd_resume,
1125 .power_restore = mmc_sd_power_restore,
1126 .alive = mmc_sd_alive,
1129 static void mmc_sd_attach_bus_ops(
struct mmc_host *host)
1133 if (!mmc_card_is_removable(host))
1134 bus_ops = &mmc_sd_ops_unsafe;
1136 bus_ops = &mmc_sd_ops;
1152 if (host->
ops->enable_preset_value) {
1153 mmc_host_clk_hold(host);
1154 host->
ops->enable_preset_value(host,
false);
1155 mmc_host_clk_release(host);
1162 mmc_sd_attach_bus_ops(host);
1182 pr_warning(
"%s: card claims to support voltages "
1183 "below the defined range. These will be ignored.\n",
1190 pr_warning(
"%s: SD card claims to support the "
1191 "incompletely defined 'low voltage range'. This "
1193 ocr &= ~MMC_VDD_165_195;
1209 err = mmc_sd_init_card(host, host->
ocr,
NULL);
1215 mmc_claim_host(host);
1225 mmc_claim_host(host);
1229 pr_err(
"%s: error %d whilst initialising SD card\n",