···23232424#include <linux/mmc/card.h>2525#include <linux/mmc/host.h>2626-#include <linux/mmc/protocol.h>2626+#include <linux/mmc/mmc.h>2727+#include <linux/mmc/sd.h>27282829#include "core.h"3030+#include "sysfs.h"3131+3232+#include "mmc_ops.h"3333+#include "sd_ops.h"29343035#define CMD_RETRIES 33136···196191EXPORT_SYMBOL(mmc_wait_for_cmd);197192198193/**199199- * mmc_wait_for_app_cmd - start an application command and wait for200200- completion201201- * @host: MMC host to start command202202- * @rca: RCA to send MMC_APP_CMD to203203- * @cmd: MMC command to start204204- * @retries: maximum number of retries205205- *206206- * Sends a MMC_APP_CMD, checks the card response, sends the command207207- * in the parameter and waits for it to complete. Return any error208208- * that occurred while the command was executing. Do not attempt to209209- * parse the response.210210- */211211-int mmc_wait_for_app_cmd(struct mmc_host *host, unsigned int rca,212212- struct mmc_command *cmd, int retries)213213-{214214- struct mmc_request mrq;215215- struct mmc_command appcmd;216216-217217- int i, err;218218-219219- BUG_ON(!host->claimed);220220- BUG_ON(retries < 0);221221-222222- err = MMC_ERR_INVALID;223223-224224- /*225225- * We have to resend MMC_APP_CMD for each attempt so226226- * we cannot use the retries field in mmc_command.227227- */228228- for (i = 0;i <= retries;i++) {229229- memset(&mrq, 0, sizeof(struct mmc_request));230230-231231- appcmd.opcode = MMC_APP_CMD;232232- appcmd.arg = rca << 16;233233- appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;234234- appcmd.retries = 0;235235- memset(appcmd.resp, 0, sizeof(appcmd.resp));236236- appcmd.data = NULL;237237-238238- mrq.cmd = &appcmd;239239- appcmd.data = NULL;240240-241241- mmc_wait_for_req(host, &mrq);242242-243243- if (appcmd.error) {244244- err = appcmd.error;245245- continue;246246- }247247-248248- /* Check that card supported application commands */249249- if (!(appcmd.resp[0] & R1_APP_CMD))250250- return MMC_ERR_FAILED;251251-252252- memset(&mrq, 0, sizeof(struct mmc_request));253253-254254- memset(cmd->resp, 0, sizeof(cmd->resp));255255- cmd->retries = 0;256256-257257- mrq.cmd = cmd;258258- cmd->data = NULL;259259-260260- mmc_wait_for_req(host, &mrq);261261-262262- err = cmd->error;263263- if (cmd->error == MMC_ERR_NONE)264264- break;265265- }266266-267267- return err;268268-}269269-270270-EXPORT_SYMBOL(mmc_wait_for_app_cmd);271271-272272-/**273194 * mmc_set_data_timeout - set the timeout for a data command274195 * @data: data phase for command275196 * @card: the MMC card associated with the data transfer···316385 host->ops->set_ios(host, ios);317386}318387319319-static int mmc_select_card(struct mmc_card *card)388388+void mmc_set_chip_select(struct mmc_host *host, int mode)320389{321321- int err;322322- struct mmc_command cmd;323323-324324- BUG_ON(!card->host->claimed);325325-326326- cmd.opcode = MMC_SELECT_CARD;327327- cmd.arg = card->rca << 16;328328- cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;329329-330330- err = mmc_wait_for_cmd(card->host, &cmd, CMD_RETRIES);331331- if (err != MMC_ERR_NONE)332332- return err;333333-334334- /*335335- * We can only change the bus width of SD cards when336336- * they are selected so we have to put the handling337337- * here.338338- *339339- * The card is in 1 bit mode by default so340340- * we only need to change if it supports the341341- * wider version.342342- */343343- if (mmc_card_sd(card) &&344344- (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4) &&345345- (card->host->caps & MMC_CAP_4_BIT_DATA)) {346346-347347- struct mmc_command cmd;348348- cmd.opcode = SD_APP_SET_BUS_WIDTH;349349- cmd.arg = SD_BUS_WIDTH_4;350350- cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;351351-352352- err = mmc_wait_for_app_cmd(card->host, card->rca,353353- &cmd, CMD_RETRIES);354354- if (err != MMC_ERR_NONE)355355- return err;356356-357357- card->host->ios.bus_width = MMC_BUS_WIDTH_4;358358- mmc_set_ios(card->host);359359- }360360-361361- return MMC_ERR_NONE;362362-}363363-364364-365365-static inline void mmc_delay(unsigned int ms)366366-{367367- if (ms < 1000 / HZ) {368368- cond_resched();369369- mdelay(ms);370370- } else {371371- msleep(ms);372372- }390390+ host->ios.chip_select = mode;391391+ mmc_set_ios(host);373392}374393375394/*···590709}591710592711/*593593- * Tell attached cards to go to IDLE state594594- */595595-static void mmc_idle_cards(struct mmc_host *host)596596-{597597- struct mmc_command cmd;598598-599599- host->ios.chip_select = MMC_CS_HIGH;600600- mmc_set_ios(host);601601-602602- mmc_delay(1);603603-604604- cmd.opcode = MMC_GO_IDLE_STATE;605605- cmd.arg = 0;606606- cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;607607-608608- mmc_wait_for_cmd(host, &cmd, 0);609609-610610- mmc_delay(1);611611-612612- host->ios.chip_select = MMC_CS_DONTCARE;613613- mmc_set_ios(host);614614-615615- mmc_delay(1);616616-}617617-618618-/*619712 * Apply power to the MMC stack. This is a two-stage process.620713 * First, we enable power to the card without the clock running.621714 * We then wait a bit for the power to stabilise. Finally,···633778 mmc_set_ios(host);634779}635780636636-static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)637637-{638638- struct mmc_command cmd;639639- int i, err = 0;640640-641641- cmd.opcode = MMC_SEND_OP_COND;642642- cmd.arg = ocr;643643- cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;644644-645645- for (i = 100; i; i--) {646646- err = mmc_wait_for_cmd(host, &cmd, 0);647647- if (err != MMC_ERR_NONE)648648- break;649649-650650- if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)651651- break;652652-653653- err = MMC_ERR_TIMEOUT;654654-655655- mmc_delay(10);656656- }657657-658658- if (rocr)659659- *rocr = cmd.resp[0];660660-661661- return err;662662-}663663-664664-static int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)665665-{666666- struct mmc_command cmd;667667- int i, err = 0;668668-669669- cmd.opcode = SD_APP_OP_COND;670670- cmd.arg = ocr;671671- cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;672672-673673- for (i = 100; i; i--) {674674- err = mmc_wait_for_app_cmd(host, 0, &cmd, CMD_RETRIES);675675- if (err != MMC_ERR_NONE)676676- break;677677-678678- if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)679679- break;680680-681681- err = MMC_ERR_TIMEOUT;682682-683683- mmc_delay(10);684684- }685685-686686- if (rocr)687687- *rocr = cmd.resp[0];688688-689689- return err;690690-}691691-692692-static int mmc_send_if_cond(struct mmc_host *host, u32 ocr, int *rsd2)693693-{694694- struct mmc_command cmd;695695- int err, sd2;696696- static const u8 test_pattern = 0xAA;697697-698698- /*699699- * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND700700- * before SD_APP_OP_COND. This command will harmlessly fail for701701- * SD 1.0 cards.702702- */703703- cmd.opcode = SD_SEND_IF_COND;704704- cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern;705705- cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;706706-707707- err = mmc_wait_for_cmd(host, &cmd, 0);708708- if (err == MMC_ERR_NONE) {709709- if ((cmd.resp[0] & 0xFF) == test_pattern) {710710- sd2 = 1;711711- } else {712712- sd2 = 0;713713- err = MMC_ERR_FAILED;714714- }715715- } else {716716- /*717717- * Treat errors as SD 1.0 card.718718- */719719- sd2 = 0;720720- err = MMC_ERR_NONE;721721- }722722- if (rsd2)723723- *rsd2 = sd2;724724- return err;725725-}726726-727781/*728782 * Discover the card by requesting its CID.729783 *···642878static void mmc_discover_card(struct mmc_host *host)643879{644880 unsigned int err;645645-646646- struct mmc_command cmd;881881+ u32 cid[4];647882648883 BUG_ON(host->card);649884650650- cmd.opcode = MMC_ALL_SEND_CID;651651- cmd.arg = 0;652652- cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;653653-654654- err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);655655- if (err == MMC_ERR_TIMEOUT) {656656- err = MMC_ERR_NONE;657657- return;658658- }885885+ err = mmc_all_send_cid(host, cid);659886 if (err != MMC_ERR_NONE) {660887 printk(KERN_ERR "%s: error requesting CID: %d\n",661888 mmc_hostname(host), err);662889 return;663890 }664891665665- host->card = mmc_alloc_card(host, cmd.resp);892892+ host->card = mmc_alloc_card(host, cid);666893 if (IS_ERR(host->card)) {667894 err = PTR_ERR(host->card);668895 host->card = NULL;···663908 if (host->mode == MMC_MODE_SD) {664909 host->card->type = MMC_TYPE_SD;665910666666- cmd.opcode = SD_SEND_RELATIVE_ADDR;667667- cmd.arg = 0;668668- cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;669669-670670- err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);911911+ err = mmc_send_relative_addr(host, &host->card->rca);671912 if (err != MMC_ERR_NONE)672913 mmc_card_set_dead(host->card);673914 else {674674- host->card->rca = cmd.resp[0] >> 16;675675-676915 if (!host->ops->get_ro) {677916 printk(KERN_WARNING "%s: host does not "678917 "support reading read-only "···681932 host->card->type = MMC_TYPE_MMC;682933 host->card->rca = 1;683934684684- cmd.opcode = MMC_SET_RELATIVE_ADDR;685685- cmd.arg = host->card->rca << 16;686686- cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;687687-688688- err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);935935+ err = mmc_set_relative_addr(host->card);689936 if (err != MMC_ERR_NONE)690937 mmc_card_set_dead(host->card);691938 }···689944690945static void mmc_read_csd(struct mmc_host *host)691946{692692- struct mmc_command cmd;693947 int err;694948695949 if (!host->card)···696952 if (mmc_card_dead(host->card))697953 return;698954699699- cmd.opcode = MMC_SEND_CSD;700700- cmd.arg = host->card->rca << 16;701701- cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;702702-703703- err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);955955+ err = mmc_send_csd(host->card, host->card->raw_csd);704956 if (err != MMC_ERR_NONE) {705957 mmc_card_set_dead(host->card);706958 return;707959 }708708-709709- memcpy(host->card->raw_csd, cmd.resp, sizeof(host->card->raw_csd));710960711961 mmc_decode_csd(host->card);712962 mmc_decode_cid(host->card);···709971static void mmc_process_ext_csd(struct mmc_host *host)710972{711973 int err;712712-713713- struct mmc_request mrq;714714- struct mmc_command cmd;715715- struct mmc_data data;716716-717974 u8 *ext_csd;718718- struct scatterlist sg;719975720976 if (!host->card)721977 return;···7321000 return;7331001 }7341002735735- memset(&cmd, 0, sizeof(struct mmc_command));736736-737737- cmd.opcode = MMC_SEND_EXT_CSD;738738- cmd.arg = 0;739739- cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;740740-741741- memset(&data, 0, sizeof(struct mmc_data));742742-743743- mmc_set_data_timeout(&data, host->card, 0);744744-745745- data.blksz = 512;746746- data.blocks = 1;747747- data.flags = MMC_DATA_READ;748748- data.sg = &sg;749749- data.sg_len = 1;750750-751751- memset(&mrq, 0, sizeof(struct mmc_request));752752-753753- mrq.cmd = &cmd;754754- mrq.data = &data;755755-756756- sg_init_one(&sg, ext_csd, 512);757757-758758- mmc_wait_for_req(host, &mrq);759759-760760- if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {10031003+ err = mmc_send_ext_csd(host->card, ext_csd);10041004+ if (err != MMC_ERR_NONE) {7611005 if (host->card->csd.capacity == (4096 * 512)) {7621006 printk(KERN_ERR "%s: unable to read EXT_CSD "7631007 "on a possible high capacity card. "···77410667751067 if (host->caps & MMC_CAP_MMC_HIGHSPEED) {7761068 /* Activate highspeed support. */777777- cmd.opcode = MMC_SWITCH;778778- cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |779779- (EXT_CSD_HS_TIMING << 16) |780780- (1 << 8) |781781- EXT_CSD_CMD_SET_NORMAL;782782- cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;783783-784784- err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);10691069+ err = mmc_switch(host->card, MMC_SWITCH_MODE_WRITE_BYTE,10701070+ EXT_CSD_HS_TIMING, 1);7851071 if (err != MMC_ERR_NONE) {7861072 printk("%s: failed to switch card to mmc v4 "7871073 "high-speed mode.\n",···7921090 /* Check for host support for wide-bus modes. */7931091 if (host->caps & MMC_CAP_4_BIT_DATA) {7941092 /* Activate 4-bit support. */795795- cmd.opcode = MMC_SWITCH;796796- cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |797797- (EXT_CSD_BUS_WIDTH << 16) |798798- (EXT_CSD_BUS_WIDTH_4 << 8) |799799- EXT_CSD_CMD_SET_NORMAL;800800- cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;801801-802802- err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);10931093+ err = mmc_switch(host->card, MMC_SWITCH_MODE_WRITE_BYTE,10941094+ EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_4 |10951095+ EXT_CSD_CMD_SET_NORMAL);8031096 if (err != MMC_ERR_NONE) {8041097 printk("%s: failed to switch card to "8051098 "mmc v4 4-bit bus mode.\n",···8131116static void mmc_read_scr(struct mmc_host *host)8141117{8151118 int err;816816- struct mmc_request mrq;817817- struct mmc_command cmd;818818- struct mmc_data data;819819- struct scatterlist sg;82011198211120 if (!host->card)8221121 return;···8211128 if (!mmc_card_sd(host->card))8221129 return;8231130824824- memset(&cmd, 0, sizeof(struct mmc_command));825825-826826- cmd.opcode = MMC_APP_CMD;827827- cmd.arg = host->card->rca << 16;828828- cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;829829-830830- err = mmc_wait_for_cmd(host, &cmd, 0);831831- if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD)) {11311131+ err = mmc_app_send_scr(host->card, host->card->raw_scr);11321132+ if (err != MMC_ERR_NONE) {8321133 mmc_card_set_dead(host->card);8331134 return;8341135 }835835-836836- memset(&cmd, 0, sizeof(struct mmc_command));837837-838838- cmd.opcode = SD_APP_SEND_SCR;839839- cmd.arg = 0;840840- cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;841841-842842- memset(&data, 0, sizeof(struct mmc_data));843843-844844- mmc_set_data_timeout(&data, host->card, 0);845845-846846- data.blksz = 1 << 3;847847- data.blocks = 1;848848- data.flags = MMC_DATA_READ;849849- data.sg = &sg;850850- data.sg_len = 1;851851-852852- memset(&mrq, 0, sizeof(struct mmc_request));853853-854854- mrq.cmd = &cmd;855855- mrq.data = &data;856856-857857- sg_init_one(&sg, (u8*)host->card->raw_scr, 8);858858-859859- mmc_wait_for_req(host, &mrq);860860-861861- if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {862862- mmc_card_set_dead(host->card);863863- return;864864- }865865-866866- host->card->raw_scr[0] = ntohl(host->card->raw_scr[0]);867867- host->card->raw_scr[1] = ntohl(host->card->raw_scr[1]);86811368691137 mmc_decode_scr(host->card);8701138}87111398721140static void mmc_read_switch_caps(struct mmc_host *host)8731141{874874- struct mmc_request mrq;875875- struct mmc_command cmd;876876- struct mmc_data data;11421142+ int err;8771143 unsigned char *status;878878- struct scatterlist sg;87911448801145 if (!(host->caps & MMC_CAP_SD_HIGHSPEED))8811146 return;···8551204 return;8561205 }8571206858858- memset(&cmd, 0, sizeof(struct mmc_command));859859-860860- cmd.opcode = SD_SWITCH;861861- cmd.arg = 0x00FFFFF1;862862- cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;863863-864864- memset(&data, 0, sizeof(struct mmc_data));865865-866866- mmc_set_data_timeout(&data, host->card, 0);867867-868868- data.blksz = 64;869869- data.blocks = 1;870870- data.flags = MMC_DATA_READ;871871- data.sg = &sg;872872- data.sg_len = 1;873873-874874- memset(&mrq, 0, sizeof(struct mmc_request));875875-876876- mrq.cmd = &cmd;877877- mrq.data = &data;878878-879879- sg_init_one(&sg, status, 64);880880-881881- mmc_wait_for_req(host, &mrq);882882-883883- if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {12071207+ err = mmc_sd_switch(host->card, SD_SWITCH_CHECK,12081208+ SD_SWITCH_GRP_ACCESS, SD_SWITCH_ACCESS_HS, status);12091209+ if (err != MMC_ERR_NONE) {8841210 printk("%s: unable to read switch capabilities, "8851211 "performance might suffer.\n",8861212 mmc_hostname(host));···8671239 if (status[13] & 0x02)8681240 host->card->sw_caps.hs_max_dtr = 50000000;8691241870870- memset(&cmd, 0, sizeof(struct mmc_command));871871-872872- cmd.opcode = SD_SWITCH;873873- cmd.arg = 0x80FFFFF1;874874- cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;875875-876876- memset(&data, 0, sizeof(struct mmc_data));877877-878878- mmc_set_data_timeout(&data, host->card, 0);879879-880880- data.blksz = 64;881881- data.blocks = 1;882882- data.flags = MMC_DATA_READ;883883- data.sg = &sg;884884- data.sg_len = 1;885885-886886- memset(&mrq, 0, sizeof(struct mmc_request));887887-888888- mrq.cmd = &cmd;889889- mrq.data = &data;890890-891891- sg_init_one(&sg, status, 64);892892-893893- mmc_wait_for_req(host, &mrq);894894-895895- if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE ||896896- (status[16] & 0xF) != 1) {12421242+ err = mmc_sd_switch(host->card, SD_SWITCH_SET,12431243+ SD_SWITCH_GRP_ACCESS, SD_SWITCH_ACCESS_HS, status);12441244+ if (err != MMC_ERR_NONE || (status[16] & 0xF) != 1) {8971245 printk(KERN_WARNING "%s: Problem switching card "8981246 "into high-speed mode!\n",8991247 mmc_hostname(host));···9181314 */9191315static void mmc_check_card(struct mmc_card *card)9201316{921921- struct mmc_command cmd;9221317 int err;92313189241319 BUG_ON(!card);9251320926926- cmd.opcode = MMC_SEND_STATUS;927927- cmd.arg = card->rca << 16;928928- cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;929929-930930- err = mmc_wait_for_cmd(card->host, &cmd, CMD_RETRIES);13211321+ err = mmc_send_status(card, NULL);9311322 if (err == MMC_ERR_NONE)9321323 return;9331324···9371338 host->mode = MMC_MODE_SD;93813399391340 mmc_power_up(host);940940- mmc_idle_cards(host);13411341+ mmc_go_idle(host);9411342942942- err = mmc_send_if_cond(host, host->ocr_avail, NULL);13431343+ err = mmc_send_if_cond(host, host->ocr_avail);9431344 if (err != MMC_ERR_NONE) {9441345 return;9451346 }···9681369 * state. We wait 1ms to give cards time to9691370 * respond.9701371 */971971- mmc_idle_cards(host);13721372+ mmc_go_idle(host);97213739731374 /*9741375 * Send the selected OCR multiple times... until the cards···9761377 * (My SanDisk card seems to need this.)9771378 */9781379 if (host->mode == MMC_MODE_SD) {979979- int err, sd2;980980- err = mmc_send_if_cond(host, host->ocr, &sd2);981981- if (err == MMC_ERR_NONE) {982982- /*983983- * If SD_SEND_IF_COND indicates an SD 2.0984984- * compliant card and we should set bit 30985985- * of the ocr to indicate that we can handle986986- * block-addressed SDHC cards.987987- */988988- mmc_send_app_op_cond(host, host->ocr | (sd2 << 30), NULL);989989- }13801380+ /*13811381+ * If SD_SEND_IF_COND indicates an SD 2.013821382+ * compliant card and we should set bit 3013831383+ * of the ocr to indicate that we can handle13841384+ * block-addressed SDHC cards.13851385+ */13861386+ err = mmc_send_if_cond(host, host->ocr);13871387+ if (err == MMC_ERR_NONE)13881388+ ocr = host->ocr | (1 << 30);13891389+13901390+ mmc_send_app_op_cond(host, ocr, NULL);9901391 } else {9911392 /* The extra bit indicates that we support high capacity */9921393 mmc_send_op_cond(host, host->ocr | (1 << 30), NULL);···10061407 err = mmc_select_card(host->card);10071408 if (err != MMC_ERR_NONE)10081409 mmc_card_set_dead(host->card);14101410+ }14111411+14121412+ /*14131413+ * The card is in 1 bit mode by default so14141414+ * we only need to change if it supports the14151415+ * wider version.14161416+ */14171417+ if (host->card && !mmc_card_dead(host->card) && 14181418+ mmc_card_sd(host->card) &&14191419+ (host->card->scr.bus_widths & SD_SCR_BUS_WIDTH_4) &&14201420+ (host->card->host->caps & MMC_CAP_4_BIT_DATA)) {14211421+ err = mmc_app_set_bus_width(host->card, SD_BUS_WIDTH_4);14221422+ if (err != MMC_ERR_NONE)14231423+ mmc_card_set_dead(host->card);14241424+ else {14251425+ host->ios.bus_width = MMC_BUS_WIDTH_4;14261426+ mmc_set_ios(host);14271427+ }10091428 }1010142910111430 if (host->mode == MMC_MODE_SD) {
+19-13
drivers/mmc/core/core.h
···22 * linux/drivers/mmc/core/core.h33 *44 * Copyright (C) 2003 Russell King, All Rights Reserved.55+ * Copyright 2007 Pierre Ossman56 *67 * This program is free software; you can redistribute it and/or modify78 * it under the terms of the GNU General Public License version 2 as89 * published by the Free Software Foundation.910 */1010-#ifndef _MMC_CORE_H1111-#define _MMC_CORE_H1212-/* core-internal functions */1313-void mmc_init_card(struct mmc_card *card, struct mmc_host *host);1414-int mmc_register_card(struct mmc_card *card);1515-void mmc_remove_card(struct mmc_card *card);1111+#ifndef _MMC_CORE_CORE_H1212+#define _MMC_CORE_CORE_H16131717-struct mmc_host *mmc_alloc_host_sysfs(int extra, struct device *dev);1818-int mmc_add_host_sysfs(struct mmc_host *host);1919-void mmc_remove_host_sysfs(struct mmc_host *host);2020-void mmc_free_host_sysfs(struct mmc_host *host);1414+#include <linux/delay.h>21152222-int mmc_schedule_work(struct work_struct *work);2323-int mmc_schedule_delayed_work(struct delayed_work *work, unsigned long delay);2424-void mmc_flush_scheduled_work(void);1616+#define MMC_CMD_RETRIES 31717+1818+void mmc_set_chip_select(struct mmc_host *host, int mode);1919+2020+static inline void mmc_delay(unsigned int ms)2121+{2222+ if (ms < 1000 / HZ) {2323+ cond_resched();2424+ mdelay(ms);2525+ } else {2626+ msleep(ms);2727+ }2828+}2929+2530#endif3131+
···11+/*22+ * linux/drivers/mmc/mmc_ops.h33+ *44+ * Copyright 2006-2007 Pierre Ossman55+ *66+ * This program is free software; you can redistribute it and/or modify77+ * it under the terms of the GNU General Public License as published by88+ * the Free Software Foundation; either version 2 of the License, or (at99+ * your option) any later version.1010+ */1111+1212+#ifndef _MMC_MMC_OPS_H1313+#define _MMC_MMC_OPS_H1414+1515+int mmc_select_card(struct mmc_card *card);1616+int mmc_deselect_cards(struct mmc_host *host);1717+int mmc_go_idle(struct mmc_host *host);1818+int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr);1919+int mmc_all_send_cid(struct mmc_host *host, u32 *cid);2020+int mmc_set_relative_addr(struct mmc_card *card);2121+int mmc_send_csd(struct mmc_card *card, u32 *csd);2222+int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd);2323+int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value);2424+int mmc_send_status(struct mmc_card *card, u32 *status);2525+2626+#endif2727+
+316
drivers/mmc/core/sd_ops.c
···11+/*22+ * linux/drivers/mmc/sd_ops.h33+ *44+ * Copyright 2006-2007 Pierre Ossman55+ *66+ * This program is free software; you can redistribute it and/or modify77+ * it under the terms of the GNU General Public License as published by88+ * the Free Software Foundation; either version 2 of the License, or (at99+ * your option) any later version.1010+ */1111+1212+#include <linux/types.h>1313+#include <asm/scatterlist.h>1414+#include <linux/scatterlist.h>1515+1616+#include <linux/mmc/host.h>1717+#include <linux/mmc/card.h>1818+#include <linux/mmc/mmc.h>1919+#include <linux/mmc/sd.h>2020+2121+#include "core.h"2222+#include "sd_ops.h"2323+2424+/**2525+ * mmc_wait_for_app_cmd - start an application command and wait for2626+ completion2727+ * @host: MMC host to start command2828+ * @rca: RCA to send MMC_APP_CMD to2929+ * @cmd: MMC command to start3030+ * @retries: maximum number of retries3131+ *3232+ * Sends a MMC_APP_CMD, checks the card response, sends the command3333+ * in the parameter and waits for it to complete. Return any error3434+ * that occurred while the command was executing. Do not attempt to3535+ * parse the response.3636+ */3737+int mmc_wait_for_app_cmd(struct mmc_host *host, struct mmc_card *card,3838+ struct mmc_command *cmd, int retries)3939+{4040+ struct mmc_request mrq;4141+4242+ int i, err;4343+4444+ BUG_ON(!cmd);4545+ BUG_ON(retries < 0);4646+4747+ err = MMC_ERR_INVALID;4848+4949+ /*5050+ * We have to resend MMC_APP_CMD for each attempt so5151+ * we cannot use the retries field in mmc_command.5252+ */5353+ for (i = 0;i <= retries;i++) {5454+ memset(&mrq, 0, sizeof(struct mmc_request));5555+5656+ err = mmc_app_cmd(host, card);5757+ if (err != MMC_ERR_NONE)5858+ continue;5959+6060+ memset(&mrq, 0, sizeof(struct mmc_request));6161+6262+ memset(cmd->resp, 0, sizeof(cmd->resp));6363+ cmd->retries = 0;6464+6565+ mrq.cmd = cmd;6666+ cmd->data = NULL;6767+6868+ mmc_wait_for_req(host, &mrq);6969+7070+ err = cmd->error;7171+ if (cmd->error == MMC_ERR_NONE)7272+ break;7373+ }7474+7575+ return err;7676+}7777+7878+EXPORT_SYMBOL(mmc_wait_for_app_cmd);7979+8080+int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card)8181+{8282+ int err;8383+ struct mmc_command cmd;8484+8585+ BUG_ON(!host);8686+ BUG_ON(card && (card->host != host));8787+8888+ cmd.opcode = MMC_APP_CMD;8989+9090+ if (card) {9191+ cmd.arg = card->rca << 16;9292+ cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;9393+ } else {9494+ cmd.arg = 0;9595+ cmd.flags = MMC_RSP_R1 | MMC_CMD_BCR;9696+ }9797+9898+ err = mmc_wait_for_cmd(host, &cmd, 0);9999+ if (err != MMC_ERR_NONE)100100+ return err;101101+102102+ /* Check that card supported application commands */103103+ if (!(cmd.resp[0] & R1_APP_CMD))104104+ return MMC_ERR_FAILED;105105+106106+ return MMC_ERR_NONE;107107+}108108+109109+int mmc_app_set_bus_width(struct mmc_card *card, int width)110110+{111111+ int err;112112+ struct mmc_command cmd;113113+114114+ BUG_ON(!card);115115+ BUG_ON(!card->host);116116+117117+ memset(&cmd, 0, sizeof(struct mmc_command));118118+119119+ cmd.opcode = SD_APP_SET_BUS_WIDTH;120120+ cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;121121+122122+ switch (width) {123123+ case MMC_BUS_WIDTH_1:124124+ cmd.arg = SD_BUS_WIDTH_1;125125+ break;126126+ case MMC_BUS_WIDTH_4:127127+ cmd.arg = SD_BUS_WIDTH_4;128128+ break;129129+ default:130130+ return MMC_ERR_INVALID;131131+ }132132+133133+ err = mmc_wait_for_app_cmd(card->host, card, &cmd, MMC_CMD_RETRIES);134134+ if (err != MMC_ERR_NONE)135135+ return err;136136+137137+ return MMC_ERR_NONE;138138+}139139+140140+int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)141141+{142142+ struct mmc_command cmd;143143+ int i, err = 0;144144+145145+ BUG_ON(!host);146146+147147+ memset(&cmd, 0, sizeof(struct mmc_command));148148+149149+ cmd.opcode = SD_APP_OP_COND;150150+ cmd.arg = ocr;151151+ cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;152152+153153+ for (i = 100; i; i--) {154154+ err = mmc_wait_for_app_cmd(host, NULL, &cmd, MMC_CMD_RETRIES);155155+ if (err != MMC_ERR_NONE)156156+ break;157157+158158+ if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)159159+ break;160160+161161+ err = MMC_ERR_TIMEOUT;162162+163163+ mmc_delay(10);164164+ }165165+166166+ if (rocr)167167+ *rocr = cmd.resp[0];168168+169169+ return err;170170+}171171+172172+int mmc_send_if_cond(struct mmc_host *host, u32 ocr)173173+{174174+ struct mmc_command cmd;175175+ int err;176176+ static const u8 test_pattern = 0xAA;177177+178178+ /*179179+ * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND180180+ * before SD_APP_OP_COND. This command will harmlessly fail for181181+ * SD 1.0 cards.182182+ */183183+ cmd.opcode = SD_SEND_IF_COND;184184+ cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern;185185+ cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;186186+187187+ err = mmc_wait_for_cmd(host, &cmd, 0);188188+ if (err != MMC_ERR_NONE)189189+ return err;190190+191191+ if ((cmd.resp[0] & 0xFF) != test_pattern)192192+ return MMC_ERR_FAILED;193193+194194+ return MMC_ERR_NONE;195195+}196196+197197+int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca)198198+{199199+ int err;200200+ struct mmc_command cmd;201201+202202+ BUG_ON(!host);203203+ BUG_ON(!rca);204204+205205+ memset(&cmd, 0, sizeof(struct mmc_command));206206+207207+ cmd.opcode = SD_SEND_RELATIVE_ADDR;208208+ cmd.arg = 0;209209+ cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;210210+211211+ err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);212212+ if (err != MMC_ERR_NONE)213213+ return err;214214+215215+ *rca = cmd.resp[0] >> 16;216216+217217+ return MMC_ERR_NONE;218218+}219219+220220+int mmc_app_send_scr(struct mmc_card *card, u32 *scr)221221+{222222+ int err;223223+ struct mmc_request mrq;224224+ struct mmc_command cmd;225225+ struct mmc_data data;226226+ struct scatterlist sg;227227+228228+ BUG_ON(!card);229229+ BUG_ON(!card->host);230230+ BUG_ON(!scr);231231+232232+ err = mmc_app_cmd(card->host, card);233233+ if (err != MMC_ERR_NONE)234234+ return err;235235+236236+ memset(&mrq, 0, sizeof(struct mmc_request));237237+ memset(&cmd, 0, sizeof(struct mmc_command));238238+ memset(&data, 0, sizeof(struct mmc_data));239239+240240+ mrq.cmd = &cmd;241241+ mrq.data = &data;242242+243243+ cmd.opcode = SD_APP_SEND_SCR;244244+ cmd.arg = 0;245245+ cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;246246+247247+ data.blksz = 8;248248+ data.blocks = 1;249249+ data.flags = MMC_DATA_READ;250250+ data.sg = &sg;251251+ data.sg_len = 1;252252+253253+ sg_init_one(&sg, scr, 8);254254+255255+ mmc_set_data_timeout(&data, card, 0);256256+257257+ mmc_wait_for_req(card->host, &mrq);258258+259259+ if (cmd.error != MMC_ERR_NONE)260260+ return cmd.error;261261+ if (data.error != MMC_ERR_NONE)262262+ return data.error;263263+264264+ scr[0] = ntohl(scr[0]);265265+ scr[1] = ntohl(scr[1]);266266+267267+ return MMC_ERR_NONE;268268+}269269+270270+int mmc_sd_switch(struct mmc_card *card, int mode, int group,271271+ u8 value, u8 *resp)272272+{273273+ struct mmc_request mrq;274274+ struct mmc_command cmd;275275+ struct mmc_data data;276276+ struct scatterlist sg;277277+278278+ BUG_ON(!card);279279+ BUG_ON(!card->host);280280+281281+ mode = !!mode;282282+ value &= 0xF;283283+284284+ memset(&mrq, 0, sizeof(struct mmc_request));285285+ memset(&cmd, 0, sizeof(struct mmc_command));286286+ memset(&data, 0, sizeof(struct mmc_data));287287+288288+ mrq.cmd = &cmd;289289+ mrq.data = &data;290290+291291+ cmd.opcode = SD_SWITCH;292292+ cmd.arg = mode << 31 | 0x00FFFFFF;293293+ cmd.arg &= ~(0xF << (group * 4));294294+ cmd.arg |= value << (group * 4);295295+ cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;296296+297297+ data.blksz = 64;298298+ data.blocks = 1;299299+ data.flags = MMC_DATA_READ;300300+ data.sg = &sg;301301+ data.sg_len = 1;302302+303303+ sg_init_one(&sg, resp, 64);304304+305305+ mmc_set_data_timeout(&data, card, 0);306306+307307+ mmc_wait_for_req(card->host, &mrq);308308+309309+ if (cmd.error != MMC_ERR_NONE)310310+ return cmd.error;311311+ if (data.error != MMC_ERR_NONE)312312+ return data.error;313313+314314+ return MMC_ERR_NONE;315315+}316316+
+25
drivers/mmc/core/sd_ops.h
···11+/*22+ * linux/drivers/mmc/sd_ops.h33+ *44+ * Copyright 2006-2007 Pierre Ossman55+ *66+ * This program is free software; you can redistribute it and/or modify77+ * it under the terms of the GNU General Public License as published by88+ * the Free Software Foundation; either version 2 of the License, or (at99+ * your option) any later version.1010+ */1111+1212+#ifndef _MMC_SD_OPS_H1313+#define _MMC_SD_OPS_H1414+1515+int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card);1616+int mmc_app_set_bus_width(struct mmc_card *card, int width);1717+int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr);1818+int mmc_send_if_cond(struct mmc_host *host, u32 ocr);1919+int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca);2020+int mmc_app_send_scr(struct mmc_card *card, u32 *scr);2121+int mmc_sd_switch(struct mmc_card *card, int mode, int group,2222+ u8 value, u8 *resp);2323+2424+#endif2525+
···11+/*22+ * linux/drivers/mmc/core/sysfs.h33+ *44+ * Copyright (C) 2003 Russell King, All Rights Reserved.55+ * Copyright 2007 Pierre Ossman66+ *77+ * This program is free software; you can redistribute it and/or modify88+ * it under the terms of the GNU General Public License version 2 as99+ * published by the Free Software Foundation.1010+ */1111+#ifndef _MMC_CORE_SYSFS_H1212+#define _MMC_CORE_SYSFS_H1313+1414+void mmc_init_card(struct mmc_card *card, struct mmc_host *host);1515+int mmc_register_card(struct mmc_card *card);1616+void mmc_remove_card(struct mmc_card *card);1717+1818+struct mmc_host *mmc_alloc_host_sysfs(int extra, struct device *dev);1919+int mmc_add_host_sysfs(struct mmc_host *host);2020+void mmc_remove_host_sysfs(struct mmc_host *host);2121+void mmc_free_host_sysfs(struct mmc_host *host);2222+2323+int mmc_schedule_work(struct work_struct *work);2424+int mmc_schedule_delayed_work(struct delayed_work *work, unsigned long delay);2525+void mmc_flush_scheduled_work(void);2626+2727+#endif
···2222 * 15 May 20022323 */24242525-#ifndef MMC_MMC_PROTOCOL_H2626-#define MMC_MMC_PROTOCOL_H2525+#ifndef MMC_MMC_H2626+#define MMC_MMC_H27272828/* Standard MMC commands (4.1) type argument response */2929 /* class 1 */···7878#define MMC_APP_CMD 55 /* ac [31:16] RCA R1 */7979#define MMC_GEN_CMD 56 /* adtc [0] RD/WR R1 */80808181-/* SD commands type argument response */8282- /* class 0 */8383-/* This is basically the same command as for MMC with some quirks. */8484-#define SD_SEND_RELATIVE_ADDR 3 /* bcr R6 */8585-#define SD_SEND_IF_COND 8 /* bcr [11:0] See below R7 */8686-8787- /* class 10 */8888-#define SD_SWITCH 6 /* adtc [31:0] See below R1 */8989-9090- /* Application commands */9191-#define SD_APP_SET_BUS_WIDTH 6 /* ac [1:0] bus width R1 */9292-#define SD_APP_SEND_NUM_WR_BLKS 22 /* adtc R1 */9393-#define SD_APP_OP_COND 41 /* bcr [31:0] OCR R3 */9494-#define SD_APP_SEND_SCR 51 /* adtc R1 */9595-9681/*9782 * MMC_SWITCH argument format:9883 *···87102 * [15:08] Value Byte88103 * [07:03] Always 089104 * [02:00] Command Set9090- */9191-9292-/*9393- * SD_SWITCH argument format:9494- *9595- * [31] Check (0) or switch (1)9696- * [30:24] Reserved (0)9797- * [23:20] Function group 69898- * [19:16] Function group 59999- * [15:12] Function group 4100100- * [11:8] Function group 3101101- * [7:4] Function group 2102102- * [3:0] Function group 1103103- */104104-105105-/*106106- * SD_SEND_IF_COND argument format:107107- *108108- * [31:12] Reserved (0)109109- * [11:8] Host Voltage Supply Flags110110- * [7:0] Check Pattern (0xAA)111105 */112106113107/*···252288#define MMC_SWITCH_MODE_SET_BITS 0x01 /* Set bits which are 1 in value */253289#define MMC_SWITCH_MODE_CLEAR_BITS 0x02 /* Clear bits which are 1 in value */254290#define MMC_SWITCH_MODE_WRITE_BYTE 0x03 /* Set target to value */255255-256256-/*257257- * SCR field definitions258258- */259259-260260-#define SCR_SPEC_VER_0 0 /* Implements system specification 1.0 - 1.01 */261261-#define SCR_SPEC_VER_1 1 /* Implements system specification 1.10 */262262-#define SCR_SPEC_VER_2 2 /* Implements system specification 2.00 */263263-264264-/*265265- * SD bus widths266266- */267267-#define SD_BUS_WIDTH_1 0268268-#define SD_BUS_WIDTH_4 2269291270292#endif /* MMC_MMC_PROTOCOL_H */271293
+83
include/linux/mmc/sd.h
···11+/*22+ * include/linux/mmc/sd.h33+ *44+ * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.55+ *66+ * This program is free software; you can redistribute it and/or modify77+ * it under the terms of the GNU General Public License as published by88+ * the Free Software Foundation; either version 2 of the License, or (at99+ * your option) any later version.1010+ */1111+1212+#ifndef MMC_SD_H1313+#define MMC_SD_H1414+1515+/* SD commands type argument response */1616+ /* class 0 */1717+/* This is basically the same command as for MMC with some quirks. */1818+#define SD_SEND_RELATIVE_ADDR 3 /* bcr R6 */1919+#define SD_SEND_IF_COND 8 /* bcr [11:0] See below R7 */2020+2121+ /* class 10 */2222+#define SD_SWITCH 6 /* adtc [31:0] See below R1 */2323+2424+ /* Application commands */2525+#define SD_APP_SET_BUS_WIDTH 6 /* ac [1:0] bus width R1 */2626+#define SD_APP_SEND_NUM_WR_BLKS 22 /* adtc R1 */2727+#define SD_APP_OP_COND 41 /* bcr [31:0] OCR R3 */2828+#define SD_APP_SEND_SCR 51 /* adtc R1 */2929+3030+/*3131+ * SD_SWITCH argument format:3232+ *3333+ * [31] Check (0) or switch (1)3434+ * [30:24] Reserved (0)3535+ * [23:20] Function group 63636+ * [19:16] Function group 53737+ * [15:12] Function group 43838+ * [11:8] Function group 33939+ * [7:4] Function group 24040+ * [3:0] Function group 14141+ */4242+4343+/*4444+ * SD_SEND_IF_COND argument format:4545+ *4646+ * [31:12] Reserved (0)4747+ * [11:8] Host Voltage Supply Flags4848+ * [7:0] Check Pattern (0xAA)4949+ */5050+5151+/*5252+ * SCR field definitions5353+ */5454+5555+#define SCR_SPEC_VER_0 0 /* Implements system specification 1.0 - 1.01 */5656+#define SCR_SPEC_VER_1 1 /* Implements system specification 1.10 */5757+#define SCR_SPEC_VER_2 2 /* Implements system specification 2.00 */5858+5959+/*6060+ * SD bus widths6161+ */6262+#define SD_BUS_WIDTH_1 06363+#define SD_BUS_WIDTH_4 26464+6565+/*6666+ * SD_SWITCH mode6767+ */6868+#define SD_SWITCH_CHECK 06969+#define SD_SWITCH_SET 17070+7171+/*7272+ * SD_SWITCH function groups7373+ */7474+#define SD_SWITCH_GRP_ACCESS 07575+7676+/*7777+ * SD_SWITCH access modes7878+ */7979+#define SD_SWITCH_ACCESS_DEF 08080+#define SD_SWITCH_ACCESS_HS 18181+8282+#endif8383+