mmc_ops.c 12 KB

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