mmc_ops.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. /*
  190. * The spec states that CSR and CID accesses have a timeout
  191. * of 64 clock cycles.
  192. */
  193. data.timeout_ns = 0;
  194. data.timeout_clks = 64;
  195. mmc_wait_for_req(host, &mrq);
  196. memcpy(buf, data_buf, len);
  197. kfree(data_buf);
  198. if (cmd.error)
  199. return cmd.error;
  200. if (data.error)
  201. return data.error;
  202. return 0;
  203. }
  204. int mmc_send_csd(struct mmc_card *card, u32 *csd)
  205. {
  206. int ret, i;
  207. if (!mmc_host_is_spi(card->host))
  208. return mmc_send_cxd_native(card->host, card->rca << 16,
  209. csd, MMC_SEND_CSD);
  210. ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd, 16);
  211. if (ret)
  212. return ret;
  213. for (i = 0;i < 4;i++)
  214. csd[i] = be32_to_cpu(csd[i]);
  215. return 0;
  216. }
  217. int mmc_send_cid(struct mmc_host *host, u32 *cid)
  218. {
  219. int ret, i;
  220. if (!mmc_host_is_spi(host)) {
  221. if (!host->card)
  222. return -EINVAL;
  223. return mmc_send_cxd_native(host, host->card->rca << 16,
  224. cid, MMC_SEND_CID);
  225. }
  226. ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid, 16);
  227. if (ret)
  228. return ret;
  229. for (i = 0;i < 4;i++)
  230. cid[i] = be32_to_cpu(cid[i]);
  231. return 0;
  232. }
  233. int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
  234. {
  235. return mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD,
  236. ext_csd, 512);
  237. }
  238. int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
  239. {
  240. struct mmc_command cmd;
  241. int err;
  242. memset(&cmd, 0, sizeof(struct mmc_command));
  243. cmd.opcode = MMC_SPI_READ_OCR;
  244. cmd.arg = highcap ? (1 << 30) : 0;
  245. cmd.flags = MMC_RSP_SPI_R3;
  246. err = mmc_wait_for_cmd(host, &cmd, 0);
  247. *ocrp = cmd.resp[1];
  248. return err;
  249. }
  250. int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
  251. {
  252. struct mmc_command cmd;
  253. int err;
  254. memset(&cmd, 0, sizeof(struct mmc_command));
  255. cmd.opcode = MMC_SPI_CRC_ON_OFF;
  256. cmd.flags = MMC_RSP_SPI_R1;
  257. cmd.arg = use_crc;
  258. err = mmc_wait_for_cmd(host, &cmd, 0);
  259. if (!err)
  260. host->use_spi_crc = use_crc;
  261. return err;
  262. }
  263. int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value)
  264. {
  265. int err;
  266. struct mmc_command cmd;
  267. BUG_ON(!card);
  268. BUG_ON(!card->host);
  269. memset(&cmd, 0, sizeof(struct mmc_command));
  270. cmd.opcode = MMC_SWITCH;
  271. cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  272. (index << 16) |
  273. (value << 8) |
  274. set;
  275. cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  276. err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
  277. if (err)
  278. return err;
  279. return 0;
  280. }
  281. int mmc_send_status(struct mmc_card *card, u32 *status)
  282. {
  283. int err;
  284. struct mmc_command cmd;
  285. BUG_ON(!card);
  286. BUG_ON(!card->host);
  287. memset(&cmd, 0, sizeof(struct mmc_command));
  288. cmd.opcode = MMC_SEND_STATUS;
  289. if (!mmc_host_is_spi(card->host))
  290. cmd.arg = card->rca << 16;
  291. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  292. err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
  293. if (err)
  294. return err;
  295. /* NOTE: callers are required to understand the difference
  296. * between "native" and SPI format status words!
  297. */
  298. if (status)
  299. *status = cmd.resp[0];
  300. return 0;
  301. }