mmc_ops.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * linux/drivers/mmc/core/mmc_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 "core.h"
  18. #include "mmc_ops.h"
  19. static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
  20. {
  21. int err;
  22. struct mmc_command cmd;
  23. BUG_ON(!host);
  24. memset(&cmd, 0, sizeof(struct mmc_command));
  25. cmd.opcode = MMC_SELECT_CARD;
  26. if (card) {
  27. cmd.arg = card->rca << 16;
  28. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  29. } else {
  30. cmd.arg = 0;
  31. cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
  32. }
  33. err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  34. if (err)
  35. return err;
  36. return 0;
  37. }
  38. int mmc_select_card(struct mmc_card *card)
  39. {
  40. BUG_ON(!card);
  41. return _mmc_select_card(card->host, card);
  42. }
  43. int mmc_deselect_cards(struct mmc_host *host)
  44. {
  45. return _mmc_select_card(host, NULL);
  46. }
  47. int mmc_go_idle(struct mmc_host *host)
  48. {
  49. int err;
  50. struct mmc_command cmd;
  51. /*
  52. * Non-SPI hosts need to prevent chipselect going active during
  53. * GO_IDLE; that would put chips into SPI mode. Remind them of
  54. * that in case of hardware that won't pull up DAT3/nCS otherwise.
  55. *
  56. * SPI hosts ignore ios.chip_select; it's managed according to
  57. * rules that must accomodate non-MMC slaves which this layer
  58. * won't even know about.
  59. */
  60. if (!mmc_host_is_spi(host)) {
  61. mmc_set_chip_select(host, MMC_CS_HIGH);
  62. mmc_delay(1);
  63. }
  64. memset(&cmd, 0, sizeof(struct mmc_command));
  65. cmd.opcode = MMC_GO_IDLE_STATE;
  66. cmd.arg = 0;
  67. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
  68. err = mmc_wait_for_cmd(host, &cmd, 0);
  69. mmc_delay(1);
  70. if (!mmc_host_is_spi(host)) {
  71. mmc_set_chip_select(host, MMC_CS_DONTCARE);
  72. mmc_delay(1);
  73. }
  74. host->use_spi_crc = 0;
  75. return err;
  76. }
  77. int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
  78. {
  79. struct mmc_command cmd;
  80. int i, err = 0;
  81. BUG_ON(!host);
  82. memset(&cmd, 0, sizeof(struct mmc_command));
  83. cmd.opcode = MMC_SEND_OP_COND;
  84. cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
  85. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
  86. for (i = 100; i; i--) {
  87. err = mmc_wait_for_cmd(host, &cmd, 0);
  88. if (err)
  89. break;
  90. /* if we're just probing, do a single pass */
  91. if (ocr == 0)
  92. break;
  93. /* otherwise wait until reset completes */
  94. if (mmc_host_is_spi(host)) {
  95. if (!(cmd.resp[0] & R1_SPI_IDLE))
  96. break;
  97. } else {
  98. if (cmd.resp[0] & MMC_CARD_BUSY)
  99. break;
  100. }
  101. err = -ETIMEDOUT;
  102. mmc_delay(10);
  103. }
  104. if (rocr && !mmc_host_is_spi(host))
  105. *rocr = cmd.resp[0];
  106. return err;
  107. }
  108. int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
  109. {
  110. int err;
  111. struct mmc_command cmd;
  112. BUG_ON(!host);
  113. BUG_ON(!cid);
  114. memset(&cmd, 0, sizeof(struct mmc_command));
  115. cmd.opcode = MMC_ALL_SEND_CID;
  116. cmd.arg = 0;
  117. cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
  118. err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  119. if (err)
  120. return err;
  121. memcpy(cid, cmd.resp, sizeof(u32) * 4);
  122. return 0;
  123. }
  124. int mmc_set_relative_addr(struct mmc_card *card)
  125. {
  126. int err;
  127. struct mmc_command cmd;
  128. BUG_ON(!card);
  129. BUG_ON(!card->host);
  130. memset(&cmd, 0, sizeof(struct mmc_command));
  131. cmd.opcode = MMC_SET_RELATIVE_ADDR;
  132. cmd.arg = card->rca << 16;
  133. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  134. err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
  135. if (err)
  136. return err;
  137. return 0;
  138. }
  139. static int
  140. mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
  141. {
  142. int err;
  143. struct mmc_command cmd;
  144. BUG_ON(!host);
  145. BUG_ON(!cxd);
  146. memset(&cmd, 0, sizeof(struct mmc_command));
  147. cmd.opcode = opcode;
  148. cmd.arg = arg;
  149. cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
  150. err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  151. if (err)
  152. return err;
  153. memcpy(cxd, cmd.resp, sizeof(u32) * 4);
  154. return 0;
  155. }
  156. static int
  157. mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
  158. u32 opcode, void *buf, unsigned len)
  159. {
  160. struct mmc_request mrq;
  161. struct mmc_command cmd;
  162. struct mmc_data data;
  163. struct scatterlist sg;
  164. void *data_buf;
  165. /* dma onto stack is unsafe/nonportable, but callers to this
  166. * routine normally provide temporary on-stack buffers ...
  167. */
  168. data_buf = kmalloc(len, GFP_KERNEL);
  169. if (data_buf == NULL)
  170. return -ENOMEM;
  171. memset(&mrq, 0, sizeof(struct mmc_request));
  172. memset(&cmd, 0, sizeof(struct mmc_command));
  173. memset(&data, 0, sizeof(struct mmc_data));
  174. mrq.cmd = &cmd;
  175. mrq.data = &data;
  176. cmd.opcode = opcode;
  177. cmd.arg = 0;
  178. /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
  179. * rely on callers to never use this with "native" calls for reading
  180. * CSD or CID. Native versions of those commands use the R2 type,
  181. * not R1 plus a data block.
  182. */
  183. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  184. data.blksz = len;
  185. data.blocks = 1;
  186. data.flags = MMC_DATA_READ;
  187. data.sg = &sg;
  188. data.sg_len = 1;
  189. sg_init_one(&sg, data_buf, len);
  190. if (card)
  191. mmc_set_data_timeout(&data, card);
  192. mmc_wait_for_req(host, &mrq);
  193. memcpy(buf, data_buf, len);
  194. kfree(data_buf);
  195. if (cmd.error)
  196. return cmd.error;
  197. if (data.error)
  198. return data.error;
  199. return 0;
  200. }
  201. int mmc_send_csd(struct mmc_card *card, u32 *csd)
  202. {
  203. if (!mmc_host_is_spi(card->host))
  204. return mmc_send_cxd_native(card->host, card->rca << 16,
  205. csd, MMC_SEND_CSD);
  206. return mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd, 16);
  207. }
  208. int mmc_send_cid(struct mmc_host *host, u32 *cid)
  209. {
  210. if (!mmc_host_is_spi(host)) {
  211. if (!host->card)
  212. return -EINVAL;
  213. return mmc_send_cxd_native(host, host->card->rca << 16,
  214. cid, MMC_SEND_CID);
  215. }
  216. return mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid, 16);
  217. }
  218. int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
  219. {
  220. return mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD,
  221. ext_csd, 512);
  222. }
  223. int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
  224. {
  225. struct mmc_command cmd;
  226. int err;
  227. memset(&cmd, 0, sizeof(struct mmc_command));
  228. cmd.opcode = MMC_SPI_READ_OCR;
  229. cmd.arg = highcap ? (1 << 30) : 0;
  230. cmd.flags = MMC_RSP_SPI_R3;
  231. err = mmc_wait_for_cmd(host, &cmd, 0);
  232. *ocrp = cmd.resp[1];
  233. return err;
  234. }
  235. int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
  236. {
  237. struct mmc_command cmd;
  238. int err;
  239. memset(&cmd, 0, sizeof(struct mmc_command));
  240. cmd.opcode = MMC_SPI_CRC_ON_OFF;
  241. cmd.flags = MMC_RSP_SPI_R1;
  242. cmd.arg = use_crc;
  243. err = mmc_wait_for_cmd(host, &cmd, 0);
  244. if (!err)
  245. host->use_spi_crc = use_crc;
  246. return err;
  247. }
  248. int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value)
  249. {
  250. int err;
  251. struct mmc_command cmd;
  252. BUG_ON(!card);
  253. BUG_ON(!card->host);
  254. memset(&cmd, 0, sizeof(struct mmc_command));
  255. cmd.opcode = MMC_SWITCH;
  256. cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  257. (index << 16) |
  258. (value << 8) |
  259. set;
  260. cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  261. err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
  262. if (err)
  263. return err;
  264. return 0;
  265. }
  266. int mmc_send_status(struct mmc_card *card, u32 *status)
  267. {
  268. int err;
  269. struct mmc_command cmd;
  270. BUG_ON(!card);
  271. BUG_ON(!card->host);
  272. memset(&cmd, 0, sizeof(struct mmc_command));
  273. cmd.opcode = MMC_SEND_STATUS;
  274. if (!mmc_host_is_spi(card->host))
  275. cmd.arg = card->rca << 16;
  276. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  277. err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
  278. if (err)
  279. return err;
  280. /* NOTE: callers are required to understand the difference
  281. * between "native" and SPI format status words!
  282. */
  283. if (status)
  284. *status = cmd.resp[0];
  285. return 0;
  286. }