mmc_ops.c 18 KB

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