mmc_ops.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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/export.h>
  13. #include <linux/types.h>
  14. #include <linux/scatterlist.h>
  15. #include <linux/mmc/host.h>
  16. #include <linux/mmc/card.h>
  17. #include <linux/mmc/mmc.h>
  18. #include "core.h"
  19. #include "mmc_ops.h"
  20. static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
  21. {
  22. int err;
  23. struct mmc_command cmd = {0};
  24. BUG_ON(!host);
  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 = {0};
  50. struct mmc_card *card = host->card;
  51. int err;
  52. if (sleep)
  53. mmc_deselect_cards(host);
  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 = {0};
  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 accommodate 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. cmd.opcode = MMC_GO_IDLE_STATE;
  92. cmd.arg = 0;
  93. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
  94. err = mmc_wait_for_cmd(host, &cmd, 0);
  95. mmc_delay(1);
  96. if (!mmc_host_is_spi(host)) {
  97. mmc_set_chip_select(host, MMC_CS_DONTCARE);
  98. mmc_delay(1);
  99. }
  100. host->use_spi_crc = 0;
  101. return err;
  102. }
  103. int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
  104. {
  105. struct mmc_command cmd = {0};
  106. int i, err = 0;
  107. BUG_ON(!host);
  108. cmd.opcode = MMC_SEND_OP_COND;
  109. cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
  110. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
  111. for (i = 100; i; i--) {
  112. err = mmc_wait_for_cmd(host, &cmd, 0);
  113. if (err)
  114. break;
  115. /* if we're just probing, do a single pass */
  116. if (ocr == 0)
  117. break;
  118. /* otherwise wait until reset completes */
  119. if (mmc_host_is_spi(host)) {
  120. if (!(cmd.resp[0] & R1_SPI_IDLE))
  121. break;
  122. } else {
  123. if (cmd.resp[0] & MMC_CARD_BUSY)
  124. break;
  125. }
  126. err = -ETIMEDOUT;
  127. mmc_delay(10);
  128. }
  129. if (rocr && !mmc_host_is_spi(host))
  130. *rocr = cmd.resp[0];
  131. return err;
  132. }
  133. int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
  134. {
  135. int err;
  136. struct mmc_command cmd = {0};
  137. BUG_ON(!host);
  138. BUG_ON(!cid);
  139. cmd.opcode = MMC_ALL_SEND_CID;
  140. cmd.arg = 0;
  141. cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
  142. err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  143. if (err)
  144. return err;
  145. memcpy(cid, cmd.resp, sizeof(u32) * 4);
  146. return 0;
  147. }
  148. int mmc_set_relative_addr(struct mmc_card *card)
  149. {
  150. int err;
  151. struct mmc_command cmd = {0};
  152. BUG_ON(!card);
  153. BUG_ON(!card->host);
  154. cmd.opcode = MMC_SET_RELATIVE_ADDR;
  155. cmd.arg = card->rca << 16;
  156. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  157. err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
  158. if (err)
  159. return err;
  160. return 0;
  161. }
  162. static int
  163. mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
  164. {
  165. int err;
  166. struct mmc_command cmd = {0};
  167. BUG_ON(!host);
  168. BUG_ON(!cxd);
  169. cmd.opcode = opcode;
  170. cmd.arg = arg;
  171. cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
  172. err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  173. if (err)
  174. return err;
  175. memcpy(cxd, cmd.resp, sizeof(u32) * 4);
  176. return 0;
  177. }
  178. static int
  179. mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
  180. u32 opcode, void *buf, unsigned len)
  181. {
  182. struct mmc_request mrq = {NULL};
  183. struct mmc_command cmd = {0};
  184. struct mmc_data data = {0};
  185. struct scatterlist sg;
  186. void *data_buf;
  187. /* dma onto stack is unsafe/nonportable, but callers to this
  188. * routine normally provide temporary on-stack buffers ...
  189. */
  190. data_buf = kmalloc(len, GFP_KERNEL);
  191. if (data_buf == NULL)
  192. return -ENOMEM;
  193. mrq.cmd = &cmd;
  194. mrq.data = &data;
  195. cmd.opcode = opcode;
  196. cmd.arg = 0;
  197. /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
  198. * rely on callers to never use this with "native" calls for reading
  199. * CSD or CID. Native versions of those commands use the R2 type,
  200. * not R1 plus a data block.
  201. */
  202. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  203. data.blksz = len;
  204. data.blocks = 1;
  205. data.flags = MMC_DATA_READ;
  206. data.sg = &sg;
  207. data.sg_len = 1;
  208. sg_init_one(&sg, data_buf, len);
  209. if (opcode == MMC_SEND_CSD || opcode == MMC_SEND_CID) {
  210. /*
  211. * The spec states that CSR and CID accesses have a timeout
  212. * of 64 clock cycles.
  213. */
  214. data.timeout_ns = 0;
  215. data.timeout_clks = 64;
  216. } else
  217. mmc_set_data_timeout(&data, card);
  218. mmc_wait_for_req(host, &mrq);
  219. memcpy(buf, data_buf, len);
  220. kfree(data_buf);
  221. if (cmd.error)
  222. return cmd.error;
  223. if (data.error)
  224. return data.error;
  225. return 0;
  226. }
  227. int mmc_send_csd(struct mmc_card *card, u32 *csd)
  228. {
  229. int ret, i;
  230. if (!mmc_host_is_spi(card->host))
  231. return mmc_send_cxd_native(card->host, card->rca << 16,
  232. csd, MMC_SEND_CSD);
  233. ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd, 16);
  234. if (ret)
  235. return ret;
  236. for (i = 0;i < 4;i++)
  237. csd[i] = be32_to_cpu(csd[i]);
  238. return 0;
  239. }
  240. int mmc_send_cid(struct mmc_host *host, u32 *cid)
  241. {
  242. int ret, i;
  243. if (!mmc_host_is_spi(host)) {
  244. if (!host->card)
  245. return -EINVAL;
  246. return mmc_send_cxd_native(host, host->card->rca << 16,
  247. cid, MMC_SEND_CID);
  248. }
  249. ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid, 16);
  250. if (ret)
  251. return ret;
  252. for (i = 0;i < 4;i++)
  253. cid[i] = be32_to_cpu(cid[i]);
  254. return 0;
  255. }
  256. int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
  257. {
  258. return mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD,
  259. ext_csd, 512);
  260. }
  261. int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
  262. {
  263. struct mmc_command cmd = {0};
  264. int err;
  265. cmd.opcode = MMC_SPI_READ_OCR;
  266. cmd.arg = highcap ? (1 << 30) : 0;
  267. cmd.flags = MMC_RSP_SPI_R3;
  268. err = mmc_wait_for_cmd(host, &cmd, 0);
  269. *ocrp = cmd.resp[1];
  270. return err;
  271. }
  272. int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
  273. {
  274. struct mmc_command cmd = {0};
  275. int err;
  276. cmd.opcode = MMC_SPI_CRC_ON_OFF;
  277. cmd.flags = MMC_RSP_SPI_R1;
  278. cmd.arg = use_crc;
  279. err = mmc_wait_for_cmd(host, &cmd, 0);
  280. if (!err)
  281. host->use_spi_crc = use_crc;
  282. return err;
  283. }
  284. /**
  285. * mmc_switch - modify EXT_CSD register
  286. * @card: the MMC card associated with the data transfer
  287. * @set: cmd set values
  288. * @index: EXT_CSD register index
  289. * @value: value to program into EXT_CSD register
  290. * @timeout_ms: timeout (ms) for operation performed by register write,
  291. * timeout of zero implies maximum possible timeout
  292. *
  293. * Modifies the EXT_CSD register for selected card.
  294. */
  295. int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
  296. unsigned int timeout_ms)
  297. {
  298. int err;
  299. struct mmc_command cmd = {0};
  300. u32 status;
  301. BUG_ON(!card);
  302. BUG_ON(!card->host);
  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. cmd.cmd_timeout_ms = timeout_ms;
  310. err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
  311. if (err)
  312. return err;
  313. /* Must check status to be sure of no errors */
  314. do {
  315. err = mmc_send_status(card, &status);
  316. if (err)
  317. return err;
  318. if (card->host->caps & MMC_CAP_WAIT_WHILE_BUSY)
  319. break;
  320. if (mmc_host_is_spi(card->host))
  321. break;
  322. } while (R1_CURRENT_STATE(status) == R1_STATE_PRG);
  323. if (mmc_host_is_spi(card->host)) {
  324. if (status & R1_SPI_ILLEGAL_COMMAND)
  325. return -EBADMSG;
  326. } else {
  327. if (status & 0xFDFFA000)
  328. pr_warning("%s: unexpected status %#x after "
  329. "switch", mmc_hostname(card->host), status);
  330. if (status & R1_SWITCH_ERROR)
  331. return -EBADMSG;
  332. }
  333. return 0;
  334. }
  335. EXPORT_SYMBOL_GPL(mmc_switch);
  336. int mmc_send_status(struct mmc_card *card, u32 *status)
  337. {
  338. int err;
  339. struct mmc_command cmd = {0};
  340. BUG_ON(!card);
  341. BUG_ON(!card->host);
  342. cmd.opcode = MMC_SEND_STATUS;
  343. if (!mmc_host_is_spi(card->host))
  344. cmd.arg = card->rca << 16;
  345. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  346. err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
  347. if (err)
  348. return err;
  349. /* NOTE: callers are required to understand the difference
  350. * between "native" and SPI format status words!
  351. */
  352. if (status)
  353. *status = cmd.resp[0];
  354. return 0;
  355. }
  356. static int
  357. mmc_send_bus_test(struct mmc_card *card, struct mmc_host *host, u8 opcode,
  358. u8 len)
  359. {
  360. struct mmc_request mrq = {NULL};
  361. struct mmc_command cmd = {0};
  362. struct mmc_data data = {0};
  363. struct scatterlist sg;
  364. u8 *data_buf;
  365. u8 *test_buf;
  366. int i, err;
  367. static u8 testdata_8bit[8] = { 0x55, 0xaa, 0, 0, 0, 0, 0, 0 };
  368. static u8 testdata_4bit[4] = { 0x5a, 0, 0, 0 };
  369. /* dma onto stack is unsafe/nonportable, but callers to this
  370. * routine normally provide temporary on-stack buffers ...
  371. */
  372. data_buf = kmalloc(len, GFP_KERNEL);
  373. if (!data_buf)
  374. return -ENOMEM;
  375. if (len == 8)
  376. test_buf = testdata_8bit;
  377. else if (len == 4)
  378. test_buf = testdata_4bit;
  379. else {
  380. pr_err("%s: Invalid bus_width %d\n",
  381. mmc_hostname(host), len);
  382. kfree(data_buf);
  383. return -EINVAL;
  384. }
  385. if (opcode == MMC_BUS_TEST_W)
  386. memcpy(data_buf, test_buf, len);
  387. mrq.cmd = &cmd;
  388. mrq.data = &data;
  389. cmd.opcode = opcode;
  390. cmd.arg = 0;
  391. /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
  392. * rely on callers to never use this with "native" calls for reading
  393. * CSD or CID. Native versions of those commands use the R2 type,
  394. * not R1 plus a data block.
  395. */
  396. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  397. data.blksz = len;
  398. data.blocks = 1;
  399. if (opcode == MMC_BUS_TEST_R)
  400. data.flags = MMC_DATA_READ;
  401. else
  402. data.flags = MMC_DATA_WRITE;
  403. data.sg = &sg;
  404. data.sg_len = 1;
  405. sg_init_one(&sg, data_buf, len);
  406. mmc_wait_for_req(host, &mrq);
  407. err = 0;
  408. if (opcode == MMC_BUS_TEST_R) {
  409. for (i = 0; i < len / 4; i++)
  410. if ((test_buf[i] ^ data_buf[i]) != 0xff) {
  411. err = -EIO;
  412. break;
  413. }
  414. }
  415. kfree(data_buf);
  416. if (cmd.error)
  417. return cmd.error;
  418. if (data.error)
  419. return data.error;
  420. return err;
  421. }
  422. int mmc_bus_test(struct mmc_card *card, u8 bus_width)
  423. {
  424. int err, width;
  425. if (bus_width == MMC_BUS_WIDTH_8)
  426. width = 8;
  427. else if (bus_width == MMC_BUS_WIDTH_4)
  428. width = 4;
  429. else if (bus_width == MMC_BUS_WIDTH_1)
  430. return 0; /* no need for test */
  431. else
  432. return -EINVAL;
  433. /*
  434. * Ignore errors from BUS_TEST_W. BUS_TEST_R will fail if there
  435. * is a problem. This improves chances that the test will work.
  436. */
  437. mmc_send_bus_test(card, card->host, MMC_BUS_TEST_W, width);
  438. err = mmc_send_bus_test(card, card->host, MMC_BUS_TEST_R, width);
  439. return err;
  440. }
  441. int mmc_send_hpi_cmd(struct mmc_card *card, u32 *status)
  442. {
  443. struct mmc_command cmd = {0};
  444. unsigned int opcode;
  445. unsigned int flags;
  446. int err;
  447. opcode = card->ext_csd.hpi_cmd;
  448. if (opcode == MMC_STOP_TRANSMISSION)
  449. flags = MMC_RSP_R1 | MMC_CMD_AC;
  450. else if (opcode == MMC_SEND_STATUS)
  451. flags = MMC_RSP_R1 | MMC_CMD_AC;
  452. cmd.opcode = opcode;
  453. cmd.arg = card->rca << 16 | 1;
  454. cmd.flags = flags;
  455. cmd.cmd_timeout_ms = card->ext_csd.out_of_int_time;
  456. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  457. if (err) {
  458. pr_warn("%s: error %d interrupting operation. "
  459. "HPI command response %#x\n", mmc_hostname(card->host),
  460. err, cmd.resp[0]);
  461. return err;
  462. }
  463. if (status)
  464. *status = cmd.resp[0];
  465. return 0;
  466. }