mmc_ops.c 13 KB

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