core.c 59 KB

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