core.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/drivers/mmc/core/core.c
  4. *
  5. * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
  6. * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
  7. * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
  8. * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/completion.h>
  14. #include <linux/device.h>
  15. #include <linux/delay.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/err.h>
  18. #include <linux/leds.h>
  19. #include <linux/scatterlist.h>
  20. #include <linux/log2.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/suspend.h>
  23. #include <linux/fault-inject.h>
  24. #include <linux/random.h>
  25. #include <linux/slab.h>
  26. #include <linux/of.h>
  27. #include <linux/mmc/card.h>
  28. #include <linux/mmc/host.h>
  29. #include <linux/mmc/mmc.h>
  30. #include <linux/mmc/sd.h>
  31. #include <linux/mmc/slot-gpio.h>
  32. #define CREATE_TRACE_POINTS
  33. #include <trace/events/mmc.h>
  34. #include "core.h"
  35. #include "card.h"
  36. #include "crypto.h"
  37. #include "bus.h"
  38. #include "host.h"
  39. #include "sdio_bus.h"
  40. #include "pwrseq.h"
  41. #include "mmc_ops.h"
  42. #include "sd_ops.h"
  43. #include "sdio_ops.h"
  44. /* The max erase timeout, used when host->max_busy_timeout isn't specified */
  45. #define MMC_ERASE_TIMEOUT_MS (60 * 1000) /* 60 s */
  46. #define SD_DISCARD_TIMEOUT_MS (250)
  47. static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
  48. /*
  49. * Enabling software CRCs on the data blocks can be a significant (30%)
  50. * performance cost, and for other reasons may not always be desired.
  51. * So we allow it to be disabled.
  52. */
  53. bool use_spi_crc = 1;
  54. module_param(use_spi_crc, bool, 0);
  55. static int mmc_schedule_delayed_work(struct delayed_work *work,
  56. unsigned long delay)
  57. {
  58. /*
  59. * We use the system_freezable_wq, because of two reasons.
  60. * First, it allows several works (not the same work item) to be
  61. * executed simultaneously. Second, the queue becomes frozen when
  62. * userspace becomes frozen during system PM.
  63. */
  64. return queue_delayed_work(system_freezable_wq, work, delay);
  65. }
  66. #ifdef CONFIG_FAIL_MMC_REQUEST
  67. /*
  68. * Internal function. Inject random data errors.
  69. * If mmc_data is NULL no errors are injected.
  70. */
  71. static void mmc_should_fail_request(struct mmc_host *host,
  72. struct mmc_request *mrq)
  73. {
  74. struct mmc_command *cmd = mrq->cmd;
  75. struct mmc_data *data = mrq->data;
  76. static const int data_errors[] = {
  77. -ETIMEDOUT,
  78. -EILSEQ,
  79. -EIO,
  80. };
  81. if (!data)
  82. return;
  83. if ((cmd && cmd->error) || data->error ||
  84. !should_fail(&host->fail_mmc_request, data->blksz * data->blocks))
  85. return;
  86. data->error = data_errors[get_random_u32_below(ARRAY_SIZE(data_errors))];
  87. data->bytes_xfered = get_random_u32_below(data->bytes_xfered >> 9) << 9;
  88. }
  89. #else /* CONFIG_FAIL_MMC_REQUEST */
  90. static inline void mmc_should_fail_request(struct mmc_host *host,
  91. struct mmc_request *mrq)
  92. {
  93. }
  94. #endif /* CONFIG_FAIL_MMC_REQUEST */
  95. static inline void mmc_complete_cmd(struct mmc_request *mrq)
  96. {
  97. if (mrq->cap_cmd_during_tfr && !completion_done(&mrq->cmd_completion))
  98. complete_all(&mrq->cmd_completion);
  99. }
  100. void mmc_command_done(struct mmc_host *host, struct mmc_request *mrq)
  101. {
  102. if (!mrq->cap_cmd_during_tfr)
  103. return;
  104. mmc_complete_cmd(mrq);
  105. pr_debug("%s: cmd done, tfr ongoing (CMD%u)\n",
  106. mmc_hostname(host), mrq->cmd->opcode);
  107. }
  108. EXPORT_SYMBOL(mmc_command_done);
  109. /**
  110. * mmc_request_done - finish processing an MMC request
  111. * @host: MMC host which completed request
  112. * @mrq: MMC request which request
  113. *
  114. * MMC drivers should call this function when they have completed
  115. * their processing of a request.
  116. */
  117. void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
  118. {
  119. struct mmc_command *cmd = mrq->cmd;
  120. int err = cmd->error;
  121. /* Flag re-tuning needed on CRC errors */
  122. if (!mmc_op_tuning(cmd->opcode) &&
  123. !host->retune_crc_disable &&
  124. (err == -EILSEQ || (mrq->sbc && mrq->sbc->error == -EILSEQ) ||
  125. (mrq->data && mrq->data->error == -EILSEQ) ||
  126. (mrq->stop && mrq->stop->error == -EILSEQ)))
  127. mmc_retune_needed(host);
  128. if (err && cmd->retries && mmc_host_is_spi(host)) {
  129. if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
  130. cmd->retries = 0;
  131. }
  132. if (host->ongoing_mrq == mrq)
  133. host->ongoing_mrq = NULL;
  134. mmc_complete_cmd(mrq);
  135. trace_mmc_request_done(host, mrq);
  136. /*
  137. * We list various conditions for the command to be considered
  138. * properly done:
  139. *
  140. * - There was no error, OK fine then
  141. * - We are not doing some kind of retry
  142. * - The card was removed (...so just complete everything no matter
  143. * if there are errors or retries)
  144. */
  145. if (!err || !cmd->retries || mmc_card_removed(host->card)) {
  146. mmc_should_fail_request(host, mrq);
  147. if (!host->ongoing_mrq)
  148. led_trigger_event(host->led, LED_OFF);
  149. if (mrq->sbc) {
  150. pr_debug("%s: req done <CMD%u>: %d: %08x %08x %08x %08x\n",
  151. mmc_hostname(host), mrq->sbc->opcode,
  152. mrq->sbc->error,
  153. mrq->sbc->resp[0], mrq->sbc->resp[1],
  154. mrq->sbc->resp[2], mrq->sbc->resp[3]);
  155. }
  156. pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
  157. mmc_hostname(host), cmd->opcode, err,
  158. cmd->resp[0], cmd->resp[1],
  159. cmd->resp[2], cmd->resp[3]);
  160. if (mrq->data) {
  161. pr_debug("%s: %d bytes transferred: %d\n",
  162. mmc_hostname(host),
  163. mrq->data->bytes_xfered, mrq->data->error);
  164. }
  165. if (mrq->stop) {
  166. pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
  167. mmc_hostname(host), mrq->stop->opcode,
  168. mrq->stop->error,
  169. mrq->stop->resp[0], mrq->stop->resp[1],
  170. mrq->stop->resp[2], mrq->stop->resp[3]);
  171. }
  172. }
  173. /*
  174. * Request starter must handle retries - see
  175. * mmc_wait_for_req_done().
  176. */
  177. if (mrq->done)
  178. mrq->done(mrq);
  179. }
  180. EXPORT_SYMBOL(mmc_request_done);
  181. static void __mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
  182. {
  183. int err;
  184. /* Assumes host controller has been runtime resumed by mmc_claim_host */
  185. err = mmc_retune(host);
  186. if (err) {
  187. mrq->cmd->error = err;
  188. mmc_request_done(host, mrq);
  189. return;
  190. }
  191. /*
  192. * For sdio rw commands we must wait for card busy otherwise some
  193. * sdio devices won't work properly.
  194. * And bypass I/O abort, reset and bus suspend operations.
  195. */
  196. if (sdio_is_io_busy(mrq->cmd->opcode, mrq->cmd->arg) &&
  197. host->ops->card_busy) {
  198. int tries = 500; /* Wait aprox 500ms at maximum */
  199. while (host->ops->card_busy(host) && --tries)
  200. mmc_delay(1);
  201. if (tries == 0) {
  202. mrq->cmd->error = -EBUSY;
  203. mmc_request_done(host, mrq);
  204. return;
  205. }
  206. }
  207. if (mrq->cap_cmd_during_tfr) {
  208. host->ongoing_mrq = mrq;
  209. /*
  210. * Retry path could come through here without having waiting on
  211. * cmd_completion, so ensure it is reinitialised.
  212. */
  213. reinit_completion(&mrq->cmd_completion);
  214. }
  215. trace_mmc_request_start(host, mrq);
  216. if (host->cqe_on)
  217. host->cqe_ops->cqe_off(host);
  218. host->ops->request(host, mrq);
  219. }
  220. static void mmc_mrq_pr_debug(struct mmc_host *host, struct mmc_request *mrq,
  221. bool cqe)
  222. {
  223. if (mrq->sbc) {
  224. pr_debug("<%s: starting CMD%u arg %08x flags %08x>\n",
  225. mmc_hostname(host), mrq->sbc->opcode,
  226. mrq->sbc->arg, mrq->sbc->flags);
  227. }
  228. if (mrq->cmd) {
  229. pr_debug("%s: starting %sCMD%u arg %08x flags %08x\n",
  230. mmc_hostname(host), cqe ? "CQE direct " : "",
  231. mrq->cmd->opcode, mrq->cmd->arg, mrq->cmd->flags);
  232. } else if (cqe) {
  233. pr_debug("%s: starting CQE transfer for tag %d blkaddr %u\n",
  234. mmc_hostname(host), mrq->tag, mrq->data->blk_addr);
  235. }
  236. if (mrq->data) {
  237. pr_debug("%s: blksz %d blocks %d flags %08x "
  238. "tsac %d ms nsac %d\n",
  239. mmc_hostname(host), mrq->data->blksz,
  240. mrq->data->blocks, mrq->data->flags,
  241. mrq->data->timeout_ns / 1000000,
  242. mrq->data->timeout_clks);
  243. }
  244. if (mrq->stop) {
  245. pr_debug("%s: CMD%u arg %08x flags %08x\n",
  246. mmc_hostname(host), mrq->stop->opcode,
  247. mrq->stop->arg, mrq->stop->flags);
  248. }
  249. }
  250. static int mmc_mrq_prep(struct mmc_host *host, struct mmc_request *mrq)
  251. {
  252. unsigned int i, sz = 0;
  253. struct scatterlist *sg;
  254. if (mrq->cmd) {
  255. mrq->cmd->error = 0;
  256. mrq->cmd->mrq = mrq;
  257. mrq->cmd->data = mrq->data;
  258. }
  259. if (mrq->sbc) {
  260. mrq->sbc->error = 0;
  261. mrq->sbc->mrq = mrq;
  262. }
  263. if (mrq->data) {
  264. if (mrq->data->blksz > host->max_blk_size ||
  265. mrq->data->blocks > host->max_blk_count ||
  266. mrq->data->blocks * mrq->data->blksz > host->max_req_size)
  267. return -EINVAL;
  268. for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
  269. sz += sg->length;
  270. if (sz != mrq->data->blocks * mrq->data->blksz)
  271. return -EINVAL;
  272. mrq->data->error = 0;
  273. mrq->data->mrq = mrq;
  274. if (mrq->stop) {
  275. mrq->data->stop = mrq->stop;
  276. mrq->stop->error = 0;
  277. mrq->stop->mrq = mrq;
  278. }
  279. }
  280. return 0;
  281. }
  282. int mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
  283. {
  284. int err;
  285. if (mrq->cmd->has_ext_addr)
  286. mmc_send_ext_addr(host, mrq->cmd->ext_addr);
  287. init_completion(&mrq->cmd_completion);
  288. mmc_retune_hold(host);
  289. if (mmc_card_removed(host->card))
  290. return -ENOMEDIUM;
  291. mmc_mrq_pr_debug(host, mrq, false);
  292. WARN_ON(!host->claimed);
  293. err = mmc_mrq_prep(host, mrq);
  294. if (err)
  295. return err;
  296. if (host->uhs2_sd_tran)
  297. mmc_uhs2_prepare_cmd(host, mrq);
  298. led_trigger_event(host->led, LED_FULL);
  299. __mmc_start_request(host, mrq);
  300. return 0;
  301. }
  302. EXPORT_SYMBOL(mmc_start_request);
  303. static void mmc_wait_done(struct mmc_request *mrq)
  304. {
  305. complete(&mrq->completion);
  306. }
  307. static inline void mmc_wait_ongoing_tfr_cmd(struct mmc_host *host)
  308. {
  309. struct mmc_request *ongoing_mrq = READ_ONCE(host->ongoing_mrq);
  310. /*
  311. * If there is an ongoing transfer, wait for the command line to become
  312. * available.
  313. */
  314. if (ongoing_mrq && !completion_done(&ongoing_mrq->cmd_completion))
  315. wait_for_completion(&ongoing_mrq->cmd_completion);
  316. }
  317. static int __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq)
  318. {
  319. int err;
  320. mmc_wait_ongoing_tfr_cmd(host);
  321. init_completion(&mrq->completion);
  322. mrq->done = mmc_wait_done;
  323. err = mmc_start_request(host, mrq);
  324. if (err) {
  325. mrq->cmd->error = err;
  326. mmc_complete_cmd(mrq);
  327. complete(&mrq->completion);
  328. }
  329. return err;
  330. }
  331. void mmc_wait_for_req_done(struct mmc_host *host, struct mmc_request *mrq)
  332. {
  333. struct mmc_command *cmd;
  334. while (1) {
  335. wait_for_completion(&mrq->completion);
  336. cmd = mrq->cmd;
  337. if (!cmd->error || !cmd->retries ||
  338. mmc_card_removed(host->card))
  339. break;
  340. mmc_retune_recheck(host);
  341. pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
  342. mmc_hostname(host), cmd->opcode, cmd->error);
  343. cmd->retries--;
  344. cmd->error = 0;
  345. __mmc_start_request(host, mrq);
  346. }
  347. mmc_retune_release(host);
  348. }
  349. EXPORT_SYMBOL(mmc_wait_for_req_done);
  350. /*
  351. * mmc_cqe_start_req - Start a CQE request.
  352. * @host: MMC host to start the request
  353. * @mrq: request to start
  354. *
  355. * Start the request, re-tuning if needed and it is possible. Returns an error
  356. * code if the request fails to start or -EBUSY if CQE is busy.
  357. */
  358. int mmc_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq)
  359. {
  360. int err;
  361. /*
  362. * CQE cannot process re-tuning commands. Caller must hold retuning
  363. * while CQE is in use. Re-tuning can happen here only when CQE has no
  364. * active requests i.e. this is the first. Note, re-tuning will call
  365. * ->cqe_off().
  366. */
  367. err = mmc_retune(host);
  368. if (err)
  369. goto out_err;
  370. mrq->host = host;
  371. mmc_mrq_pr_debug(host, mrq, true);
  372. err = mmc_mrq_prep(host, mrq);
  373. if (err)
  374. goto out_err;
  375. if (host->uhs2_sd_tran)
  376. mmc_uhs2_prepare_cmd(host, mrq);
  377. err = host->cqe_ops->cqe_request(host, mrq);
  378. if (err)
  379. goto out_err;
  380. trace_mmc_request_start(host, mrq);
  381. return 0;
  382. out_err:
  383. if (mrq->cmd) {
  384. pr_debug("%s: failed to start CQE direct CMD%u, error %d\n",
  385. mmc_hostname(host), mrq->cmd->opcode, err);
  386. } else {
  387. pr_debug("%s: failed to start CQE transfer for tag %d, error %d\n",
  388. mmc_hostname(host), mrq->tag, err);
  389. }
  390. return err;
  391. }
  392. EXPORT_SYMBOL(mmc_cqe_start_req);
  393. /**
  394. * mmc_cqe_request_done - CQE has finished processing an MMC request
  395. * @host: MMC host which completed request
  396. * @mrq: MMC request which completed
  397. *
  398. * CQE drivers should call this function when they have completed
  399. * their processing of a request.
  400. */
  401. void mmc_cqe_request_done(struct mmc_host *host, struct mmc_request *mrq)
  402. {
  403. mmc_should_fail_request(host, mrq);
  404. /* Flag re-tuning needed on CRC errors */
  405. if ((mrq->cmd && mrq->cmd->error == -EILSEQ) ||
  406. (mrq->data && mrq->data->error == -EILSEQ))
  407. mmc_retune_needed(host);
  408. trace_mmc_request_done(host, mrq);
  409. if (mrq->cmd) {
  410. pr_debug("%s: CQE req done (direct CMD%u): %d\n",
  411. mmc_hostname(host), mrq->cmd->opcode, mrq->cmd->error);
  412. } else {
  413. pr_debug("%s: CQE transfer done tag %d\n",
  414. mmc_hostname(host), mrq->tag);
  415. }
  416. if (mrq->data) {
  417. pr_debug("%s: %d bytes transferred: %d\n",
  418. mmc_hostname(host),
  419. mrq->data->bytes_xfered, mrq->data->error);
  420. }
  421. mrq->done(mrq);
  422. }
  423. EXPORT_SYMBOL(mmc_cqe_request_done);
  424. /**
  425. * mmc_cqe_post_req - CQE post process of a completed MMC request
  426. * @host: MMC host
  427. * @mrq: MMC request to be processed
  428. */
  429. void mmc_cqe_post_req(struct mmc_host *host, struct mmc_request *mrq)
  430. {
  431. if (host->cqe_ops->cqe_post_req)
  432. host->cqe_ops->cqe_post_req(host, mrq);
  433. }
  434. EXPORT_SYMBOL(mmc_cqe_post_req);
  435. /* Arbitrary 1 second timeout */
  436. #define MMC_CQE_RECOVERY_TIMEOUT 1000
  437. /*
  438. * mmc_cqe_recovery - Recover from CQE errors.
  439. * @host: MMC host to recover
  440. *
  441. * Recovery consists of stopping CQE, stopping eMMC, discarding the queue
  442. * in eMMC, and discarding the queue in CQE. CQE must call
  443. * mmc_cqe_request_done() on all requests. An error is returned if the eMMC
  444. * fails to discard its queue.
  445. */
  446. int mmc_cqe_recovery(struct mmc_host *host)
  447. {
  448. struct mmc_command cmd;
  449. int err;
  450. mmc_retune_hold_now(host);
  451. /*
  452. * Recovery is expected seldom, if at all, but it reduces performance,
  453. * so make sure it is not completely silent.
  454. */
  455. pr_warn("%s: running CQE recovery\n", mmc_hostname(host));
  456. host->cqe_ops->cqe_recovery_start(host);
  457. memset(&cmd, 0, sizeof(cmd));
  458. cmd.opcode = MMC_STOP_TRANSMISSION;
  459. cmd.flags = MMC_RSP_R1B_NO_CRC | MMC_CMD_AC; /* Ignore CRC */
  460. cmd.busy_timeout = MMC_CQE_RECOVERY_TIMEOUT;
  461. mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  462. mmc_poll_for_busy(host->card, MMC_CQE_RECOVERY_TIMEOUT, true, MMC_BUSY_IO);
  463. memset(&cmd, 0, sizeof(cmd));
  464. cmd.opcode = MMC_CMDQ_TASK_MGMT;
  465. cmd.arg = 1; /* Discard entire queue */
  466. cmd.flags = MMC_RSP_R1B_NO_CRC | MMC_CMD_AC; /* Ignore CRC */
  467. cmd.busy_timeout = MMC_CQE_RECOVERY_TIMEOUT;
  468. err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  469. host->cqe_ops->cqe_recovery_finish(host);
  470. if (err)
  471. err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  472. mmc_retune_release(host);
  473. return err;
  474. }
  475. EXPORT_SYMBOL(mmc_cqe_recovery);
  476. /**
  477. * mmc_is_req_done - Determine if a 'cap_cmd_during_tfr' request is done
  478. * @host: MMC host
  479. * @mrq: MMC request
  480. *
  481. * mmc_is_req_done() is used with requests that have
  482. * mrq->cap_cmd_during_tfr = true. mmc_is_req_done() must be called after
  483. * starting a request and before waiting for it to complete. That is,
  484. * either in between calls to mmc_start_req(), or after mmc_wait_for_req()
  485. * and before mmc_wait_for_req_done(). If it is called at other times the
  486. * result is not meaningful.
  487. */
  488. bool mmc_is_req_done(struct mmc_host *host, struct mmc_request *mrq)
  489. {
  490. return completion_done(&mrq->completion);
  491. }
  492. EXPORT_SYMBOL(mmc_is_req_done);
  493. /**
  494. * mmc_wait_for_req - start a request and wait for completion
  495. * @host: MMC host to start command
  496. * @mrq: MMC request to start
  497. *
  498. * Start a new MMC custom command request for a host, and wait
  499. * for the command to complete. In the case of 'cap_cmd_during_tfr'
  500. * requests, the transfer is ongoing and the caller can issue further
  501. * commands that do not use the data lines, and then wait by calling
  502. * mmc_wait_for_req_done().
  503. * Does not attempt to parse the response.
  504. */
  505. void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
  506. {
  507. __mmc_start_req(host, mrq);
  508. if (!mrq->cap_cmd_during_tfr)
  509. mmc_wait_for_req_done(host, mrq);
  510. }
  511. EXPORT_SYMBOL(mmc_wait_for_req);
  512. /**
  513. * mmc_wait_for_cmd - start a command and wait for completion
  514. * @host: MMC host to start command
  515. * @cmd: MMC command to start
  516. * @retries: maximum number of retries
  517. *
  518. * Start a new MMC command for a host, and wait for the command
  519. * to complete. Return any error that occurred while the command
  520. * was executing. Do not attempt to parse the response.
  521. */
  522. int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
  523. {
  524. struct mmc_request mrq = {};
  525. WARN_ON(!host->claimed);
  526. memset(cmd->resp, 0, sizeof(cmd->resp));
  527. cmd->retries = retries;
  528. mrq.cmd = cmd;
  529. cmd->data = NULL;
  530. mmc_wait_for_req(host, &mrq);
  531. return cmd->error;
  532. }
  533. EXPORT_SYMBOL(mmc_wait_for_cmd);
  534. /**
  535. * mmc_set_data_timeout - set the timeout for a data command
  536. * @data: data phase for command
  537. * @card: the MMC card associated with the data transfer
  538. *
  539. * Computes the data timeout parameters according to the
  540. * correct algorithm given the card type.
  541. */
  542. void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
  543. {
  544. unsigned int mult;
  545. /*
  546. * SDIO cards only define an upper 1 s limit on access.
  547. */
  548. if (mmc_card_sdio(card)) {
  549. data->timeout_ns = 1000000000;
  550. data->timeout_clks = 0;
  551. return;
  552. }
  553. /*
  554. * SD cards use a 100 multiplier rather than 10
  555. */
  556. mult = mmc_card_sd(card) ? 100 : 10;
  557. /*
  558. * Scale up the multiplier (and therefore the timeout) by
  559. * the r2w factor for writes.
  560. */
  561. if (data->flags & MMC_DATA_WRITE)
  562. mult <<= card->csd.r2w_factor;
  563. data->timeout_ns = card->csd.taac_ns * mult;
  564. data->timeout_clks = card->csd.taac_clks * mult;
  565. /*
  566. * SD cards also have an upper limit on the timeout.
  567. */
  568. if (mmc_card_sd(card)) {
  569. unsigned int timeout_us, limit_us;
  570. timeout_us = data->timeout_ns / 1000;
  571. if (card->host->ios.clock)
  572. timeout_us += data->timeout_clks * 1000 /
  573. (card->host->ios.clock / 1000);
  574. if (data->flags & MMC_DATA_WRITE)
  575. /*
  576. * The MMC spec "It is strongly recommended
  577. * for hosts to implement more than 500ms
  578. * timeout value even if the card indicates
  579. * the 250ms maximum busy length." Even the
  580. * previous value of 300ms is known to be
  581. * insufficient for some cards.
  582. */
  583. limit_us = 3000000;
  584. else
  585. limit_us = 100000;
  586. /*
  587. * SDHC cards always use these fixed values.
  588. */
  589. if (timeout_us > limit_us) {
  590. data->timeout_ns = limit_us * 1000;
  591. data->timeout_clks = 0;
  592. }
  593. /* assign limit value if invalid */
  594. if (timeout_us == 0)
  595. data->timeout_ns = limit_us * 1000;
  596. }
  597. /*
  598. * Some cards require longer data read timeout than indicated in CSD.
  599. * Address this by setting the read timeout to a "reasonably high"
  600. * value. For the cards tested, 600ms has proven enough. If necessary,
  601. * this value can be increased if other problematic cards require this.
  602. */
  603. if (mmc_card_long_read_time(card) && data->flags & MMC_DATA_READ) {
  604. data->timeout_ns = 600000000;
  605. data->timeout_clks = 0;
  606. }
  607. /*
  608. * Some cards need very high timeouts if driven in SPI mode.
  609. * The worst observed timeout was 900ms after writing a
  610. * continuous stream of data until the internal logic
  611. * overflowed.
  612. */
  613. if (mmc_host_is_spi(card->host)) {
  614. if (data->flags & MMC_DATA_WRITE) {
  615. if (data->timeout_ns < 1000000000)
  616. data->timeout_ns = 1000000000; /* 1s */
  617. } else {
  618. if (data->timeout_ns < 100000000)
  619. data->timeout_ns = 100000000; /* 100ms */
  620. }
  621. }
  622. }
  623. EXPORT_SYMBOL(mmc_set_data_timeout);
  624. /*
  625. * Allow claiming an already claimed host if the context is the same or there is
  626. * no context but the task is the same.
  627. */
  628. static inline bool mmc_ctx_matches(struct mmc_host *host, struct mmc_ctx *ctx,
  629. struct task_struct *task)
  630. {
  631. return host->claimer == ctx ||
  632. (!ctx && task && host->claimer->task == task);
  633. }
  634. static inline void mmc_ctx_set_claimer(struct mmc_host *host,
  635. struct mmc_ctx *ctx,
  636. struct task_struct *task)
  637. {
  638. if (!host->claimer) {
  639. if (ctx)
  640. host->claimer = ctx;
  641. else
  642. host->claimer = &host->default_ctx;
  643. }
  644. if (task)
  645. host->claimer->task = task;
  646. }
  647. /**
  648. * __mmc_claim_host - exclusively claim a host
  649. * @host: mmc host to claim
  650. * @ctx: context that claims the host or NULL in which case the default
  651. * context will be used
  652. * @abort: whether or not the operation should be aborted
  653. *
  654. * Claim a host for a set of operations. If @abort is non null and
  655. * dereference a non-zero value then this will return prematurely with
  656. * that non-zero value without acquiring the lock. Returns zero
  657. * with the lock held otherwise.
  658. */
  659. int __mmc_claim_host(struct mmc_host *host, struct mmc_ctx *ctx,
  660. atomic_t *abort)
  661. {
  662. struct task_struct *task = ctx ? NULL : current;
  663. DECLARE_WAITQUEUE(wait, current);
  664. unsigned long flags;
  665. int stop;
  666. bool pm = false;
  667. might_sleep();
  668. add_wait_queue(&host->wq, &wait);
  669. spin_lock_irqsave(&host->lock, flags);
  670. while (1) {
  671. set_current_state(TASK_UNINTERRUPTIBLE);
  672. stop = abort ? atomic_read(abort) : 0;
  673. if (stop || !host->claimed || mmc_ctx_matches(host, ctx, task))
  674. break;
  675. spin_unlock_irqrestore(&host->lock, flags);
  676. schedule();
  677. spin_lock_irqsave(&host->lock, flags);
  678. }
  679. set_current_state(TASK_RUNNING);
  680. if (!stop) {
  681. host->claimed = 1;
  682. mmc_ctx_set_claimer(host, ctx, task);
  683. host->claim_cnt += 1;
  684. if (host->claim_cnt == 1)
  685. pm = true;
  686. } else
  687. wake_up(&host->wq);
  688. spin_unlock_irqrestore(&host->lock, flags);
  689. remove_wait_queue(&host->wq, &wait);
  690. if (pm)
  691. pm_runtime_get_sync(mmc_dev(host));
  692. return stop;
  693. }
  694. EXPORT_SYMBOL(__mmc_claim_host);
  695. /**
  696. * mmc_release_host - release a host
  697. * @host: mmc host to release
  698. *
  699. * Release a MMC host, allowing others to claim the host
  700. * for their operations.
  701. */
  702. void mmc_release_host(struct mmc_host *host)
  703. {
  704. unsigned long flags;
  705. WARN_ON(!host->claimed);
  706. spin_lock_irqsave(&host->lock, flags);
  707. if (--host->claim_cnt) {
  708. /* Release for nested claim */
  709. spin_unlock_irqrestore(&host->lock, flags);
  710. } else {
  711. host->claimed = 0;
  712. host->claimer->task = NULL;
  713. host->claimer = NULL;
  714. spin_unlock_irqrestore(&host->lock, flags);
  715. wake_up(&host->wq);
  716. pm_runtime_mark_last_busy(mmc_dev(host));
  717. if (host->caps & MMC_CAP_SYNC_RUNTIME_PM)
  718. pm_runtime_put_sync_suspend(mmc_dev(host));
  719. else
  720. pm_runtime_put_autosuspend(mmc_dev(host));
  721. }
  722. }
  723. EXPORT_SYMBOL(mmc_release_host);
  724. /*
  725. * This is a helper function, which fetches a runtime pm reference for the
  726. * card device and also claims the host.
  727. */
  728. void mmc_get_card(struct mmc_card *card, struct mmc_ctx *ctx)
  729. {
  730. pm_runtime_get_sync(&card->dev);
  731. __mmc_claim_host(card->host, ctx, NULL);
  732. }
  733. EXPORT_SYMBOL(mmc_get_card);
  734. /*
  735. * This is a helper function, which releases the host and drops the runtime
  736. * pm reference for the card device.
  737. */
  738. void mmc_put_card(struct mmc_card *card, struct mmc_ctx *ctx)
  739. {
  740. struct mmc_host *host = card->host;
  741. WARN_ON(ctx && host->claimer != ctx);
  742. mmc_release_host(host);
  743. pm_runtime_put_autosuspend(&card->dev);
  744. }
  745. EXPORT_SYMBOL(mmc_put_card);
  746. /*
  747. * Internal function that does the actual ios call to the host driver,
  748. * optionally printing some debug output.
  749. */
  750. static inline void mmc_set_ios(struct mmc_host *host)
  751. {
  752. struct mmc_ios *ios = &host->ios;
  753. pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
  754. "width %u timing %u\n",
  755. mmc_hostname(host), ios->clock, ios->bus_mode,
  756. ios->power_mode, ios->chip_select, ios->vdd,
  757. 1 << ios->bus_width, ios->timing);
  758. host->ops->set_ios(host, ios);
  759. }
  760. /*
  761. * Control chip select pin on a host.
  762. */
  763. void mmc_set_chip_select(struct mmc_host *host, int mode)
  764. {
  765. host->ios.chip_select = mode;
  766. mmc_set_ios(host);
  767. }
  768. /*
  769. * Sets the host clock to the highest possible frequency that
  770. * is below "hz".
  771. */
  772. void mmc_set_clock(struct mmc_host *host, unsigned int hz)
  773. {
  774. WARN_ON(hz && hz < host->f_min);
  775. if (hz > host->f_max)
  776. hz = host->f_max;
  777. host->ios.clock = hz;
  778. mmc_set_ios(host);
  779. }
  780. int mmc_execute_tuning(struct mmc_card *card)
  781. {
  782. struct mmc_host *host = card->host;
  783. u32 opcode;
  784. int err;
  785. if (!host->ops->execute_tuning)
  786. return 0;
  787. if (host->cqe_on)
  788. host->cqe_ops->cqe_off(host);
  789. if (mmc_card_mmc(card))
  790. opcode = MMC_SEND_TUNING_BLOCK_HS200;
  791. else
  792. opcode = MMC_SEND_TUNING_BLOCK;
  793. err = host->ops->execute_tuning(host, opcode);
  794. if (!err) {
  795. mmc_retune_clear(host);
  796. mmc_retune_enable(host);
  797. return 0;
  798. }
  799. /* Only print error when we don't check for card removal */
  800. if (!host->detect_change) {
  801. pr_err("%s: tuning execution failed: %d\n",
  802. mmc_hostname(host), err);
  803. mmc_debugfs_err_stats_inc(host, MMC_ERR_TUNING);
  804. }
  805. return err;
  806. }
  807. /*
  808. * Change the bus mode (open drain/push-pull) of a host.
  809. */
  810. void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
  811. {
  812. host->ios.bus_mode = mode;
  813. mmc_set_ios(host);
  814. }
  815. /*
  816. * Change data bus width of a host.
  817. */
  818. void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
  819. {
  820. host->ios.bus_width = width;
  821. mmc_set_ios(host);
  822. }
  823. /*
  824. * Set initial state after a power cycle or a hw_reset.
  825. */
  826. void mmc_set_initial_state(struct mmc_host *host)
  827. {
  828. if (host->cqe_on)
  829. host->cqe_ops->cqe_off(host);
  830. mmc_retune_disable(host);
  831. if (mmc_host_is_spi(host))
  832. host->ios.chip_select = MMC_CS_HIGH;
  833. else
  834. host->ios.chip_select = MMC_CS_DONTCARE;
  835. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  836. host->ios.bus_width = MMC_BUS_WIDTH_1;
  837. host->ios.timing = MMC_TIMING_LEGACY;
  838. host->ios.drv_type = 0;
  839. host->ios.enhanced_strobe = false;
  840. /*
  841. * Make sure we are in non-enhanced strobe mode before we
  842. * actually enable it in ext_csd.
  843. */
  844. if ((host->caps2 & MMC_CAP2_HS400_ES) &&
  845. host->ops->hs400_enhanced_strobe)
  846. host->ops->hs400_enhanced_strobe(host, &host->ios);
  847. mmc_set_ios(host);
  848. mmc_crypto_set_initial_state(host);
  849. }
  850. /**
  851. * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
  852. * @vdd: voltage (mV)
  853. * @low_bits: prefer low bits in boundary cases
  854. *
  855. * This function returns the OCR bit number according to the provided @vdd
  856. * value. If conversion is not possible a negative errno value returned.
  857. *
  858. * Depending on the @low_bits flag the function prefers low or high OCR bits
  859. * on boundary voltages. For example,
  860. * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
  861. * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
  862. *
  863. * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
  864. */
  865. static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
  866. {
  867. const int max_bit = ilog2(MMC_VDD_35_36);
  868. int bit;
  869. if (vdd < 1650 || vdd > 3600)
  870. return -EINVAL;
  871. if (vdd >= 1650 && vdd <= 1950)
  872. return ilog2(MMC_VDD_165_195);
  873. if (low_bits)
  874. vdd -= 1;
  875. /* Base 2000 mV, step 100 mV, bit's base 8. */
  876. bit = (vdd - 2000) / 100 + 8;
  877. if (bit > max_bit)
  878. return max_bit;
  879. return bit;
  880. }
  881. /**
  882. * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
  883. * @vdd_min: minimum voltage value (mV)
  884. * @vdd_max: maximum voltage value (mV)
  885. *
  886. * This function returns the OCR mask bits according to the provided @vdd_min
  887. * and @vdd_max values. If conversion is not possible the function returns 0.
  888. *
  889. * Notes wrt boundary cases:
  890. * This function sets the OCR bits for all boundary voltages, for example
  891. * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
  892. * MMC_VDD_34_35 mask.
  893. */
  894. u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
  895. {
  896. u32 mask = 0;
  897. if (vdd_max < vdd_min)
  898. return 0;
  899. /* Prefer high bits for the boundary vdd_max values. */
  900. vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
  901. if (vdd_max < 0)
  902. return 0;
  903. /* Prefer low bits for the boundary vdd_min values. */
  904. vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
  905. if (vdd_min < 0)
  906. return 0;
  907. /* Fill the mask, from max bit to min bit. */
  908. while (vdd_max >= vdd_min)
  909. mask |= 1 << vdd_max--;
  910. return mask;
  911. }
  912. static int mmc_of_get_func_num(struct device_node *node)
  913. {
  914. u32 reg;
  915. int ret;
  916. ret = of_property_read_u32(node, "reg", &reg);
  917. if (ret < 0)
  918. return ret;
  919. return reg;
  920. }
  921. struct device_node *mmc_of_find_child_device(struct mmc_host *host,
  922. unsigned func_num)
  923. {
  924. struct device_node *node;
  925. if (!host->parent || !host->parent->of_node)
  926. return NULL;
  927. for_each_child_of_node(host->parent->of_node, node) {
  928. if (mmc_of_get_func_num(node) == func_num)
  929. return node;
  930. }
  931. return NULL;
  932. }
  933. /*
  934. * Mask off any voltages we don't support and select
  935. * the lowest voltage
  936. */
  937. u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
  938. {
  939. int bit;
  940. /*
  941. * Sanity check the voltages that the card claims to
  942. * support.
  943. */
  944. if (ocr & 0x7F) {
  945. dev_warn(mmc_dev(host),
  946. "card claims to support voltages below defined range\n");
  947. ocr &= ~0x7F;
  948. }
  949. ocr &= host->ocr_avail;
  950. if (!ocr) {
  951. dev_warn(mmc_dev(host), "no support for card's volts\n");
  952. return 0;
  953. }
  954. if (!mmc_card_uhs2(host) && host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) {
  955. bit = ffs(ocr) - 1;
  956. ocr &= 3 << bit;
  957. mmc_power_cycle(host, ocr);
  958. } else {
  959. bit = fls(ocr) - 1;
  960. /*
  961. * The bit variable represents the highest voltage bit set in
  962. * the OCR register.
  963. * To keep a range of 2 values (e.g. 3.2V/3.3V and 3.3V/3.4V),
  964. * we must shift the mask '3' with (bit - 1).
  965. */
  966. ocr &= 3 << (bit - 1);
  967. if (bit != host->ios.vdd)
  968. dev_warn(mmc_dev(host), "exceeding card's volts\n");
  969. }
  970. return ocr;
  971. }
  972. int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage)
  973. {
  974. int err = 0;
  975. int old_signal_voltage = host->ios.signal_voltage;
  976. host->ios.signal_voltage = signal_voltage;
  977. if (host->ops->start_signal_voltage_switch)
  978. err = host->ops->start_signal_voltage_switch(host, &host->ios);
  979. if (err)
  980. host->ios.signal_voltage = old_signal_voltage;
  981. return err;
  982. }
  983. void mmc_set_initial_signal_voltage(struct mmc_host *host)
  984. {
  985. /* Try to set signal voltage to 3.3V but fall back to 1.8v or 1.2v */
  986. if (!mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330))
  987. dev_dbg(mmc_dev(host), "Initial signal voltage of 3.3v\n");
  988. else if (!mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180))
  989. dev_dbg(mmc_dev(host), "Initial signal voltage of 1.8v\n");
  990. else if (!mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120))
  991. dev_dbg(mmc_dev(host), "Initial signal voltage of 1.2v\n");
  992. }
  993. int mmc_host_set_uhs_voltage(struct mmc_host *host)
  994. {
  995. u32 clock;
  996. /*
  997. * During a signal voltage level switch, the clock must be gated
  998. * for 5 ms according to the SD spec
  999. */
  1000. clock = host->ios.clock;
  1001. host->ios.clock = 0;
  1002. mmc_set_ios(host);
  1003. if (mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180))
  1004. return -EAGAIN;
  1005. /* Keep clock gated for at least 10 ms, though spec only says 5 ms */
  1006. mmc_delay(10);
  1007. host->ios.clock = clock;
  1008. mmc_set_ios(host);
  1009. return 0;
  1010. }
  1011. int mmc_set_uhs_voltage(struct mmc_host *host, u32 ocr)
  1012. {
  1013. struct mmc_command cmd = {};
  1014. int err = 0;
  1015. /*
  1016. * If we cannot switch voltages, return failure so the caller
  1017. * can continue without UHS mode
  1018. */
  1019. if (!host->ops->start_signal_voltage_switch)
  1020. return -EPERM;
  1021. if (!host->ops->card_busy)
  1022. pr_warn("%s: cannot verify signal voltage switch\n",
  1023. mmc_hostname(host));
  1024. cmd.opcode = SD_SWITCH_VOLTAGE;
  1025. cmd.arg = 0;
  1026. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1027. err = mmc_wait_for_cmd(host, &cmd, 0);
  1028. if (err)
  1029. goto power_cycle;
  1030. if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
  1031. return -EIO;
  1032. /*
  1033. * The card should drive cmd and dat[0:3] low immediately
  1034. * after the response of cmd11, but wait 1 ms to be sure
  1035. */
  1036. mmc_delay(1);
  1037. if (host->ops->card_busy && !host->ops->card_busy(host)) {
  1038. err = -EAGAIN;
  1039. goto power_cycle;
  1040. }
  1041. if (mmc_host_set_uhs_voltage(host)) {
  1042. /*
  1043. * Voltages may not have been switched, but we've already
  1044. * sent CMD11, so a power cycle is required anyway
  1045. */
  1046. err = -EAGAIN;
  1047. goto power_cycle;
  1048. }
  1049. /* Wait for at least 1 ms according to spec */
  1050. mmc_delay(1);
  1051. /*
  1052. * Failure to switch is indicated by the card holding
  1053. * dat[0:3] low
  1054. */
  1055. if (host->ops->card_busy && host->ops->card_busy(host))
  1056. err = -EAGAIN;
  1057. power_cycle:
  1058. if (err) {
  1059. pr_debug("%s: Signal voltage switch failed, "
  1060. "power cycling card\n", mmc_hostname(host));
  1061. mmc_power_cycle(host, ocr);
  1062. }
  1063. return err;
  1064. }
  1065. /*
  1066. * Select timing parameters for host.
  1067. */
  1068. void mmc_set_timing(struct mmc_host *host, unsigned int timing)
  1069. {
  1070. host->ios.timing = timing;
  1071. mmc_set_ios(host);
  1072. }
  1073. /*
  1074. * Select appropriate driver type for host.
  1075. */
  1076. void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
  1077. {
  1078. host->ios.drv_type = drv_type;
  1079. mmc_set_ios(host);
  1080. }
  1081. int mmc_select_drive_strength(struct mmc_card *card, unsigned int max_dtr,
  1082. int card_drv_type, int *drv_type)
  1083. {
  1084. struct mmc_host *host = card->host;
  1085. int host_drv_type = SD_DRIVER_TYPE_B;
  1086. *drv_type = 0;
  1087. if (!host->ops->select_drive_strength)
  1088. return 0;
  1089. /* Use SD definition of driver strength for hosts */
  1090. if (host->caps & MMC_CAP_DRIVER_TYPE_A)
  1091. host_drv_type |= SD_DRIVER_TYPE_A;
  1092. if (host->caps & MMC_CAP_DRIVER_TYPE_C)
  1093. host_drv_type |= SD_DRIVER_TYPE_C;
  1094. if (host->caps & MMC_CAP_DRIVER_TYPE_D)
  1095. host_drv_type |= SD_DRIVER_TYPE_D;
  1096. /*
  1097. * The drive strength that the hardware can support
  1098. * depends on the board design. Pass the appropriate
  1099. * information and let the hardware specific code
  1100. * return what is possible given the options
  1101. */
  1102. return host->ops->select_drive_strength(card, max_dtr,
  1103. host_drv_type,
  1104. card_drv_type,
  1105. drv_type);
  1106. }
  1107. /*
  1108. * Apply power to the MMC stack. This is a two-stage process.
  1109. * First, we enable power to the card without the clock running.
  1110. * We then wait a bit for the power to stabilise. Finally,
  1111. * enable the bus drivers and clock to the card.
  1112. *
  1113. * We must _NOT_ enable the clock prior to power stablising.
  1114. *
  1115. * If a host does all the power sequencing itself, ignore the
  1116. * initial MMC_POWER_UP stage.
  1117. */
  1118. void mmc_power_up(struct mmc_host *host, u32 ocr)
  1119. {
  1120. if (host->ios.power_mode == MMC_POWER_ON)
  1121. return;
  1122. mmc_pwrseq_pre_power_on(host);
  1123. host->ios.vdd = fls(ocr) - 1;
  1124. host->ios.power_mode = MMC_POWER_UP;
  1125. /* Set initial state and call mmc_set_ios */
  1126. mmc_set_initial_state(host);
  1127. mmc_set_initial_signal_voltage(host);
  1128. /*
  1129. * This delay should be sufficient to allow the power supply
  1130. * to reach the minimum voltage.
  1131. */
  1132. mmc_delay(host->ios.power_delay_ms);
  1133. mmc_pwrseq_post_power_on(host);
  1134. host->ios.clock = host->f_init;
  1135. host->ios.power_mode = MMC_POWER_ON;
  1136. mmc_set_ios(host);
  1137. /*
  1138. * This delay must be at least 74 clock sizes, or 1 ms, or the
  1139. * time required to reach a stable voltage.
  1140. */
  1141. mmc_delay(host->ios.power_delay_ms);
  1142. }
  1143. void mmc_power_off(struct mmc_host *host)
  1144. {
  1145. if (host->ios.power_mode == MMC_POWER_OFF)
  1146. return;
  1147. mmc_pwrseq_power_off(host);
  1148. host->ios.clock = 0;
  1149. host->ios.vdd = 0;
  1150. host->ios.power_mode = MMC_POWER_OFF;
  1151. /* Set initial state and call mmc_set_ios */
  1152. mmc_set_initial_state(host);
  1153. /*
  1154. * Some configurations, such as the 802.11 SDIO card in the OLPC
  1155. * XO-1.5, require a short delay after poweroff before the card
  1156. * can be successfully turned on again.
  1157. */
  1158. mmc_delay(1);
  1159. }
  1160. void mmc_power_cycle(struct mmc_host *host, u32 ocr)
  1161. {
  1162. mmc_power_off(host);
  1163. /* Wait at least 1 ms according to SD spec */
  1164. mmc_delay(1);
  1165. mmc_power_up(host, ocr);
  1166. }
  1167. /**
  1168. * mmc_handle_undervoltage - Handle an undervoltage event on the MMC bus
  1169. * @host: The MMC host that detected the undervoltage condition
  1170. *
  1171. * This function is called when an undervoltage event is detected on one of
  1172. * the MMC regulators.
  1173. *
  1174. * Returns: 0 on success or a negative error code on failure.
  1175. */
  1176. int mmc_handle_undervoltage(struct mmc_host *host)
  1177. {
  1178. /* Stop the host to prevent races with card removal */
  1179. __mmc_stop_host(host);
  1180. if (!host->bus_ops || !host->bus_ops->handle_undervoltage)
  1181. return 0;
  1182. dev_warn(mmc_dev(host), "%s: Undervoltage detected, initiating emergency stop\n",
  1183. mmc_hostname(host));
  1184. return host->bus_ops->handle_undervoltage(host);
  1185. }
  1186. /*
  1187. * Assign a mmc bus handler to a host. Only one bus handler may control a
  1188. * host at any given time.
  1189. */
  1190. void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
  1191. {
  1192. host->bus_ops = ops;
  1193. }
  1194. /*
  1195. * Remove the current bus handler from a host.
  1196. */
  1197. void mmc_detach_bus(struct mmc_host *host)
  1198. {
  1199. host->bus_ops = NULL;
  1200. }
  1201. void _mmc_detect_change(struct mmc_host *host, unsigned long delay, bool cd_irq)
  1202. {
  1203. /*
  1204. * Prevent system sleep for 5s to allow user space to consume the
  1205. * corresponding uevent. This is especially useful, when CD irq is used
  1206. * as a system wakeup, but doesn't hurt in other cases.
  1207. */
  1208. if (cd_irq && !(host->caps & MMC_CAP_NEEDS_POLL))
  1209. __pm_wakeup_event(host->ws, 5000);
  1210. host->detect_change = 1;
  1211. mmc_schedule_delayed_work(&host->detect, delay);
  1212. }
  1213. /**
  1214. * mmc_detect_change - process change of state on a MMC socket
  1215. * @host: host which changed state.
  1216. * @delay: optional delay to wait before detection (jiffies)
  1217. *
  1218. * MMC drivers should call this when they detect a card has been
  1219. * inserted or removed. The MMC layer will confirm that any
  1220. * present card is still functional, and initialize any newly
  1221. * inserted.
  1222. */
  1223. void mmc_detect_change(struct mmc_host *host, unsigned long delay)
  1224. {
  1225. _mmc_detect_change(host, delay, true);
  1226. }
  1227. EXPORT_SYMBOL(mmc_detect_change);
  1228. void mmc_init_erase(struct mmc_card *card)
  1229. {
  1230. unsigned int sz;
  1231. if (is_power_of_2(card->erase_size))
  1232. card->erase_shift = ffs(card->erase_size) - 1;
  1233. else
  1234. card->erase_shift = 0;
  1235. /*
  1236. * It is possible to erase an arbitrarily large area of an SD or MMC
  1237. * card. That is not desirable because it can take a long time
  1238. * (minutes) potentially delaying more important I/O, and also the
  1239. * timeout calculations become increasingly hugely over-estimated.
  1240. * Consequently, 'pref_erase' is defined as a guide to limit erases
  1241. * to that size and alignment.
  1242. *
  1243. * For SD cards that define Allocation Unit size, limit erases to one
  1244. * Allocation Unit at a time.
  1245. * For MMC, have a stab at ai good value and for modern cards it will
  1246. * end up being 4MiB. Note that if the value is too small, it can end
  1247. * up taking longer to erase. Also note, erase_size is already set to
  1248. * High Capacity Erase Size if available when this function is called.
  1249. */
  1250. if (mmc_card_sd(card) && card->ssr.au) {
  1251. card->pref_erase = card->ssr.au;
  1252. card->erase_shift = ffs(card->ssr.au) - 1;
  1253. } else if (card->erase_size) {
  1254. sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
  1255. if (sz < 128)
  1256. card->pref_erase = 512 * 1024 / 512;
  1257. else if (sz < 512)
  1258. card->pref_erase = 1024 * 1024 / 512;
  1259. else if (sz < 1024)
  1260. card->pref_erase = 2 * 1024 * 1024 / 512;
  1261. else
  1262. card->pref_erase = 4 * 1024 * 1024 / 512;
  1263. if (card->pref_erase < card->erase_size)
  1264. card->pref_erase = card->erase_size;
  1265. else {
  1266. sz = card->pref_erase % card->erase_size;
  1267. if (sz)
  1268. card->pref_erase += card->erase_size - sz;
  1269. }
  1270. } else
  1271. card->pref_erase = 0;
  1272. }
  1273. static bool is_trim_arg(unsigned int arg)
  1274. {
  1275. return (arg & MMC_TRIM_OR_DISCARD_ARGS) && arg != MMC_DISCARD_ARG;
  1276. }
  1277. static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
  1278. unsigned int arg, unsigned int qty)
  1279. {
  1280. unsigned int erase_timeout;
  1281. if (arg == MMC_DISCARD_ARG ||
  1282. (arg == MMC_TRIM_ARG && card->ext_csd.rev >= 6)) {
  1283. erase_timeout = card->ext_csd.trim_timeout;
  1284. } else if (card->ext_csd.erase_group_def & 1) {
  1285. /* High Capacity Erase Group Size uses HC timeouts */
  1286. if (arg == MMC_TRIM_ARG)
  1287. erase_timeout = card->ext_csd.trim_timeout;
  1288. else
  1289. erase_timeout = card->ext_csd.hc_erase_timeout;
  1290. } else {
  1291. /* CSD Erase Group Size uses write timeout */
  1292. unsigned int mult = (10 << card->csd.r2w_factor);
  1293. unsigned int timeout_clks = card->csd.taac_clks * mult;
  1294. unsigned int timeout_us;
  1295. /* Avoid overflow: e.g. taac_ns=80000000 mult=1280 */
  1296. if (card->csd.taac_ns < 1000000)
  1297. timeout_us = (card->csd.taac_ns * mult) / 1000;
  1298. else
  1299. timeout_us = (card->csd.taac_ns / 1000) * mult;
  1300. /*
  1301. * ios.clock is only a target. The real clock rate might be
  1302. * less but not that much less, so fudge it by multiplying by 2.
  1303. */
  1304. timeout_clks <<= 1;
  1305. timeout_us += (timeout_clks * 1000) /
  1306. (card->host->ios.clock / 1000);
  1307. erase_timeout = timeout_us / 1000;
  1308. /*
  1309. * Theoretically, the calculation could underflow so round up
  1310. * to 1ms in that case.
  1311. */
  1312. if (!erase_timeout)
  1313. erase_timeout = 1;
  1314. }
  1315. /* Multiplier for secure operations */
  1316. if (arg & MMC_SECURE_ARGS) {
  1317. if (arg == MMC_SECURE_ERASE_ARG)
  1318. erase_timeout *= card->ext_csd.sec_erase_mult;
  1319. else
  1320. erase_timeout *= card->ext_csd.sec_trim_mult;
  1321. }
  1322. erase_timeout *= qty;
  1323. /*
  1324. * Ensure at least a 1 second timeout for SPI as per
  1325. * 'mmc_set_data_timeout()'
  1326. */
  1327. if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
  1328. erase_timeout = 1000;
  1329. return erase_timeout;
  1330. }
  1331. static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
  1332. unsigned int arg,
  1333. unsigned int qty)
  1334. {
  1335. unsigned int erase_timeout;
  1336. /* for DISCARD none of the below calculation applies.
  1337. * the busy timeout is 250msec per discard command.
  1338. */
  1339. if (arg == SD_DISCARD_ARG)
  1340. return SD_DISCARD_TIMEOUT_MS;
  1341. if (card->ssr.erase_timeout) {
  1342. /* Erase timeout specified in SD Status Register (SSR) */
  1343. erase_timeout = card->ssr.erase_timeout * qty +
  1344. card->ssr.erase_offset;
  1345. } else {
  1346. /*
  1347. * Erase timeout not specified in SD Status Register (SSR) so
  1348. * use 250ms per write block.
  1349. */
  1350. erase_timeout = 250 * qty;
  1351. }
  1352. /* Must not be less than 1 second */
  1353. if (erase_timeout < 1000)
  1354. erase_timeout = 1000;
  1355. return erase_timeout;
  1356. }
  1357. static unsigned int mmc_erase_timeout(struct mmc_card *card,
  1358. unsigned int arg,
  1359. unsigned int qty)
  1360. {
  1361. if (mmc_card_sd(card))
  1362. return mmc_sd_erase_timeout(card, arg, qty);
  1363. else
  1364. return mmc_mmc_erase_timeout(card, arg, qty);
  1365. }
  1366. static int mmc_do_erase(struct mmc_card *card, sector_t from,
  1367. sector_t to, unsigned int arg)
  1368. {
  1369. struct mmc_command cmd = {};
  1370. unsigned int qty = 0, busy_timeout = 0;
  1371. bool use_r1b_resp;
  1372. int err;
  1373. mmc_retune_hold(card->host);
  1374. /*
  1375. * qty is used to calculate the erase timeout which depends on how many
  1376. * erase groups (or allocation units in SD terminology) are affected.
  1377. * We count erasing part of an erase group as one erase group.
  1378. * For SD, the allocation units are always a power of 2. For MMC, the
  1379. * erase group size is almost certainly also power of 2, but it does not
  1380. * seem to insist on that in the JEDEC standard, so we fall back to
  1381. * division in that case. SD may not specify an allocation unit size,
  1382. * in which case the timeout is based on the number of write blocks.
  1383. *
  1384. * Note that the timeout for secure trim 2 will only be correct if the
  1385. * number of erase groups specified is the same as the total of all
  1386. * preceding secure trim 1 commands. Since the power may have been
  1387. * lost since the secure trim 1 commands occurred, it is generally
  1388. * impossible to calculate the secure trim 2 timeout correctly.
  1389. */
  1390. if (card->erase_shift)
  1391. qty += ((to >> card->erase_shift) -
  1392. (from >> card->erase_shift)) + 1;
  1393. else if (mmc_card_sd(card))
  1394. qty += to - from + 1;
  1395. else
  1396. qty += (mmc_sector_div(to, card->erase_size) -
  1397. mmc_sector_div(from, card->erase_size)) + 1;
  1398. if (!mmc_card_blockaddr(card)) {
  1399. from <<= 9;
  1400. to <<= 9;
  1401. }
  1402. if (mmc_card_sd(card))
  1403. cmd.opcode = SD_ERASE_WR_BLK_START;
  1404. else
  1405. cmd.opcode = MMC_ERASE_GROUP_START;
  1406. cmd.arg = from;
  1407. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1408. if (mmc_card_ult_capacity(card)) {
  1409. cmd.ext_addr = from >> 32;
  1410. cmd.has_ext_addr = true;
  1411. }
  1412. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1413. if (err) {
  1414. pr_err("mmc_erase: group start error %d, "
  1415. "status %#x\n", err, cmd.resp[0]);
  1416. err = -EIO;
  1417. goto out;
  1418. }
  1419. memset(&cmd, 0, sizeof(struct mmc_command));
  1420. if (mmc_card_sd(card))
  1421. cmd.opcode = SD_ERASE_WR_BLK_END;
  1422. else
  1423. cmd.opcode = MMC_ERASE_GROUP_END;
  1424. cmd.arg = to;
  1425. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1426. if (mmc_card_ult_capacity(card)) {
  1427. cmd.ext_addr = to >> 32;
  1428. cmd.has_ext_addr = true;
  1429. }
  1430. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1431. if (err) {
  1432. pr_err("mmc_erase: group end error %d, status %#x\n",
  1433. err, cmd.resp[0]);
  1434. err = -EIO;
  1435. goto out;
  1436. }
  1437. memset(&cmd, 0, sizeof(struct mmc_command));
  1438. cmd.opcode = MMC_ERASE;
  1439. cmd.arg = arg;
  1440. busy_timeout = mmc_erase_timeout(card, arg, qty);
  1441. use_r1b_resp = mmc_prepare_busy_cmd(card->host, &cmd, busy_timeout);
  1442. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1443. if (err) {
  1444. pr_err("mmc_erase: erase error %d, status %#x\n",
  1445. err, cmd.resp[0]);
  1446. err = -EIO;
  1447. goto out;
  1448. }
  1449. if (mmc_host_is_spi(card->host))
  1450. goto out;
  1451. /*
  1452. * In case of when R1B + MMC_CAP_WAIT_WHILE_BUSY is used, the polling
  1453. * shall be avoided.
  1454. */
  1455. if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp)
  1456. goto out;
  1457. /* Let's poll to find out when the erase operation completes. */
  1458. err = mmc_poll_for_busy(card, busy_timeout, false, MMC_BUSY_ERASE);
  1459. out:
  1460. mmc_retune_release(card->host);
  1461. return err;
  1462. }
  1463. static unsigned int mmc_align_erase_size(struct mmc_card *card,
  1464. sector_t *from,
  1465. sector_t *to,
  1466. unsigned int nr)
  1467. {
  1468. sector_t from_new = *from;
  1469. unsigned int nr_new = nr, rem;
  1470. /*
  1471. * When the 'card->erase_size' is power of 2, we can use round_up/down()
  1472. * to align the erase size efficiently.
  1473. */
  1474. if (is_power_of_2(card->erase_size)) {
  1475. sector_t temp = from_new;
  1476. from_new = round_up(temp, card->erase_size);
  1477. rem = from_new - temp;
  1478. if (nr_new > rem)
  1479. nr_new -= rem;
  1480. else
  1481. return 0;
  1482. nr_new = round_down(nr_new, card->erase_size);
  1483. } else {
  1484. rem = mmc_sector_mod(from_new, card->erase_size);
  1485. if (rem) {
  1486. rem = card->erase_size - rem;
  1487. from_new += rem;
  1488. if (nr_new > rem)
  1489. nr_new -= rem;
  1490. else
  1491. return 0;
  1492. }
  1493. rem = nr_new % card->erase_size;
  1494. if (rem)
  1495. nr_new -= rem;
  1496. }
  1497. if (nr_new == 0)
  1498. return 0;
  1499. *to = from_new + nr_new;
  1500. *from = from_new;
  1501. return nr_new;
  1502. }
  1503. /**
  1504. * mmc_erase - erase sectors.
  1505. * @card: card to erase
  1506. * @from: first sector to erase
  1507. * @nr: number of sectors to erase
  1508. * @arg: erase command argument
  1509. *
  1510. * Caller must claim host before calling this function.
  1511. */
  1512. int mmc_erase(struct mmc_card *card, sector_t from, unsigned int nr,
  1513. unsigned int arg)
  1514. {
  1515. unsigned int rem;
  1516. sector_t to = from + nr;
  1517. int err;
  1518. if (!(card->csd.cmdclass & CCC_ERASE))
  1519. return -EOPNOTSUPP;
  1520. if (!card->erase_size)
  1521. return -EOPNOTSUPP;
  1522. if (mmc_card_sd(card) && arg != SD_ERASE_ARG && arg != SD_DISCARD_ARG)
  1523. return -EOPNOTSUPP;
  1524. if (mmc_card_mmc(card) && (arg & MMC_SECURE_ARGS) &&
  1525. !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
  1526. return -EOPNOTSUPP;
  1527. if (mmc_card_mmc(card) && is_trim_arg(arg) &&
  1528. !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
  1529. return -EOPNOTSUPP;
  1530. if (arg == MMC_SECURE_ERASE_ARG) {
  1531. if (mmc_sector_mod(from, card->erase_size) || nr % card->erase_size)
  1532. return -EINVAL;
  1533. }
  1534. if (arg == MMC_ERASE_ARG)
  1535. nr = mmc_align_erase_size(card, &from, &to, nr);
  1536. if (nr == 0)
  1537. return 0;
  1538. if (to <= from)
  1539. return -EINVAL;
  1540. /* 'from' and 'to' are inclusive */
  1541. to -= 1;
  1542. /*
  1543. * Special case where only one erase-group fits in the timeout budget:
  1544. * If the region crosses an erase-group boundary on this particular
  1545. * case, we will be trimming more than one erase-group which, does not
  1546. * fit in the timeout budget of the controller, so we need to split it
  1547. * and call mmc_do_erase() twice if necessary. This special case is
  1548. * identified by the card->eg_boundary flag.
  1549. */
  1550. rem = card->erase_size - mmc_sector_mod(from, card->erase_size);
  1551. if ((arg & MMC_TRIM_OR_DISCARD_ARGS) && card->eg_boundary && nr > rem) {
  1552. err = mmc_do_erase(card, from, from + rem - 1, arg);
  1553. from += rem;
  1554. if ((err) || (to <= from))
  1555. return err;
  1556. }
  1557. return mmc_do_erase(card, from, to, arg);
  1558. }
  1559. EXPORT_SYMBOL(mmc_erase);
  1560. bool mmc_card_can_erase(struct mmc_card *card)
  1561. {
  1562. return (card->csd.cmdclass & CCC_ERASE && card->erase_size);
  1563. }
  1564. EXPORT_SYMBOL(mmc_card_can_erase);
  1565. bool mmc_card_can_trim(struct mmc_card *card)
  1566. {
  1567. return ((card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN) &&
  1568. (!(card->quirks & MMC_QUIRK_TRIM_BROKEN)));
  1569. }
  1570. EXPORT_SYMBOL(mmc_card_can_trim);
  1571. bool mmc_card_can_discard(struct mmc_card *card)
  1572. {
  1573. /*
  1574. * As there's no way to detect the discard support bit at v4.5
  1575. * use the s/w feature support filed.
  1576. */
  1577. return (card->ext_csd.feature_support & MMC_DISCARD_FEATURE);
  1578. }
  1579. EXPORT_SYMBOL(mmc_card_can_discard);
  1580. bool mmc_card_can_sanitize(struct mmc_card *card)
  1581. {
  1582. if (!mmc_card_can_trim(card) && !mmc_card_can_erase(card))
  1583. return false;
  1584. if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE)
  1585. return true;
  1586. return false;
  1587. }
  1588. bool mmc_card_can_secure_erase_trim(struct mmc_card *card)
  1589. {
  1590. return ((card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN) &&
  1591. !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN));
  1592. }
  1593. EXPORT_SYMBOL(mmc_card_can_secure_erase_trim);
  1594. bool mmc_card_can_cmd23(struct mmc_card *card)
  1595. {
  1596. return ((mmc_card_mmc(card) &&
  1597. card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
  1598. (mmc_card_sd(card) && !mmc_card_ult_capacity(card) &&
  1599. card->scr.cmds & SD_SCR_CMD23_SUPPORT));
  1600. }
  1601. EXPORT_SYMBOL(mmc_card_can_cmd23);
  1602. int mmc_erase_group_aligned(struct mmc_card *card, sector_t from,
  1603. unsigned int nr)
  1604. {
  1605. if (!card->erase_size)
  1606. return 0;
  1607. if (mmc_sector_mod(from, card->erase_size) || nr % card->erase_size)
  1608. return 0;
  1609. return 1;
  1610. }
  1611. EXPORT_SYMBOL(mmc_erase_group_aligned);
  1612. static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
  1613. unsigned int arg)
  1614. {
  1615. struct mmc_host *host = card->host;
  1616. unsigned int max_discard, x, y, qty = 0, max_qty, min_qty, timeout;
  1617. unsigned int last_timeout = 0;
  1618. unsigned int max_busy_timeout = host->max_busy_timeout ?
  1619. host->max_busy_timeout : MMC_ERASE_TIMEOUT_MS;
  1620. if (card->erase_shift) {
  1621. max_qty = UINT_MAX >> card->erase_shift;
  1622. min_qty = card->pref_erase >> card->erase_shift;
  1623. } else if (mmc_card_sd(card)) {
  1624. max_qty = UINT_MAX;
  1625. min_qty = card->pref_erase;
  1626. } else {
  1627. max_qty = UINT_MAX / card->erase_size;
  1628. min_qty = card->pref_erase / card->erase_size;
  1629. }
  1630. /*
  1631. * We should not only use 'host->max_busy_timeout' as the limitation
  1632. * when deciding the max discard sectors. We should set a balance value
  1633. * to improve the erase speed, and it can not get too long timeout at
  1634. * the same time.
  1635. *
  1636. * Here we set 'card->pref_erase' as the minimal discard sectors no
  1637. * matter what size of 'host->max_busy_timeout', but if the
  1638. * 'host->max_busy_timeout' is large enough for more discard sectors,
  1639. * then we can continue to increase the max discard sectors until we
  1640. * get a balance value. In cases when the 'host->max_busy_timeout'
  1641. * isn't specified, use the default max erase timeout.
  1642. */
  1643. do {
  1644. y = 0;
  1645. for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
  1646. timeout = mmc_erase_timeout(card, arg, qty + x);
  1647. if (qty + x > min_qty && timeout > max_busy_timeout)
  1648. break;
  1649. if (timeout < last_timeout)
  1650. break;
  1651. last_timeout = timeout;
  1652. y = x;
  1653. }
  1654. qty += y;
  1655. } while (y);
  1656. if (!qty)
  1657. return 0;
  1658. /*
  1659. * When specifying a sector range to trim, chances are we might cross
  1660. * an erase-group boundary even if the amount of sectors is less than
  1661. * one erase-group.
  1662. * If we can only fit one erase-group in the controller timeout budget,
  1663. * we have to care that erase-group boundaries are not crossed by a
  1664. * single trim operation. We flag that special case with "eg_boundary".
  1665. * In all other cases we can just decrement qty and pretend that we
  1666. * always touch (qty + 1) erase-groups as a simple optimization.
  1667. */
  1668. if (qty == 1)
  1669. card->eg_boundary = 1;
  1670. else
  1671. qty--;
  1672. /* Convert qty to sectors */
  1673. if (card->erase_shift)
  1674. max_discard = qty << card->erase_shift;
  1675. else if (mmc_card_sd(card))
  1676. max_discard = qty + 1;
  1677. else
  1678. max_discard = qty * card->erase_size;
  1679. return max_discard;
  1680. }
  1681. unsigned int mmc_calc_max_discard(struct mmc_card *card)
  1682. {
  1683. struct mmc_host *host = card->host;
  1684. unsigned int max_discard, max_trim;
  1685. /*
  1686. * Without erase_group_def set, MMC erase timeout depends on clock
  1687. * frequence which can change. In that case, the best choice is
  1688. * just the preferred erase size.
  1689. */
  1690. if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
  1691. return card->pref_erase;
  1692. max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
  1693. if (mmc_card_can_trim(card)) {
  1694. max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
  1695. if (max_trim < max_discard || max_discard == 0)
  1696. max_discard = max_trim;
  1697. } else if (max_discard < card->erase_size) {
  1698. max_discard = 0;
  1699. }
  1700. pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
  1701. mmc_hostname(host), max_discard, host->max_busy_timeout ?
  1702. host->max_busy_timeout : MMC_ERASE_TIMEOUT_MS);
  1703. return max_discard;
  1704. }
  1705. EXPORT_SYMBOL(mmc_calc_max_discard);
  1706. bool mmc_card_is_blockaddr(struct mmc_card *card)
  1707. {
  1708. return card ? mmc_card_blockaddr(card) : false;
  1709. }
  1710. EXPORT_SYMBOL(mmc_card_is_blockaddr);
  1711. int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
  1712. {
  1713. struct mmc_command cmd = {};
  1714. if (mmc_card_blockaddr(card) || mmc_card_ddr52(card) ||
  1715. mmc_card_hs400(card) || mmc_card_hs400es(card))
  1716. return 0;
  1717. cmd.opcode = MMC_SET_BLOCKLEN;
  1718. cmd.arg = blocklen;
  1719. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1720. return mmc_wait_for_cmd(card->host, &cmd, 5);
  1721. }
  1722. EXPORT_SYMBOL(mmc_set_blocklen);
  1723. static void mmc_hw_reset_for_init(struct mmc_host *host)
  1724. {
  1725. mmc_pwrseq_reset(host);
  1726. if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->card_hw_reset)
  1727. return;
  1728. host->ops->card_hw_reset(host);
  1729. }
  1730. /**
  1731. * mmc_hw_reset - reset the card in hardware
  1732. * @card: card to be reset
  1733. *
  1734. * Hard reset the card. This function is only for upper layers, like the
  1735. * block layer or card drivers. You cannot use it in host drivers (struct
  1736. * mmc_card might be gone then).
  1737. *
  1738. * Return: 0 on success, -errno on failure
  1739. */
  1740. int mmc_hw_reset(struct mmc_card *card)
  1741. {
  1742. struct mmc_host *host = card->host;
  1743. int ret;
  1744. ret = host->bus_ops->hw_reset(host);
  1745. if (ret < 0)
  1746. pr_warn("%s: tried to HW reset card, got error %d\n",
  1747. mmc_hostname(host), ret);
  1748. return ret;
  1749. }
  1750. EXPORT_SYMBOL(mmc_hw_reset);
  1751. int mmc_sw_reset(struct mmc_card *card)
  1752. {
  1753. struct mmc_host *host = card->host;
  1754. int ret;
  1755. if (!host->bus_ops->sw_reset)
  1756. return -EOPNOTSUPP;
  1757. ret = host->bus_ops->sw_reset(host);
  1758. if (ret)
  1759. pr_warn("%s: tried to SW reset card, got error %d\n",
  1760. mmc_hostname(host), ret);
  1761. return ret;
  1762. }
  1763. EXPORT_SYMBOL(mmc_sw_reset);
  1764. static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
  1765. {
  1766. host->f_init = freq;
  1767. pr_debug("%s: %s: trying to init card at %u Hz\n",
  1768. mmc_hostname(host), __func__, host->f_init);
  1769. mmc_power_up(host, host->ocr_avail);
  1770. /*
  1771. * Some eMMCs (with VCCQ always on) may not be reset after power up, so
  1772. * do a hardware reset if possible.
  1773. */
  1774. mmc_hw_reset_for_init(host);
  1775. /*
  1776. * sdio_reset sends CMD52 to reset card. Since we do not know
  1777. * if the card is being re-initialized, just send it. CMD52
  1778. * should be ignored by SD/eMMC cards.
  1779. * Skip it if we already know that we do not support SDIO commands
  1780. */
  1781. if (!(host->caps2 & MMC_CAP2_NO_SDIO))
  1782. sdio_reset(host);
  1783. mmc_go_idle(host);
  1784. if (!(host->caps2 & MMC_CAP2_NO_SD)) {
  1785. if (mmc_send_if_cond_pcie(host, host->ocr_avail))
  1786. goto out;
  1787. if (mmc_card_sd_express(host))
  1788. return 0;
  1789. }
  1790. /* Order's important: probe SDIO, then SD, then MMC */
  1791. if (!(host->caps2 & MMC_CAP2_NO_SDIO))
  1792. if (!mmc_attach_sdio(host))
  1793. return 0;
  1794. if (!(host->caps2 & MMC_CAP2_NO_SD))
  1795. if (!mmc_attach_sd(host))
  1796. return 0;
  1797. if (!(host->caps2 & MMC_CAP2_NO_MMC))
  1798. if (!mmc_attach_mmc(host))
  1799. return 0;
  1800. out:
  1801. mmc_power_off(host);
  1802. return -EIO;
  1803. }
  1804. int _mmc_detect_card_removed(struct mmc_host *host)
  1805. {
  1806. int ret;
  1807. if (!host->card || mmc_card_removed(host->card))
  1808. return 1;
  1809. ret = host->bus_ops->alive(host);
  1810. /*
  1811. * Card detect status and alive check may be out of sync if card is
  1812. * removed slowly, when card detect switch changes while card/slot
  1813. * pads are still contacted in hardware (refer to "SD Card Mechanical
  1814. * Addendum, Appendix C: Card Detection Switch"). So reschedule a
  1815. * detect work 200ms later for this case.
  1816. */
  1817. if (!ret && host->ops->get_cd && !host->ops->get_cd(host)) {
  1818. mmc_detect_change(host, msecs_to_jiffies(200));
  1819. pr_debug("%s: card removed too slowly\n", mmc_hostname(host));
  1820. }
  1821. if (ret) {
  1822. mmc_card_set_removed(host->card);
  1823. pr_debug("%s: card remove detected\n", mmc_hostname(host));
  1824. }
  1825. return ret;
  1826. }
  1827. int mmc_detect_card_removed(struct mmc_host *host)
  1828. {
  1829. struct mmc_card *card = host->card;
  1830. int ret;
  1831. WARN_ON(!host->claimed);
  1832. if (!card)
  1833. return 1;
  1834. if (!mmc_card_is_removable(host))
  1835. return 0;
  1836. ret = mmc_card_removed(card);
  1837. /*
  1838. * The card will be considered unchanged unless we have been asked to
  1839. * detect a change or host requires polling to provide card detection.
  1840. */
  1841. if (!host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL))
  1842. return ret;
  1843. host->detect_change = 0;
  1844. if (!ret) {
  1845. ret = _mmc_detect_card_removed(host);
  1846. if (ret && (host->caps & MMC_CAP_NEEDS_POLL)) {
  1847. /*
  1848. * Schedule a detect work as soon as possible to let a
  1849. * rescan handle the card removal.
  1850. */
  1851. cancel_delayed_work(&host->detect);
  1852. _mmc_detect_change(host, 0, false);
  1853. }
  1854. }
  1855. return ret;
  1856. }
  1857. EXPORT_SYMBOL(mmc_detect_card_removed);
  1858. int mmc_card_alternative_gpt_sector(struct mmc_card *card, sector_t *gpt_sector)
  1859. {
  1860. unsigned int boot_sectors_num;
  1861. if ((!(card->host->caps2 & MMC_CAP2_ALT_GPT_TEGRA)))
  1862. return -EOPNOTSUPP;
  1863. /* filter out unrelated cards */
  1864. if (card->ext_csd.rev < 3 ||
  1865. !mmc_card_mmc(card) ||
  1866. !mmc_card_is_blockaddr(card) ||
  1867. mmc_card_is_removable(card->host))
  1868. return -ENOENT;
  1869. /*
  1870. * eMMC storage has two special boot partitions in addition to the
  1871. * main one. NVIDIA's bootloader linearizes eMMC boot0->boot1->main
  1872. * accesses, this means that the partition table addresses are shifted
  1873. * by the size of boot partitions. In accordance with the eMMC
  1874. * specification, the boot partition size is calculated as follows:
  1875. *
  1876. * boot partition size = 128K byte x BOOT_SIZE_MULT
  1877. *
  1878. * Calculate number of sectors occupied by the both boot partitions.
  1879. */
  1880. boot_sectors_num = card->ext_csd.raw_boot_mult * SZ_128K /
  1881. SZ_512 * MMC_NUM_BOOT_PARTITION;
  1882. /* Defined by NVIDIA and used by Android devices. */
  1883. *gpt_sector = card->ext_csd.sectors - boot_sectors_num - 1;
  1884. return 0;
  1885. }
  1886. EXPORT_SYMBOL(mmc_card_alternative_gpt_sector);
  1887. void mmc_rescan(struct work_struct *work)
  1888. {
  1889. struct mmc_host *host =
  1890. container_of(work, struct mmc_host, detect.work);
  1891. int i;
  1892. if (host->rescan_disable)
  1893. return;
  1894. /* If there is a non-removable card registered, only scan once */
  1895. if (!mmc_card_is_removable(host) && host->rescan_entered)
  1896. return;
  1897. host->rescan_entered = 1;
  1898. if (host->trigger_card_event && host->ops->card_event) {
  1899. mmc_claim_host(host);
  1900. host->ops->card_event(host);
  1901. mmc_release_host(host);
  1902. host->trigger_card_event = false;
  1903. }
  1904. /* Verify a registered card to be functional, else remove it. */
  1905. if (host->bus_ops)
  1906. host->bus_ops->detect(host);
  1907. host->detect_change = 0;
  1908. /* if there still is a card present, stop here */
  1909. if (host->bus_ops != NULL)
  1910. goto out;
  1911. mmc_claim_host(host);
  1912. if (mmc_card_is_removable(host) && host->ops->get_cd &&
  1913. host->ops->get_cd(host) == 0) {
  1914. mmc_power_off(host);
  1915. mmc_release_host(host);
  1916. goto out;
  1917. }
  1918. /* If an SD express card is present, then leave it as is. */
  1919. if (mmc_card_sd_express(host)) {
  1920. mmc_release_host(host);
  1921. goto out;
  1922. }
  1923. /*
  1924. * Ideally we should favor initialization of legacy SD cards and defer
  1925. * UHS-II enumeration. However, it seems like cards doesn't reliably
  1926. * announce their support for UHS-II in the response to the ACMD41,
  1927. * while initializing the legacy SD interface. Therefore, let's start
  1928. * with UHS-II for now.
  1929. */
  1930. if (!mmc_attach_sd_uhs2(host)) {
  1931. mmc_release_host(host);
  1932. goto out;
  1933. }
  1934. for (i = 0; i < ARRAY_SIZE(freqs); i++) {
  1935. unsigned int freq = freqs[i];
  1936. if (freq > host->f_max) {
  1937. if (i + 1 < ARRAY_SIZE(freqs))
  1938. continue;
  1939. freq = host->f_max;
  1940. }
  1941. if (!mmc_rescan_try_freq(host, max(freq, host->f_min)))
  1942. break;
  1943. if (freqs[i] <= host->f_min)
  1944. break;
  1945. }
  1946. /* A non-removable card should have been detected by now. */
  1947. if (!mmc_card_is_removable(host) && !host->bus_ops)
  1948. pr_info("%s: Failed to initialize a non-removable card",
  1949. mmc_hostname(host));
  1950. /*
  1951. * Ignore the command timeout errors observed during
  1952. * the card init as those are excepted.
  1953. */
  1954. host->err_stats[MMC_ERR_CMD_TIMEOUT] = 0;
  1955. mmc_release_host(host);
  1956. out:
  1957. if (host->caps & MMC_CAP_NEEDS_POLL)
  1958. mmc_schedule_delayed_work(&host->detect, HZ);
  1959. }
  1960. void mmc_start_host(struct mmc_host *host)
  1961. {
  1962. bool power_up = !(host->caps2 &
  1963. (MMC_CAP2_NO_PRESCAN_POWERUP | MMC_CAP2_SD_UHS2));
  1964. host->f_init = max(min(freqs[0], host->f_max), host->f_min);
  1965. host->rescan_disable = 0;
  1966. if (power_up) {
  1967. mmc_claim_host(host);
  1968. mmc_power_up(host, host->ocr_avail);
  1969. mmc_release_host(host);
  1970. }
  1971. mmc_gpiod_request_cd_irq(host);
  1972. _mmc_detect_change(host, 0, false);
  1973. }
  1974. void __mmc_stop_host(struct mmc_host *host)
  1975. {
  1976. if (host->rescan_disable)
  1977. return;
  1978. if (host->slot.cd_irq >= 0) {
  1979. mmc_gpio_set_cd_wake(host, false);
  1980. disable_irq(host->slot.cd_irq);
  1981. }
  1982. host->rescan_disable = 1;
  1983. cancel_delayed_work_sync(&host->detect);
  1984. }
  1985. void mmc_stop_host(struct mmc_host *host)
  1986. {
  1987. __mmc_stop_host(host);
  1988. /* clear pm flags now and let card drivers set them as needed */
  1989. host->pm_flags = 0;
  1990. if (host->bus_ops) {
  1991. /* Calling bus_ops->remove() with a claimed host can deadlock */
  1992. host->bus_ops->remove(host);
  1993. mmc_claim_host(host);
  1994. mmc_detach_bus(host);
  1995. mmc_power_off(host);
  1996. mmc_release_host(host);
  1997. return;
  1998. }
  1999. mmc_claim_host(host);
  2000. mmc_power_off(host);
  2001. mmc_release_host(host);
  2002. }
  2003. static int __init mmc_init(void)
  2004. {
  2005. int ret;
  2006. ret = mmc_register_bus();
  2007. if (ret)
  2008. return ret;
  2009. ret = mmc_register_host_class();
  2010. if (ret)
  2011. goto unregister_bus;
  2012. ret = sdio_register_bus();
  2013. if (ret)
  2014. goto unregister_host_class;
  2015. return 0;
  2016. unregister_host_class:
  2017. mmc_unregister_host_class();
  2018. unregister_bus:
  2019. mmc_unregister_bus();
  2020. return ret;
  2021. }
  2022. static void __exit mmc_exit(void)
  2023. {
  2024. sdio_unregister_bus();
  2025. mmc_unregister_host_class();
  2026. mmc_unregister_bus();
  2027. }
  2028. subsys_initcall(mmc_init);
  2029. module_exit(mmc_exit);
  2030. MODULE_DESCRIPTION("MMC core driver");
  2031. MODULE_LICENSE("GPL");