sd_ops.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. 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. err = mmc_app_cmd(host, card);
  69. if (err) {
  70. /* no point in retrying; no APP commands allowed */
  71. if (mmc_host_is_spi(host)) {
  72. if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
  73. break;
  74. }
  75. continue;
  76. }
  77. memset(&mrq, 0, sizeof(struct mmc_request));
  78. memset(cmd->resp, 0, sizeof(cmd->resp));
  79. cmd->retries = 0;
  80. mrq.cmd = cmd;
  81. cmd->data = NULL;
  82. mmc_wait_for_req(host, &mrq);
  83. err = cmd->error;
  84. if (!cmd->error)
  85. break;
  86. /* no point in retrying illegal APP commands */
  87. if (mmc_host_is_spi(host)) {
  88. if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
  89. break;
  90. }
  91. }
  92. return err;
  93. }
  94. EXPORT_SYMBOL(mmc_wait_for_app_cmd);
  95. int mmc_app_set_bus_width(struct mmc_card *card, int width)
  96. {
  97. int err;
  98. struct mmc_command cmd;
  99. BUG_ON(!card);
  100. BUG_ON(!card->host);
  101. memset(&cmd, 0, sizeof(struct mmc_command));
  102. cmd.opcode = SD_APP_SET_BUS_WIDTH;
  103. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  104. switch (width) {
  105. case MMC_BUS_WIDTH_1:
  106. cmd.arg = SD_BUS_WIDTH_1;
  107. break;
  108. case MMC_BUS_WIDTH_4:
  109. cmd.arg = SD_BUS_WIDTH_4;
  110. break;
  111. default:
  112. return -EINVAL;
  113. }
  114. err = mmc_wait_for_app_cmd(card->host, card, &cmd, MMC_CMD_RETRIES);
  115. if (err)
  116. return err;
  117. return 0;
  118. }
  119. int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
  120. {
  121. struct mmc_command cmd;
  122. int i, err = 0;
  123. BUG_ON(!host);
  124. memset(&cmd, 0, sizeof(struct mmc_command));
  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 (rocr && !mmc_host_is_spi(host))
  150. *rocr = cmd.resp[0];
  151. return err;
  152. }
  153. int mmc_send_if_cond(struct mmc_host *host, u32 ocr)
  154. {
  155. struct mmc_command cmd;
  156. int err;
  157. static const u8 test_pattern = 0xAA;
  158. u8 result_pattern;
  159. /*
  160. * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
  161. * before SD_APP_OP_COND. This command will harmlessly fail for
  162. * SD 1.0 cards.
  163. */
  164. cmd.opcode = SD_SEND_IF_COND;
  165. cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern;
  166. cmd.flags = MMC_RSP_SPI_R7 | MMC_RSP_R7 | MMC_CMD_BCR;
  167. err = mmc_wait_for_cmd(host, &cmd, 0);
  168. if (err)
  169. return err;
  170. if (mmc_host_is_spi(host))
  171. result_pattern = cmd.resp[1] & 0xFF;
  172. else
  173. result_pattern = cmd.resp[0] & 0xFF;
  174. if (result_pattern != test_pattern)
  175. return -EIO;
  176. return 0;
  177. }
  178. int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca)
  179. {
  180. int err;
  181. struct mmc_command cmd;
  182. BUG_ON(!host);
  183. BUG_ON(!rca);
  184. memset(&cmd, 0, sizeof(struct mmc_command));
  185. cmd.opcode = SD_SEND_RELATIVE_ADDR;
  186. cmd.arg = 0;
  187. cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
  188. err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  189. if (err)
  190. return err;
  191. *rca = cmd.resp[0] >> 16;
  192. return 0;
  193. }
  194. int mmc_app_send_scr(struct mmc_card *card, u32 *scr)
  195. {
  196. int err;
  197. struct mmc_request mrq;
  198. struct mmc_command cmd;
  199. struct mmc_data data;
  200. struct scatterlist sg;
  201. void *data_buf;
  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. /* dma onto stack is unsafe/nonportable, but callers to this
  210. * routine normally provide temporary on-stack buffers ...
  211. */
  212. data_buf = kmalloc(sizeof(card->raw_scr), GFP_KERNEL);
  213. if (data_buf == NULL)
  214. return -ENOMEM;
  215. memset(&mrq, 0, sizeof(struct mmc_request));
  216. memset(&cmd, 0, sizeof(struct mmc_command));
  217. memset(&data, 0, sizeof(struct mmc_data));
  218. mrq.cmd = &cmd;
  219. mrq.data = &data;
  220. cmd.opcode = SD_APP_SEND_SCR;
  221. cmd.arg = 0;
  222. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  223. data.blksz = 8;
  224. data.blocks = 1;
  225. data.flags = MMC_DATA_READ;
  226. data.sg = &sg;
  227. data.sg_len = 1;
  228. sg_init_one(&sg, data_buf, 8);
  229. mmc_set_data_timeout(&data, card);
  230. mmc_wait_for_req(card->host, &mrq);
  231. memcpy(scr, data_buf, sizeof(card->raw_scr));
  232. kfree(data_buf);
  233. if (cmd.error)
  234. return cmd.error;
  235. if (data.error)
  236. return data.error;
  237. scr[0] = be32_to_cpu(scr[0]);
  238. scr[1] = be32_to_cpu(scr[1]);
  239. return 0;
  240. }
  241. int mmc_sd_switch(struct mmc_card *card, int mode, int group,
  242. u8 value, u8 *resp)
  243. {
  244. struct mmc_request mrq;
  245. struct mmc_command cmd;
  246. struct mmc_data data;
  247. struct scatterlist sg;
  248. BUG_ON(!card);
  249. BUG_ON(!card->host);
  250. /* NOTE: caller guarantees resp is heap-allocated */
  251. mode = !!mode;
  252. value &= 0xF;
  253. memset(&mrq, 0, sizeof(struct mmc_request));
  254. memset(&cmd, 0, sizeof(struct mmc_command));
  255. memset(&data, 0, sizeof(struct mmc_data));
  256. mrq.cmd = &cmd;
  257. mrq.data = &data;
  258. cmd.opcode = SD_SWITCH;
  259. cmd.arg = mode << 31 | 0x00FFFFFF;
  260. cmd.arg &= ~(0xF << (group * 4));
  261. cmd.arg |= value << (group * 4);
  262. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  263. data.blksz = 64;
  264. data.blocks = 1;
  265. data.flags = MMC_DATA_READ;
  266. data.sg = &sg;
  267. data.sg_len = 1;
  268. sg_init_one(&sg, resp, 64);
  269. mmc_set_data_timeout(&data, card);
  270. mmc_wait_for_req(card->host, &mrq);
  271. if (cmd.error)
  272. return cmd.error;
  273. if (data.error)
  274. return data.error;
  275. return 0;
  276. }
  277. int mmc_app_sd_status(struct mmc_card *card, void *ssr)
  278. {
  279. int err;
  280. struct mmc_request mrq;
  281. struct mmc_command cmd;
  282. struct mmc_data data;
  283. struct scatterlist sg;
  284. BUG_ON(!card);
  285. BUG_ON(!card->host);
  286. BUG_ON(!ssr);
  287. /* NOTE: caller guarantees ssr is heap-allocated */
  288. err = mmc_app_cmd(card->host, card);
  289. if (err)
  290. return err;
  291. memset(&mrq, 0, sizeof(struct mmc_request));
  292. memset(&cmd, 0, sizeof(struct mmc_command));
  293. memset(&data, 0, sizeof(struct mmc_data));
  294. mrq.cmd = &cmd;
  295. mrq.data = &data;
  296. cmd.opcode = SD_APP_SD_STATUS;
  297. cmd.arg = 0;
  298. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_ADTC;
  299. data.blksz = 64;
  300. data.blocks = 1;
  301. data.flags = MMC_DATA_READ;
  302. data.sg = &sg;
  303. data.sg_len = 1;
  304. sg_init_one(&sg, ssr, 64);
  305. mmc_set_data_timeout(&data, card);
  306. mmc_wait_for_req(card->host, &mrq);
  307. if (cmd.error)
  308. return cmd.error;
  309. if (data.error)
  310. return data.error;
  311. return 0;
  312. }