mmc_ops.c 15 KB

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