mmc_ops.c 7.9 KB

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