sd_ops.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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/slab.h>
  12. #include <linux/types.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. memset(&cmd, 0, sizeof(struct mmc_command));
  27. cmd.opcode = MMC_APP_CMD;
  28. if (card) {
  29. cmd.arg = card->rca << 16;
  30. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  31. } else {
  32. cmd.arg = 0;
  33. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_BCR;
  34. }
  35. err = mmc_wait_for_cmd(host, &cmd, 0);
  36. if (err)
  37. return err;
  38. /* Check that card supported application commands */
  39. if (!mmc_host_is_spi(host) && !(cmd.resp[0] & R1_APP_CMD))
  40. return -EOPNOTSUPP;
  41. return 0;
  42. }
  43. /**
  44. * mmc_wait_for_app_cmd - start an application command and wait for
  45. completion
  46. * @host: MMC host to start command
  47. * @card: Card to send MMC_APP_CMD to
  48. * @cmd: MMC command to start
  49. * @retries: maximum number of retries
  50. *
  51. * Sends a MMC_APP_CMD, checks the card response, sends the command
  52. * in the parameter and waits for it to complete. Return any error
  53. * that occurred while the command was executing. Do not attempt to
  54. * parse the response.
  55. */
  56. int mmc_wait_for_app_cmd(struct mmc_host *host, struct mmc_card *card,
  57. struct mmc_command *cmd, int retries)
  58. {
  59. struct mmc_request mrq;
  60. int i, err;
  61. BUG_ON(!cmd);
  62. BUG_ON(retries < 0);
  63. err = -EIO;
  64. /*
  65. * We have to resend MMC_APP_CMD for each attempt so
  66. * we cannot use the retries field in mmc_command.
  67. */
  68. for (i = 0;i <= retries;i++) {
  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. void *data_buf;
  203. BUG_ON(!card);
  204. BUG_ON(!card->host);
  205. BUG_ON(!scr);
  206. /* NOTE: caller guarantees scr is heap-allocated */
  207. err = mmc_app_cmd(card->host, card);
  208. if (err)
  209. return err;
  210. /* dma onto stack is unsafe/nonportable, but callers to this
  211. * routine normally provide temporary on-stack buffers ...
  212. */
  213. data_buf = kmalloc(sizeof(card->raw_scr), GFP_KERNEL);
  214. if (data_buf == NULL)
  215. return -ENOMEM;
  216. memset(&mrq, 0, sizeof(struct mmc_request));
  217. memset(&cmd, 0, sizeof(struct mmc_command));
  218. memset(&data, 0, sizeof(struct mmc_data));
  219. mrq.cmd = &cmd;
  220. mrq.data = &data;
  221. cmd.opcode = SD_APP_SEND_SCR;
  222. cmd.arg = 0;
  223. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  224. data.blksz = 8;
  225. data.blocks = 1;
  226. data.flags = MMC_DATA_READ;
  227. data.sg = &sg;
  228. data.sg_len = 1;
  229. sg_init_one(&sg, data_buf, 8);
  230. mmc_set_data_timeout(&data, card);
  231. mmc_wait_for_req(card->host, &mrq);
  232. memcpy(scr, data_buf, sizeof(card->raw_scr));
  233. kfree(data_buf);
  234. if (cmd.error)
  235. return cmd.error;
  236. if (data.error)
  237. return data.error;
  238. scr[0] = be32_to_cpu(scr[0]);
  239. scr[1] = be32_to_cpu(scr[1]);
  240. return 0;
  241. }
  242. int mmc_sd_switch(struct mmc_card *card, int mode, int group,
  243. u8 value, u8 *resp)
  244. {
  245. struct mmc_request mrq;
  246. struct mmc_command cmd;
  247. struct mmc_data data;
  248. struct scatterlist sg;
  249. BUG_ON(!card);
  250. BUG_ON(!card->host);
  251. /* NOTE: caller guarantees resp is heap-allocated */
  252. mode = !!mode;
  253. value &= 0xF;
  254. memset(&mrq, 0, sizeof(struct mmc_request));
  255. memset(&cmd, 0, sizeof(struct mmc_command));
  256. memset(&data, 0, sizeof(struct mmc_data));
  257. mrq.cmd = &cmd;
  258. mrq.data = &data;
  259. cmd.opcode = SD_SWITCH;
  260. cmd.arg = mode << 31 | 0x00FFFFFF;
  261. cmd.arg &= ~(0xF << (group * 4));
  262. cmd.arg |= value << (group * 4);
  263. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  264. data.blksz = 64;
  265. data.blocks = 1;
  266. data.flags = MMC_DATA_READ;
  267. data.sg = &sg;
  268. data.sg_len = 1;
  269. sg_init_one(&sg, resp, 64);
  270. mmc_set_data_timeout(&data, card);
  271. mmc_wait_for_req(card->host, &mrq);
  272. if (cmd.error)
  273. return cmd.error;
  274. if (data.error)
  275. return data.error;
  276. return 0;
  277. }
  278. int mmc_app_sd_status(struct mmc_card *card, void *ssr)
  279. {
  280. int err;
  281. struct mmc_request mrq;
  282. struct mmc_command cmd;
  283. struct mmc_data data;
  284. struct scatterlist sg;
  285. BUG_ON(!card);
  286. BUG_ON(!card->host);
  287. BUG_ON(!ssr);
  288. /* NOTE: caller guarantees ssr is heap-allocated */
  289. err = mmc_app_cmd(card->host, card);
  290. if (err)
  291. return err;
  292. memset(&mrq, 0, sizeof(struct mmc_request));
  293. memset(&cmd, 0, sizeof(struct mmc_command));
  294. memset(&data, 0, sizeof(struct mmc_data));
  295. mrq.cmd = &cmd;
  296. mrq.data = &data;
  297. cmd.opcode = SD_APP_SD_STATUS;
  298. cmd.arg = 0;
  299. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_ADTC;
  300. data.blksz = 64;
  301. data.blocks = 1;
  302. data.flags = MMC_DATA_READ;
  303. data.sg = &sg;
  304. data.sg_len = 1;
  305. sg_init_one(&sg, ssr, 64);
  306. mmc_set_data_timeout(&data, card);
  307. mmc_wait_for_req(card->host, &mrq);
  308. if (cmd.error)
  309. return cmd.error;
  310. if (data.error)
  311. return data.error;
  312. return 0;
  313. }