mmc_ops.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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. #define MMC_OPS_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
  21. static inline int __mmc_send_status(struct mmc_card *card, u32 *status,
  22. bool ignore_crc)
  23. {
  24. int err;
  25. struct mmc_command cmd = {0};
  26. BUG_ON(!card);
  27. BUG_ON(!card->host);
  28. cmd.opcode = MMC_SEND_STATUS;
  29. if (!mmc_host_is_spi(card->host))
  30. cmd.arg = card->rca << 16;
  31. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  32. if (ignore_crc)
  33. cmd.flags &= ~MMC_RSP_CRC;
  34. err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
  35. if (err)
  36. return err;
  37. /* NOTE: callers are required to understand the difference
  38. * between "native" and SPI format status words!
  39. */
  40. if (status)
  41. *status = cmd.resp[0];
  42. return 0;
  43. }
  44. int mmc_send_status(struct mmc_card *card, u32 *status)
  45. {
  46. return __mmc_send_status(card, status, false);
  47. }
  48. static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
  49. {
  50. int err;
  51. struct mmc_command cmd = {0};
  52. BUG_ON(!host);
  53. cmd.opcode = MMC_SELECT_CARD;
  54. if (card) {
  55. cmd.arg = card->rca << 16;
  56. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  57. } else {
  58. cmd.arg = 0;
  59. cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
  60. }
  61. err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  62. if (err)
  63. return err;
  64. return 0;
  65. }
  66. int mmc_select_card(struct mmc_card *card)
  67. {
  68. BUG_ON(!card);
  69. return _mmc_select_card(card->host, card);
  70. }
  71. int mmc_deselect_cards(struct mmc_host *host)
  72. {
  73. return _mmc_select_card(host, NULL);
  74. }
  75. int mmc_go_idle(struct mmc_host *host)
  76. {
  77. int err;
  78. struct mmc_command cmd = {0};
  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 accommodate 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. 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 = {0};
  107. int i, err = 0;
  108. BUG_ON(!host);
  109. cmd.opcode = MMC_SEND_OP_COND;
  110. cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
  111. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
  112. for (i = 100; i; i--) {
  113. err = mmc_wait_for_cmd(host, &cmd, 0);
  114. if (err)
  115. break;
  116. /* if we're just probing, do a single pass */
  117. if (ocr == 0)
  118. break;
  119. /* otherwise wait until reset completes */
  120. if (mmc_host_is_spi(host)) {
  121. if (!(cmd.resp[0] & R1_SPI_IDLE))
  122. break;
  123. } else {
  124. if (cmd.resp[0] & MMC_CARD_BUSY)
  125. break;
  126. }
  127. err = -ETIMEDOUT;
  128. mmc_delay(10);
  129. }
  130. if (rocr && !mmc_host_is_spi(host))
  131. *rocr = cmd.resp[0];
  132. return err;
  133. }
  134. int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
  135. {
  136. int err;
  137. struct mmc_command cmd = {0};
  138. BUG_ON(!host);
  139. BUG_ON(!cid);
  140. cmd.opcode = MMC_ALL_SEND_CID;
  141. cmd.arg = 0;
  142. cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
  143. err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  144. if (err)
  145. return err;
  146. memcpy(cid, cmd.resp, sizeof(u32) * 4);
  147. return 0;
  148. }
  149. int mmc_set_relative_addr(struct mmc_card *card)
  150. {
  151. int err;
  152. struct mmc_command cmd = {0};
  153. BUG_ON(!card);
  154. BUG_ON(!card->host);
  155. cmd.opcode = MMC_SET_RELATIVE_ADDR;
  156. cmd.arg = card->rca << 16;
  157. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  158. err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
  159. if (err)
  160. return err;
  161. return 0;
  162. }
  163. static int
  164. mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
  165. {
  166. int err;
  167. struct mmc_command cmd = {0};
  168. BUG_ON(!host);
  169. BUG_ON(!cxd);
  170. cmd.opcode = opcode;
  171. cmd.arg = arg;
  172. cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
  173. err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  174. if (err)
  175. return err;
  176. memcpy(cxd, cmd.resp, sizeof(u32) * 4);
  177. return 0;
  178. }
  179. /*
  180. * NOTE: void *buf, caller for the buf is required to use DMA-capable
  181. * buffer or on-stack buffer (with some overhead in callee).
  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 = {NULL};
  188. struct mmc_command cmd = {0};
  189. struct mmc_data data = {0};
  190. struct scatterlist sg;
  191. void *data_buf;
  192. int is_on_stack;
  193. is_on_stack = object_is_on_stack(buf);
  194. if (is_on_stack) {
  195. /*
  196. * dma onto stack is unsafe/nonportable, but callers to this
  197. * routine normally provide temporary on-stack buffers ...
  198. */
  199. data_buf = kmalloc(len, GFP_KERNEL);
  200. if (!data_buf)
  201. return -ENOMEM;
  202. } else
  203. data_buf = buf;
  204. mrq.cmd = &cmd;
  205. mrq.data = &data;
  206. cmd.opcode = opcode;
  207. cmd.arg = 0;
  208. /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
  209. * rely on callers to never use this with "native" calls for reading
  210. * CSD or CID. Native versions of those commands use the R2 type,
  211. * not R1 plus a data block.
  212. */
  213. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  214. data.blksz = len;
  215. data.blocks = 1;
  216. data.flags = MMC_DATA_READ;
  217. data.sg = &sg;
  218. data.sg_len = 1;
  219. sg_init_one(&sg, data_buf, len);
  220. if (opcode == MMC_SEND_CSD || opcode == MMC_SEND_CID) {
  221. /*
  222. * The spec states that CSR and CID accesses have a timeout
  223. * of 64 clock cycles.
  224. */
  225. data.timeout_ns = 0;
  226. data.timeout_clks = 64;
  227. } else
  228. mmc_set_data_timeout(&data, card);
  229. mmc_wait_for_req(host, &mrq);
  230. if (is_on_stack) {
  231. memcpy(buf, data_buf, len);
  232. kfree(data_buf);
  233. }
  234. if (cmd.error)
  235. return cmd.error;
  236. if (data.error)
  237. return data.error;
  238. return 0;
  239. }
  240. int mmc_send_csd(struct mmc_card *card, u32 *csd)
  241. {
  242. int ret, i;
  243. u32 *csd_tmp;
  244. if (!mmc_host_is_spi(card->host))
  245. return mmc_send_cxd_native(card->host, card->rca << 16,
  246. csd, MMC_SEND_CSD);
  247. csd_tmp = kmalloc(16, GFP_KERNEL);
  248. if (!csd_tmp)
  249. return -ENOMEM;
  250. ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd_tmp, 16);
  251. if (ret)
  252. goto err;
  253. for (i = 0;i < 4;i++)
  254. csd[i] = be32_to_cpu(csd_tmp[i]);
  255. err:
  256. kfree(csd_tmp);
  257. return ret;
  258. }
  259. int mmc_send_cid(struct mmc_host *host, u32 *cid)
  260. {
  261. int ret, i;
  262. u32 *cid_tmp;
  263. if (!mmc_host_is_spi(host)) {
  264. if (!host->card)
  265. return -EINVAL;
  266. return mmc_send_cxd_native(host, host->card->rca << 16,
  267. cid, MMC_SEND_CID);
  268. }
  269. cid_tmp = kmalloc(16, GFP_KERNEL);
  270. if (!cid_tmp)
  271. return -ENOMEM;
  272. ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid_tmp, 16);
  273. if (ret)
  274. goto err;
  275. for (i = 0;i < 4;i++)
  276. cid[i] = be32_to_cpu(cid_tmp[i]);
  277. err:
  278. kfree(cid_tmp);
  279. return ret;
  280. }
  281. int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
  282. {
  283. return mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD,
  284. ext_csd, 512);
  285. }
  286. EXPORT_SYMBOL_GPL(mmc_send_ext_csd);
  287. int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
  288. {
  289. struct mmc_command cmd = {0};
  290. int err;
  291. cmd.opcode = MMC_SPI_READ_OCR;
  292. cmd.arg = highcap ? (1 << 30) : 0;
  293. cmd.flags = MMC_RSP_SPI_R3;
  294. err = mmc_wait_for_cmd(host, &cmd, 0);
  295. *ocrp = cmd.resp[1];
  296. return err;
  297. }
  298. int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
  299. {
  300. struct mmc_command cmd = {0};
  301. int err;
  302. cmd.opcode = MMC_SPI_CRC_ON_OFF;
  303. cmd.flags = MMC_RSP_SPI_R1;
  304. cmd.arg = use_crc;
  305. err = mmc_wait_for_cmd(host, &cmd, 0);
  306. if (!err)
  307. host->use_spi_crc = use_crc;
  308. return err;
  309. }
  310. /**
  311. * __mmc_switch - modify EXT_CSD register
  312. * @card: the MMC card associated with the data transfer
  313. * @set: cmd set values
  314. * @index: EXT_CSD register index
  315. * @value: value to program into EXT_CSD register
  316. * @timeout_ms: timeout (ms) for operation performed by register write,
  317. * timeout of zero implies maximum possible timeout
  318. * @use_busy_signal: use the busy signal as response type
  319. * @send_status: send status cmd to poll for busy
  320. * @ignore_crc: ignore CRC errors when sending status cmd to poll for busy
  321. *
  322. * Modifies the EXT_CSD register for selected card.
  323. */
  324. int __mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
  325. unsigned int timeout_ms, bool use_busy_signal, bool send_status,
  326. bool ignore_crc)
  327. {
  328. struct mmc_host *host = card->host;
  329. int err;
  330. struct mmc_command cmd = {0};
  331. unsigned long timeout;
  332. u32 status = 0;
  333. bool use_r1b_resp = use_busy_signal;
  334. /*
  335. * If the cmd timeout and the max_busy_timeout of the host are both
  336. * specified, let's validate them. A failure means we need to prevent
  337. * the host from doing hw busy detection, which is done by converting
  338. * to a R1 response instead of a R1B.
  339. */
  340. if (timeout_ms && host->max_busy_timeout &&
  341. (timeout_ms > host->max_busy_timeout))
  342. use_r1b_resp = false;
  343. cmd.opcode = MMC_SWITCH;
  344. cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  345. (index << 16) |
  346. (value << 8) |
  347. set;
  348. cmd.flags = MMC_CMD_AC;
  349. if (use_r1b_resp) {
  350. cmd.flags |= MMC_RSP_SPI_R1B | MMC_RSP_R1B;
  351. /*
  352. * A busy_timeout of zero means the host can decide to use
  353. * whatever value it finds suitable.
  354. */
  355. cmd.busy_timeout = timeout_ms;
  356. } else {
  357. cmd.flags |= MMC_RSP_SPI_R1 | MMC_RSP_R1;
  358. }
  359. if (index == EXT_CSD_SANITIZE_START)
  360. cmd.sanitize_busy = true;
  361. err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  362. if (err)
  363. return err;
  364. /* No need to check card status in case of unblocking command */
  365. if (!use_busy_signal)
  366. return 0;
  367. /*
  368. * CRC errors shall only be ignored in cases were CMD13 is used to poll
  369. * to detect busy completion.
  370. */
  371. if ((host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp)
  372. ignore_crc = false;
  373. /* We have an unspecified cmd timeout, use the fallback value. */
  374. if (!timeout_ms)
  375. timeout_ms = MMC_OPS_TIMEOUT_MS;
  376. /* Must check status to be sure of no errors. */
  377. timeout = jiffies + msecs_to_jiffies(timeout_ms);
  378. do {
  379. if (send_status) {
  380. err = __mmc_send_status(card, &status, ignore_crc);
  381. if (err)
  382. return err;
  383. }
  384. if ((host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp)
  385. break;
  386. if (mmc_host_is_spi(host))
  387. break;
  388. /*
  389. * We are not allowed to issue a status command and the host
  390. * does'nt support MMC_CAP_WAIT_WHILE_BUSY, then we can only
  391. * rely on waiting for the stated timeout to be sufficient.
  392. */
  393. if (!send_status) {
  394. mmc_delay(timeout_ms);
  395. return 0;
  396. }
  397. /* Timeout if the device never leaves the program state. */
  398. if (time_after(jiffies, timeout)) {
  399. pr_err("%s: Card stuck in programming state! %s\n",
  400. mmc_hostname(host), __func__);
  401. return -ETIMEDOUT;
  402. }
  403. } while (R1_CURRENT_STATE(status) == R1_STATE_PRG);
  404. if (mmc_host_is_spi(host)) {
  405. if (status & R1_SPI_ILLEGAL_COMMAND)
  406. return -EBADMSG;
  407. } else {
  408. if (status & 0xFDFFA000)
  409. pr_warn("%s: unexpected status %#x after switch\n",
  410. mmc_hostname(host), status);
  411. if (status & R1_SWITCH_ERROR)
  412. return -EBADMSG;
  413. }
  414. return 0;
  415. }
  416. EXPORT_SYMBOL_GPL(__mmc_switch);
  417. int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
  418. unsigned int timeout_ms)
  419. {
  420. return __mmc_switch(card, set, index, value, timeout_ms, true, true,
  421. false);
  422. }
  423. EXPORT_SYMBOL_GPL(mmc_switch);
  424. static int
  425. mmc_send_bus_test(struct mmc_card *card, struct mmc_host *host, u8 opcode,
  426. u8 len)
  427. {
  428. struct mmc_request mrq = {NULL};
  429. struct mmc_command cmd = {0};
  430. struct mmc_data data = {0};
  431. struct scatterlist sg;
  432. u8 *data_buf;
  433. u8 *test_buf;
  434. int i, err;
  435. static u8 testdata_8bit[8] = { 0x55, 0xaa, 0, 0, 0, 0, 0, 0 };
  436. static u8 testdata_4bit[4] = { 0x5a, 0, 0, 0 };
  437. /* dma onto stack is unsafe/nonportable, but callers to this
  438. * routine normally provide temporary on-stack buffers ...
  439. */
  440. data_buf = kmalloc(len, GFP_KERNEL);
  441. if (!data_buf)
  442. return -ENOMEM;
  443. if (len == 8)
  444. test_buf = testdata_8bit;
  445. else if (len == 4)
  446. test_buf = testdata_4bit;
  447. else {
  448. pr_err("%s: Invalid bus_width %d\n",
  449. mmc_hostname(host), len);
  450. kfree(data_buf);
  451. return -EINVAL;
  452. }
  453. if (opcode == MMC_BUS_TEST_W)
  454. memcpy(data_buf, test_buf, len);
  455. mrq.cmd = &cmd;
  456. mrq.data = &data;
  457. cmd.opcode = opcode;
  458. cmd.arg = 0;
  459. /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
  460. * rely on callers to never use this with "native" calls for reading
  461. * CSD or CID. Native versions of those commands use the R2 type,
  462. * not R1 plus a data block.
  463. */
  464. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  465. data.blksz = len;
  466. data.blocks = 1;
  467. if (opcode == MMC_BUS_TEST_R)
  468. data.flags = MMC_DATA_READ;
  469. else
  470. data.flags = MMC_DATA_WRITE;
  471. data.sg = &sg;
  472. data.sg_len = 1;
  473. mmc_set_data_timeout(&data, card);
  474. sg_init_one(&sg, data_buf, len);
  475. mmc_wait_for_req(host, &mrq);
  476. err = 0;
  477. if (opcode == MMC_BUS_TEST_R) {
  478. for (i = 0; i < len / 4; i++)
  479. if ((test_buf[i] ^ data_buf[i]) != 0xff) {
  480. err = -EIO;
  481. break;
  482. }
  483. }
  484. kfree(data_buf);
  485. if (cmd.error)
  486. return cmd.error;
  487. if (data.error)
  488. return data.error;
  489. return err;
  490. }
  491. int mmc_bus_test(struct mmc_card *card, u8 bus_width)
  492. {
  493. int err, width;
  494. if (bus_width == MMC_BUS_WIDTH_8)
  495. width = 8;
  496. else if (bus_width == MMC_BUS_WIDTH_4)
  497. width = 4;
  498. else if (bus_width == MMC_BUS_WIDTH_1)
  499. return 0; /* no need for test */
  500. else
  501. return -EINVAL;
  502. /*
  503. * Ignore errors from BUS_TEST_W. BUS_TEST_R will fail if there
  504. * is a problem. This improves chances that the test will work.
  505. */
  506. mmc_send_bus_test(card, card->host, MMC_BUS_TEST_W, width);
  507. err = mmc_send_bus_test(card, card->host, MMC_BUS_TEST_R, width);
  508. return err;
  509. }
  510. int mmc_send_hpi_cmd(struct mmc_card *card, u32 *status)
  511. {
  512. struct mmc_command cmd = {0};
  513. unsigned int opcode;
  514. int err;
  515. if (!card->ext_csd.hpi) {
  516. pr_warning("%s: Card didn't support HPI command\n",
  517. mmc_hostname(card->host));
  518. return -EINVAL;
  519. }
  520. opcode = card->ext_csd.hpi_cmd;
  521. if (opcode == MMC_STOP_TRANSMISSION)
  522. cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
  523. else if (opcode == MMC_SEND_STATUS)
  524. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  525. cmd.opcode = opcode;
  526. cmd.arg = card->rca << 16 | 1;
  527. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  528. if (err) {
  529. pr_warn("%s: error %d interrupting operation. "
  530. "HPI command response %#x\n", mmc_hostname(card->host),
  531. err, cmd.resp[0]);
  532. return err;
  533. }
  534. if (status)
  535. *status = cmd.resp[0];
  536. return 0;
  537. }