mmc_ops.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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_card_sleepawake(struct mmc_host *host, int sleep)
  47. {
  48. struct mmc_command cmd;
  49. struct mmc_card *card = host->card;
  50. int err;
  51. if (sleep)
  52. mmc_deselect_cards(host);
  53. memset(&cmd, 0, sizeof(struct mmc_command));
  54. cmd.opcode = MMC_SLEEP_AWAKE;
  55. cmd.arg = card->rca << 16;
  56. if (sleep)
  57. cmd.arg |= 1 << 15;
  58. cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
  59. err = mmc_wait_for_cmd(host, &cmd, 0);
  60. if (err)
  61. return err;
  62. /*
  63. * If the host does not wait while the card signals busy, then we will
  64. * will have to wait the sleep/awake timeout. Note, we cannot use the
  65. * SEND_STATUS command to poll the status because that command (and most
  66. * others) is invalid while the card sleeps.
  67. */
  68. if (!(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
  69. mmc_delay(DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000));
  70. if (!sleep)
  71. err = mmc_select_card(card);
  72. return err;
  73. }
  74. int mmc_go_idle(struct mmc_host *host)
  75. {
  76. int err;
  77. struct mmc_command cmd;
  78. /*
  79. * Non-SPI hosts need to prevent chipselect going active during
  80. * GO_IDLE; that would put chips into SPI mode. Remind them of
  81. * that in case of hardware that won't pull up DAT3/nCS otherwise.
  82. *
  83. * SPI hosts ignore ios.chip_select; it's managed according to
  84. * rules that must accomodate non-MMC slaves which this layer
  85. * won't even know about.
  86. */
  87. if (!mmc_host_is_spi(host)) {
  88. mmc_set_chip_select(host, MMC_CS_HIGH);
  89. mmc_delay(1);
  90. }
  91. memset(&cmd, 0, sizeof(struct mmc_command));
  92. cmd.opcode = MMC_GO_IDLE_STATE;
  93. cmd.arg = 0;
  94. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
  95. err = mmc_wait_for_cmd(host, &cmd, 0);
  96. mmc_delay(1);
  97. if (!mmc_host_is_spi(host)) {
  98. mmc_set_chip_select(host, MMC_CS_DONTCARE);
  99. mmc_delay(1);
  100. }
  101. host->use_spi_crc = 0;
  102. return err;
  103. }
  104. int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
  105. {
  106. struct mmc_command cmd;
  107. int i, err = 0;
  108. BUG_ON(!host);
  109. memset(&cmd, 0, sizeof(struct mmc_command));
  110. cmd.opcode = MMC_SEND_OP_COND;
  111. cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
  112. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
  113. for (i = 100; i; i--) {
  114. err = mmc_wait_for_cmd(host, &cmd, 0);
  115. if (err)
  116. break;
  117. /* if we're just probing, do a single pass */
  118. if (ocr == 0)
  119. break;
  120. /* otherwise wait until reset completes */
  121. if (mmc_host_is_spi(host)) {
  122. if (!(cmd.resp[0] & R1_SPI_IDLE))
  123. break;
  124. } else {
  125. if (cmd.resp[0] & MMC_CARD_BUSY)
  126. break;
  127. }
  128. err = -ETIMEDOUT;
  129. mmc_delay(10);
  130. }
  131. if (rocr && !mmc_host_is_spi(host))
  132. *rocr = cmd.resp[0];
  133. return err;
  134. }
  135. int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
  136. {
  137. int err;
  138. struct mmc_command cmd;
  139. BUG_ON(!host);
  140. BUG_ON(!cid);
  141. memset(&cmd, 0, sizeof(struct mmc_command));
  142. cmd.opcode = MMC_ALL_SEND_CID;
  143. cmd.arg = 0;
  144. cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
  145. err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  146. if (err)
  147. return err;
  148. memcpy(cid, cmd.resp, sizeof(u32) * 4);
  149. return 0;
  150. }
  151. int mmc_set_relative_addr(struct mmc_card *card)
  152. {
  153. int err;
  154. struct mmc_command cmd;
  155. BUG_ON(!card);
  156. BUG_ON(!card->host);
  157. memset(&cmd, 0, sizeof(struct mmc_command));
  158. cmd.opcode = MMC_SET_RELATIVE_ADDR;
  159. cmd.arg = card->rca << 16;
  160. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  161. err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
  162. if (err)
  163. return err;
  164. return 0;
  165. }
  166. static int
  167. mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
  168. {
  169. int err;
  170. struct mmc_command cmd;
  171. BUG_ON(!host);
  172. BUG_ON(!cxd);
  173. memset(&cmd, 0, sizeof(struct mmc_command));
  174. cmd.opcode = opcode;
  175. cmd.arg = arg;
  176. cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
  177. err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  178. if (err)
  179. return err;
  180. memcpy(cxd, cmd.resp, sizeof(u32) * 4);
  181. return 0;
  182. }
  183. static int
  184. mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
  185. u32 opcode, void *buf, unsigned len)
  186. {
  187. struct mmc_request mrq;
  188. struct mmc_command cmd;
  189. struct mmc_data data;
  190. struct scatterlist sg;
  191. void *data_buf;
  192. /* dma onto stack is unsafe/nonportable, but callers to this
  193. * routine normally provide temporary on-stack buffers ...
  194. */
  195. data_buf = kmalloc(len, GFP_KERNEL);
  196. if (data_buf == NULL)
  197. return -ENOMEM;
  198. memset(&mrq, 0, sizeof(struct mmc_request));
  199. memset(&cmd, 0, sizeof(struct mmc_command));
  200. memset(&data, 0, sizeof(struct mmc_data));
  201. mrq.cmd = &cmd;
  202. mrq.data = &data;
  203. cmd.opcode = opcode;
  204. cmd.arg = 0;
  205. /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
  206. * rely on callers to never use this with "native" calls for reading
  207. * CSD or CID. Native versions of those commands use the R2 type,
  208. * not R1 plus a data block.
  209. */
  210. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  211. data.blksz = len;
  212. data.blocks = 1;
  213. data.flags = MMC_DATA_READ;
  214. data.sg = &sg;
  215. data.sg_len = 1;
  216. sg_init_one(&sg, data_buf, len);
  217. if (opcode == MMC_SEND_CSD || opcode == MMC_SEND_CID) {
  218. /*
  219. * The spec states that CSR and CID accesses have a timeout
  220. * of 64 clock cycles.
  221. */
  222. data.timeout_ns = 0;
  223. data.timeout_clks = 64;
  224. } else
  225. mmc_set_data_timeout(&data, card);
  226. mmc_wait_for_req(host, &mrq);
  227. memcpy(buf, data_buf, len);
  228. kfree(data_buf);
  229. if (cmd.error)
  230. return cmd.error;
  231. if (data.error)
  232. return data.error;
  233. return 0;
  234. }
  235. int mmc_send_csd(struct mmc_card *card, u32 *csd)
  236. {
  237. int ret, i;
  238. if (!mmc_host_is_spi(card->host))
  239. return mmc_send_cxd_native(card->host, card->rca << 16,
  240. csd, MMC_SEND_CSD);
  241. ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd, 16);
  242. if (ret)
  243. return ret;
  244. for (i = 0;i < 4;i++)
  245. csd[i] = be32_to_cpu(csd[i]);
  246. return 0;
  247. }
  248. int mmc_send_cid(struct mmc_host *host, u32 *cid)
  249. {
  250. int ret, i;
  251. if (!mmc_host_is_spi(host)) {
  252. if (!host->card)
  253. return -EINVAL;
  254. return mmc_send_cxd_native(host, host->card->rca << 16,
  255. cid, MMC_SEND_CID);
  256. }
  257. ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid, 16);
  258. if (ret)
  259. return ret;
  260. for (i = 0;i < 4;i++)
  261. cid[i] = be32_to_cpu(cid[i]);
  262. return 0;
  263. }
  264. int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
  265. {
  266. return mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD,
  267. ext_csd, 512);
  268. }
  269. int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
  270. {
  271. struct mmc_command cmd;
  272. int err;
  273. memset(&cmd, 0, sizeof(struct mmc_command));
  274. cmd.opcode = MMC_SPI_READ_OCR;
  275. cmd.arg = highcap ? (1 << 30) : 0;
  276. cmd.flags = MMC_RSP_SPI_R3;
  277. err = mmc_wait_for_cmd(host, &cmd, 0);
  278. *ocrp = cmd.resp[1];
  279. return err;
  280. }
  281. int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
  282. {
  283. struct mmc_command cmd;
  284. int err;
  285. memset(&cmd, 0, sizeof(struct mmc_command));
  286. cmd.opcode = MMC_SPI_CRC_ON_OFF;
  287. cmd.flags = MMC_RSP_SPI_R1;
  288. cmd.arg = use_crc;
  289. err = mmc_wait_for_cmd(host, &cmd, 0);
  290. if (!err)
  291. host->use_spi_crc = use_crc;
  292. return err;
  293. }
  294. int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value)
  295. {
  296. int err;
  297. struct mmc_command cmd;
  298. u32 status;
  299. BUG_ON(!card);
  300. BUG_ON(!card->host);
  301. memset(&cmd, 0, sizeof(struct mmc_command));
  302. cmd.opcode = MMC_SWITCH;
  303. cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  304. (index << 16) |
  305. (value << 8) |
  306. set;
  307. cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  308. err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
  309. if (err)
  310. return err;
  311. /* Must check status to be sure of no errors */
  312. do {
  313. err = mmc_send_status(card, &status);
  314. if (err)
  315. return err;
  316. if (card->host->caps & MMC_CAP_WAIT_WHILE_BUSY)
  317. break;
  318. if (mmc_host_is_spi(card->host))
  319. break;
  320. } while (R1_CURRENT_STATE(status) == 7);
  321. if (mmc_host_is_spi(card->host)) {
  322. if (status & R1_SPI_ILLEGAL_COMMAND)
  323. return -EBADMSG;
  324. } else {
  325. if (status & 0xFDFFA000)
  326. printk(KERN_WARNING "%s: unexpected status %#x after "
  327. "switch", mmc_hostname(card->host), status);
  328. if (status & R1_SWITCH_ERROR)
  329. return -EBADMSG;
  330. }
  331. return 0;
  332. }
  333. int mmc_send_status(struct mmc_card *card, u32 *status)
  334. {
  335. int err;
  336. struct mmc_command cmd;
  337. BUG_ON(!card);
  338. BUG_ON(!card->host);
  339. memset(&cmd, 0, sizeof(struct mmc_command));
  340. cmd.opcode = MMC_SEND_STATUS;
  341. if (!mmc_host_is_spi(card->host))
  342. cmd.arg = card->rca << 16;
  343. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  344. err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
  345. if (err)
  346. return err;
  347. /* NOTE: callers are required to understand the difference
  348. * between "native" and SPI format status words!
  349. */
  350. if (status)
  351. *status = cmd.resp[0];
  352. return 0;
  353. }