mmc_ops.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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_card_sleepawake(struct mmc_host *host, int sleep)
  49. {
  50. struct mmc_command cmd = {0};
  51. struct mmc_card *card = host->card;
  52. int err;
  53. if (sleep)
  54. mmc_deselect_cards(host);
  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 = {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. static int
  180. mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
  181. u32 opcode, void *buf, unsigned len)
  182. {
  183. struct mmc_request mrq = {NULL};
  184. struct mmc_command cmd = {0};
  185. struct mmc_data data = {0};
  186. struct scatterlist sg;
  187. void *data_buf;
  188. /* dma onto stack is unsafe/nonportable, but callers to this
  189. * routine normally provide temporary on-stack buffers ...
  190. */
  191. data_buf = kmalloc(len, GFP_KERNEL);
  192. if (data_buf == NULL)
  193. return -ENOMEM;
  194. mrq.cmd = &cmd;
  195. mrq.data = &data;
  196. cmd.opcode = opcode;
  197. cmd.arg = 0;
  198. /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
  199. * rely on callers to never use this with "native" calls for reading
  200. * CSD or CID. Native versions of those commands use the R2 type,
  201. * not R1 plus a data block.
  202. */
  203. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  204. data.blksz = len;
  205. data.blocks = 1;
  206. data.flags = MMC_DATA_READ;
  207. data.sg = &sg;
  208. data.sg_len = 1;
  209. sg_init_one(&sg, data_buf, len);
  210. if (opcode == MMC_SEND_CSD || opcode == MMC_SEND_CID) {
  211. /*
  212. * The spec states that CSR and CID accesses have a timeout
  213. * of 64 clock cycles.
  214. */
  215. data.timeout_ns = 0;
  216. data.timeout_clks = 64;
  217. } else
  218. mmc_set_data_timeout(&data, card);
  219. mmc_wait_for_req(host, &mrq);
  220. memcpy(buf, data_buf, len);
  221. kfree(data_buf);
  222. if (cmd.error)
  223. return cmd.error;
  224. if (data.error)
  225. return data.error;
  226. return 0;
  227. }
  228. int mmc_send_csd(struct mmc_card *card, u32 *csd)
  229. {
  230. int ret, i;
  231. if (!mmc_host_is_spi(card->host))
  232. return mmc_send_cxd_native(card->host, card->rca << 16,
  233. csd, MMC_SEND_CSD);
  234. ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd, 16);
  235. if (ret)
  236. return ret;
  237. for (i = 0;i < 4;i++)
  238. csd[i] = be32_to_cpu(csd[i]);
  239. return 0;
  240. }
  241. int mmc_send_cid(struct mmc_host *host, u32 *cid)
  242. {
  243. int ret, i;
  244. if (!mmc_host_is_spi(host)) {
  245. if (!host->card)
  246. return -EINVAL;
  247. return mmc_send_cxd_native(host, host->card->rca << 16,
  248. cid, MMC_SEND_CID);
  249. }
  250. ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid, 16);
  251. if (ret)
  252. return ret;
  253. for (i = 0;i < 4;i++)
  254. cid[i] = be32_to_cpu(cid[i]);
  255. return 0;
  256. }
  257. int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
  258. {
  259. return mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD,
  260. ext_csd, 512);
  261. }
  262. EXPORT_SYMBOL_GPL(mmc_send_ext_csd);
  263. int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
  264. {
  265. struct mmc_command cmd = {0};
  266. int err;
  267. cmd.opcode = MMC_SPI_READ_OCR;
  268. cmd.arg = highcap ? (1 << 30) : 0;
  269. cmd.flags = MMC_RSP_SPI_R3;
  270. err = mmc_wait_for_cmd(host, &cmd, 0);
  271. *ocrp = cmd.resp[1];
  272. return err;
  273. }
  274. int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
  275. {
  276. struct mmc_command cmd = {0};
  277. int err;
  278. cmd.opcode = MMC_SPI_CRC_ON_OFF;
  279. cmd.flags = MMC_RSP_SPI_R1;
  280. cmd.arg = use_crc;
  281. err = mmc_wait_for_cmd(host, &cmd, 0);
  282. if (!err)
  283. host->use_spi_crc = use_crc;
  284. return err;
  285. }
  286. /**
  287. * __mmc_switch - modify EXT_CSD register
  288. * @card: the MMC card associated with the data transfer
  289. * @set: cmd set values
  290. * @index: EXT_CSD register index
  291. * @value: value to program into EXT_CSD register
  292. * @timeout_ms: timeout (ms) for operation performed by register write,
  293. * timeout of zero implies maximum possible timeout
  294. * @use_busy_signal: use the busy signal as response type
  295. * @ignore_timeout: set this flag only for commands which can be HPIed
  296. *
  297. * Modifies the EXT_CSD register for selected card.
  298. */
  299. int __mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
  300. unsigned int timeout_ms, bool use_busy_signal,
  301. bool ignore_timeout)
  302. {
  303. int err;
  304. struct mmc_command cmd = {0};
  305. unsigned long timeout;
  306. u32 status;
  307. BUG_ON(!card);
  308. BUG_ON(!card->host);
  309. cmd.opcode = MMC_SWITCH;
  310. cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  311. (index << 16) |
  312. (value << 8) |
  313. set;
  314. cmd.flags = MMC_CMD_AC;
  315. if (use_busy_signal)
  316. cmd.flags |= MMC_RSP_SPI_R1B | MMC_RSP_R1B;
  317. else
  318. cmd.flags |= MMC_RSP_SPI_R1 | MMC_RSP_R1;
  319. cmd.cmd_timeout_ms = timeout_ms;
  320. cmd.ignore_timeout = ignore_timeout;
  321. err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
  322. if (err)
  323. return err;
  324. /* No need to check card status in case of unblocking command */
  325. if (!use_busy_signal)
  326. return 0;
  327. /* Must check status to be sure of no errors */
  328. timeout = jiffies + msecs_to_jiffies(MMC_OPS_TIMEOUT_MS);
  329. do {
  330. err = mmc_send_status(card, &status);
  331. if (err)
  332. return err;
  333. if (card->host->caps & MMC_CAP_WAIT_WHILE_BUSY)
  334. break;
  335. if (mmc_host_is_spi(card->host))
  336. break;
  337. /* Timeout if the device never leaves the program state. */
  338. if (time_after(jiffies, timeout)) {
  339. pr_err("%s: Card stuck in programming state! %s\n",
  340. mmc_hostname(card->host), __func__);
  341. return -ETIMEDOUT;
  342. }
  343. } while (R1_CURRENT_STATE(status) == R1_STATE_PRG);
  344. if (mmc_host_is_spi(card->host)) {
  345. if (status & R1_SPI_ILLEGAL_COMMAND)
  346. return -EBADMSG;
  347. } else {
  348. if (status & 0xFDFFA000)
  349. pr_warning("%s: unexpected status %#x after "
  350. "switch", mmc_hostname(card->host), status);
  351. if (status & R1_SWITCH_ERROR)
  352. return -EBADMSG;
  353. }
  354. return 0;
  355. }
  356. EXPORT_SYMBOL_GPL(__mmc_switch);
  357. int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
  358. unsigned int timeout_ms)
  359. {
  360. return __mmc_switch(card, set, index, value, timeout_ms, true, false);
  361. }
  362. EXPORT_SYMBOL_GPL(mmc_switch);
  363. int mmc_switch_ignore_timeout(struct mmc_card *card, u8 set, u8 index, u8 value,
  364. unsigned int timeout_ms)
  365. {
  366. return __mmc_switch(card, set, index, value, timeout_ms, true, true);
  367. }
  368. EXPORT_SYMBOL(mmc_switch_ignore_timeout);
  369. int mmc_send_status(struct mmc_card *card, u32 *status)
  370. {
  371. int err;
  372. struct mmc_command cmd = {0};
  373. BUG_ON(!card);
  374. BUG_ON(!card->host);
  375. cmd.opcode = MMC_SEND_STATUS;
  376. if (!mmc_host_is_spi(card->host))
  377. cmd.arg = card->rca << 16;
  378. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  379. err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
  380. if (err)
  381. return err;
  382. /* NOTE: callers are required to understand the difference
  383. * between "native" and SPI format status words!
  384. */
  385. if (status)
  386. *status = cmd.resp[0];
  387. return 0;
  388. }
  389. static int
  390. mmc_send_bus_test(struct mmc_card *card, struct mmc_host *host, u8 opcode,
  391. u8 len)
  392. {
  393. struct mmc_request mrq = {NULL};
  394. struct mmc_command cmd = {0};
  395. struct mmc_data data = {0};
  396. struct scatterlist sg;
  397. u8 *data_buf;
  398. u8 *test_buf;
  399. int i, err;
  400. static u8 testdata_8bit[8] = { 0x55, 0xaa, 0, 0, 0, 0, 0, 0 };
  401. static u8 testdata_4bit[4] = { 0x5a, 0, 0, 0 };
  402. /* dma onto stack is unsafe/nonportable, but callers to this
  403. * routine normally provide temporary on-stack buffers ...
  404. */
  405. data_buf = kmalloc(len, GFP_KERNEL);
  406. if (!data_buf)
  407. return -ENOMEM;
  408. if (len == 8)
  409. test_buf = testdata_8bit;
  410. else if (len == 4)
  411. test_buf = testdata_4bit;
  412. else {
  413. pr_err("%s: Invalid bus_width %d\n",
  414. mmc_hostname(host), len);
  415. kfree(data_buf);
  416. return -EINVAL;
  417. }
  418. if (opcode == MMC_BUS_TEST_W)
  419. memcpy(data_buf, test_buf, len);
  420. mrq.cmd = &cmd;
  421. mrq.data = &data;
  422. cmd.opcode = opcode;
  423. cmd.arg = 0;
  424. /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
  425. * rely on callers to never use this with "native" calls for reading
  426. * CSD or CID. Native versions of those commands use the R2 type,
  427. * not R1 plus a data block.
  428. */
  429. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  430. data.blksz = len;
  431. data.blocks = 1;
  432. if (opcode == MMC_BUS_TEST_R)
  433. data.flags = MMC_DATA_READ;
  434. else
  435. data.flags = MMC_DATA_WRITE;
  436. data.sg = &sg;
  437. data.sg_len = 1;
  438. data.timeout_ns = 1000000;
  439. data.timeout_clks = 0;
  440. sg_init_one(&sg, data_buf, len);
  441. mmc_wait_for_req(host, &mrq);
  442. err = 0;
  443. if (opcode == MMC_BUS_TEST_R) {
  444. for (i = 0; i < len / 4; i++)
  445. if ((test_buf[i] ^ data_buf[i]) != 0xff) {
  446. err = -EIO;
  447. break;
  448. }
  449. }
  450. kfree(data_buf);
  451. if (cmd.error)
  452. return cmd.error;
  453. if (data.error)
  454. return data.error;
  455. return err;
  456. }
  457. int mmc_bus_test(struct mmc_card *card, u8 bus_width)
  458. {
  459. int err, width;
  460. if (bus_width == MMC_BUS_WIDTH_8)
  461. width = 8;
  462. else if (bus_width == MMC_BUS_WIDTH_4)
  463. width = 4;
  464. else if (bus_width == MMC_BUS_WIDTH_1)
  465. return 0; /* no need for test */
  466. else
  467. return -EINVAL;
  468. /*
  469. * Ignore errors from BUS_TEST_W. BUS_TEST_R will fail if there
  470. * is a problem. This improves chances that the test will work.
  471. */
  472. mmc_send_bus_test(card, card->host, MMC_BUS_TEST_W, width);
  473. err = mmc_send_bus_test(card, card->host, MMC_BUS_TEST_R, width);
  474. return err;
  475. }
  476. int mmc_send_hpi_cmd(struct mmc_card *card, u32 *status)
  477. {
  478. struct mmc_command cmd = {0};
  479. unsigned int opcode;
  480. int err;
  481. if (!card->ext_csd.hpi_en) {
  482. pr_warning("%s: Card didn't support HPI command\n",
  483. mmc_hostname(card->host));
  484. return -EINVAL;
  485. }
  486. opcode = card->ext_csd.hpi_cmd;
  487. if (opcode == MMC_STOP_TRANSMISSION)
  488. cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
  489. else if (opcode == MMC_SEND_STATUS)
  490. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  491. cmd.opcode = opcode;
  492. cmd.arg = card->rca << 16 | 1;
  493. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  494. if (err) {
  495. pr_debug("%s: error %d interrupting operation. "
  496. "HPI command response %#x\n", mmc_hostname(card->host),
  497. err, cmd.resp[0]);
  498. return err;
  499. }
  500. if (status)
  501. *status = cmd.resp[0];
  502. return 0;
  503. }