2 * linux/drivers/mmc/core/sd_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 #if 1 // add by Victor Yu. 12-02-2008
14 #include <linux/pagemap.h>
16 #include <asm/scatterlist.h>
17 #include <linux/scatterlist.h>
19 #include <linux/mmc/host.h>
20 #include <linux/mmc/card.h>
21 #include <linux/mmc/mmc.h>
22 #include <linux/mmc/sd.h>
27 static int mmc_app_cmd(struct mmc_host
*host
, struct mmc_card
*card
)
30 struct mmc_command cmd
;
33 BUG_ON(card
&& (card
->host
!= host
));
35 cmd
.opcode
= MMC_APP_CMD
;
38 cmd
.arg
= card
->rca
<< 16;
39 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
42 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_BCR
;
45 err
= mmc_wait_for_cmd(host
, &cmd
, 0);
46 if (err
!= MMC_ERR_NONE
)
49 /* Check that card supported application commands */
50 if (!(cmd
.resp
[0] & R1_APP_CMD
))
51 return MMC_ERR_FAILED
;
57 * mmc_wait_for_app_cmd - start an application command and wait for
59 * @host: MMC host to start command
60 * @card: Card to send MMC_APP_CMD to
61 * @cmd: MMC command to start
62 * @retries: maximum number of retries
64 * Sends a MMC_APP_CMD, checks the card response, sends the command
65 * in the parameter and waits for it to complete. Return any error
66 * that occurred while the command was executing. Do not attempt to
69 int mmc_wait_for_app_cmd(struct mmc_host
*host
, struct mmc_card
*card
,
70 struct mmc_command
*cmd
, int retries
)
72 struct mmc_request mrq
;
79 err
= MMC_ERR_INVALID
;
82 * We have to resend MMC_APP_CMD for each attempt so
83 * we cannot use the retries field in mmc_command.
85 for (i
= 0;i
<= retries
;i
++) {
86 memset(&mrq
, 0, sizeof(struct mmc_request
));
88 err
= mmc_app_cmd(host
, card
);
89 if (err
!= MMC_ERR_NONE
)
92 memset(&mrq
, 0, sizeof(struct mmc_request
));
94 memset(cmd
->resp
, 0, sizeof(cmd
->resp
));
100 mmc_wait_for_req(host
, &mrq
);
103 if (cmd
->error
== MMC_ERR_NONE
)
110 EXPORT_SYMBOL(mmc_wait_for_app_cmd
);
112 int mmc_app_set_bus_width(struct mmc_card
*card
, int width
)
115 struct mmc_command cmd
;
120 memset(&cmd
, 0, sizeof(struct mmc_command
));
122 cmd
.opcode
= SD_APP_SET_BUS_WIDTH
;
123 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
126 case MMC_BUS_WIDTH_1
:
127 cmd
.arg
= SD_BUS_WIDTH_1
;
129 case MMC_BUS_WIDTH_4
:
130 cmd
.arg
= SD_BUS_WIDTH_4
;
133 return MMC_ERR_INVALID
;
136 err
= mmc_wait_for_app_cmd(card
->host
, card
, &cmd
, MMC_CMD_RETRIES
);
137 if (err
!= MMC_ERR_NONE
)
143 int mmc_send_app_op_cond(struct mmc_host
*host
, u32 ocr
, u32
*rocr
)
145 struct mmc_command cmd
;
150 memset(&cmd
, 0, sizeof(struct mmc_command
));
152 cmd
.opcode
= SD_APP_OP_COND
;
154 cmd
.flags
= MMC_RSP_R3
| MMC_CMD_BCR
;
156 for (i
= 100; i
; i
--) {
157 err
= mmc_wait_for_app_cmd(host
, NULL
, &cmd
, MMC_CMD_RETRIES
);
158 if (err
!= MMC_ERR_NONE
)
161 if (cmd
.resp
[0] & MMC_CARD_BUSY
|| ocr
== 0)
164 err
= MMC_ERR_TIMEOUT
;
175 int mmc_send_if_cond(struct mmc_host
*host
, u32 ocr
)
177 struct mmc_command cmd
;
179 static const u8 test_pattern
= 0xAA;
182 * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
183 * before SD_APP_OP_COND. This command will harmlessly fail for
186 cmd
.opcode
= SD_SEND_IF_COND
;
187 cmd
.arg
= ((ocr
& 0xFF8000) != 0) << 8 | test_pattern
;
188 cmd
.flags
= MMC_RSP_R7
| MMC_CMD_BCR
;
190 err
= mmc_wait_for_cmd(host
, &cmd
, 0);
191 if (err
!= MMC_ERR_NONE
)
194 if ((cmd
.resp
[0] & 0xFF) != test_pattern
)
195 return MMC_ERR_FAILED
;
200 int mmc_send_relative_addr(struct mmc_host
*host
, unsigned int *rca
)
203 struct mmc_command cmd
;
208 memset(&cmd
, 0, sizeof(struct mmc_command
));
210 cmd
.opcode
= SD_SEND_RELATIVE_ADDR
;
212 cmd
.flags
= MMC_RSP_R6
| MMC_CMD_BCR
;
214 err
= mmc_wait_for_cmd(host
, &cmd
, MMC_CMD_RETRIES
);
215 if (err
!= MMC_ERR_NONE
)
218 *rca
= cmd
.resp
[0] >> 16;
223 int mmc_app_send_scr(struct mmc_card
*card
, u32
*scr
)
226 struct mmc_request mrq
;
227 struct mmc_command cmd
;
228 struct mmc_data data
;
229 struct scatterlist sg
;
235 err
= mmc_app_cmd(card
->host
, card
);
236 if (err
!= MMC_ERR_NONE
)
239 memset(&mrq
, 0, sizeof(struct mmc_request
));
240 memset(&cmd
, 0, sizeof(struct mmc_command
));
241 memset(&data
, 0, sizeof(struct mmc_data
));
246 cmd
.opcode
= SD_APP_SEND_SCR
;
248 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_ADTC
;
252 data
.flags
= MMC_DATA_READ
;
256 #if 0 // mask by Victor Yu. 12-03-2008
257 sg_init_one(&sg
, scr
, 8);
259 sg_init_one(&sg
, (u8
*)scr
, 8);
262 mmc_set_data_timeout(&data
, card
, 0);
264 mmc_wait_for_req(card
->host
, &mrq
);
266 if (cmd
.error
!= MMC_ERR_NONE
)
268 if (data
.error
!= MMC_ERR_NONE
)
271 scr
[0] = ntohl(scr
[0]);
272 scr
[1] = ntohl(scr
[1]);
277 int mmc_sd_switch(struct mmc_card
*card
, int mode
, int group
, u8 value
, u8
*resp
)
279 struct mmc_request mrq
;
280 struct mmc_command cmd
;
281 struct mmc_data data
;
282 struct scatterlist sg
;
290 memset(&mrq
, 0, sizeof(struct mmc_request
));
291 memset(&cmd
, 0, sizeof(struct mmc_command
));
292 memset(&data
, 0, sizeof(struct mmc_data
));
297 cmd
.opcode
= SD_SWITCH
;
298 cmd
.arg
= mode
<< 31 | 0x00FFFFFF;
299 cmd
.arg
&= ~(0xF << (group
* 4));
300 cmd
.arg
|= value
<< (group
* 4);
301 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_ADTC
;
305 data
.flags
= MMC_DATA_READ
;
309 sg_init_one(&sg
, resp
, 64);
311 mmc_set_data_timeout(&data
, card
, 0);
313 mmc_wait_for_req(card
->host
, &mrq
);
315 if (cmd
.error
!= MMC_ERR_NONE
)
317 if (data
.error
!= MMC_ERR_NONE
)