mmc_ops.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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 "card.h"
  20. #include "host.h"
  21. #include "mmc_ops.h"
  22. #define MMC_OPS_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
  23. static const u8 tuning_blk_pattern_4bit[] = {
  24. 0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc,
  25. 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
  26. 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
  27. 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
  28. 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
  29. 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
  30. 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
  31. 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde,
  32. };
  33. static const u8 tuning_blk_pattern_8bit[] = {
  34. 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
  35. 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
  36. 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
  37. 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
  38. 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
  39. 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
  40. 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
  41. 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
  42. 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
  43. 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
  44. 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
  45. 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
  46. 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
  47. 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
  48. 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
  49. 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
  50. };
  51. int __mmc_send_status(struct mmc_card *card, u32 *status, unsigned int retries)
  52. {
  53. int err;
  54. struct mmc_command cmd = {};
  55. cmd.opcode = MMC_SEND_STATUS;
  56. if (!mmc_host_is_spi(card->host))
  57. cmd.arg = card->rca << 16;
  58. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  59. err = mmc_wait_for_cmd(card->host, &cmd, retries);
  60. if (err)
  61. return err;
  62. /* NOTE: callers are required to understand the difference
  63. * between "native" and SPI format status words!
  64. */
  65. if (status)
  66. *status = cmd.resp[0];
  67. return 0;
  68. }
  69. EXPORT_SYMBOL_GPL(__mmc_send_status);
  70. int mmc_send_status(struct mmc_card *card, u32 *status)
  71. {
  72. return __mmc_send_status(card, status, MMC_CMD_RETRIES);
  73. }
  74. static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
  75. {
  76. struct mmc_command cmd = {};
  77. cmd.opcode = MMC_SELECT_CARD;
  78. if (card) {
  79. cmd.arg = card->rca << 16;
  80. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  81. } else {
  82. cmd.arg = 0;
  83. cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
  84. }
  85. return mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  86. }
  87. int mmc_select_card(struct mmc_card *card)
  88. {
  89. return _mmc_select_card(card->host, card);
  90. }
  91. int mmc_deselect_cards(struct mmc_host *host)
  92. {
  93. return _mmc_select_card(host, NULL);
  94. }
  95. /*
  96. * Write the value specified in the device tree or board code into the optional
  97. * 16 bit Driver Stage Register. This can be used to tune raise/fall times and
  98. * drive strength of the DAT and CMD outputs. The actual meaning of a given
  99. * value is hardware dependant.
  100. * The presence of the DSR register can be determined from the CSD register,
  101. * bit 76.
  102. */
  103. int mmc_set_dsr(struct mmc_host *host)
  104. {
  105. struct mmc_command cmd = {};
  106. cmd.opcode = MMC_SET_DSR;
  107. cmd.arg = (host->dsr << 16) | 0xffff;
  108. cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
  109. return mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  110. }
  111. int mmc_go_idle(struct mmc_host *host)
  112. {
  113. int err;
  114. struct mmc_command cmd = {};
  115. /*
  116. * Non-SPI hosts need to prevent chipselect going active during
  117. * GO_IDLE; that would put chips into SPI mode. Remind them of
  118. * that in case of hardware that won't pull up DAT3/nCS otherwise.
  119. *
  120. * SPI hosts ignore ios.chip_select; it's managed according to
  121. * rules that must accommodate non-MMC slaves which this layer
  122. * won't even know about.
  123. */
  124. if (!mmc_host_is_spi(host)) {
  125. mmc_set_chip_select(host, MMC_CS_HIGH);
  126. mmc_delay(1);
  127. }
  128. cmd.opcode = MMC_GO_IDLE_STATE;
  129. cmd.arg = 0;
  130. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
  131. err = mmc_wait_for_cmd(host, &cmd, 0);
  132. mmc_delay(1);
  133. if (!mmc_host_is_spi(host)) {
  134. mmc_set_chip_select(host, MMC_CS_DONTCARE);
  135. mmc_delay(1);
  136. }
  137. host->use_spi_crc = 0;
  138. return err;
  139. }
  140. int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
  141. {
  142. struct mmc_command cmd = {};
  143. int i, err = 0;
  144. cmd.opcode = MMC_SEND_OP_COND;
  145. cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
  146. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
  147. for (i = 100; i; i--) {
  148. err = mmc_wait_for_cmd(host, &cmd, 0);
  149. if (err)
  150. break;
  151. /* if we're just probing, do a single pass */
  152. if (ocr == 0)
  153. break;
  154. /* otherwise wait until reset completes */
  155. if (mmc_host_is_spi(host)) {
  156. if (!(cmd.resp[0] & R1_SPI_IDLE))
  157. break;
  158. } else {
  159. if (cmd.resp[0] & MMC_CARD_BUSY)
  160. break;
  161. }
  162. err = -ETIMEDOUT;
  163. mmc_delay(10);
  164. }
  165. if (rocr && !mmc_host_is_spi(host))
  166. *rocr = cmd.resp[0];
  167. return err;
  168. }
  169. static int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
  170. {
  171. int err;
  172. struct mmc_command cmd = {};
  173. cmd.opcode = MMC_ALL_SEND_CID;
  174. cmd.arg = 0;
  175. cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
  176. err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  177. if (err)
  178. return err;
  179. memcpy(cid, cmd.resp, sizeof(u32) * 4);
  180. return 0;
  181. }
  182. int mmc_set_relative_addr(struct mmc_card *card)
  183. {
  184. struct mmc_command cmd = {};
  185. cmd.opcode = MMC_SET_RELATIVE_ADDR;
  186. cmd.arg = card->rca << 16;
  187. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  188. return mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
  189. }
  190. static int
  191. mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
  192. {
  193. int err;
  194. struct mmc_command cmd = {};
  195. cmd.opcode = opcode;
  196. cmd.arg = arg;
  197. cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
  198. err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  199. if (err)
  200. return err;
  201. memcpy(cxd, cmd.resp, sizeof(u32) * 4);
  202. return 0;
  203. }
  204. /*
  205. * NOTE: void *buf, caller for the buf is required to use DMA-capable
  206. * buffer or on-stack buffer (with some overhead in callee).
  207. */
  208. static int
  209. mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
  210. u32 opcode, void *buf, unsigned len)
  211. {
  212. struct mmc_request mrq = {};
  213. struct mmc_command cmd = {};
  214. struct mmc_data data = {};
  215. struct scatterlist sg;
  216. mrq.cmd = &cmd;
  217. mrq.data = &data;
  218. cmd.opcode = opcode;
  219. cmd.arg = 0;
  220. /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
  221. * rely on callers to never use this with "native" calls for reading
  222. * CSD or CID. Native versions of those commands use the R2 type,
  223. * not R1 plus a data block.
  224. */
  225. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  226. data.blksz = len;
  227. data.blocks = 1;
  228. data.flags = MMC_DATA_READ;
  229. data.sg = &sg;
  230. data.sg_len = 1;
  231. sg_init_one(&sg, buf, len);
  232. if (opcode == MMC_SEND_CSD || opcode == MMC_SEND_CID) {
  233. /*
  234. * The spec states that CSR and CID accesses have a timeout
  235. * of 64 clock cycles.
  236. */
  237. data.timeout_ns = 0;
  238. data.timeout_clks = 64;
  239. } else
  240. mmc_set_data_timeout(&data, card);
  241. mmc_wait_for_req(host, &mrq);
  242. if (cmd.error)
  243. return cmd.error;
  244. if (data.error)
  245. return data.error;
  246. return 0;
  247. }
  248. int mmc_send_csd(struct mmc_card *card, u32 *csd)
  249. {
  250. int ret, i;
  251. __be32 *csd_tmp;
  252. if (!mmc_host_is_spi(card->host))
  253. return mmc_send_cxd_native(card->host, card->rca << 16,
  254. csd, MMC_SEND_CSD);
  255. csd_tmp = kzalloc(16, GFP_KERNEL);
  256. if (!csd_tmp)
  257. return -ENOMEM;
  258. ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd_tmp, 16);
  259. if (ret)
  260. goto err;
  261. for (i = 0; i < 4; i++)
  262. csd[i] = be32_to_cpu(csd_tmp[i]);
  263. err:
  264. kfree(csd_tmp);
  265. return ret;
  266. }
  267. static int mmc_spi_send_cid(struct mmc_host *host, u32 *cid)
  268. {
  269. int ret, i;
  270. __be32 *cid_tmp;
  271. cid_tmp = kzalloc(16, GFP_KERNEL);
  272. if (!cid_tmp)
  273. return -ENOMEM;
  274. ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid_tmp, 16);
  275. if (ret)
  276. goto err;
  277. for (i = 0; i < 4; i++)
  278. cid[i] = be32_to_cpu(cid_tmp[i]);
  279. err:
  280. kfree(cid_tmp);
  281. return ret;
  282. }
  283. int mmc_send_cid(struct mmc_host *host, u32 *cid)
  284. {
  285. if (mmc_host_is_spi(host))
  286. return mmc_spi_send_cid(host, cid);
  287. return mmc_all_send_cid(host, cid);
  288. }
  289. int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
  290. {
  291. int err;
  292. u8 *ext_csd;
  293. if (!card || !new_ext_csd)
  294. return -EINVAL;
  295. if (!mmc_can_ext_csd(card))
  296. return -EOPNOTSUPP;
  297. /*
  298. * As the ext_csd is so large and mostly unused, we don't store the
  299. * raw block in mmc_card.
  300. */
  301. ext_csd = kzalloc(512, GFP_KERNEL);
  302. if (!ext_csd)
  303. return -ENOMEM;
  304. err = mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD, ext_csd,
  305. 512);
  306. if (err)
  307. kfree(ext_csd);
  308. else
  309. *new_ext_csd = ext_csd;
  310. return err;
  311. }
  312. EXPORT_SYMBOL_GPL(mmc_get_ext_csd);
  313. int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
  314. {
  315. struct mmc_command cmd = {};
  316. int err;
  317. cmd.opcode = MMC_SPI_READ_OCR;
  318. cmd.arg = highcap ? (1 << 30) : 0;
  319. cmd.flags = MMC_RSP_SPI_R3;
  320. err = mmc_wait_for_cmd(host, &cmd, 0);
  321. *ocrp = cmd.resp[1];
  322. return err;
  323. }
  324. int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
  325. {
  326. struct mmc_command cmd = {};
  327. int err;
  328. cmd.opcode = MMC_SPI_CRC_ON_OFF;
  329. cmd.flags = MMC_RSP_SPI_R1;
  330. cmd.arg = use_crc;
  331. err = mmc_wait_for_cmd(host, &cmd, 0);
  332. if (!err)
  333. host->use_spi_crc = use_crc;
  334. return err;
  335. }
  336. static int mmc_switch_status_error(struct mmc_host *host, u32 status)
  337. {
  338. if (mmc_host_is_spi(host)) {
  339. if (status & R1_SPI_ILLEGAL_COMMAND)
  340. return -EBADMSG;
  341. } else {
  342. if (status & 0xFDFFA000)
  343. pr_warn("%s: unexpected status %#x after switch\n",
  344. mmc_hostname(host), status);
  345. if (status & R1_SWITCH_ERROR)
  346. return -EBADMSG;
  347. }
  348. return 0;
  349. }
  350. /* Caller must hold re-tuning */
  351. int __mmc_switch_status(struct mmc_card *card, bool crc_err_fatal)
  352. {
  353. u32 status;
  354. int err;
  355. err = mmc_send_status(card, &status);
  356. if (!crc_err_fatal && err == -EILSEQ)
  357. return 0;
  358. if (err)
  359. return err;
  360. return mmc_switch_status_error(card->host, status);
  361. }
  362. int mmc_switch_status(struct mmc_card *card)
  363. {
  364. return __mmc_switch_status(card, true);
  365. }
  366. static int mmc_poll_for_busy(struct mmc_card *card, unsigned int timeout_ms,
  367. bool send_status, bool retry_crc_err)
  368. {
  369. struct mmc_host *host = card->host;
  370. int err;
  371. unsigned long timeout;
  372. u32 status = 0;
  373. bool expired = false;
  374. bool busy = false;
  375. /* We have an unspecified cmd timeout, use the fallback value. */
  376. if (!timeout_ms)
  377. timeout_ms = MMC_OPS_TIMEOUT_MS;
  378. /*
  379. * In cases when not allowed to poll by using CMD13 or because we aren't
  380. * capable of polling by using ->card_busy(), then rely on waiting the
  381. * stated timeout to be sufficient.
  382. */
  383. if (!send_status && !host->ops->card_busy) {
  384. mmc_delay(timeout_ms);
  385. return 0;
  386. }
  387. timeout = jiffies + msecs_to_jiffies(timeout_ms) + 1;
  388. do {
  389. /*
  390. * Due to the possibility of being preempted while polling,
  391. * check the expiration time first.
  392. */
  393. expired = time_after(jiffies, timeout);
  394. if (host->ops->card_busy) {
  395. busy = host->ops->card_busy(host);
  396. } else {
  397. err = mmc_send_status(card, &status);
  398. if (retry_crc_err && err == -EILSEQ) {
  399. busy = true;
  400. } else if (err) {
  401. return err;
  402. } else {
  403. err = mmc_switch_status_error(host, status);
  404. if (err)
  405. return err;
  406. busy = R1_CURRENT_STATE(status) == R1_STATE_PRG;
  407. }
  408. }
  409. /* Timeout if the device still remains busy. */
  410. if (expired && busy) {
  411. pr_err("%s: Card stuck being busy! %s\n",
  412. mmc_hostname(host), __func__);
  413. return -ETIMEDOUT;
  414. }
  415. } while (busy);
  416. return 0;
  417. }
  418. /**
  419. * __mmc_switch - modify EXT_CSD register
  420. * @card: the MMC card associated with the data transfer
  421. * @set: cmd set values
  422. * @index: EXT_CSD register index
  423. * @value: value to program into EXT_CSD register
  424. * @timeout_ms: timeout (ms) for operation performed by register write,
  425. * timeout of zero implies maximum possible timeout
  426. * @timing: new timing to change to
  427. * @use_busy_signal: use the busy signal as response type
  428. * @send_status: send status cmd to poll for busy
  429. * @retry_crc_err: retry when CRC errors when polling with CMD13 for busy
  430. *
  431. * Modifies the EXT_CSD register for selected card.
  432. */
  433. int __mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
  434. unsigned int timeout_ms, unsigned char timing,
  435. bool use_busy_signal, bool send_status, bool retry_crc_err)
  436. {
  437. struct mmc_host *host = card->host;
  438. int err;
  439. struct mmc_command cmd = {};
  440. bool use_r1b_resp = use_busy_signal;
  441. unsigned char old_timing = host->ios.timing;
  442. mmc_retune_hold(host);
  443. /*
  444. * If the cmd timeout and the max_busy_timeout of the host are both
  445. * specified, let's validate them. A failure means we need to prevent
  446. * the host from doing hw busy detection, which is done by converting
  447. * to a R1 response instead of a R1B.
  448. */
  449. if (timeout_ms && host->max_busy_timeout &&
  450. (timeout_ms > host->max_busy_timeout))
  451. use_r1b_resp = false;
  452. cmd.opcode = MMC_SWITCH;
  453. cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  454. (index << 16) |
  455. (value << 8) |
  456. set;
  457. cmd.flags = MMC_CMD_AC;
  458. if (use_r1b_resp) {
  459. cmd.flags |= MMC_RSP_SPI_R1B | MMC_RSP_R1B;
  460. /*
  461. * A busy_timeout of zero means the host can decide to use
  462. * whatever value it finds suitable.
  463. */
  464. cmd.busy_timeout = timeout_ms;
  465. } else {
  466. cmd.flags |= MMC_RSP_SPI_R1 | MMC_RSP_R1;
  467. }
  468. if (index == EXT_CSD_SANITIZE_START)
  469. cmd.sanitize_busy = true;
  470. err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  471. if (err)
  472. goto out;
  473. /* No need to check card status in case of unblocking command */
  474. if (!use_busy_signal)
  475. goto out;
  476. /*If SPI or used HW busy detection above, then we don't need to poll. */
  477. if (((host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp) ||
  478. mmc_host_is_spi(host))
  479. goto out_tim;
  480. /* Let's try to poll to find out when the command is completed. */
  481. err = mmc_poll_for_busy(card, timeout_ms, send_status, retry_crc_err);
  482. if (err)
  483. goto out;
  484. out_tim:
  485. /* Switch to new timing before check switch status. */
  486. if (timing)
  487. mmc_set_timing(host, timing);
  488. if (send_status) {
  489. err = mmc_switch_status(card);
  490. if (err && timing)
  491. mmc_set_timing(host, old_timing);
  492. }
  493. out:
  494. mmc_retune_release(host);
  495. return err;
  496. }
  497. int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
  498. unsigned int timeout_ms)
  499. {
  500. return __mmc_switch(card, set, index, value, timeout_ms, 0,
  501. true, true, false);
  502. }
  503. EXPORT_SYMBOL_GPL(mmc_switch);
  504. int mmc_send_tuning(struct mmc_host *host, u32 opcode, int *cmd_error)
  505. {
  506. struct mmc_request mrq = {};
  507. struct mmc_command cmd = {};
  508. struct mmc_data data = {};
  509. struct scatterlist sg;
  510. struct mmc_ios *ios = &host->ios;
  511. const u8 *tuning_block_pattern;
  512. int size, err = 0;
  513. u8 *data_buf;
  514. if (ios->bus_width == MMC_BUS_WIDTH_8) {
  515. tuning_block_pattern = tuning_blk_pattern_8bit;
  516. size = sizeof(tuning_blk_pattern_8bit);
  517. } else if (ios->bus_width == MMC_BUS_WIDTH_4) {
  518. tuning_block_pattern = tuning_blk_pattern_4bit;
  519. size = sizeof(tuning_blk_pattern_4bit);
  520. } else
  521. return -EINVAL;
  522. data_buf = kzalloc(size, GFP_KERNEL);
  523. if (!data_buf)
  524. return -ENOMEM;
  525. mrq.cmd = &cmd;
  526. mrq.data = &data;
  527. cmd.opcode = opcode;
  528. cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  529. data.blksz = size;
  530. data.blocks = 1;
  531. data.flags = MMC_DATA_READ;
  532. /*
  533. * According to the tuning specs, Tuning process
  534. * is normally shorter 40 executions of CMD19,
  535. * and timeout value should be shorter than 150 ms
  536. */
  537. data.timeout_ns = 150 * NSEC_PER_MSEC;
  538. data.sg = &sg;
  539. data.sg_len = 1;
  540. sg_init_one(&sg, data_buf, size);
  541. mmc_wait_for_req(host, &mrq);
  542. if (cmd_error)
  543. *cmd_error = cmd.error;
  544. if (cmd.error) {
  545. err = cmd.error;
  546. goto out;
  547. }
  548. if (data.error) {
  549. err = data.error;
  550. goto out;
  551. }
  552. if (memcmp(data_buf, tuning_block_pattern, size))
  553. err = -EIO;
  554. out:
  555. kfree(data_buf);
  556. return err;
  557. }
  558. EXPORT_SYMBOL_GPL(mmc_send_tuning);
  559. int mmc_abort_tuning(struct mmc_host *host, u32 opcode)
  560. {
  561. struct mmc_command cmd = {};
  562. /*
  563. * eMMC specification specifies that CMD12 can be used to stop a tuning
  564. * command, but SD specification does not, so do nothing unless it is
  565. * eMMC.
  566. */
  567. if (opcode != MMC_SEND_TUNING_BLOCK_HS200)
  568. return 0;
  569. cmd.opcode = MMC_STOP_TRANSMISSION;
  570. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  571. /*
  572. * For drivers that override R1 to R1b, set an arbitrary timeout based
  573. * on the tuning timeout i.e. 150ms.
  574. */
  575. cmd.busy_timeout = 150;
  576. return mmc_wait_for_cmd(host, &cmd, 0);
  577. }
  578. EXPORT_SYMBOL_GPL(mmc_abort_tuning);
  579. static int
  580. mmc_send_bus_test(struct mmc_card *card, struct mmc_host *host, u8 opcode,
  581. u8 len)
  582. {
  583. struct mmc_request mrq = {};
  584. struct mmc_command cmd = {};
  585. struct mmc_data data = {};
  586. struct scatterlist sg;
  587. u8 *data_buf;
  588. u8 *test_buf;
  589. int i, err;
  590. static u8 testdata_8bit[8] = { 0x55, 0xaa, 0, 0, 0, 0, 0, 0 };
  591. static u8 testdata_4bit[4] = { 0x5a, 0, 0, 0 };
  592. /* dma onto stack is unsafe/nonportable, but callers to this
  593. * routine normally provide temporary on-stack buffers ...
  594. */
  595. data_buf = kmalloc(len, GFP_KERNEL);
  596. if (!data_buf)
  597. return -ENOMEM;
  598. if (len == 8)
  599. test_buf = testdata_8bit;
  600. else if (len == 4)
  601. test_buf = testdata_4bit;
  602. else {
  603. pr_err("%s: Invalid bus_width %d\n",
  604. mmc_hostname(host), len);
  605. kfree(data_buf);
  606. return -EINVAL;
  607. }
  608. if (opcode == MMC_BUS_TEST_W)
  609. memcpy(data_buf, test_buf, len);
  610. mrq.cmd = &cmd;
  611. mrq.data = &data;
  612. cmd.opcode = opcode;
  613. cmd.arg = 0;
  614. /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
  615. * rely on callers to never use this with "native" calls for reading
  616. * CSD or CID. Native versions of those commands use the R2 type,
  617. * not R1 plus a data block.
  618. */
  619. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  620. data.blksz = len;
  621. data.blocks = 1;
  622. if (opcode == MMC_BUS_TEST_R)
  623. data.flags = MMC_DATA_READ;
  624. else
  625. data.flags = MMC_DATA_WRITE;
  626. data.sg = &sg;
  627. data.sg_len = 1;
  628. mmc_set_data_timeout(&data, card);
  629. sg_init_one(&sg, data_buf, len);
  630. mmc_wait_for_req(host, &mrq);
  631. err = 0;
  632. if (opcode == MMC_BUS_TEST_R) {
  633. for (i = 0; i < len / 4; i++)
  634. if ((test_buf[i] ^ data_buf[i]) != 0xff) {
  635. err = -EIO;
  636. break;
  637. }
  638. }
  639. kfree(data_buf);
  640. if (cmd.error)
  641. return cmd.error;
  642. if (data.error)
  643. return data.error;
  644. return err;
  645. }
  646. int mmc_bus_test(struct mmc_card *card, u8 bus_width)
  647. {
  648. int width;
  649. if (bus_width == MMC_BUS_WIDTH_8)
  650. width = 8;
  651. else if (bus_width == MMC_BUS_WIDTH_4)
  652. width = 4;
  653. else if (bus_width == MMC_BUS_WIDTH_1)
  654. return 0; /* no need for test */
  655. else
  656. return -EINVAL;
  657. /*
  658. * Ignore errors from BUS_TEST_W. BUS_TEST_R will fail if there
  659. * is a problem. This improves chances that the test will work.
  660. */
  661. mmc_send_bus_test(card, card->host, MMC_BUS_TEST_W, width);
  662. return mmc_send_bus_test(card, card->host, MMC_BUS_TEST_R, width);
  663. }
  664. static int mmc_send_hpi_cmd(struct mmc_card *card, u32 *status)
  665. {
  666. struct mmc_command cmd = {};
  667. unsigned int opcode;
  668. int err;
  669. if (!card->ext_csd.hpi) {
  670. pr_warn("%s: Card didn't support HPI command\n",
  671. mmc_hostname(card->host));
  672. return -EINVAL;
  673. }
  674. opcode = card->ext_csd.hpi_cmd;
  675. if (opcode == MMC_STOP_TRANSMISSION)
  676. cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
  677. else if (opcode == MMC_SEND_STATUS)
  678. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  679. cmd.opcode = opcode;
  680. cmd.arg = card->rca << 16 | 1;
  681. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  682. if (err) {
  683. pr_warn("%s: error %d interrupting operation. "
  684. "HPI command response %#x\n", mmc_hostname(card->host),
  685. err, cmd.resp[0]);
  686. return err;
  687. }
  688. if (status)
  689. *status = cmd.resp[0];
  690. return 0;
  691. }
  692. /**
  693. * mmc_interrupt_hpi - Issue for High priority Interrupt
  694. * @card: the MMC card associated with the HPI transfer
  695. *
  696. * Issued High Priority Interrupt, and check for card status
  697. * until out-of prg-state.
  698. */
  699. int mmc_interrupt_hpi(struct mmc_card *card)
  700. {
  701. int err;
  702. u32 status;
  703. unsigned long prg_wait;
  704. if (!card->ext_csd.hpi_en) {
  705. pr_info("%s: HPI enable bit unset\n", mmc_hostname(card->host));
  706. return 1;
  707. }
  708. mmc_claim_host(card->host);
  709. err = mmc_send_status(card, &status);
  710. if (err) {
  711. pr_err("%s: Get card status fail\n", mmc_hostname(card->host));
  712. goto out;
  713. }
  714. switch (R1_CURRENT_STATE(status)) {
  715. case R1_STATE_IDLE:
  716. case R1_STATE_READY:
  717. case R1_STATE_STBY:
  718. case R1_STATE_TRAN:
  719. /*
  720. * In idle and transfer states, HPI is not needed and the caller
  721. * can issue the next intended command immediately
  722. */
  723. goto out;
  724. case R1_STATE_PRG:
  725. break;
  726. default:
  727. /* In all other states, it's illegal to issue HPI */
  728. pr_debug("%s: HPI cannot be sent. Card state=%d\n",
  729. mmc_hostname(card->host), R1_CURRENT_STATE(status));
  730. err = -EINVAL;
  731. goto out;
  732. }
  733. err = mmc_send_hpi_cmd(card, &status);
  734. if (err)
  735. goto out;
  736. prg_wait = jiffies + msecs_to_jiffies(card->ext_csd.out_of_int_time);
  737. do {
  738. err = mmc_send_status(card, &status);
  739. if (!err && R1_CURRENT_STATE(status) == R1_STATE_TRAN)
  740. break;
  741. if (time_after(jiffies, prg_wait))
  742. err = -ETIMEDOUT;
  743. } while (!err);
  744. out:
  745. mmc_release_host(card->host);
  746. return err;
  747. }
  748. int mmc_can_ext_csd(struct mmc_card *card)
  749. {
  750. return (card && card->csd.mmca_vsn > CSD_SPEC_VER_3);
  751. }
  752. /**
  753. * mmc_stop_bkops - stop ongoing BKOPS
  754. * @card: MMC card to check BKOPS
  755. *
  756. * Send HPI command to stop ongoing background operations to
  757. * allow rapid servicing of foreground operations, e.g. read/
  758. * writes. Wait until the card comes out of the programming state
  759. * to avoid errors in servicing read/write requests.
  760. */
  761. int mmc_stop_bkops(struct mmc_card *card)
  762. {
  763. int err = 0;
  764. err = mmc_interrupt_hpi(card);
  765. /*
  766. * If err is EINVAL, we can't issue an HPI.
  767. * It should complete the BKOPS.
  768. */
  769. if (!err || (err == -EINVAL)) {
  770. mmc_card_clr_doing_bkops(card);
  771. mmc_retune_release(card->host);
  772. err = 0;
  773. }
  774. return err;
  775. }
  776. static int mmc_read_bkops_status(struct mmc_card *card)
  777. {
  778. int err;
  779. u8 *ext_csd;
  780. mmc_claim_host(card->host);
  781. err = mmc_get_ext_csd(card, &ext_csd);
  782. mmc_release_host(card->host);
  783. if (err)
  784. return err;
  785. card->ext_csd.raw_bkops_status = ext_csd[EXT_CSD_BKOPS_STATUS];
  786. card->ext_csd.raw_exception_status = ext_csd[EXT_CSD_EXP_EVENTS_STATUS];
  787. kfree(ext_csd);
  788. return 0;
  789. }
  790. /**
  791. * mmc_start_bkops - start BKOPS for supported cards
  792. * @card: MMC card to start BKOPS
  793. * @form_exception: A flag to indicate if this function was
  794. * called due to an exception raised by the card
  795. *
  796. * Start background operations whenever requested.
  797. * When the urgent BKOPS bit is set in a R1 command response
  798. * then background operations should be started immediately.
  799. */
  800. void mmc_start_bkops(struct mmc_card *card, bool from_exception)
  801. {
  802. int err;
  803. int timeout;
  804. bool use_busy_signal;
  805. if (!card->ext_csd.man_bkops_en || mmc_card_doing_bkops(card))
  806. return;
  807. err = mmc_read_bkops_status(card);
  808. if (err) {
  809. pr_err("%s: Failed to read bkops status: %d\n",
  810. mmc_hostname(card->host), err);
  811. return;
  812. }
  813. if (!card->ext_csd.raw_bkops_status)
  814. return;
  815. if (card->ext_csd.raw_bkops_status < EXT_CSD_BKOPS_LEVEL_2 &&
  816. from_exception)
  817. return;
  818. mmc_claim_host(card->host);
  819. if (card->ext_csd.raw_bkops_status >= EXT_CSD_BKOPS_LEVEL_2) {
  820. timeout = MMC_OPS_TIMEOUT_MS;
  821. use_busy_signal = true;
  822. } else {
  823. timeout = 0;
  824. use_busy_signal = false;
  825. }
  826. mmc_retune_hold(card->host);
  827. err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  828. EXT_CSD_BKOPS_START, 1, timeout, 0,
  829. use_busy_signal, true, false);
  830. if (err) {
  831. pr_warn("%s: Error %d starting bkops\n",
  832. mmc_hostname(card->host), err);
  833. mmc_retune_release(card->host);
  834. goto out;
  835. }
  836. /*
  837. * For urgent bkops status (LEVEL_2 and more)
  838. * bkops executed synchronously, otherwise
  839. * the operation is in progress
  840. */
  841. if (!use_busy_signal)
  842. mmc_card_set_doing_bkops(card);
  843. else
  844. mmc_retune_release(card->host);
  845. out:
  846. mmc_release_host(card->host);
  847. }
  848. /*
  849. * Flush the cache to the non-volatile storage.
  850. */
  851. int mmc_flush_cache(struct mmc_card *card)
  852. {
  853. int err = 0;
  854. if (mmc_card_mmc(card) &&
  855. (card->ext_csd.cache_size > 0) &&
  856. (card->ext_csd.cache_ctrl & 1)) {
  857. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  858. EXT_CSD_FLUSH_CACHE, 1, 0);
  859. if (err)
  860. pr_err("%s: cache flush error %d\n",
  861. mmc_hostname(card->host), err);
  862. }
  863. return err;
  864. }
  865. EXPORT_SYMBOL(mmc_flush_cache);
  866. static int mmc_cmdq_switch(struct mmc_card *card, bool enable)
  867. {
  868. u8 val = enable ? EXT_CSD_CMDQ_MODE_ENABLED : 0;
  869. int err;
  870. if (!card->ext_csd.cmdq_support)
  871. return -EOPNOTSUPP;
  872. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_CMDQ_MODE_EN,
  873. val, card->ext_csd.generic_cmd6_time);
  874. if (!err)
  875. card->ext_csd.cmdq_en = enable;
  876. return err;
  877. }
  878. int mmc_cmdq_enable(struct mmc_card *card)
  879. {
  880. return mmc_cmdq_switch(card, true);
  881. }
  882. EXPORT_SYMBOL_GPL(mmc_cmdq_enable);
  883. int mmc_cmdq_disable(struct mmc_card *card)
  884. {
  885. return mmc_cmdq_switch(card, false);
  886. }
  887. EXPORT_SYMBOL_GPL(mmc_cmdq_disable);