sd_ops.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * linux/drivers/mmc/core/sd_ops.h
  3. *
  4. * Copyright 2006-2007 Pierre Ossman
  5. *
  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.
  10. */
  11. #include <linux/types.h>
  12. #include <asm/scatterlist.h>
  13. #include <linux/scatterlist.h>
  14. #include <linux/mmc/host.h>
  15. #include <linux/mmc/card.h>
  16. #include <linux/mmc/mmc.h>
  17. #include <linux/mmc/sd.h>
  18. #include "core.h"
  19. #include "sd_ops.h"
  20. static int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card)
  21. {
  22. int err;
  23. struct mmc_command cmd;
  24. BUG_ON(!host);
  25. BUG_ON(card && (card->host != host));
  26. cmd.opcode = MMC_APP_CMD;
  27. if (card) {
  28. cmd.arg = card->rca << 16;
  29. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  30. } else {
  31. cmd.arg = 0;
  32. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_BCR;
  33. }
  34. err = mmc_wait_for_cmd(host, &cmd, 0);
  35. if (err)
  36. return err;
  37. /* Check that card supported application commands */
  38. if (!mmc_host_is_spi(host) && !(cmd.resp[0] & R1_APP_CMD))
  39. return -EOPNOTSUPP;
  40. return 0;
  41. }
  42. /**
  43. * mmc_wait_for_app_cmd - start an application command and wait for
  44. completion
  45. * @host: MMC host to start command
  46. * @card: Card to send MMC_APP_CMD to
  47. * @cmd: MMC command to start
  48. * @retries: maximum number of retries
  49. *
  50. * Sends a MMC_APP_CMD, checks the card response, sends the command
  51. * in the parameter and waits for it to complete. Return any error
  52. * that occurred while the command was executing. Do not attempt to
  53. * parse the response.
  54. */
  55. int mmc_wait_for_app_cmd(struct mmc_host *host, struct mmc_card *card,
  56. struct mmc_command *cmd, int retries)
  57. {
  58. struct mmc_request mrq;
  59. int i, err;
  60. BUG_ON(!cmd);
  61. BUG_ON(retries < 0);
  62. err = -EIO;
  63. /*
  64. * We have to resend MMC_APP_CMD for each attempt so
  65. * we cannot use the retries field in mmc_command.
  66. */
  67. for (i = 0;i <= retries;i++) {
  68. memset(&mrq, 0, sizeof(struct mmc_request));
  69. err = mmc_app_cmd(host, card);
  70. if (err) {
  71. /* no point in retrying; no APP commands allowed */
  72. if (mmc_host_is_spi(host)) {
  73. if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
  74. break;
  75. }
  76. continue;
  77. }
  78. memset(&mrq, 0, sizeof(struct mmc_request));
  79. memset(cmd->resp, 0, sizeof(cmd->resp));
  80. cmd->retries = 0;
  81. mrq.cmd = cmd;
  82. cmd->data = NULL;
  83. mmc_wait_for_req(host, &mrq);
  84. err = cmd->error;
  85. if (!cmd->error)
  86. break;
  87. /* no point in retrying illegal APP commands */
  88. if (mmc_host_is_spi(host)) {
  89. if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
  90. break;
  91. }
  92. }
  93. return err;
  94. }
  95. EXPORT_SYMBOL(mmc_wait_for_app_cmd);
  96. int mmc_app_set_bus_width(struct mmc_card *card, int width)
  97. {
  98. int err;
  99. struct mmc_command cmd;
  100. BUG_ON(!card);
  101. BUG_ON(!card->host);
  102. memset(&cmd, 0, sizeof(struct mmc_command));
  103. cmd.opcode = SD_APP_SET_BUS_WIDTH;
  104. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  105. switch (width) {
  106. case MMC_BUS_WIDTH_1:
  107. cmd.arg = SD_BUS_WIDTH_1;
  108. break;
  109. case MMC_BUS_WIDTH_4:
  110. cmd.arg = SD_BUS_WIDTH_4;
  111. break;
  112. default:
  113. return -EINVAL;
  114. }
  115. err = mmc_wait_for_app_cmd(card->host, card, &cmd, MMC_CMD_RETRIES);
  116. if (err)
  117. return err;
  118. return 0;
  119. }
  120. int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
  121. {
  122. struct mmc_command cmd;
  123. int i, err = 0;
  124. BUG_ON(!host);
  125. memset(&cmd, 0, sizeof(struct mmc_command));
  126. cmd.opcode = SD_APP_OP_COND;
  127. if (mmc_host_is_spi(host))
  128. cmd.arg = ocr & (1 << 30); /* SPI only defines one bit */
  129. else
  130. cmd.arg = ocr;
  131. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
  132. for (i = 100; i; i--) {
  133. err = mmc_wait_for_app_cmd(host, NULL, &cmd, MMC_CMD_RETRIES);
  134. if (err)
  135. break;
  136. /* if we're just probing, do a single pass */
  137. if (ocr == 0)
  138. break;
  139. /* otherwise wait until reset completes */
  140. if (mmc_host_is_spi(host)) {
  141. if (!(cmd.resp[0] & R1_SPI_IDLE))
  142. break;
  143. } else {
  144. if (cmd.resp[0] & MMC_CARD_BUSY)
  145. break;
  146. }
  147. err = -ETIMEDOUT;
  148. mmc_delay(10);
  149. }
  150. if (rocr && !mmc_host_is_spi(host))
  151. *rocr = cmd.resp[0];
  152. return err;
  153. }
  154. int mmc_send_if_cond(struct mmc_host *host, u32 ocr)
  155. {
  156. struct mmc_command cmd;
  157. int err;
  158. static const u8 test_pattern = 0xAA;
  159. u8 result_pattern;
  160. /*
  161. * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
  162. * before SD_APP_OP_COND. This command will harmlessly fail for
  163. * SD 1.0 cards.
  164. */
  165. cmd.opcode = SD_SEND_IF_COND;
  166. cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern;
  167. cmd.flags = MMC_RSP_SPI_R7 | MMC_RSP_R7 | MMC_CMD_BCR;
  168. err = mmc_wait_for_cmd(host, &cmd, 0);
  169. if (err)
  170. return err;
  171. if (mmc_host_is_spi(host))
  172. result_pattern = cmd.resp[1] & 0xFF;
  173. else
  174. result_pattern = cmd.resp[0] & 0xFF;
  175. if (result_pattern != test_pattern)
  176. return -EIO;
  177. return 0;
  178. }
  179. int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca)
  180. {
  181. int err;
  182. struct mmc_command cmd;
  183. BUG_ON(!host);
  184. BUG_ON(!rca);
  185. memset(&cmd, 0, sizeof(struct mmc_command));
  186. cmd.opcode = SD_SEND_RELATIVE_ADDR;
  187. cmd.arg = 0;
  188. cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
  189. err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  190. if (err)
  191. return err;
  192. *rca = cmd.resp[0] >> 16;
  193. return 0;
  194. }
  195. int mmc_app_send_scr(struct mmc_card *card, u32 *scr)
  196. {
  197. int err;
  198. struct mmc_request mrq;
  199. struct mmc_command cmd;
  200. struct mmc_data data;
  201. struct scatterlist sg;
  202. BUG_ON(!card);
  203. BUG_ON(!card->host);
  204. BUG_ON(!scr);
  205. /* NOTE: caller guarantees scr is heap-allocated */
  206. err = mmc_app_cmd(card->host, card);
  207. if (err)
  208. return err;
  209. memset(&mrq, 0, sizeof(struct mmc_request));
  210. memset(&cmd, 0, sizeof(struct mmc_command));
  211. memset(&data, 0, sizeof(struct mmc_data));
  212. mrq.cmd = &cmd;
  213. mrq.data = &data;
  214. cmd.opcode = SD_APP_SEND_SCR;
  215. cmd.arg = 0;
  216. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  217. data.blksz = 8;
  218. data.blocks = 1;
  219. data.flags = MMC_DATA_READ;
  220. data.sg = &sg;
  221. data.sg_len = 1;
  222. sg_init_one(&sg, scr, 8);
  223. mmc_set_data_timeout(&data, card);
  224. mmc_wait_for_req(card->host, &mrq);
  225. if (cmd.error)
  226. return cmd.error;
  227. if (data.error)
  228. return data.error;
  229. scr[0] = ntohl(scr[0]);
  230. scr[1] = ntohl(scr[1]);
  231. return 0;
  232. }
  233. int mmc_sd_switch(struct mmc_card *card, int mode, int group,
  234. u8 value, u8 *resp)
  235. {
  236. struct mmc_request mrq;
  237. struct mmc_command cmd;
  238. struct mmc_data data;
  239. struct scatterlist sg;
  240. BUG_ON(!card);
  241. BUG_ON(!card->host);
  242. /* NOTE: caller guarantees resp is heap-allocated */
  243. mode = !!mode;
  244. value &= 0xF;
  245. memset(&mrq, 0, sizeof(struct mmc_request));
  246. memset(&cmd, 0, sizeof(struct mmc_command));
  247. memset(&data, 0, sizeof(struct mmc_data));
  248. mrq.cmd = &cmd;
  249. mrq.data = &data;
  250. cmd.opcode = SD_SWITCH;
  251. cmd.arg = mode << 31 | 0x00FFFFFF;
  252. cmd.arg &= ~(0xF << (group * 4));
  253. cmd.arg |= value << (group * 4);
  254. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  255. data.blksz = 64;
  256. data.blocks = 1;
  257. data.flags = MMC_DATA_READ;
  258. data.sg = &sg;
  259. data.sg_len = 1;
  260. sg_init_one(&sg, resp, 64);
  261. mmc_set_data_timeout(&data, card);
  262. mmc_wait_for_req(card->host, &mrq);
  263. if (cmd.error)
  264. return cmd.error;
  265. if (data.error)
  266. return data.error;
  267. return 0;
  268. }