sd_ops.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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/export.h>
  14. #include <linux/scatterlist.h>
  15. #include <linux/mmc/host.h>
  16. #include <linux/mmc/card.h>
  17. #include <linux/mmc/mmc.h>
  18. #include <linux/mmc/sd.h>
  19. #include "core.h"
  20. #include "sd_ops.h"
  21. int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card)
  22. {
  23. int err;
  24. struct mmc_command cmd = {0};
  25. BUG_ON(!host);
  26. BUG_ON(card && (card->host != host));
  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. EXPORT_SYMBOL_GPL(mmc_app_cmd);
  44. /**
  45. * mmc_wait_for_app_cmd - start an application command and wait for
  46. completion
  47. * @host: MMC host to start command
  48. * @card: Card to send MMC_APP_CMD to
  49. * @cmd: MMC command to start
  50. * @retries: maximum number of retries
  51. *
  52. * Sends a MMC_APP_CMD, checks the card response, sends the command
  53. * in the parameter and waits for it to complete. Return any error
  54. * that occurred while the command was executing. Do not attempt to
  55. * parse the response.
  56. */
  57. int mmc_wait_for_app_cmd(struct mmc_host *host, struct mmc_card *card,
  58. struct mmc_command *cmd, int retries)
  59. {
  60. struct mmc_request mrq = {NULL};
  61. int i, err;
  62. BUG_ON(!cmd);
  63. BUG_ON(retries < 0);
  64. err = -EIO;
  65. /*
  66. * We have to resend MMC_APP_CMD for each attempt so
  67. * we cannot use the retries field in mmc_command.
  68. */
  69. for (i = 0;i <= retries;i++) {
  70. err = mmc_app_cmd(host, card);
  71. if (err) {
  72. /* no point in retrying; no APP commands allowed */
  73. if (mmc_host_is_spi(host)) {
  74. if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
  75. break;
  76. }
  77. continue;
  78. }
  79. memset(&mrq, 0, sizeof(struct mmc_request));
  80. memset(cmd->resp, 0, sizeof(cmd->resp));
  81. cmd->retries = 0;
  82. mrq.cmd = cmd;
  83. cmd->data = NULL;
  84. mmc_wait_for_req(host, &mrq);
  85. err = cmd->error;
  86. if (!cmd->error)
  87. break;
  88. /* no point in retrying illegal APP commands */
  89. if (mmc_host_is_spi(host)) {
  90. if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
  91. break;
  92. }
  93. }
  94. return err;
  95. }
  96. EXPORT_SYMBOL(mmc_wait_for_app_cmd);
  97. int mmc_app_set_bus_width(struct mmc_card *card, int width)
  98. {
  99. int err;
  100. struct mmc_command cmd = {0};
  101. BUG_ON(!card);
  102. BUG_ON(!card->host);
  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 = {0};
  123. int i, err = 0;
  124. BUG_ON(!host);
  125. cmd.opcode = SD_APP_OP_COND;
  126. if (mmc_host_is_spi(host))
  127. cmd.arg = ocr & (1 << 30); /* SPI only defines one bit */
  128. else
  129. cmd.arg = ocr;
  130. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
  131. for (i = 100; i; i--) {
  132. err = mmc_wait_for_app_cmd(host, NULL, &cmd, MMC_CMD_RETRIES);
  133. if (err)
  134. break;
  135. /* if we're just probing, do a single pass */
  136. if (ocr == 0)
  137. break;
  138. /* otherwise wait until reset completes */
  139. if (mmc_host_is_spi(host)) {
  140. if (!(cmd.resp[0] & R1_SPI_IDLE))
  141. break;
  142. } else {
  143. if (cmd.resp[0] & MMC_CARD_BUSY)
  144. break;
  145. }
  146. err = -ETIMEDOUT;
  147. mmc_delay(10);
  148. }
  149. if (!i)
  150. pr_err("%s: card never left busy state\n", mmc_hostname(host));
  151. if (rocr && !mmc_host_is_spi(host))
  152. *rocr = cmd.resp[0];
  153. return err;
  154. }
  155. int mmc_send_if_cond(struct mmc_host *host, u32 ocr)
  156. {
  157. struct mmc_command cmd = {0};
  158. int err;
  159. static const u8 test_pattern = 0xAA;
  160. u8 result_pattern;
  161. /*
  162. * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
  163. * before SD_APP_OP_COND. This command will harmlessly fail for
  164. * SD 1.0 cards.
  165. */
  166. cmd.opcode = SD_SEND_IF_COND;
  167. cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern;
  168. cmd.flags = MMC_RSP_SPI_R7 | MMC_RSP_R7 | MMC_CMD_BCR;
  169. err = mmc_wait_for_cmd(host, &cmd, 0);
  170. if (err)
  171. return err;
  172. if (mmc_host_is_spi(host))
  173. result_pattern = cmd.resp[1] & 0xFF;
  174. else
  175. result_pattern = cmd.resp[0] & 0xFF;
  176. if (result_pattern != test_pattern)
  177. return -EIO;
  178. return 0;
  179. }
  180. int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca)
  181. {
  182. int err;
  183. struct mmc_command cmd = {0};
  184. BUG_ON(!host);
  185. BUG_ON(!rca);
  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 = {NULL};
  199. struct mmc_command cmd = {0};
  200. struct mmc_data data = {0};
  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. mrq.cmd = &cmd;
  217. mrq.data = &data;
  218. cmd.opcode = SD_APP_SEND_SCR;
  219. cmd.arg = 0;
  220. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  221. data.blksz = 8;
  222. data.blocks = 1;
  223. data.flags = MMC_DATA_READ;
  224. data.sg = &sg;
  225. data.sg_len = 1;
  226. sg_init_one(&sg, data_buf, 8);
  227. mmc_set_data_timeout(&data, card);
  228. mmc_wait_for_req(card->host, &mrq);
  229. memcpy(scr, data_buf, sizeof(card->raw_scr));
  230. kfree(data_buf);
  231. if (cmd.error)
  232. return cmd.error;
  233. if (data.error)
  234. return data.error;
  235. scr[0] = be32_to_cpu(scr[0]);
  236. scr[1] = be32_to_cpu(scr[1]);
  237. return 0;
  238. }
  239. int mmc_sd_switch(struct mmc_card *card, int mode, int group,
  240. u8 value, u8 *resp)
  241. {
  242. struct mmc_request mrq = {NULL};
  243. struct mmc_command cmd = {0};
  244. struct mmc_data data = {0};
  245. struct scatterlist sg;
  246. BUG_ON(!card);
  247. BUG_ON(!card->host);
  248. /* NOTE: caller guarantees resp is heap-allocated */
  249. mode = !!mode;
  250. value &= 0xF;
  251. mrq.cmd = &cmd;
  252. mrq.data = &data;
  253. cmd.opcode = SD_SWITCH;
  254. cmd.arg = mode << 31 | 0x00FFFFFF;
  255. cmd.arg &= ~(0xF << (group * 4));
  256. cmd.arg |= value << (group * 4);
  257. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  258. data.blksz = 64;
  259. data.blocks = 1;
  260. data.flags = MMC_DATA_READ;
  261. data.sg = &sg;
  262. data.sg_len = 1;
  263. sg_init_one(&sg, resp, 64);
  264. mmc_set_data_timeout(&data, card);
  265. mmc_wait_for_req(card->host, &mrq);
  266. if (cmd.error)
  267. return cmd.error;
  268. if (data.error)
  269. return data.error;
  270. return 0;
  271. }
  272. int mmc_app_sd_status(struct mmc_card *card, void *ssr)
  273. {
  274. int err;
  275. struct mmc_request mrq = {NULL};
  276. struct mmc_command cmd = {0};
  277. struct mmc_data data = {0};
  278. struct scatterlist sg;
  279. BUG_ON(!card);
  280. BUG_ON(!card->host);
  281. BUG_ON(!ssr);
  282. /* NOTE: caller guarantees ssr is heap-allocated */
  283. err = mmc_app_cmd(card->host, card);
  284. if (err)
  285. return err;
  286. mrq.cmd = &cmd;
  287. mrq.data = &data;
  288. cmd.opcode = SD_APP_SD_STATUS;
  289. cmd.arg = 0;
  290. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_ADTC;
  291. data.blksz = 64;
  292. data.blocks = 1;
  293. data.flags = MMC_DATA_READ;
  294. data.sg = &sg;
  295. data.sg_len = 1;
  296. sg_init_one(&sg, ssr, 64);
  297. mmc_set_data_timeout(&data, card);
  298. mmc_wait_for_req(card->host, &mrq);
  299. if (cmd.error)
  300. return cmd.error;
  301. if (data.error)
  302. return data.error;
  303. return 0;
  304. }