mmc_ops.c 7.7 KB

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