2 * linux/drivers/mmc/mmc_ops.h
4 * Copyright 2006-2007 Pierre Ossman
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
12 #include <linux/types.h>
13 #include <asm/scatterlist.h>
14 #include <linux/scatterlist.h>
16 #include <linux/mmc/host.h>
17 #include <linux/mmc/card.h>
18 #include <linux/mmc/mmc.h>
23 static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
26 struct mmc_command cmd;
30 memset(&cmd, 0, sizeof(struct mmc_command));
32 cmd.opcode = MMC_SELECT_CARD;
35 cmd.arg = card->rca << 16;
36 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
39 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
42 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
43 if (err != MMC_ERR_NONE)
49 int mmc_select_card(struct mmc_card *card)
53 return _mmc_select_card(card->host, card);
56 int mmc_deselect_cards(struct mmc_host *host)
58 return _mmc_select_card(host, NULL);
61 int mmc_go_idle(struct mmc_host *host)
64 struct mmc_command cmd;
66 mmc_set_chip_select(host, MMC_CS_HIGH);
70 memset(&cmd, 0, sizeof(struct mmc_command));
72 cmd.opcode = MMC_GO_IDLE_STATE;
74 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
76 err = mmc_wait_for_cmd(host, &cmd, 0);
80 mmc_set_chip_select(host, MMC_CS_DONTCARE);
87 int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
89 struct mmc_command cmd;
94 memset(&cmd, 0, sizeof(struct mmc_command));
96 cmd.opcode = MMC_SEND_OP_COND;
98 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
100 for (i = 100; i; i--) {
101 err = mmc_wait_for_cmd(host, &cmd, 0);
102 if (err != MMC_ERR_NONE)
105 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
108 err = MMC_ERR_TIMEOUT;
119 int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
122 struct mmc_command cmd;
127 memset(&cmd, 0, sizeof(struct mmc_command));
129 cmd.opcode = MMC_ALL_SEND_CID;
131 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
133 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
134 if (err != MMC_ERR_NONE)
137 memcpy(cid, cmd.resp, sizeof(u32) * 4);
142 int mmc_set_relative_addr(struct mmc_card *card)
145 struct mmc_command cmd;
150 memset(&cmd, 0, sizeof(struct mmc_command));
152 cmd.opcode = MMC_SET_RELATIVE_ADDR;
153 cmd.arg = card->rca << 16;
154 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
156 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
157 if (err != MMC_ERR_NONE)
163 int mmc_send_csd(struct mmc_card *card, u32 *csd)
166 struct mmc_command cmd;
172 memset(&cmd, 0, sizeof(struct mmc_command));
174 cmd.opcode = MMC_SEND_CSD;
175 cmd.arg = card->rca << 16;
176 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
178 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
179 if (err != MMC_ERR_NONE)
182 memcpy(csd, cmd.resp, sizeof(u32) * 4);
187 int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
189 struct mmc_request mrq;
190 struct mmc_command cmd;
191 struct mmc_data data;
192 struct scatterlist sg;
198 memset(&mrq, 0, sizeof(struct mmc_request));
199 memset(&cmd, 0, sizeof(struct mmc_command));
200 memset(&data, 0, sizeof(struct mmc_data));
205 cmd.opcode = MMC_SEND_EXT_CSD;
207 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
211 data.flags = MMC_DATA_READ;
215 sg_init_one(&sg, ext_csd, 512);
217 mmc_set_data_timeout(&data, card, 0);
219 mmc_wait_for_req(card->host, &mrq);
221 if (cmd.error != MMC_ERR_NONE)
223 if (data.error != MMC_ERR_NONE)
229 int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value)
232 struct mmc_command cmd;
237 memset(&cmd, 0, sizeof(struct mmc_command));
239 cmd.opcode = MMC_SWITCH;
240 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
244 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
246 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
247 if (err != MMC_ERR_NONE)
253 int mmc_send_status(struct mmc_card *card, u32 *status)
256 struct mmc_command cmd;
261 memset(&cmd, 0, sizeof(struct mmc_command));
263 cmd.opcode = MMC_SEND_STATUS;
264 cmd.arg = card->rca << 16;
265 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
267 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
268 if (err != MMC_ERR_NONE)
272 *status = cmd.resp[0];