mmc_ops.c 18 KB

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