aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mmc/mmc.c
diff options
context:
space:
mode:
authorPierre Ossman <drzeus@drzeus.cx>2007-01-03 13:47:29 -0500
committerPierre Ossman <drzeus@drzeus.cx>2007-05-01 07:04:18 -0400
commitb855885e3b60cf6f9452848712a62517b94583eb (patch)
tree2e8a6fb8d8992ac8fb968a26c6db8778c2b5e791 /drivers/mmc/mmc.c
parentb5af25bee2de2f6cd1ac74ba737cbc4f3d303e5d (diff)
mmc: deprecate mmc bus topology
The classic MMC bus was defined as multi card bus system, which is reflected in the design in the MMC layer. When SD showed up, the bus topology was abandoned and a star topology (one card per host) was mandated. MMC version 4 has followed this, officially deprecating the bus topology. As we do not have any known users of the bus topology we can remove support for it. This will simplify the code and rectify some incorrect assumptions in the newer additions. Signed-off-by: Pierre Ossman <drzeus@drzeus.cx>
Diffstat (limited to 'drivers/mmc/mmc.c')
-rw-r--r--drivers/mmc/mmc.c921
1 files changed, 393 insertions, 528 deletions
diff --git a/drivers/mmc/mmc.c b/drivers/mmc/mmc.c
index 94c04725726d..3f50b8882c89 100644
--- a/drivers/mmc/mmc.c
+++ b/drivers/mmc/mmc.c
@@ -3,7 +3,7 @@
3 * 3 *
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved. 4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved. 5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
6 * SD support Copyright (C) 2005 Pierre Ossman, All Rights Reserved. 6 * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
7 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved. 7 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
8 * 8 *
9 * This program is free software; you can redistribute it and/or modify 9 * This program is free software; you can redistribute it and/or modify
@@ -316,8 +316,6 @@ void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
316} 316}
317EXPORT_SYMBOL(mmc_set_data_timeout); 317EXPORT_SYMBOL(mmc_set_data_timeout);
318 318
319static int mmc_select_card(struct mmc_host *host, struct mmc_card *card);
320
321/** 319/**
322 * __mmc_claim_host - exclusively claim a host 320 * __mmc_claim_host - exclusively claim a host
323 * @host: mmc host to claim 321 * @host: mmc host to claim
@@ -329,11 +327,10 @@ static int mmc_select_card(struct mmc_host *host, struct mmc_card *card);
329 * 327 *
330 * Note: you should use mmc_card_claim_host or mmc_claim_host. 328 * Note: you should use mmc_card_claim_host or mmc_claim_host.
331 */ 329 */
332int __mmc_claim_host(struct mmc_host *host, struct mmc_card *card) 330void mmc_claim_host(struct mmc_host *host)
333{ 331{
334 DECLARE_WAITQUEUE(wait, current); 332 DECLARE_WAITQUEUE(wait, current);
335 unsigned long flags; 333 unsigned long flags;
336 int err = 0;
337 334
338 add_wait_queue(&host->wq, &wait); 335 add_wait_queue(&host->wq, &wait);
339 spin_lock_irqsave(&host->lock, flags); 336 spin_lock_irqsave(&host->lock, flags);
@@ -349,17 +346,9 @@ int __mmc_claim_host(struct mmc_host *host, struct mmc_card *card)
349 host->claimed = 1; 346 host->claimed = 1;
350 spin_unlock_irqrestore(&host->lock, flags); 347 spin_unlock_irqrestore(&host->lock, flags);
351 remove_wait_queue(&host->wq, &wait); 348 remove_wait_queue(&host->wq, &wait);
352
353 if (card != (void *)-1) {
354 err = mmc_select_card(host, card);
355 if (err != MMC_ERR_NONE)
356 return err;
357 }
358
359 return err;
360} 349}
361 350
362EXPORT_SYMBOL(__mmc_claim_host); 351EXPORT_SYMBOL(mmc_claim_host);
363 352
364/** 353/**
365 * mmc_release_host - release a host 354 * mmc_release_host - release a host
@@ -396,23 +385,18 @@ static inline void mmc_set_ios(struct mmc_host *host)
396 host->ops->set_ios(host, ios); 385 host->ops->set_ios(host, ios);
397} 386}
398 387
399static int mmc_select_card(struct mmc_host *host, struct mmc_card *card) 388static int mmc_select_card(struct mmc_card *card)
400{ 389{
401 int err; 390 int err;
402 struct mmc_command cmd; 391 struct mmc_command cmd;
403 392
404 BUG_ON(!host->claimed); 393 BUG_ON(!card->host->claimed);
405
406 if (host->card_selected == card)
407 return MMC_ERR_NONE;
408
409 host->card_selected = card;
410 394
411 cmd.opcode = MMC_SELECT_CARD; 395 cmd.opcode = MMC_SELECT_CARD;
412 cmd.arg = card->rca << 16; 396 cmd.arg = card->rca << 16;
413 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 397 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
414 398
415 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); 399 err = mmc_wait_for_cmd(card->host, &cmd, CMD_RETRIES);
416 if (err != MMC_ERR_NONE) 400 if (err != MMC_ERR_NONE)
417 return err; 401 return err;
418 402
@@ -426,49 +410,24 @@ static int mmc_select_card(struct mmc_host *host, struct mmc_card *card)
426 * wider version. 410 * wider version.
427 */ 411 */
428 if (mmc_card_sd(card) && 412 if (mmc_card_sd(card) &&
429 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) { 413 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4) &&
414 (card->host->caps & MMC_CAP_4_BIT_DATA)) {
430 415
431 /* 416 struct mmc_command cmd;
432 * Default bus width is 1 bit. 417 cmd.opcode = SD_APP_SET_BUS_WIDTH;
433 */ 418 cmd.arg = SD_BUS_WIDTH_4;
434 host->ios.bus_width = MMC_BUS_WIDTH_1; 419 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
435
436 if (host->caps & MMC_CAP_4_BIT_DATA) {
437 struct mmc_command cmd;
438 cmd.opcode = SD_APP_SET_BUS_WIDTH;
439 cmd.arg = SD_BUS_WIDTH_4;
440 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
441
442 err = mmc_wait_for_app_cmd(host, card->rca, &cmd,
443 CMD_RETRIES);
444 if (err != MMC_ERR_NONE)
445 return err;
446
447 host->ios.bus_width = MMC_BUS_WIDTH_4;
448 }
449 }
450
451 mmc_set_ios(host);
452
453 return MMC_ERR_NONE;
454}
455
456/*
457 * Ensure that no card is selected.
458 */
459static void mmc_deselect_cards(struct mmc_host *host)
460{
461 struct mmc_command cmd;
462
463 if (host->card_selected) {
464 host->card_selected = NULL;
465 420
466 cmd.opcode = MMC_SELECT_CARD; 421 err = mmc_wait_for_app_cmd(card->host, card->rca,
467 cmd.arg = 0; 422 &cmd, CMD_RETRIES);
468 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC; 423 if (err != MMC_ERR_NONE)
424 return err;
469 425
470 mmc_wait_for_cmd(host, &cmd, 0); 426 card->host->ios.bus_width = MMC_BUS_WIDTH_4;
427 mmc_set_ios(card->host);
471 } 428 }
429
430 return MMC_ERR_NONE;
472} 431}
473 432
474 433
@@ -732,27 +691,12 @@ static void mmc_decode_scr(struct mmc_card *card)
732} 691}
733 692
734/* 693/*
735 * Locate a MMC card on this MMC host given a raw CID. 694 * Allocate a new MMC card
736 */
737static struct mmc_card *mmc_find_card(struct mmc_host *host, u32 *raw_cid)
738{
739 struct mmc_card *card;
740
741 list_for_each_entry(card, &host->cards, node) {
742 if (memcmp(card->raw_cid, raw_cid, sizeof(card->raw_cid)) == 0)
743 return card;
744 }
745 return NULL;
746}
747
748/*
749 * Allocate a new MMC card, and assign a unique RCA.
750 */ 695 */
751static struct mmc_card * 696static struct mmc_card *
752mmc_alloc_card(struct mmc_host *host, u32 *raw_cid, unsigned int *frca) 697mmc_alloc_card(struct mmc_host *host, u32 *raw_cid)
753{ 698{
754 struct mmc_card *card, *c; 699 struct mmc_card *card;
755 unsigned int rca = *frca;
756 700
757 card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL); 701 card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL);
758 if (!card) 702 if (!card)
@@ -761,17 +705,6 @@ mmc_alloc_card(struct mmc_host *host, u32 *raw_cid, unsigned int *frca)
761 mmc_init_card(card, host); 705 mmc_init_card(card, host);
762 memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid)); 706 memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid));
763 707
764 again:
765 list_for_each_entry(c, &host->cards, node)
766 if (c->rca == rca) {
767 rca++;
768 goto again;
769 }
770
771 card->rca = rca;
772
773 *frca = rca;
774
775 return card; 708 return card;
776} 709}
777 710
@@ -937,128 +870,128 @@ static int mmc_send_if_cond(struct mmc_host *host, u32 ocr, int *rsd2)
937} 870}
938 871
939/* 872/*
940 * Discover cards by requesting their CID. If this command 873 * Discover the card by requesting its CID.
941 * times out, it is not an error; there are no further cards
942 * to be discovered. Add new cards to the list.
943 * 874 *
944 * Create a mmc_card entry for each discovered card, assigning 875 * Create a mmc_card entry for the discovered card, assigning
945 * it an RCA, and save the raw CID for decoding later. 876 * it an RCA, and save the raw CID for decoding later.
946 */ 877 */
947static void mmc_discover_cards(struct mmc_host *host) 878static void mmc_discover_card(struct mmc_host *host)
948{ 879{
949 struct mmc_card *card; 880 unsigned int err;
950 unsigned int first_rca = 1, err;
951 881
952 while (1) { 882 struct mmc_command cmd;
953 struct mmc_command cmd;
954 883
955 cmd.opcode = MMC_ALL_SEND_CID; 884 BUG_ON(host->card);
885
886 cmd.opcode = MMC_ALL_SEND_CID;
887 cmd.arg = 0;
888 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
889
890 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
891 if (err == MMC_ERR_TIMEOUT) {
892 err = MMC_ERR_NONE;
893 return;
894 }
895 if (err != MMC_ERR_NONE) {
896 printk(KERN_ERR "%s: error requesting CID: %d\n",
897 mmc_hostname(host), err);
898 return;
899 }
900
901 host->card = mmc_alloc_card(host, cmd.resp);
902 if (IS_ERR(host->card)) {
903 err = PTR_ERR(host->card);
904 host->card = NULL;
905 return;
906 }
907
908 if (host->mode == MMC_MODE_SD) {
909 host->card->type = MMC_TYPE_SD;
910
911 cmd.opcode = SD_SEND_RELATIVE_ADDR;
956 cmd.arg = 0; 912 cmd.arg = 0;
957 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 913 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
958 914
959 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); 915 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
960 if (err == MMC_ERR_TIMEOUT) { 916 if (err != MMC_ERR_NONE)
961 err = MMC_ERR_NONE; 917 mmc_card_set_dead(host->card);
962 break; 918 else {
963 } 919 host->card->rca = cmd.resp[0] >> 16;
964 if (err != MMC_ERR_NONE) { 920
965 printk(KERN_ERR "%s: error requesting CID: %d\n", 921 if (!host->ops->get_ro) {
966 mmc_hostname(host), err); 922 printk(KERN_WARNING "%s: host does not "
967 break; 923 "support reading read-only "
968 } 924 "switch. assuming write-enable.\n",
969 925 mmc_hostname(host));
970 card = mmc_find_card(host, cmd.resp); 926 } else {
971 if (!card) { 927 if (host->ops->get_ro(host))
972 card = mmc_alloc_card(host, cmd.resp, &first_rca); 928 mmc_card_set_readonly(host->card);
973 if (IS_ERR(card)) {
974 err = PTR_ERR(card);
975 break;
976 } 929 }
977 list_add(&card->node, &host->cards);
978 } 930 }
931 } else {
932 host->card->type = MMC_TYPE_MMC;
933 host->card->rca = 1;
979 934
980 card->state &= ~MMC_STATE_DEAD; 935 cmd.opcode = MMC_SET_RELATIVE_ADDR;
981 936 cmd.arg = host->card->rca << 16;
982 if (host->mode == MMC_MODE_SD) { 937 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
983 card->type = MMC_TYPE_SD; 938
984 939 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
985 cmd.opcode = SD_SEND_RELATIVE_ADDR; 940 if (err != MMC_ERR_NONE)
986 cmd.arg = 0; 941 mmc_card_set_dead(host->card);
987 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
988
989 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
990 if (err != MMC_ERR_NONE)
991 mmc_card_set_dead(card);
992 else {
993 card->rca = cmd.resp[0] >> 16;
994
995 if (!host->ops->get_ro) {
996 printk(KERN_WARNING "%s: host does not "
997 "support reading read-only "
998 "switch. assuming write-enable.\n",
999 mmc_hostname(host));
1000 } else {
1001 if (host->ops->get_ro(host))
1002 mmc_card_set_readonly(card);
1003 }
1004 }
1005 } else {
1006 card->type = MMC_TYPE_MMC;
1007 cmd.opcode = MMC_SET_RELATIVE_ADDR;
1008 cmd.arg = card->rca << 16;
1009 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1010
1011 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1012 if (err != MMC_ERR_NONE)
1013 mmc_card_set_dead(card);
1014 }
1015 } 942 }
1016} 943}
1017 944
1018static void mmc_read_csds(struct mmc_host *host) 945static void mmc_read_csd(struct mmc_host *host)
1019{ 946{
1020 struct mmc_card *card; 947 struct mmc_command cmd;
1021 948 int err;
1022 list_for_each_entry(card, &host->cards, node) {
1023 struct mmc_command cmd;
1024 int err;
1025 949
1026 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT)) 950 if (!host->card)
1027 continue; 951 return;
952 if (mmc_card_dead(host->card))
953 return;
1028 954
1029 cmd.opcode = MMC_SEND_CSD; 955 cmd.opcode = MMC_SEND_CSD;
1030 cmd.arg = card->rca << 16; 956 cmd.arg = host->card->rca << 16;
1031 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC; 957 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
1032 958
1033 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); 959 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1034 if (err != MMC_ERR_NONE) { 960 if (err != MMC_ERR_NONE) {
1035 mmc_card_set_dead(card); 961 mmc_card_set_dead(host->card);
1036 continue; 962 return;
1037 } 963 }
1038 964
1039 memcpy(card->raw_csd, cmd.resp, sizeof(card->raw_csd)); 965 memcpy(host->card->raw_csd, cmd.resp, sizeof(host->card->raw_csd));
1040 966
1041 mmc_decode_csd(card); 967 mmc_decode_csd(host->card);
1042 mmc_decode_cid(card); 968 mmc_decode_cid(host->card);
1043 }
1044} 969}
1045 970
1046static void mmc_process_ext_csds(struct mmc_host *host) 971static void mmc_process_ext_csd(struct mmc_host *host)
1047{ 972{
1048 int err; 973 int err;
1049 struct mmc_card *card;
1050 974
1051 struct mmc_request mrq; 975 struct mmc_request mrq;
1052 struct mmc_command cmd; 976 struct mmc_command cmd;
1053 struct mmc_data data; 977 struct mmc_data data;
1054 978
979 u8 *ext_csd;
1055 struct scatterlist sg; 980 struct scatterlist sg;
1056 981
982 if (!host->card)
983 return;
984 if (mmc_card_dead(host->card))
985 return;
986 if (mmc_card_sd(host->card))
987 return;
988 if (host->card->csd.mmca_vsn < CSD_SPEC_VER_4)
989 return;
990
1057 /* 991 /*
1058 * As the ext_csd is so large and mostly unused, we don't store the 992 * As the ext_csd is so large and mostly unused, we don't store the
1059 * raw block in mmc_card. 993 * raw block in mmc_card.
1060 */ 994 */
1061 u8 *ext_csd;
1062 ext_csd = kmalloc(512, GFP_KERNEL); 995 ext_csd = kmalloc(512, GFP_KERNEL);
1063 if (!ext_csd) { 996 if (!ext_csd) {
1064 printk("%s: could not allocate a buffer to receive the ext_csd." 997 printk("%s: could not allocate a buffer to receive the ext_csd."
@@ -1067,211 +1000,184 @@ static void mmc_process_ext_csds(struct mmc_host *host)
1067 return; 1000 return;
1068 } 1001 }
1069 1002
1070 list_for_each_entry(card, &host->cards, node) { 1003 memset(&cmd, 0, sizeof(struct mmc_command));
1071 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
1072 continue;
1073 if (mmc_card_sd(card))
1074 continue;
1075 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
1076 continue;
1077
1078 err = mmc_select_card(host, card);
1079 if (err != MMC_ERR_NONE) {
1080 mmc_card_set_dead(card);
1081 continue;
1082 }
1083
1084 memset(&cmd, 0, sizeof(struct mmc_command));
1085 1004
1086 cmd.opcode = MMC_SEND_EXT_CSD; 1005 cmd.opcode = MMC_SEND_EXT_CSD;
1087 cmd.arg = 0; 1006 cmd.arg = 0;
1088 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1007 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1089 1008
1090 memset(&data, 0, sizeof(struct mmc_data)); 1009 memset(&data, 0, sizeof(struct mmc_data));
1091 1010
1092 mmc_set_data_timeout(&data, card, 0); 1011 mmc_set_data_timeout(&data, host->card, 0);
1093 1012
1094 data.blksz = 512; 1013 data.blksz = 512;
1095 data.blocks = 1; 1014 data.blocks = 1;
1096 data.flags = MMC_DATA_READ; 1015 data.flags = MMC_DATA_READ;
1097 data.sg = &sg; 1016 data.sg = &sg;
1098 data.sg_len = 1; 1017 data.sg_len = 1;
1099 1018
1100 memset(&mrq, 0, sizeof(struct mmc_request)); 1019 memset(&mrq, 0, sizeof(struct mmc_request));
1101 1020
1102 mrq.cmd = &cmd; 1021 mrq.cmd = &cmd;
1103 mrq.data = &data; 1022 mrq.data = &data;
1104 1023
1105 sg_init_one(&sg, ext_csd, 512); 1024 sg_init_one(&sg, ext_csd, 512);
1106 1025
1107 mmc_wait_for_req(host, &mrq); 1026 mmc_wait_for_req(host, &mrq);
1108 1027
1109 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) { 1028 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
1110 if (card->csd.capacity == (4096 * 512)) { 1029 if (host->card->csd.capacity == (4096 * 512)) {
1111 printk(KERN_ERR "%s: unable to read EXT_CSD " 1030 printk(KERN_ERR "%s: unable to read EXT_CSD "
1112 "on a possible high capacity card. " 1031 "on a possible high capacity card. "
1113 "Card will be ignored.\n", 1032 "Card will be ignored.\n",
1114 mmc_hostname(card->host)); 1033 mmc_hostname(host));
1115 mmc_card_set_dead(card); 1034 mmc_card_set_dead(host->card);
1116 } else { 1035 } else {
1117 printk(KERN_WARNING "%s: unable to read " 1036 printk(KERN_WARNING "%s: unable to read "
1118 "EXT_CSD, performance might " 1037 "EXT_CSD, performance might "
1119 "suffer.\n", 1038 "suffer.\n",
1120 mmc_hostname(card->host)); 1039 mmc_hostname(host));
1121 }
1122 continue;
1123 } 1040 }
1041 goto out;
1042 }
1124 1043
1125 card->ext_csd.sectors = 1044 host->card->ext_csd.sectors =
1126 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 | 1045 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
1127 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 | 1046 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
1128 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 | 1047 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
1129 ext_csd[EXT_CSD_SEC_CNT + 3] << 24; 1048 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
1130 if (card->ext_csd.sectors) 1049 if (host->card->ext_csd.sectors)
1131 mmc_card_set_blockaddr(card); 1050 mmc_card_set_blockaddr(host->card);
1051
1052 switch (ext_csd[EXT_CSD_CARD_TYPE]) {
1053 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
1054 host->card->ext_csd.hs_max_dtr = 52000000;
1055 break;
1056 case EXT_CSD_CARD_TYPE_26:
1057 host->card->ext_csd.hs_max_dtr = 26000000;
1058 break;
1059 default:
1060 /* MMC v4 spec says this cannot happen */
1061 printk("%s: card is mmc v4 but doesn't support "
1062 "any high-speed modes.\n",
1063 mmc_hostname(host));
1064 goto out;
1065 }
1132 1066
1133 switch (ext_csd[EXT_CSD_CARD_TYPE]) { 1067 if (host->caps & MMC_CAP_MMC_HIGHSPEED) {
1134 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26: 1068 /* Activate highspeed support. */
1135 card->ext_csd.hs_max_dtr = 52000000; 1069 cmd.opcode = MMC_SWITCH;
1136 break; 1070 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1137 case EXT_CSD_CARD_TYPE_26: 1071 (EXT_CSD_HS_TIMING << 16) |
1138 card->ext_csd.hs_max_dtr = 26000000; 1072 (1 << 8) |
1139 break; 1073 EXT_CSD_CMD_SET_NORMAL;
1140 default: 1074 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1141 /* MMC v4 spec says this cannot happen */
1142 printk("%s: card is mmc v4 but doesn't support "
1143 "any high-speed modes.\n",
1144 mmc_hostname(card->host));
1145 continue;
1146 }
1147 1075
1148 if (host->caps & MMC_CAP_MMC_HIGHSPEED) { 1076 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1149 /* Activate highspeed support. */ 1077 if (err != MMC_ERR_NONE) {
1150 cmd.opcode = MMC_SWITCH; 1078 printk("%s: failed to switch card to mmc v4 "
1151 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | 1079 "high-speed mode.\n",
1152 (EXT_CSD_HS_TIMING << 16) | 1080 mmc_hostname(host));
1153 (1 << 8) | 1081 goto out;
1154 EXT_CSD_CMD_SET_NORMAL; 1082 }
1155 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1156
1157 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1158 if (err != MMC_ERR_NONE) {
1159 printk("%s: failed to switch card to mmc v4 "
1160 "high-speed mode.\n",
1161 mmc_hostname(card->host));
1162 continue;
1163 }
1164 1083
1165 mmc_card_set_highspeed(card); 1084 mmc_card_set_highspeed(host->card);
1166 1085
1167 host->ios.timing = MMC_TIMING_MMC_HS; 1086 host->ios.timing = MMC_TIMING_MMC_HS;
1168 mmc_set_ios(host); 1087 mmc_set_ios(host);
1169 } 1088 }
1170 1089
1171 /* Check for host support for wide-bus modes. */ 1090 /* Check for host support for wide-bus modes. */
1172 if (host->caps & MMC_CAP_4_BIT_DATA) { 1091 if (host->caps & MMC_CAP_4_BIT_DATA) {
1173 /* Activate 4-bit support. */ 1092 /* Activate 4-bit support. */
1174 cmd.opcode = MMC_SWITCH; 1093 cmd.opcode = MMC_SWITCH;
1175 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | 1094 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1176 (EXT_CSD_BUS_WIDTH << 16) | 1095 (EXT_CSD_BUS_WIDTH << 16) |
1177 (EXT_CSD_BUS_WIDTH_4 << 8) | 1096 (EXT_CSD_BUS_WIDTH_4 << 8) |
1178 EXT_CSD_CMD_SET_NORMAL; 1097 EXT_CSD_CMD_SET_NORMAL;
1179 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; 1098 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1180
1181 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1182 if (err != MMC_ERR_NONE) {
1183 printk("%s: failed to switch card to "
1184 "mmc v4 4-bit bus mode.\n",
1185 mmc_hostname(card->host));
1186 continue;
1187 }
1188 1099
1189 host->ios.bus_width = MMC_BUS_WIDTH_4; 1100 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1190 mmc_set_ios(host); 1101 if (err != MMC_ERR_NONE) {
1102 printk("%s: failed to switch card to "
1103 "mmc v4 4-bit bus mode.\n",
1104 mmc_hostname(host));
1105 goto out;
1191 } 1106 }
1107
1108 host->ios.bus_width = MMC_BUS_WIDTH_4;
1109 mmc_set_ios(host);
1192 } 1110 }
1193 1111
1112out:
1194 kfree(ext_csd); 1113 kfree(ext_csd);
1195
1196 mmc_deselect_cards(host);
1197} 1114}
1198 1115
1199static void mmc_read_scrs(struct mmc_host *host) 1116static void mmc_read_scr(struct mmc_host *host)
1200{ 1117{
1201 int err; 1118 int err;
1202 struct mmc_card *card;
1203 struct mmc_request mrq; 1119 struct mmc_request mrq;
1204 struct mmc_command cmd; 1120 struct mmc_command cmd;
1205 struct mmc_data data; 1121 struct mmc_data data;
1206 struct scatterlist sg; 1122 struct scatterlist sg;
1207 1123
1208 list_for_each_entry(card, &host->cards, node) { 1124 if (!host->card)
1209 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT)) 1125 return;
1210 continue; 1126 if (mmc_card_dead(host->card))
1211 if (!mmc_card_sd(card)) 1127 return;
1212 continue; 1128 if (!mmc_card_sd(host->card))
1213 1129 return;
1214 err = mmc_select_card(host, card);
1215 if (err != MMC_ERR_NONE) {
1216 mmc_card_set_dead(card);
1217 continue;
1218 }
1219
1220 memset(&cmd, 0, sizeof(struct mmc_command));
1221
1222 cmd.opcode = MMC_APP_CMD;
1223 cmd.arg = card->rca << 16;
1224 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1225 1130
1226 err = mmc_wait_for_cmd(host, &cmd, 0); 1131 memset(&cmd, 0, sizeof(struct mmc_command));
1227 if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD)) {
1228 mmc_card_set_dead(card);
1229 continue;
1230 }
1231 1132
1232 memset(&cmd, 0, sizeof(struct mmc_command)); 1133 cmd.opcode = MMC_APP_CMD;
1134 cmd.arg = host->card->rca << 16;
1135 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1233 1136
1234 cmd.opcode = SD_APP_SEND_SCR; 1137 err = mmc_wait_for_cmd(host, &cmd, 0);
1235 cmd.arg = 0; 1138 if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD)) {
1236 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1139 mmc_card_set_dead(host->card);
1140 return;
1141 }
1237 1142
1238 memset(&data, 0, sizeof(struct mmc_data)); 1143 memset(&cmd, 0, sizeof(struct mmc_command));
1239 1144
1240 mmc_set_data_timeout(&data, card, 0); 1145 cmd.opcode = SD_APP_SEND_SCR;
1146 cmd.arg = 0;
1147 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1241 1148
1242 data.blksz = 1 << 3; 1149 memset(&data, 0, sizeof(struct mmc_data));
1243 data.blocks = 1;
1244 data.flags = MMC_DATA_READ;
1245 data.sg = &sg;
1246 data.sg_len = 1;
1247 1150
1248 memset(&mrq, 0, sizeof(struct mmc_request)); 1151 mmc_set_data_timeout(&data, host->card, 0);
1249 1152
1250 mrq.cmd = &cmd; 1153 data.blksz = 1 << 3;
1251 mrq.data = &data; 1154 data.blocks = 1;
1155 data.flags = MMC_DATA_READ;
1156 data.sg = &sg;
1157 data.sg_len = 1;
1252 1158
1253 sg_init_one(&sg, (u8*)card->raw_scr, 8); 1159 memset(&mrq, 0, sizeof(struct mmc_request));
1254 1160
1255 mmc_wait_for_req(host, &mrq); 1161 mrq.cmd = &cmd;
1162 mrq.data = &data;
1256 1163
1257 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) { 1164 sg_init_one(&sg, (u8*)host->card->raw_scr, 8);
1258 mmc_card_set_dead(card);
1259 continue;
1260 }
1261 1165
1262 card->raw_scr[0] = ntohl(card->raw_scr[0]); 1166 mmc_wait_for_req(host, &mrq);
1263 card->raw_scr[1] = ntohl(card->raw_scr[1]);
1264 1167
1265 mmc_decode_scr(card); 1168 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
1169 mmc_card_set_dead(host->card);
1170 return;
1266 } 1171 }
1267 1172
1268 mmc_deselect_cards(host); 1173 host->card->raw_scr[0] = ntohl(host->card->raw_scr[0]);
1174 host->card->raw_scr[1] = ntohl(host->card->raw_scr[1]);
1175
1176 mmc_decode_scr(host->card);
1269} 1177}
1270 1178
1271static void mmc_read_switch_caps(struct mmc_host *host) 1179static void mmc_read_switch_caps(struct mmc_host *host)
1272{ 1180{
1273 int err;
1274 struct mmc_card *card;
1275 struct mmc_request mrq; 1181 struct mmc_request mrq;
1276 struct mmc_command cmd; 1182 struct mmc_command cmd;
1277 struct mmc_data data; 1183 struct mmc_data data;
@@ -1281,6 +1187,15 @@ static void mmc_read_switch_caps(struct mmc_host *host)
1281 if (!(host->caps & MMC_CAP_SD_HIGHSPEED)) 1187 if (!(host->caps & MMC_CAP_SD_HIGHSPEED))
1282 return; 1188 return;
1283 1189
1190 if (!host->card)
1191 return;
1192 if (mmc_card_dead(host->card))
1193 return;
1194 if (!mmc_card_sd(host->card))
1195 return;
1196 if (host->card->scr.sda_vsn < SCR_SPEC_VER_1)
1197 return;
1198
1284 status = kmalloc(64, GFP_KERNEL); 1199 status = kmalloc(64, GFP_KERNEL);
1285 if (!status) { 1200 if (!status) {
1286 printk(KERN_WARNING "%s: Unable to allocate buffer for " 1201 printk(KERN_WARNING "%s: Unable to allocate buffer for "
@@ -1289,116 +1204,98 @@ static void mmc_read_switch_caps(struct mmc_host *host)
1289 return; 1204 return;
1290 } 1205 }
1291 1206
1292 list_for_each_entry(card, &host->cards, node) { 1207 memset(&cmd, 0, sizeof(struct mmc_command));
1293 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
1294 continue;
1295 if (!mmc_card_sd(card))
1296 continue;
1297 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
1298 continue;
1299
1300 err = mmc_select_card(host, card);
1301 if (err != MMC_ERR_NONE) {
1302 mmc_card_set_dead(card);
1303 continue;
1304 }
1305
1306 memset(&cmd, 0, sizeof(struct mmc_command));
1307 1208
1308 cmd.opcode = SD_SWITCH; 1209 cmd.opcode = SD_SWITCH;
1309 cmd.arg = 0x00FFFFF1; 1210 cmd.arg = 0x00FFFFF1;
1310 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1211 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1311 1212
1312 memset(&data, 0, sizeof(struct mmc_data)); 1213 memset(&data, 0, sizeof(struct mmc_data));
1313 1214
1314 mmc_set_data_timeout(&data, card, 0); 1215 mmc_set_data_timeout(&data, host->card, 0);
1315 1216
1316 data.blksz = 64; 1217 data.blksz = 64;
1317 data.blocks = 1; 1218 data.blocks = 1;
1318 data.flags = MMC_DATA_READ; 1219 data.flags = MMC_DATA_READ;
1319 data.sg = &sg; 1220 data.sg = &sg;
1320 data.sg_len = 1; 1221 data.sg_len = 1;
1321 1222
1322 memset(&mrq, 0, sizeof(struct mmc_request)); 1223 memset(&mrq, 0, sizeof(struct mmc_request));
1323 1224
1324 mrq.cmd = &cmd; 1225 mrq.cmd = &cmd;
1325 mrq.data = &data; 1226 mrq.data = &data;
1326 1227
1327 sg_init_one(&sg, status, 64); 1228 sg_init_one(&sg, status, 64);
1328 1229
1329 mmc_wait_for_req(host, &mrq); 1230 mmc_wait_for_req(host, &mrq);
1330 1231
1331 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) { 1232 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
1332 printk("%s: unable to read switch capabilities, " 1233 printk("%s: unable to read switch capabilities, "
1333 "performance might suffer.\n", 1234 "performance might suffer.\n",
1334 mmc_hostname(card->host)); 1235 mmc_hostname(host));
1335 continue; 1236 goto out;
1336 } 1237 }
1337 1238
1338 if (status[13] & 0x02) 1239 if (status[13] & 0x02)
1339 card->sw_caps.hs_max_dtr = 50000000; 1240 host->card->sw_caps.hs_max_dtr = 50000000;
1340 1241
1341 memset(&cmd, 0, sizeof(struct mmc_command)); 1242 memset(&cmd, 0, sizeof(struct mmc_command));
1342 1243
1343 cmd.opcode = SD_SWITCH; 1244 cmd.opcode = SD_SWITCH;
1344 cmd.arg = 0x80FFFFF1; 1245 cmd.arg = 0x80FFFFF1;
1345 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1246 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1346 1247
1347 memset(&data, 0, sizeof(struct mmc_data)); 1248 memset(&data, 0, sizeof(struct mmc_data));
1348 1249
1349 mmc_set_data_timeout(&data, card, 0); 1250 mmc_set_data_timeout(&data, host->card, 0);
1350 1251
1351 data.blksz = 64; 1252 data.blksz = 64;
1352 data.blocks = 1; 1253 data.blocks = 1;
1353 data.flags = MMC_DATA_READ; 1254 data.flags = MMC_DATA_READ;
1354 data.sg = &sg; 1255 data.sg = &sg;
1355 data.sg_len = 1; 1256 data.sg_len = 1;
1356 1257
1357 memset(&mrq, 0, sizeof(struct mmc_request)); 1258 memset(&mrq, 0, sizeof(struct mmc_request));
1358 1259
1359 mrq.cmd = &cmd; 1260 mrq.cmd = &cmd;
1360 mrq.data = &data; 1261 mrq.data = &data;
1361 1262
1362 sg_init_one(&sg, status, 64); 1263 sg_init_one(&sg, status, 64);
1363 1264
1364 mmc_wait_for_req(host, &mrq); 1265 mmc_wait_for_req(host, &mrq);
1365 1266
1366 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE || 1267 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE ||
1367 (status[16] & 0xF) != 1) { 1268 (status[16] & 0xF) != 1) {
1368 printk(KERN_WARNING "%s: Problem switching card " 1269 printk(KERN_WARNING "%s: Problem switching card "
1369 "into high-speed mode!\n", 1270 "into high-speed mode!\n",
1370 mmc_hostname(host)); 1271 mmc_hostname(host));
1371 continue; 1272 goto out;
1372 } 1273 }
1373 1274
1374 mmc_card_set_highspeed(card); 1275 mmc_card_set_highspeed(host->card);
1375 1276
1376 host->ios.timing = MMC_TIMING_SD_HS; 1277 host->ios.timing = MMC_TIMING_SD_HS;
1377 mmc_set_ios(host); 1278 mmc_set_ios(host);
1378 }
1379 1279
1280out:
1380 kfree(status); 1281 kfree(status);
1381
1382 mmc_deselect_cards(host);
1383} 1282}
1384 1283
1385static unsigned int mmc_calculate_clock(struct mmc_host *host) 1284static unsigned int mmc_calculate_clock(struct mmc_host *host)
1386{ 1285{
1387 struct mmc_card *card;
1388 unsigned int max_dtr = host->f_max; 1286 unsigned int max_dtr = host->f_max;
1389 1287
1390 list_for_each_entry(card, &host->cards, node) 1288 if (host->card && !mmc_card_dead(host->card)) {
1391 if (!mmc_card_dead(card)) { 1289 if (mmc_card_highspeed(host->card) && mmc_card_sd(host->card)) {
1392 if (mmc_card_highspeed(card) && mmc_card_sd(card)) { 1290 if (max_dtr > host->card->sw_caps.hs_max_dtr)
1393 if (max_dtr > card->sw_caps.hs_max_dtr) 1291 max_dtr = host->card->sw_caps.hs_max_dtr;
1394 max_dtr = card->sw_caps.hs_max_dtr; 1292 } else if (mmc_card_highspeed(host->card) && !mmc_card_sd(host->card)) {
1395 } else if (mmc_card_highspeed(card) && !mmc_card_sd(card)) { 1293 if (max_dtr > host->card->ext_csd.hs_max_dtr)
1396 if (max_dtr > card->ext_csd.hs_max_dtr) 1294 max_dtr = host->card->ext_csd.hs_max_dtr;
1397 max_dtr = card->ext_csd.hs_max_dtr; 1295 } else if (max_dtr > host->card->csd.max_dtr) {
1398 } else if (max_dtr > card->csd.max_dtr) { 1296 max_dtr = host->card->csd.max_dtr;
1399 max_dtr = card->csd.max_dtr;
1400 }
1401 } 1297 }
1298 }
1402 1299
1403 pr_debug("%s: selected %d.%03dMHz transfer rate\n", 1300 pr_debug("%s: selected %d.%03dMHz transfer rate\n",
1404 mmc_hostname(host), 1301 mmc_hostname(host),
@@ -1415,92 +1312,66 @@ static unsigned int mmc_calculate_clock(struct mmc_host *host)
1415 * A request for status does not cause a state change in data 1312 * A request for status does not cause a state change in data
1416 * transfer mode. 1313 * transfer mode.
1417 */ 1314 */
1418static void mmc_check_cards(struct mmc_host *host) 1315static void mmc_check_card(struct mmc_card *card)
1419{ 1316{
1420 struct list_head *l, *n; 1317 struct mmc_command cmd;
1421 1318 int err;
1422 mmc_deselect_cards(host);
1423 1319
1424 list_for_each_safe(l, n, &host->cards) { 1320 BUG_ON(!card);
1425 struct mmc_card *card = mmc_list_to_card(l);
1426 struct mmc_command cmd;
1427 int err;
1428 1321
1429 cmd.opcode = MMC_SEND_STATUS; 1322 cmd.opcode = MMC_SEND_STATUS;
1430 cmd.arg = card->rca << 16; 1323 cmd.arg = card->rca << 16;
1431 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1324 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1432 1325
1433 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); 1326 err = mmc_wait_for_cmd(card->host, &cmd, CMD_RETRIES);
1434 if (err == MMC_ERR_NONE) 1327 if (err == MMC_ERR_NONE)
1435 continue; 1328 return;
1436 1329
1437 mmc_card_set_dead(card); 1330 mmc_card_set_dead(card);
1438 }
1439} 1331}
1440 1332
1441static void mmc_setup(struct mmc_host *host) 1333static void mmc_setup(struct mmc_host *host)
1442{ 1334{
1443 if (host->ios.power_mode != MMC_POWER_ON) { 1335 int err;
1444 int err; 1336 u32 ocr;
1445 u32 ocr;
1446
1447 host->mode = MMC_MODE_SD;
1448
1449 mmc_power_up(host);
1450 mmc_idle_cards(host);
1451 1337
1452 err = mmc_send_if_cond(host, host->ocr_avail, NULL); 1338 host->mode = MMC_MODE_SD;
1453 if (err != MMC_ERR_NONE) {
1454 return;
1455 }
1456 err = mmc_send_app_op_cond(host, 0, &ocr);
1457 1339
1458 /* 1340 mmc_power_up(host);
1459 * If we fail to detect any SD cards then try 1341 mmc_idle_cards(host);
1460 * searching for MMC cards.
1461 */
1462 if (err != MMC_ERR_NONE) {
1463 host->mode = MMC_MODE_MMC;
1464
1465 err = mmc_send_op_cond(host, 0, &ocr);
1466 if (err != MMC_ERR_NONE)
1467 return;
1468 }
1469 1342
1470 host->ocr = mmc_select_voltage(host, ocr); 1343 err = mmc_send_if_cond(host, host->ocr_avail, NULL);
1344 if (err != MMC_ERR_NONE) {
1345 return;
1346 }
1347 err = mmc_send_app_op_cond(host, 0, &ocr);
1471 1348
1472 /* 1349 /*
1473 * Since we're changing the OCR value, we seem to 1350 * If we fail to detect any SD cards then try
1474 * need to tell some cards to go back to the idle 1351 * searching for MMC cards.
1475 * state. We wait 1ms to give cards time to 1352 */
1476 * respond. 1353 if (err != MMC_ERR_NONE) {
1477 */ 1354 host->mode = MMC_MODE_MMC;
1478 if (host->ocr)
1479 mmc_idle_cards(host);
1480 } else {
1481 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1482 host->ios.clock = host->f_min;
1483 mmc_set_ios(host);
1484 1355
1485 /* 1356 err = mmc_send_op_cond(host, 0, &ocr);
1486 * We should remember the OCR mask from the existing 1357 if (err != MMC_ERR_NONE)
1487 * cards, and detect the new cards OCR mask, combine 1358 return;
1488 * the two and re-select the VDD. However, if we do
1489 * change VDD, we should do an idle, and then do a
1490 * full re-initialisation. We would need to notify
1491 * drivers so that they can re-setup the cards as
1492 * well, while keeping their queues at bay.
1493 *
1494 * For the moment, we take the easy way out - if the
1495 * new cards don't like our currently selected VDD,
1496 * they drop off the bus.
1497 */
1498 } 1359 }
1499 1360
1361 host->ocr = mmc_select_voltage(host, ocr);
1362
1500 if (host->ocr == 0) 1363 if (host->ocr == 0)
1501 return; 1364 return;
1502 1365
1503 /* 1366 /*
1367 * Since we're changing the OCR value, we seem to
1368 * need to tell some cards to go back to the idle
1369 * state. We wait 1ms to give cards time to
1370 * respond.
1371 */
1372 mmc_idle_cards(host);
1373
1374 /*
1504 * Send the selected OCR multiple times... until the cards 1375 * Send the selected OCR multiple times... until the cards
1505 * all get the idea that they should be ready for CMD2. 1376 * all get the idea that they should be ready for CMD2.
1506 * (My SanDisk card seems to need this.) 1377 * (My SanDisk card seems to need this.)
@@ -1522,7 +1393,7 @@ static void mmc_setup(struct mmc_host *host)
1522 mmc_send_op_cond(host, host->ocr | (1 << 30), NULL); 1393 mmc_send_op_cond(host, host->ocr | (1 << 30), NULL);
1523 } 1394 }
1524 1395
1525 mmc_discover_cards(host); 1396 mmc_discover_card(host);
1526 1397
1527 /* 1398 /*
1528 * Ok, now switch to push-pull mode. 1399 * Ok, now switch to push-pull mode.
@@ -1530,13 +1401,19 @@ static void mmc_setup(struct mmc_host *host)
1530 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; 1401 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1531 mmc_set_ios(host); 1402 mmc_set_ios(host);
1532 1403
1533 mmc_read_csds(host); 1404 mmc_read_csd(host);
1405
1406 if (host->card && !mmc_card_dead(host->card)) {
1407 err = mmc_select_card(host->card);
1408 if (err != MMC_ERR_NONE)
1409 mmc_card_set_dead(host->card);
1410 }
1534 1411
1535 if (host->mode == MMC_MODE_SD) { 1412 if (host->mode == MMC_MODE_SD) {
1536 mmc_read_scrs(host); 1413 mmc_read_scr(host);
1537 mmc_read_switch_caps(host); 1414 mmc_read_switch_caps(host);
1538 } else 1415 } else
1539 mmc_process_ext_csds(host); 1416 mmc_process_ext_csd(host);
1540} 1417}
1541 1418
1542 1419
@@ -1566,31 +1443,29 @@ static void mmc_rescan(struct work_struct *work)
1566{ 1443{
1567 struct mmc_host *host = 1444 struct mmc_host *host =
1568 container_of(work, struct mmc_host, detect.work); 1445 container_of(work, struct mmc_host, detect.work);
1569 struct list_head *l, *n;
1570 unsigned char power_mode;
1571 1446
1572 mmc_claim_host(host); 1447 mmc_claim_host(host);
1573 1448
1574 /* 1449 /*
1575 * Check for removed cards and newly inserted ones. We check for 1450 * Check for removed card and newly inserted ones. We check for
1576 * removed cards first so we can intelligently re-select the VDD. 1451 * removed cards first so we can intelligently re-select the VDD.
1577 */ 1452 */
1578 power_mode = host->ios.power_mode; 1453 if (host->card) {
1579 if (power_mode == MMC_POWER_ON) 1454 mmc_check_card(host->card);
1580 mmc_check_cards(host);
1581 1455
1582 mmc_setup(host); 1456 mmc_release_host(host);
1583 1457
1584 /* 1458 if (mmc_card_dead(host->card)) {
1585 * Some broken cards process CMD1 even in stand-by state. There is 1459 mmc_remove_card(host->card);
1586 * no reply, but an ILLEGAL_COMMAND error is cached and returned 1460 host->card = NULL;
1587 * after next command. We poll for card status here to clear any 1461 }
1588 * possibly pending error. 1462
1589 */ 1463 goto out;
1590 if (power_mode == MMC_POWER_ON) 1464 }
1591 mmc_check_cards(host);
1592 1465
1593 if (!list_empty(&host->cards)) { 1466 mmc_setup(host);
1467
1468 if (host->card && !mmc_card_dead(host->card)) {
1594 /* 1469 /*
1595 * (Re-)calculate the fastest clock rate which the 1470 * (Re-)calculate the fastest clock rate which the
1596 * attached cards and the host support. 1471 * attached cards and the host support.
@@ -1601,31 +1476,28 @@ static void mmc_rescan(struct work_struct *work)
1601 1476
1602 mmc_release_host(host); 1477 mmc_release_host(host);
1603 1478
1604 list_for_each_safe(l, n, &host->cards) { 1479 /*
1605 struct mmc_card *card = mmc_list_to_card(l); 1480 * If this is a new and good card, register it.
1606 1481 */
1607 /* 1482 if (host->card && !mmc_card_dead(host->card)) {
1608 * If this is a new and good card, register it. 1483 if (mmc_register_card(host->card))
1609 */ 1484 mmc_card_set_dead(host->card);
1610 if (!mmc_card_present(card) && !mmc_card_dead(card)) { 1485 }
1611 if (mmc_register_card(card))
1612 mmc_card_set_dead(card);
1613 }
1614 1486
1615 /* 1487 /*
1616 * If this card is dead, destroy it. 1488 * If this card is dead, destroy it.
1617 */ 1489 */
1618 if (mmc_card_dead(card)) { 1490 if (host->card && mmc_card_dead(host->card)) {
1619 list_del(&card->node); 1491 mmc_remove_card(host->card);
1620 mmc_remove_card(card); 1492 host->card = NULL;
1621 }
1622 } 1493 }
1623 1494
1495out:
1624 /* 1496 /*
1625 * If we discover that there are no cards on the 1497 * If we discover that there are no cards on the
1626 * bus, turn off the clock and power down. 1498 * bus, turn off the clock and power down.
1627 */ 1499 */
1628 if (list_empty(&host->cards)) 1500 if (!host->card)
1629 mmc_power_off(host); 1501 mmc_power_off(host);
1630} 1502}
1631 1503
@@ -1645,7 +1517,6 @@ struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
1645 if (host) { 1517 if (host) {
1646 spin_lock_init(&host->lock); 1518 spin_lock_init(&host->lock);
1647 init_waitqueue_head(&host->wq); 1519 init_waitqueue_head(&host->wq);
1648 INIT_LIST_HEAD(&host->cards);
1649 INIT_DELAYED_WORK(&host->detect, mmc_rescan); 1520 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
1650 1521
1651 /* 1522 /*
@@ -1694,8 +1565,6 @@ EXPORT_SYMBOL(mmc_add_host);
1694 */ 1565 */
1695void mmc_remove_host(struct mmc_host *host) 1566void mmc_remove_host(struct mmc_host *host)
1696{ 1567{
1697 struct list_head *l, *n;
1698
1699#ifdef CONFIG_MMC_DEBUG 1568#ifdef CONFIG_MMC_DEBUG
1700 mmc_claim_host(host); 1569 mmc_claim_host(host);
1701 host->removed = 1; 1570 host->removed = 1;
@@ -1704,10 +1573,9 @@ void mmc_remove_host(struct mmc_host *host)
1704 1573
1705 mmc_flush_scheduled_work(); 1574 mmc_flush_scheduled_work();
1706 1575
1707 list_for_each_safe(l, n, &host->cards) { 1576 if (host->card) {
1708 struct mmc_card *card = mmc_list_to_card(l); 1577 mmc_remove_card(host->card);
1709 1578 host->card = NULL;
1710 mmc_remove_card(card);
1711 } 1579 }
1712 1580
1713 mmc_power_off(host); 1581 mmc_power_off(host);
@@ -1738,14 +1606,11 @@ EXPORT_SYMBOL(mmc_free_host);
1738 */ 1606 */
1739int mmc_suspend_host(struct mmc_host *host, pm_message_t state) 1607int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1740{ 1608{
1741 struct list_head *l, *n;
1742
1743 mmc_flush_scheduled_work(); 1609 mmc_flush_scheduled_work();
1744 1610
1745 list_for_each_safe(l, n, &host->cards) { 1611 if (host->card) {
1746 struct mmc_card *card = mmc_list_to_card(l); 1612 mmc_remove_card(host->card);
1747 1613 host->card = NULL;
1748 mmc_remove_card(card);
1749 } 1614 }
1750 1615
1751 mmc_power_off(host); 1616 mmc_power_off(host);