mmc_ops.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * linux/drivers/mmc/core/mmc_ops.h
  4. *
  5. * Copyright 2006-2007 Pierre Ossman
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/export.h>
  9. #include <linux/types.h>
  10. #include <linux/scatterlist.h>
  11. #include <linux/mmc/host.h>
  12. #include <linux/mmc/card.h>
  13. #include <linux/mmc/mmc.h>
  14. #include "core.h"
  15. #include "card.h"
  16. #include "host.h"
  17. #include "mmc_ops.h"
  18. #define MMC_BKOPS_TIMEOUT_MS (120 * 1000) /* 120s */
  19. #define MMC_SANITIZE_TIMEOUT_MS (240 * 1000) /* 240s */
  20. #define MMC_OP_COND_PERIOD_US (4 * 1000) /* 4ms */
  21. #define MMC_OP_COND_TIMEOUT_MS 1000 /* 1s */
  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. struct mmc_busy_data {
  51. struct mmc_card *card;
  52. bool retry_crc_err;
  53. enum mmc_busy_cmd busy_cmd;
  54. };
  55. struct mmc_op_cond_busy_data {
  56. struct mmc_host *host;
  57. u32 ocr;
  58. struct mmc_command *cmd;
  59. };
  60. int __mmc_send_status(struct mmc_card *card, u32 *status, unsigned int retries)
  61. {
  62. int err;
  63. struct mmc_command cmd = {};
  64. cmd.opcode = MMC_SEND_STATUS;
  65. if (!mmc_host_is_spi(card->host))
  66. cmd.arg = card->rca << 16;
  67. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  68. err = mmc_wait_for_cmd(card->host, &cmd, retries);
  69. if (err)
  70. return err;
  71. /* NOTE: callers are required to understand the difference
  72. * between "native" and SPI format status words!
  73. */
  74. if (status)
  75. *status = cmd.resp[0];
  76. return 0;
  77. }
  78. EXPORT_SYMBOL_GPL(__mmc_send_status);
  79. int mmc_send_status(struct mmc_card *card, u32 *status)
  80. {
  81. return __mmc_send_status(card, status, MMC_CMD_RETRIES);
  82. }
  83. EXPORT_SYMBOL_GPL(mmc_send_status);
  84. static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
  85. {
  86. struct mmc_command cmd = {};
  87. cmd.opcode = MMC_SELECT_CARD;
  88. if (card) {
  89. cmd.arg = card->rca << 16;
  90. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  91. } else {
  92. cmd.arg = 0;
  93. cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
  94. }
  95. return mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  96. }
  97. int mmc_select_card(struct mmc_card *card)
  98. {
  99. return _mmc_select_card(card->host, card);
  100. }
  101. int mmc_deselect_cards(struct mmc_host *host)
  102. {
  103. return _mmc_select_card(host, NULL);
  104. }
  105. /*
  106. * Write the value specified in the device tree or board code into the optional
  107. * 16 bit Driver Stage Register. This can be used to tune raise/fall times and
  108. * drive strength of the DAT and CMD outputs. The actual meaning of a given
  109. * value is hardware dependant.
  110. * The presence of the DSR register can be determined from the CSD register,
  111. * bit 76.
  112. */
  113. int mmc_set_dsr(struct mmc_host *host)
  114. {
  115. struct mmc_command cmd = {};
  116. cmd.opcode = MMC_SET_DSR;
  117. cmd.arg = (host->dsr << 16) | 0xffff;
  118. cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
  119. return mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  120. }
  121. int __mmc_go_idle(struct mmc_host *host)
  122. {
  123. struct mmc_command cmd = {};
  124. int err;
  125. cmd.opcode = MMC_GO_IDLE_STATE;
  126. cmd.arg = 0;
  127. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
  128. err = mmc_wait_for_cmd(host, &cmd, 0);
  129. mmc_delay(1);
  130. return err;
  131. }
  132. int mmc_go_idle(struct mmc_host *host)
  133. {
  134. int err;
  135. /*
  136. * Non-SPI hosts need to prevent chipselect going active during
  137. * GO_IDLE; that would put chips into SPI mode. Remind them of
  138. * that in case of hardware that won't pull up DAT3/nCS otherwise.
  139. *
  140. * SPI hosts ignore ios.chip_select; it's managed according to
  141. * rules that must accommodate non-MMC slaves which this layer
  142. * won't even know about.
  143. */
  144. if (!mmc_host_is_spi(host)) {
  145. mmc_set_chip_select(host, MMC_CS_HIGH);
  146. mmc_delay(1);
  147. }
  148. err = __mmc_go_idle(host);
  149. if (!mmc_host_is_spi(host)) {
  150. mmc_set_chip_select(host, MMC_CS_DONTCARE);
  151. mmc_delay(1);
  152. }
  153. host->use_spi_crc = 0;
  154. return err;
  155. }
  156. static int __mmc_send_op_cond_cb(void *cb_data, bool *busy)
  157. {
  158. struct mmc_op_cond_busy_data *data = cb_data;
  159. struct mmc_host *host = data->host;
  160. struct mmc_command *cmd = data->cmd;
  161. u32 ocr = data->ocr;
  162. int err = 0;
  163. err = mmc_wait_for_cmd(host, cmd, 0);
  164. if (err)
  165. return err;
  166. if (mmc_host_is_spi(host)) {
  167. if (!(cmd->resp[0] & R1_SPI_IDLE)) {
  168. *busy = false;
  169. return 0;
  170. }
  171. } else {
  172. if (cmd->resp[0] & MMC_CARD_BUSY) {
  173. *busy = false;
  174. return 0;
  175. }
  176. }
  177. *busy = true;
  178. /*
  179. * According to eMMC specification v5.1 section 6.4.3, we
  180. * should issue CMD1 repeatedly in the idle state until
  181. * the eMMC is ready. Otherwise some eMMC devices seem to enter
  182. * the inactive mode after mmc_init_card() issued CMD0 when
  183. * the eMMC device is busy.
  184. */
  185. if (!ocr && !mmc_host_is_spi(host))
  186. cmd->arg = cmd->resp[0] | BIT(30);
  187. return 0;
  188. }
  189. int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
  190. {
  191. struct mmc_command cmd = {};
  192. int err = 0;
  193. struct mmc_op_cond_busy_data cb_data = {
  194. .host = host,
  195. .ocr = ocr,
  196. .cmd = &cmd
  197. };
  198. cmd.opcode = MMC_SEND_OP_COND;
  199. cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
  200. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
  201. err = __mmc_poll_for_busy(host, MMC_OP_COND_PERIOD_US,
  202. MMC_OP_COND_TIMEOUT_MS,
  203. &__mmc_send_op_cond_cb, &cb_data);
  204. if (err)
  205. return err;
  206. if (rocr && !mmc_host_is_spi(host))
  207. *rocr = cmd.resp[0];
  208. return err;
  209. }
  210. int mmc_set_relative_addr(struct mmc_card *card)
  211. {
  212. struct mmc_command cmd = {};
  213. cmd.opcode = MMC_SET_RELATIVE_ADDR;
  214. cmd.arg = card->rca << 16;
  215. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  216. return mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
  217. }
  218. static int
  219. mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
  220. {
  221. int err;
  222. struct mmc_command cmd = {};
  223. cmd.opcode = opcode;
  224. cmd.arg = arg;
  225. cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
  226. err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  227. if (err)
  228. return err;
  229. memcpy(cxd, cmd.resp, sizeof(u32) * 4);
  230. return 0;
  231. }
  232. /*
  233. * NOTE: void *buf, caller for the buf is required to use DMA-capable
  234. * buffer or on-stack buffer (with some overhead in callee).
  235. */
  236. int mmc_send_adtc_data(struct mmc_card *card, struct mmc_host *host, u32 opcode,
  237. u32 args, void *buf, unsigned len)
  238. {
  239. struct mmc_request mrq = {};
  240. struct mmc_command cmd = {};
  241. struct mmc_data data = {};
  242. struct scatterlist sg;
  243. mrq.cmd = &cmd;
  244. mrq.data = &data;
  245. cmd.opcode = opcode;
  246. cmd.arg = args;
  247. /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
  248. * rely on callers to never use this with "native" calls for reading
  249. * CSD or CID. Native versions of those commands use the R2 type,
  250. * not R1 plus a data block.
  251. */
  252. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  253. data.blksz = len;
  254. data.blocks = 1;
  255. data.flags = MMC_DATA_READ;
  256. data.sg = &sg;
  257. data.sg_len = 1;
  258. sg_init_one(&sg, buf, len);
  259. if (opcode == MMC_SEND_CSD || opcode == MMC_SEND_CID) {
  260. /*
  261. * The spec states that CSR and CID accesses have a timeout
  262. * of 64 clock cycles.
  263. */
  264. data.timeout_ns = 0;
  265. data.timeout_clks = 64;
  266. } else
  267. mmc_set_data_timeout(&data, card);
  268. mmc_wait_for_req(host, &mrq);
  269. if (cmd.error)
  270. return cmd.error;
  271. if (data.error)
  272. return data.error;
  273. return 0;
  274. }
  275. static int mmc_spi_send_cxd(struct mmc_host *host, u32 *cxd, u32 opcode)
  276. {
  277. int ret, i;
  278. __be32 *cxd_tmp;
  279. cxd_tmp = kzalloc(16, GFP_KERNEL);
  280. if (!cxd_tmp)
  281. return -ENOMEM;
  282. ret = mmc_send_adtc_data(NULL, host, opcode, 0, cxd_tmp, 16);
  283. if (ret)
  284. goto err;
  285. for (i = 0; i < 4; i++)
  286. cxd[i] = be32_to_cpu(cxd_tmp[i]);
  287. err:
  288. kfree(cxd_tmp);
  289. return ret;
  290. }
  291. int mmc_send_csd(struct mmc_card *card, u32 *csd)
  292. {
  293. if (mmc_host_is_spi(card->host))
  294. return mmc_spi_send_cxd(card->host, csd, MMC_SEND_CSD);
  295. return mmc_send_cxd_native(card->host, card->rca << 16, csd,
  296. MMC_SEND_CSD);
  297. }
  298. int mmc_send_cid(struct mmc_host *host, u32 *cid)
  299. {
  300. if (mmc_host_is_spi(host))
  301. return mmc_spi_send_cxd(host, cid, MMC_SEND_CID);
  302. return mmc_send_cxd_native(host, 0, cid, MMC_ALL_SEND_CID);
  303. }
  304. int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
  305. {
  306. int err;
  307. u8 *ext_csd;
  308. if (!card || !new_ext_csd)
  309. return -EINVAL;
  310. if (!mmc_card_can_ext_csd(card))
  311. return -EOPNOTSUPP;
  312. /*
  313. * As the ext_csd is so large and mostly unused, we don't store the
  314. * raw block in mmc_card.
  315. */
  316. ext_csd = kzalloc(512, GFP_KERNEL);
  317. if (!ext_csd)
  318. return -ENOMEM;
  319. err = mmc_send_adtc_data(card, card->host, MMC_SEND_EXT_CSD, 0, ext_csd,
  320. 512);
  321. if (err)
  322. kfree(ext_csd);
  323. else
  324. *new_ext_csd = ext_csd;
  325. return err;
  326. }
  327. EXPORT_SYMBOL_GPL(mmc_get_ext_csd);
  328. int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
  329. {
  330. struct mmc_command cmd = {};
  331. int err;
  332. cmd.opcode = MMC_SPI_READ_OCR;
  333. cmd.arg = highcap ? (1 << 30) : 0;
  334. cmd.flags = MMC_RSP_SPI_R3;
  335. err = mmc_wait_for_cmd(host, &cmd, 0);
  336. *ocrp = cmd.resp[1];
  337. return err;
  338. }
  339. int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
  340. {
  341. struct mmc_command cmd = {};
  342. int err;
  343. cmd.opcode = MMC_SPI_CRC_ON_OFF;
  344. cmd.flags = MMC_RSP_SPI_R1;
  345. cmd.arg = use_crc;
  346. err = mmc_wait_for_cmd(host, &cmd, 0);
  347. if (!err)
  348. host->use_spi_crc = use_crc;
  349. return err;
  350. }
  351. static int mmc_switch_status_error(struct mmc_host *host, u32 status)
  352. {
  353. if (mmc_host_is_spi(host)) {
  354. if (status & R1_SPI_ILLEGAL_COMMAND)
  355. return -EBADMSG;
  356. } else {
  357. if (R1_STATUS(status))
  358. pr_warn("%s: unexpected status %#x after switch\n",
  359. mmc_hostname(host), status);
  360. if (status & R1_SWITCH_ERROR)
  361. return -EBADMSG;
  362. }
  363. return 0;
  364. }
  365. /* Caller must hold re-tuning */
  366. int mmc_switch_status(struct mmc_card *card, bool crc_err_fatal)
  367. {
  368. u32 status;
  369. int err;
  370. err = mmc_send_status(card, &status);
  371. if (!crc_err_fatal && err == -EILSEQ)
  372. return 0;
  373. if (err)
  374. return err;
  375. return mmc_switch_status_error(card->host, status);
  376. }
  377. static int mmc_busy_cb(void *cb_data, bool *busy)
  378. {
  379. struct mmc_busy_data *data = cb_data;
  380. struct mmc_host *host = data->card->host;
  381. u32 status = 0;
  382. int err;
  383. if (data->busy_cmd != MMC_BUSY_IO && host->ops->card_busy) {
  384. *busy = host->ops->card_busy(host);
  385. return 0;
  386. }
  387. err = mmc_send_status(data->card, &status);
  388. if (data->retry_crc_err && err == -EILSEQ) {
  389. *busy = true;
  390. return 0;
  391. }
  392. if (err)
  393. return err;
  394. switch (data->busy_cmd) {
  395. case MMC_BUSY_CMD6:
  396. err = mmc_switch_status_error(host, status);
  397. break;
  398. case MMC_BUSY_ERASE:
  399. err = R1_STATUS(status) ? -EIO : 0;
  400. break;
  401. case MMC_BUSY_HPI:
  402. case MMC_BUSY_EXTR_SINGLE:
  403. case MMC_BUSY_IO:
  404. break;
  405. default:
  406. err = -EINVAL;
  407. }
  408. if (err)
  409. return err;
  410. *busy = !mmc_ready_for_data(status);
  411. return 0;
  412. }
  413. int __mmc_poll_for_busy(struct mmc_host *host, unsigned int period_us,
  414. unsigned int timeout_ms,
  415. int (*busy_cb)(void *cb_data, bool *busy),
  416. void *cb_data)
  417. {
  418. int err;
  419. unsigned long timeout;
  420. unsigned int udelay = period_us ? period_us : 32, udelay_max = 32768;
  421. bool expired = false;
  422. bool busy = false;
  423. timeout = jiffies + msecs_to_jiffies(timeout_ms) + 1;
  424. do {
  425. /*
  426. * Due to the possibility of being preempted while polling,
  427. * check the expiration time first.
  428. */
  429. expired = time_after(jiffies, timeout);
  430. err = (*busy_cb)(cb_data, &busy);
  431. if (err)
  432. return err;
  433. /* Timeout if the device still remains busy. */
  434. if (expired && busy) {
  435. pr_err("%s: Card stuck being busy! %s\n",
  436. mmc_hostname(host), __func__);
  437. return -ETIMEDOUT;
  438. }
  439. /* Throttle the polling rate to avoid hogging the CPU. */
  440. if (busy) {
  441. usleep_range(udelay, udelay * 2);
  442. if (udelay < udelay_max)
  443. udelay *= 2;
  444. }
  445. } while (busy);
  446. return 0;
  447. }
  448. EXPORT_SYMBOL_GPL(__mmc_poll_for_busy);
  449. int mmc_poll_for_busy(struct mmc_card *card, unsigned int timeout_ms,
  450. bool retry_crc_err, enum mmc_busy_cmd busy_cmd)
  451. {
  452. struct mmc_host *host = card->host;
  453. struct mmc_busy_data cb_data;
  454. cb_data.card = card;
  455. cb_data.retry_crc_err = retry_crc_err;
  456. cb_data.busy_cmd = busy_cmd;
  457. return __mmc_poll_for_busy(host, 0, timeout_ms, &mmc_busy_cb, &cb_data);
  458. }
  459. EXPORT_SYMBOL_GPL(mmc_poll_for_busy);
  460. bool mmc_prepare_busy_cmd(struct mmc_host *host, struct mmc_command *cmd,
  461. unsigned int timeout_ms)
  462. {
  463. /*
  464. * If the max_busy_timeout of the host is specified, make sure it's
  465. * enough to fit the used timeout_ms. In case it's not, let's instruct
  466. * the host to avoid HW busy detection, by converting to a R1 response
  467. * instead of a R1B. Note, some hosts requires R1B, which also means
  468. * they are on their own when it comes to deal with the busy timeout.
  469. */
  470. if (!(host->caps & MMC_CAP_NEED_RSP_BUSY) && host->max_busy_timeout &&
  471. (timeout_ms > host->max_busy_timeout)) {
  472. cmd->flags = MMC_CMD_AC | MMC_RSP_SPI_R1 | MMC_RSP_R1;
  473. return false;
  474. }
  475. cmd->flags = MMC_CMD_AC | MMC_RSP_SPI_R1B | MMC_RSP_R1B;
  476. cmd->busy_timeout = timeout_ms;
  477. return true;
  478. }
  479. EXPORT_SYMBOL_GPL(mmc_prepare_busy_cmd);
  480. /**
  481. * __mmc_switch - modify EXT_CSD register
  482. * @card: the MMC card associated with the data transfer
  483. * @set: cmd set values
  484. * @index: EXT_CSD register index
  485. * @value: value to program into EXT_CSD register
  486. * @timeout_ms: timeout (ms) for operation performed by register write,
  487. * timeout of zero implies maximum possible timeout
  488. * @timing: new timing to change to
  489. * @send_status: send status cmd to poll for busy
  490. * @retry_crc_err: retry when CRC errors when polling with CMD13 for busy
  491. * @retries: number of retries
  492. *
  493. * Modifies the EXT_CSD register for selected card.
  494. */
  495. int __mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
  496. unsigned int timeout_ms, unsigned char timing,
  497. bool send_status, bool retry_crc_err, unsigned int retries)
  498. {
  499. struct mmc_host *host = card->host;
  500. int err;
  501. struct mmc_command cmd = {};
  502. bool use_r1b_resp;
  503. unsigned char old_timing = host->ios.timing;
  504. mmc_retune_hold(host);
  505. if (!timeout_ms) {
  506. pr_warn("%s: unspecified timeout for CMD6 - use generic\n",
  507. mmc_hostname(host));
  508. timeout_ms = card->ext_csd.generic_cmd6_time;
  509. }
  510. cmd.opcode = MMC_SWITCH;
  511. cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  512. (index << 16) |
  513. (value << 8) |
  514. set;
  515. use_r1b_resp = mmc_prepare_busy_cmd(host, &cmd, timeout_ms);
  516. err = mmc_wait_for_cmd(host, &cmd, retries);
  517. if (err)
  518. goto out;
  519. /*If SPI or used HW busy detection above, then we don't need to poll. */
  520. if (((host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp) ||
  521. mmc_host_is_spi(host))
  522. goto out_tim;
  523. /*
  524. * If the host doesn't support HW polling via the ->card_busy() ops and
  525. * when it's not allowed to poll by using CMD13, then we need to rely on
  526. * waiting the stated timeout to be sufficient.
  527. */
  528. if (!send_status && !host->ops->card_busy) {
  529. mmc_delay(timeout_ms);
  530. goto out_tim;
  531. }
  532. /* Let's try to poll to find out when the command is completed. */
  533. err = mmc_poll_for_busy(card, timeout_ms, retry_crc_err, MMC_BUSY_CMD6);
  534. if (err)
  535. goto out;
  536. out_tim:
  537. /* Switch to new timing before check switch status. */
  538. if (timing)
  539. mmc_set_timing(host, timing);
  540. if (send_status) {
  541. err = mmc_switch_status(card, true);
  542. if (err && timing)
  543. mmc_set_timing(host, old_timing);
  544. }
  545. out:
  546. mmc_retune_release(host);
  547. return err;
  548. }
  549. int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
  550. unsigned int timeout_ms)
  551. {
  552. return __mmc_switch(card, set, index, value, timeout_ms, 0,
  553. true, false, MMC_CMD_RETRIES);
  554. }
  555. EXPORT_SYMBOL_GPL(mmc_switch);
  556. int mmc_send_tuning(struct mmc_host *host, u32 opcode, int *cmd_error)
  557. {
  558. struct mmc_request mrq = {};
  559. struct mmc_command cmd = {};
  560. struct mmc_data data = {};
  561. struct scatterlist sg;
  562. struct mmc_ios *ios = &host->ios;
  563. const u8 *tuning_block_pattern;
  564. int size, err = 0;
  565. u8 *data_buf;
  566. if (ios->bus_width == MMC_BUS_WIDTH_8) {
  567. tuning_block_pattern = tuning_blk_pattern_8bit;
  568. size = sizeof(tuning_blk_pattern_8bit);
  569. } else if (ios->bus_width == MMC_BUS_WIDTH_4) {
  570. tuning_block_pattern = tuning_blk_pattern_4bit;
  571. size = sizeof(tuning_blk_pattern_4bit);
  572. } else
  573. return -EINVAL;
  574. data_buf = kzalloc(size, GFP_KERNEL);
  575. if (!data_buf)
  576. return -ENOMEM;
  577. mrq.cmd = &cmd;
  578. mrq.data = &data;
  579. cmd.opcode = opcode;
  580. cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  581. data.blksz = size;
  582. data.blocks = 1;
  583. data.flags = MMC_DATA_READ;
  584. /*
  585. * According to the tuning specs, Tuning process
  586. * is normally shorter 40 executions of CMD19,
  587. * and timeout value should be shorter than 150 ms
  588. */
  589. data.timeout_ns = 150 * NSEC_PER_MSEC;
  590. data.sg = &sg;
  591. data.sg_len = 1;
  592. sg_init_one(&sg, data_buf, size);
  593. mmc_wait_for_req(host, &mrq);
  594. if (cmd_error)
  595. *cmd_error = cmd.error;
  596. if (cmd.error) {
  597. err = cmd.error;
  598. goto out;
  599. }
  600. if (data.error) {
  601. err = data.error;
  602. goto out;
  603. }
  604. if (memcmp(data_buf, tuning_block_pattern, size))
  605. err = -EIO;
  606. out:
  607. kfree(data_buf);
  608. return err;
  609. }
  610. EXPORT_SYMBOL_GPL(mmc_send_tuning);
  611. int mmc_send_abort_tuning(struct mmc_host *host, u32 opcode)
  612. {
  613. struct mmc_command cmd = {};
  614. /*
  615. * eMMC specification specifies that CMD12 can be used to stop a tuning
  616. * command, but SD specification does not, so do nothing unless it is
  617. * eMMC.
  618. */
  619. if (opcode != MMC_SEND_TUNING_BLOCK_HS200)
  620. return 0;
  621. cmd.opcode = MMC_STOP_TRANSMISSION;
  622. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  623. /*
  624. * For drivers that override R1 to R1b, set an arbitrary timeout based
  625. * on the tuning timeout i.e. 150ms.
  626. */
  627. cmd.busy_timeout = 150;
  628. return mmc_wait_for_cmd(host, &cmd, 0);
  629. }
  630. EXPORT_SYMBOL_GPL(mmc_send_abort_tuning);
  631. static int
  632. mmc_send_bus_test(struct mmc_card *card, struct mmc_host *host, u8 opcode,
  633. u8 len)
  634. {
  635. struct mmc_request mrq = {};
  636. struct mmc_command cmd = {};
  637. struct mmc_data data = {};
  638. struct scatterlist sg;
  639. u8 *data_buf;
  640. u8 *test_buf;
  641. int i, err;
  642. static u8 testdata_8bit[8] = { 0x55, 0xaa, 0, 0, 0, 0, 0, 0 };
  643. static u8 testdata_4bit[4] = { 0x5a, 0, 0, 0 };
  644. /* dma onto stack is unsafe/nonportable, but callers to this
  645. * routine normally provide temporary on-stack buffers ...
  646. */
  647. data_buf = kmalloc(len, GFP_KERNEL);
  648. if (!data_buf)
  649. return -ENOMEM;
  650. if (len == 8)
  651. test_buf = testdata_8bit;
  652. else if (len == 4)
  653. test_buf = testdata_4bit;
  654. else {
  655. pr_err("%s: Invalid bus_width %d\n",
  656. mmc_hostname(host), len);
  657. kfree(data_buf);
  658. return -EINVAL;
  659. }
  660. if (opcode == MMC_BUS_TEST_W)
  661. memcpy(data_buf, test_buf, len);
  662. mrq.cmd = &cmd;
  663. mrq.data = &data;
  664. cmd.opcode = opcode;
  665. cmd.arg = 0;
  666. /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
  667. * rely on callers to never use this with "native" calls for reading
  668. * CSD or CID. Native versions of those commands use the R2 type,
  669. * not R1 plus a data block.
  670. */
  671. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  672. data.blksz = len;
  673. data.blocks = 1;
  674. if (opcode == MMC_BUS_TEST_R)
  675. data.flags = MMC_DATA_READ;
  676. else
  677. data.flags = MMC_DATA_WRITE;
  678. data.sg = &sg;
  679. data.sg_len = 1;
  680. mmc_set_data_timeout(&data, card);
  681. sg_init_one(&sg, data_buf, len);
  682. mmc_wait_for_req(host, &mrq);
  683. err = 0;
  684. if (opcode == MMC_BUS_TEST_R) {
  685. for (i = 0; i < len / 4; i++)
  686. if ((test_buf[i] ^ data_buf[i]) != 0xff) {
  687. err = -EIO;
  688. break;
  689. }
  690. }
  691. kfree(data_buf);
  692. if (cmd.error)
  693. return cmd.error;
  694. if (data.error)
  695. return data.error;
  696. return err;
  697. }
  698. int mmc_bus_test(struct mmc_card *card, u8 bus_width)
  699. {
  700. int width;
  701. if (bus_width == MMC_BUS_WIDTH_8)
  702. width = 8;
  703. else if (bus_width == MMC_BUS_WIDTH_4)
  704. width = 4;
  705. else if (bus_width == MMC_BUS_WIDTH_1)
  706. return 0; /* no need for test */
  707. else
  708. return -EINVAL;
  709. /*
  710. * Ignore errors from BUS_TEST_W. BUS_TEST_R will fail if there
  711. * is a problem. This improves chances that the test will work.
  712. */
  713. mmc_send_bus_test(card, card->host, MMC_BUS_TEST_W, width);
  714. return mmc_send_bus_test(card, card->host, MMC_BUS_TEST_R, width);
  715. }
  716. static int mmc_send_hpi_cmd(struct mmc_card *card)
  717. {
  718. unsigned int busy_timeout_ms = card->ext_csd.out_of_int_time;
  719. struct mmc_host *host = card->host;
  720. bool use_r1b_resp = false;
  721. struct mmc_command cmd = {};
  722. int err;
  723. cmd.opcode = card->ext_csd.hpi_cmd;
  724. cmd.arg = card->rca << 16 | 1;
  725. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  726. if (cmd.opcode == MMC_STOP_TRANSMISSION)
  727. use_r1b_resp = mmc_prepare_busy_cmd(host, &cmd,
  728. busy_timeout_ms);
  729. err = mmc_wait_for_cmd(host, &cmd, 0);
  730. if (err) {
  731. pr_warn("%s: HPI error %d. Command response %#x\n",
  732. mmc_hostname(host), err, cmd.resp[0]);
  733. return err;
  734. }
  735. /* No need to poll when using HW busy detection. */
  736. if (host->caps & MMC_CAP_WAIT_WHILE_BUSY && use_r1b_resp)
  737. return 0;
  738. /* Let's poll to find out when the HPI request completes. */
  739. return mmc_poll_for_busy(card, busy_timeout_ms, false, MMC_BUSY_HPI);
  740. }
  741. /**
  742. * mmc_interrupt_hpi - Issue for High priority Interrupt
  743. * @card: the MMC card associated with the HPI transfer
  744. *
  745. * Issued High Priority Interrupt, and check for card status
  746. * until out-of prg-state.
  747. */
  748. static int mmc_interrupt_hpi(struct mmc_card *card)
  749. {
  750. int err;
  751. u32 status;
  752. if (!card->ext_csd.hpi_en) {
  753. pr_info("%s: HPI enable bit unset\n", mmc_hostname(card->host));
  754. return 1;
  755. }
  756. err = mmc_send_status(card, &status);
  757. if (err) {
  758. pr_err("%s: Get card status fail\n", mmc_hostname(card->host));
  759. goto out;
  760. }
  761. switch (R1_CURRENT_STATE(status)) {
  762. case R1_STATE_IDLE:
  763. case R1_STATE_READY:
  764. case R1_STATE_STBY:
  765. case R1_STATE_TRAN:
  766. /*
  767. * In idle and transfer states, HPI is not needed and the caller
  768. * can issue the next intended command immediately
  769. */
  770. goto out;
  771. case R1_STATE_PRG:
  772. break;
  773. default:
  774. /* In all other states, it's illegal to issue HPI */
  775. pr_debug("%s: HPI cannot be sent. Card state=%d\n",
  776. mmc_hostname(card->host), R1_CURRENT_STATE(status));
  777. err = -EINVAL;
  778. goto out;
  779. }
  780. err = mmc_send_hpi_cmd(card);
  781. out:
  782. return err;
  783. }
  784. bool mmc_card_can_ext_csd(struct mmc_card *card)
  785. {
  786. return (card && card->csd.mmca_vsn > CSD_SPEC_VER_3);
  787. }
  788. static int mmc_read_bkops_status(struct mmc_card *card)
  789. {
  790. int err;
  791. u8 *ext_csd;
  792. err = mmc_get_ext_csd(card, &ext_csd);
  793. if (err)
  794. return err;
  795. card->ext_csd.raw_bkops_status = ext_csd[EXT_CSD_BKOPS_STATUS];
  796. card->ext_csd.raw_exception_status = ext_csd[EXT_CSD_EXP_EVENTS_STATUS];
  797. kfree(ext_csd);
  798. return 0;
  799. }
  800. /**
  801. * mmc_run_bkops - Run BKOPS for supported cards
  802. * @card: MMC card to run BKOPS for
  803. *
  804. * Run background operations synchronously for cards having manual BKOPS
  805. * enabled and in case it reports urgent BKOPS level.
  806. */
  807. void mmc_run_bkops(struct mmc_card *card)
  808. {
  809. int err;
  810. if (!card->ext_csd.man_bkops_en)
  811. return;
  812. err = mmc_read_bkops_status(card);
  813. if (err) {
  814. pr_err("%s: Failed to read bkops status: %d\n",
  815. mmc_hostname(card->host), err);
  816. return;
  817. }
  818. if (!card->ext_csd.raw_bkops_status ||
  819. card->ext_csd.raw_bkops_status < EXT_CSD_BKOPS_LEVEL_2)
  820. return;
  821. mmc_retune_hold(card->host);
  822. /*
  823. * For urgent BKOPS status, LEVEL_2 and higher, let's execute
  824. * synchronously. Future wise, we may consider to start BKOPS, for less
  825. * urgent levels by using an asynchronous background task, when idle.
  826. */
  827. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  828. EXT_CSD_BKOPS_START, 1, MMC_BKOPS_TIMEOUT_MS);
  829. /*
  830. * If the BKOPS timed out, the card is probably still busy in the
  831. * R1_STATE_PRG. Rather than continue to wait, let's try to abort
  832. * it with a HPI command to get back into R1_STATE_TRAN.
  833. */
  834. if (err == -ETIMEDOUT && !mmc_interrupt_hpi(card))
  835. pr_warn("%s: BKOPS aborted\n", mmc_hostname(card->host));
  836. else if (err)
  837. pr_warn("%s: Error %d running bkops\n",
  838. mmc_hostname(card->host), err);
  839. mmc_retune_release(card->host);
  840. }
  841. EXPORT_SYMBOL(mmc_run_bkops);
  842. static int mmc_cmdq_switch(struct mmc_card *card, bool enable)
  843. {
  844. u8 val = enable ? EXT_CSD_CMDQ_MODE_ENABLED : 0;
  845. int err;
  846. if (!card->ext_csd.cmdq_support)
  847. return -EOPNOTSUPP;
  848. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_CMDQ_MODE_EN,
  849. val, card->ext_csd.generic_cmd6_time);
  850. if (!err)
  851. card->ext_csd.cmdq_en = enable;
  852. return err;
  853. }
  854. int mmc_cmdq_enable(struct mmc_card *card)
  855. {
  856. return mmc_cmdq_switch(card, true);
  857. }
  858. EXPORT_SYMBOL_GPL(mmc_cmdq_enable);
  859. int mmc_cmdq_disable(struct mmc_card *card)
  860. {
  861. return mmc_cmdq_switch(card, false);
  862. }
  863. EXPORT_SYMBOL_GPL(mmc_cmdq_disable);
  864. int mmc_sanitize(struct mmc_card *card, unsigned int timeout_ms)
  865. {
  866. struct mmc_host *host = card->host;
  867. int err;
  868. if (!mmc_card_can_sanitize(card)) {
  869. pr_warn("%s: Sanitize not supported\n", mmc_hostname(host));
  870. return -EOPNOTSUPP;
  871. }
  872. if (!timeout_ms)
  873. timeout_ms = MMC_SANITIZE_TIMEOUT_MS;
  874. pr_debug("%s: Sanitize in progress...\n", mmc_hostname(host));
  875. mmc_retune_hold(host);
  876. err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_SANITIZE_START,
  877. 1, timeout_ms, 0, true, false, 0);
  878. if (err)
  879. pr_err("%s: Sanitize failed err=%d\n", mmc_hostname(host), err);
  880. /*
  881. * If the sanitize operation timed out, the card is probably still busy
  882. * in the R1_STATE_PRG. Rather than continue to wait, let's try to abort
  883. * it with a HPI command to get back into R1_STATE_TRAN.
  884. */
  885. if (err == -ETIMEDOUT && !mmc_interrupt_hpi(card))
  886. pr_warn("%s: Sanitize aborted\n", mmc_hostname(host));
  887. mmc_retune_release(host);
  888. pr_debug("%s: Sanitize completed\n", mmc_hostname(host));
  889. return err;
  890. }
  891. EXPORT_SYMBOL_GPL(mmc_sanitize);
  892. /**
  893. * mmc_read_tuning() - read data blocks from the mmc
  894. * @host: mmc host doing the read
  895. * @blksz: data block size
  896. * @blocks: number of blocks to read
  897. *
  898. * Read one or more blocks of data from the beginning of the mmc. This is a
  899. * low-level helper for tuning operation. It is assumed that CMD23 can be used
  900. * for multi-block read if the host supports it.
  901. *
  902. * Note: Allocate and free a temporary buffer to store the data read. The data
  903. * is not available outside of the function, only the status of the read
  904. * operation.
  905. *
  906. * Return: 0 in case of success, otherwise -EIO / -ENOMEM / -E2BIG
  907. */
  908. int mmc_read_tuning(struct mmc_host *host, unsigned int blksz, unsigned int blocks)
  909. {
  910. struct mmc_request mrq = {};
  911. struct mmc_command sbc = {};
  912. struct mmc_command cmd = {};
  913. struct mmc_command stop = {};
  914. struct mmc_data data = {};
  915. struct scatterlist sg;
  916. void *buf;
  917. unsigned int len;
  918. if (blocks > 1) {
  919. if (mmc_host_can_cmd23(host)) {
  920. mrq.sbc = &sbc;
  921. sbc.opcode = MMC_SET_BLOCK_COUNT;
  922. sbc.arg = blocks;
  923. sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
  924. }
  925. cmd.opcode = MMC_READ_MULTIPLE_BLOCK;
  926. mrq.stop = &stop;
  927. stop.opcode = MMC_STOP_TRANSMISSION;
  928. stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  929. } else {
  930. cmd.opcode = MMC_READ_SINGLE_BLOCK;
  931. }
  932. mrq.cmd = &cmd;
  933. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  934. mrq.data = &data;
  935. data.flags = MMC_DATA_READ;
  936. data.blksz = blksz;
  937. data.blocks = blocks;
  938. data.blk_addr = 0;
  939. data.sg = &sg;
  940. data.sg_len = 1;
  941. data.timeout_ns = 1000000000;
  942. if (check_mul_overflow(blksz, blocks, &len))
  943. return -E2BIG;
  944. buf = kmalloc(len, GFP_KERNEL);
  945. if (!buf)
  946. return -ENOMEM;
  947. sg_init_one(&sg, buf, len);
  948. mmc_wait_for_req(host, &mrq);
  949. kfree(buf);
  950. if (sbc.error || cmd.error || data.error)
  951. return -EIO;
  952. return 0;
  953. }
  954. EXPORT_SYMBOL_GPL(mmc_read_tuning);