core.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024
  1. /*
  2. * linux/drivers/mmc/core/core.c
  3. *
  4. * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
  5. * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
  6. * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
  7. * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/completion.h>
  17. #include <linux/device.h>
  18. #include <linux/delay.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/err.h>
  21. #include <linux/leds.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/log2.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/pm_wakeup.h>
  27. #include <linux/suspend.h>
  28. #include <linux/fault-inject.h>
  29. #include <linux/random.h>
  30. #include <linux/slab.h>
  31. #include <linux/of.h>
  32. #include <linux/mmc/card.h>
  33. #include <linux/mmc/host.h>
  34. #include <linux/mmc/mmc.h>
  35. #include <linux/mmc/sd.h>
  36. #include <linux/mmc/slot-gpio.h>
  37. #define CREATE_TRACE_POINTS
  38. #include <trace/events/mmc.h>
  39. #include "core.h"
  40. #include "card.h"
  41. #include "bus.h"
  42. #include "host.h"
  43. #include "sdio_bus.h"
  44. #include "pwrseq.h"
  45. #include "mmc_ops.h"
  46. #include "sd_ops.h"
  47. #include "sdio_ops.h"
  48. /* If the device is not responding */
  49. #define MMC_CORE_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
  50. /* The max erase timeout, used when host->max_busy_timeout isn't specified */
  51. #define MMC_ERASE_TIMEOUT_MS (60 * 1000) /* 60 s */
  52. static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
  53. /*
  54. * Enabling software CRCs on the data blocks can be a significant (30%)
  55. * performance cost, and for other reasons may not always be desired.
  56. * So we allow it it to be disabled.
  57. */
  58. bool use_spi_crc = 1;
  59. module_param(use_spi_crc, bool, 0);
  60. #ifdef CONFIG_MMC_MQ_DEFAULT
  61. bool mmc_use_blk_mq = true;
  62. #else
  63. bool mmc_use_blk_mq = false;
  64. #endif
  65. module_param_named(use_blk_mq, mmc_use_blk_mq, bool, S_IWUSR | S_IRUGO);
  66. static int mmc_schedule_delayed_work(struct delayed_work *work,
  67. unsigned long delay)
  68. {
  69. /*
  70. * We use the system_freezable_wq, because of two reasons.
  71. * First, it allows several works (not the same work item) to be
  72. * executed simultaneously. Second, the queue becomes frozen when
  73. * userspace becomes frozen during system PM.
  74. */
  75. return queue_delayed_work(system_freezable_wq, work, delay);
  76. }
  77. #ifdef CONFIG_FAIL_MMC_REQUEST
  78. /*
  79. * Internal function. Inject random data errors.
  80. * If mmc_data is NULL no errors are injected.
  81. */
  82. static void mmc_should_fail_request(struct mmc_host *host,
  83. struct mmc_request *mrq)
  84. {
  85. struct mmc_command *cmd = mrq->cmd;
  86. struct mmc_data *data = mrq->data;
  87. static const int data_errors[] = {
  88. -ETIMEDOUT,
  89. -EILSEQ,
  90. -EIO,
  91. };
  92. if (!data)
  93. return;
  94. if (cmd->error || data->error ||
  95. !should_fail(&host->fail_mmc_request, data->blksz * data->blocks))
  96. return;
  97. data->error = data_errors[prandom_u32() % ARRAY_SIZE(data_errors)];
  98. data->bytes_xfered = (prandom_u32() % (data->bytes_xfered >> 9)) << 9;
  99. }
  100. #else /* CONFIG_FAIL_MMC_REQUEST */
  101. static inline void mmc_should_fail_request(struct mmc_host *host,
  102. struct mmc_request *mrq)
  103. {
  104. }
  105. #endif /* CONFIG_FAIL_MMC_REQUEST */
  106. static inline void mmc_complete_cmd(struct mmc_request *mrq)
  107. {
  108. if (mrq->cap_cmd_during_tfr && !completion_done(&mrq->cmd_completion))
  109. complete_all(&mrq->cmd_completion);
  110. }
  111. void mmc_command_done(struct mmc_host *host, struct mmc_request *mrq)
  112. {
  113. if (!mrq->cap_cmd_during_tfr)
  114. return;
  115. mmc_complete_cmd(mrq);
  116. pr_debug("%s: cmd done, tfr ongoing (CMD%u)\n",
  117. mmc_hostname(host), mrq->cmd->opcode);
  118. }
  119. EXPORT_SYMBOL(mmc_command_done);
  120. /**
  121. * mmc_request_done - finish processing an MMC request
  122. * @host: MMC host which completed request
  123. * @mrq: MMC request which request
  124. *
  125. * MMC drivers should call this function when they have completed
  126. * their processing of a request.
  127. */
  128. void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
  129. {
  130. struct mmc_command *cmd = mrq->cmd;
  131. int err = cmd->error;
  132. /* Flag re-tuning needed on CRC errors */
  133. if ((cmd->opcode != MMC_SEND_TUNING_BLOCK &&
  134. cmd->opcode != MMC_SEND_TUNING_BLOCK_HS200) &&
  135. (err == -EILSEQ || (mrq->sbc && mrq->sbc->error == -EILSEQ) ||
  136. (mrq->data && mrq->data->error == -EILSEQ) ||
  137. (mrq->stop && mrq->stop->error == -EILSEQ)))
  138. mmc_retune_needed(host);
  139. if (err && cmd->retries && mmc_host_is_spi(host)) {
  140. if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
  141. cmd->retries = 0;
  142. }
  143. if (host->ongoing_mrq == mrq)
  144. host->ongoing_mrq = NULL;
  145. mmc_complete_cmd(mrq);
  146. trace_mmc_request_done(host, mrq);
  147. /*
  148. * We list various conditions for the command to be considered
  149. * properly done:
  150. *
  151. * - There was no error, OK fine then
  152. * - We are not doing some kind of retry
  153. * - The card was removed (...so just complete everything no matter
  154. * if there are errors or retries)
  155. */
  156. if (!err || !cmd->retries || mmc_card_removed(host->card)) {
  157. mmc_should_fail_request(host, mrq);
  158. if (!host->ongoing_mrq)
  159. led_trigger_event(host->led, LED_OFF);
  160. if (mrq->sbc) {
  161. pr_debug("%s: req done <CMD%u>: %d: %08x %08x %08x %08x\n",
  162. mmc_hostname(host), mrq->sbc->opcode,
  163. mrq->sbc->error,
  164. mrq->sbc->resp[0], mrq->sbc->resp[1],
  165. mrq->sbc->resp[2], mrq->sbc->resp[3]);
  166. }
  167. pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
  168. mmc_hostname(host), cmd->opcode, err,
  169. cmd->resp[0], cmd->resp[1],
  170. cmd->resp[2], cmd->resp[3]);
  171. if (mrq->data) {
  172. pr_debug("%s: %d bytes transferred: %d\n",
  173. mmc_hostname(host),
  174. mrq->data->bytes_xfered, mrq->data->error);
  175. }
  176. if (mrq->stop) {
  177. pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
  178. mmc_hostname(host), mrq->stop->opcode,
  179. mrq->stop->error,
  180. mrq->stop->resp[0], mrq->stop->resp[1],
  181. mrq->stop->resp[2], mrq->stop->resp[3]);
  182. }
  183. }
  184. /*
  185. * Request starter must handle retries - see
  186. * mmc_wait_for_req_done().
  187. */
  188. if (mrq->done)
  189. mrq->done(mrq);
  190. }
  191. EXPORT_SYMBOL(mmc_request_done);
  192. static void __mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
  193. {
  194. int err;
  195. /* Assumes host controller has been runtime resumed by mmc_claim_host */
  196. err = mmc_retune(host);
  197. if (err) {
  198. mrq->cmd->error = err;
  199. mmc_request_done(host, mrq);
  200. return;
  201. }
  202. /*
  203. * For sdio rw commands we must wait for card busy otherwise some
  204. * sdio devices won't work properly.
  205. * And bypass I/O abort, reset and bus suspend operations.
  206. */
  207. if (sdio_is_io_busy(mrq->cmd->opcode, mrq->cmd->arg) &&
  208. host->ops->card_busy) {
  209. int tries = 500; /* Wait aprox 500ms at maximum */
  210. while (host->ops->card_busy(host) && --tries)
  211. mmc_delay(1);
  212. if (tries == 0) {
  213. mrq->cmd->error = -EBUSY;
  214. mmc_request_done(host, mrq);
  215. return;
  216. }
  217. }
  218. if (mrq->cap_cmd_during_tfr) {
  219. host->ongoing_mrq = mrq;
  220. /*
  221. * Retry path could come through here without having waiting on
  222. * cmd_completion, so ensure it is reinitialised.
  223. */
  224. reinit_completion(&mrq->cmd_completion);
  225. }
  226. trace_mmc_request_start(host, mrq);
  227. if (host->cqe_on)
  228. host->cqe_ops->cqe_off(host);
  229. host->ops->request(host, mrq);
  230. }
  231. static void mmc_mrq_pr_debug(struct mmc_host *host, struct mmc_request *mrq,
  232. bool cqe)
  233. {
  234. if (mrq->sbc) {
  235. pr_debug("<%s: starting CMD%u arg %08x flags %08x>\n",
  236. mmc_hostname(host), mrq->sbc->opcode,
  237. mrq->sbc->arg, mrq->sbc->flags);
  238. }
  239. if (mrq->cmd) {
  240. pr_debug("%s: starting %sCMD%u arg %08x flags %08x\n",
  241. mmc_hostname(host), cqe ? "CQE direct " : "",
  242. mrq->cmd->opcode, mrq->cmd->arg, mrq->cmd->flags);
  243. } else if (cqe) {
  244. pr_debug("%s: starting CQE transfer for tag %d blkaddr %u\n",
  245. mmc_hostname(host), mrq->tag, mrq->data->blk_addr);
  246. }
  247. if (mrq->data) {
  248. pr_debug("%s: blksz %d blocks %d flags %08x "
  249. "tsac %d ms nsac %d\n",
  250. mmc_hostname(host), mrq->data->blksz,
  251. mrq->data->blocks, mrq->data->flags,
  252. mrq->data->timeout_ns / 1000000,
  253. mrq->data->timeout_clks);
  254. }
  255. if (mrq->stop) {
  256. pr_debug("%s: CMD%u arg %08x flags %08x\n",
  257. mmc_hostname(host), mrq->stop->opcode,
  258. mrq->stop->arg, mrq->stop->flags);
  259. }
  260. }
  261. static int mmc_mrq_prep(struct mmc_host *host, struct mmc_request *mrq)
  262. {
  263. unsigned int i, sz = 0;
  264. struct scatterlist *sg;
  265. if (mrq->cmd) {
  266. mrq->cmd->error = 0;
  267. mrq->cmd->mrq = mrq;
  268. mrq->cmd->data = mrq->data;
  269. }
  270. if (mrq->sbc) {
  271. mrq->sbc->error = 0;
  272. mrq->sbc->mrq = mrq;
  273. }
  274. if (mrq->data) {
  275. if (mrq->data->blksz > host->max_blk_size ||
  276. mrq->data->blocks > host->max_blk_count ||
  277. mrq->data->blocks * mrq->data->blksz > host->max_req_size)
  278. return -EINVAL;
  279. for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
  280. sz += sg->length;
  281. if (sz != mrq->data->blocks * mrq->data->blksz)
  282. return -EINVAL;
  283. mrq->data->error = 0;
  284. mrq->data->mrq = mrq;
  285. if (mrq->stop) {
  286. mrq->data->stop = mrq->stop;
  287. mrq->stop->error = 0;
  288. mrq->stop->mrq = mrq;
  289. }
  290. }
  291. return 0;
  292. }
  293. int mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
  294. {
  295. int err;
  296. mmc_retune_hold(host);
  297. if (mmc_card_removed(host->card))
  298. return -ENOMEDIUM;
  299. mmc_mrq_pr_debug(host, mrq, false);
  300. WARN_ON(!host->claimed);
  301. err = mmc_mrq_prep(host, mrq);
  302. if (err)
  303. return err;
  304. led_trigger_event(host->led, LED_FULL);
  305. __mmc_start_request(host, mrq);
  306. return 0;
  307. }
  308. EXPORT_SYMBOL(mmc_start_request);
  309. /*
  310. * mmc_wait_data_done() - done callback for data request
  311. * @mrq: done data request
  312. *
  313. * Wakes up mmc context, passed as a callback to host controller driver
  314. */
  315. static void mmc_wait_data_done(struct mmc_request *mrq)
  316. {
  317. struct mmc_context_info *context_info = &mrq->host->context_info;
  318. context_info->is_done_rcv = true;
  319. wake_up_interruptible(&context_info->wait);
  320. }
  321. static void mmc_wait_done(struct mmc_request *mrq)
  322. {
  323. complete(&mrq->completion);
  324. }
  325. static inline void mmc_wait_ongoing_tfr_cmd(struct mmc_host *host)
  326. {
  327. struct mmc_request *ongoing_mrq = READ_ONCE(host->ongoing_mrq);
  328. /*
  329. * If there is an ongoing transfer, wait for the command line to become
  330. * available.
  331. */
  332. if (ongoing_mrq && !completion_done(&ongoing_mrq->cmd_completion))
  333. wait_for_completion(&ongoing_mrq->cmd_completion);
  334. }
  335. /*
  336. *__mmc_start_data_req() - starts data request
  337. * @host: MMC host to start the request
  338. * @mrq: data request to start
  339. *
  340. * Sets the done callback to be called when request is completed by the card.
  341. * Starts data mmc request execution
  342. * If an ongoing transfer is already in progress, wait for the command line
  343. * to become available before sending another command.
  344. */
  345. static int __mmc_start_data_req(struct mmc_host *host, struct mmc_request *mrq)
  346. {
  347. int err;
  348. mmc_wait_ongoing_tfr_cmd(host);
  349. mrq->done = mmc_wait_data_done;
  350. mrq->host = host;
  351. init_completion(&mrq->cmd_completion);
  352. err = mmc_start_request(host, mrq);
  353. if (err) {
  354. mrq->cmd->error = err;
  355. mmc_complete_cmd(mrq);
  356. mmc_wait_data_done(mrq);
  357. }
  358. return err;
  359. }
  360. static int __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq)
  361. {
  362. int err;
  363. mmc_wait_ongoing_tfr_cmd(host);
  364. init_completion(&mrq->completion);
  365. mrq->done = mmc_wait_done;
  366. init_completion(&mrq->cmd_completion);
  367. err = mmc_start_request(host, mrq);
  368. if (err) {
  369. mrq->cmd->error = err;
  370. mmc_complete_cmd(mrq);
  371. complete(&mrq->completion);
  372. }
  373. return err;
  374. }
  375. void mmc_wait_for_req_done(struct mmc_host *host, struct mmc_request *mrq)
  376. {
  377. struct mmc_command *cmd;
  378. while (1) {
  379. wait_for_completion(&mrq->completion);
  380. cmd = mrq->cmd;
  381. /*
  382. * If host has timed out waiting for the sanitize
  383. * to complete, card might be still in programming state
  384. * so let's try to bring the card out of programming
  385. * state.
  386. */
  387. if (cmd->sanitize_busy && cmd->error == -ETIMEDOUT) {
  388. if (!mmc_interrupt_hpi(host->card)) {
  389. pr_warn("%s: %s: Interrupted sanitize\n",
  390. mmc_hostname(host), __func__);
  391. cmd->error = 0;
  392. break;
  393. } else {
  394. pr_err("%s: %s: Failed to interrupt sanitize\n",
  395. mmc_hostname(host), __func__);
  396. }
  397. }
  398. if (!cmd->error || !cmd->retries ||
  399. mmc_card_removed(host->card))
  400. break;
  401. mmc_retune_recheck(host);
  402. pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
  403. mmc_hostname(host), cmd->opcode, cmd->error);
  404. cmd->retries--;
  405. cmd->error = 0;
  406. __mmc_start_request(host, mrq);
  407. }
  408. mmc_retune_release(host);
  409. }
  410. EXPORT_SYMBOL(mmc_wait_for_req_done);
  411. /*
  412. * mmc_cqe_start_req - Start a CQE request.
  413. * @host: MMC host to start the request
  414. * @mrq: request to start
  415. *
  416. * Start the request, re-tuning if needed and it is possible. Returns an error
  417. * code if the request fails to start or -EBUSY if CQE is busy.
  418. */
  419. int mmc_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq)
  420. {
  421. int err;
  422. /*
  423. * CQE cannot process re-tuning commands. Caller must hold retuning
  424. * while CQE is in use. Re-tuning can happen here only when CQE has no
  425. * active requests i.e. this is the first. Note, re-tuning will call
  426. * ->cqe_off().
  427. */
  428. err = mmc_retune(host);
  429. if (err)
  430. goto out_err;
  431. mrq->host = host;
  432. mmc_mrq_pr_debug(host, mrq, true);
  433. err = mmc_mrq_prep(host, mrq);
  434. if (err)
  435. goto out_err;
  436. err = host->cqe_ops->cqe_request(host, mrq);
  437. if (err)
  438. goto out_err;
  439. trace_mmc_request_start(host, mrq);
  440. return 0;
  441. out_err:
  442. if (mrq->cmd) {
  443. pr_debug("%s: failed to start CQE direct CMD%u, error %d\n",
  444. mmc_hostname(host), mrq->cmd->opcode, err);
  445. } else {
  446. pr_debug("%s: failed to start CQE transfer for tag %d, error %d\n",
  447. mmc_hostname(host), mrq->tag, err);
  448. }
  449. return err;
  450. }
  451. EXPORT_SYMBOL(mmc_cqe_start_req);
  452. /**
  453. * mmc_cqe_request_done - CQE has finished processing an MMC request
  454. * @host: MMC host which completed request
  455. * @mrq: MMC request which completed
  456. *
  457. * CQE drivers should call this function when they have completed
  458. * their processing of a request.
  459. */
  460. void mmc_cqe_request_done(struct mmc_host *host, struct mmc_request *mrq)
  461. {
  462. mmc_should_fail_request(host, mrq);
  463. /* Flag re-tuning needed on CRC errors */
  464. if ((mrq->cmd && mrq->cmd->error == -EILSEQ) ||
  465. (mrq->data && mrq->data->error == -EILSEQ))
  466. mmc_retune_needed(host);
  467. trace_mmc_request_done(host, mrq);
  468. if (mrq->cmd) {
  469. pr_debug("%s: CQE req done (direct CMD%u): %d\n",
  470. mmc_hostname(host), mrq->cmd->opcode, mrq->cmd->error);
  471. } else {
  472. pr_debug("%s: CQE transfer done tag %d\n",
  473. mmc_hostname(host), mrq->tag);
  474. }
  475. if (mrq->data) {
  476. pr_debug("%s: %d bytes transferred: %d\n",
  477. mmc_hostname(host),
  478. mrq->data->bytes_xfered, mrq->data->error);
  479. }
  480. mrq->done(mrq);
  481. }
  482. EXPORT_SYMBOL(mmc_cqe_request_done);
  483. /**
  484. * mmc_cqe_post_req - CQE post process of a completed MMC request
  485. * @host: MMC host
  486. * @mrq: MMC request to be processed
  487. */
  488. void mmc_cqe_post_req(struct mmc_host *host, struct mmc_request *mrq)
  489. {
  490. if (host->cqe_ops->cqe_post_req)
  491. host->cqe_ops->cqe_post_req(host, mrq);
  492. }
  493. EXPORT_SYMBOL(mmc_cqe_post_req);
  494. /* Arbitrary 1 second timeout */
  495. #define MMC_CQE_RECOVERY_TIMEOUT 1000
  496. /*
  497. * mmc_cqe_recovery - Recover from CQE errors.
  498. * @host: MMC host to recover
  499. *
  500. * Recovery consists of stopping CQE, stopping eMMC, discarding the queue in
  501. * in eMMC, and discarding the queue in CQE. CQE must call
  502. * mmc_cqe_request_done() on all requests. An error is returned if the eMMC
  503. * fails to discard its queue.
  504. */
  505. int mmc_cqe_recovery(struct mmc_host *host)
  506. {
  507. struct mmc_command cmd;
  508. int err;
  509. mmc_retune_hold_now(host);
  510. /*
  511. * Recovery is expected seldom, if at all, but it reduces performance,
  512. * so make sure it is not completely silent.
  513. */
  514. pr_warn("%s: running CQE recovery\n", mmc_hostname(host));
  515. host->cqe_ops->cqe_recovery_start(host);
  516. memset(&cmd, 0, sizeof(cmd));
  517. cmd.opcode = MMC_STOP_TRANSMISSION,
  518. cmd.flags = MMC_RSP_R1B | MMC_CMD_AC,
  519. cmd.flags &= ~MMC_RSP_CRC; /* Ignore CRC */
  520. cmd.busy_timeout = MMC_CQE_RECOVERY_TIMEOUT,
  521. mmc_wait_for_cmd(host, &cmd, 0);
  522. memset(&cmd, 0, sizeof(cmd));
  523. cmd.opcode = MMC_CMDQ_TASK_MGMT;
  524. cmd.arg = 1; /* Discard entire queue */
  525. cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
  526. cmd.flags &= ~MMC_RSP_CRC; /* Ignore CRC */
  527. cmd.busy_timeout = MMC_CQE_RECOVERY_TIMEOUT,
  528. err = mmc_wait_for_cmd(host, &cmd, 0);
  529. host->cqe_ops->cqe_recovery_finish(host);
  530. mmc_retune_release(host);
  531. return err;
  532. }
  533. EXPORT_SYMBOL(mmc_cqe_recovery);
  534. /**
  535. * mmc_is_req_done - Determine if a 'cap_cmd_during_tfr' request is done
  536. * @host: MMC host
  537. * @mrq: MMC request
  538. *
  539. * mmc_is_req_done() is used with requests that have
  540. * mrq->cap_cmd_during_tfr = true. mmc_is_req_done() must be called after
  541. * starting a request and before waiting for it to complete. That is,
  542. * either in between calls to mmc_start_req(), or after mmc_wait_for_req()
  543. * and before mmc_wait_for_req_done(). If it is called at other times the
  544. * result is not meaningful.
  545. */
  546. bool mmc_is_req_done(struct mmc_host *host, struct mmc_request *mrq)
  547. {
  548. if (host->areq)
  549. return host->context_info.is_done_rcv;
  550. else
  551. return completion_done(&mrq->completion);
  552. }
  553. EXPORT_SYMBOL(mmc_is_req_done);
  554. /**
  555. * mmc_finalize_areq() - finalize an asynchronous request
  556. * @host: MMC host to finalize any ongoing request on
  557. *
  558. * Returns the status of the ongoing asynchronous request, but
  559. * MMC_BLK_SUCCESS if no request was going on.
  560. */
  561. static enum mmc_blk_status mmc_finalize_areq(struct mmc_host *host)
  562. {
  563. struct mmc_context_info *context_info = &host->context_info;
  564. enum mmc_blk_status status;
  565. if (!host->areq)
  566. return MMC_BLK_SUCCESS;
  567. while (1) {
  568. wait_event_interruptible(context_info->wait,
  569. (context_info->is_done_rcv ||
  570. context_info->is_new_req));
  571. if (context_info->is_done_rcv) {
  572. struct mmc_command *cmd;
  573. context_info->is_done_rcv = false;
  574. cmd = host->areq->mrq->cmd;
  575. if (!cmd->error || !cmd->retries ||
  576. mmc_card_removed(host->card)) {
  577. status = host->areq->err_check(host->card,
  578. host->areq);
  579. break; /* return status */
  580. } else {
  581. mmc_retune_recheck(host);
  582. pr_info("%s: req failed (CMD%u): %d, retrying...\n",
  583. mmc_hostname(host),
  584. cmd->opcode, cmd->error);
  585. cmd->retries--;
  586. cmd->error = 0;
  587. __mmc_start_request(host, host->areq->mrq);
  588. continue; /* wait for done/new event again */
  589. }
  590. }
  591. return MMC_BLK_NEW_REQUEST;
  592. }
  593. mmc_retune_release(host);
  594. /*
  595. * Check BKOPS urgency for each R1 response
  596. */
  597. if (host->card && mmc_card_mmc(host->card) &&
  598. ((mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1) ||
  599. (mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1B)) &&
  600. (host->areq->mrq->cmd->resp[0] & R1_EXCEPTION_EVENT)) {
  601. mmc_start_bkops(host->card, true);
  602. }
  603. return status;
  604. }
  605. /**
  606. * mmc_start_areq - start an asynchronous request
  607. * @host: MMC host to start command
  608. * @areq: asynchronous request to start
  609. * @ret_stat: out parameter for status
  610. *
  611. * Start a new MMC custom command request for a host.
  612. * If there is on ongoing async request wait for completion
  613. * of that request and start the new one and return.
  614. * Does not wait for the new request to complete.
  615. *
  616. * Returns the completed request, NULL in case of none completed.
  617. * Wait for the an ongoing request (previoulsy started) to complete and
  618. * return the completed request. If there is no ongoing request, NULL
  619. * is returned without waiting. NULL is not an error condition.
  620. */
  621. struct mmc_async_req *mmc_start_areq(struct mmc_host *host,
  622. struct mmc_async_req *areq,
  623. enum mmc_blk_status *ret_stat)
  624. {
  625. enum mmc_blk_status status;
  626. int start_err = 0;
  627. struct mmc_async_req *previous = host->areq;
  628. /* Prepare a new request */
  629. if (areq)
  630. mmc_pre_req(host, areq->mrq);
  631. /* Finalize previous request */
  632. status = mmc_finalize_areq(host);
  633. if (ret_stat)
  634. *ret_stat = status;
  635. /* The previous request is still going on... */
  636. if (status == MMC_BLK_NEW_REQUEST)
  637. return NULL;
  638. /* Fine so far, start the new request! */
  639. if (status == MMC_BLK_SUCCESS && areq)
  640. start_err = __mmc_start_data_req(host, areq->mrq);
  641. /* Postprocess the old request at this point */
  642. if (host->areq)
  643. mmc_post_req(host, host->areq->mrq, 0);
  644. /* Cancel a prepared request if it was not started. */
  645. if ((status != MMC_BLK_SUCCESS || start_err) && areq)
  646. mmc_post_req(host, areq->mrq, -EINVAL);
  647. if (status != MMC_BLK_SUCCESS)
  648. host->areq = NULL;
  649. else
  650. host->areq = areq;
  651. return previous;
  652. }
  653. EXPORT_SYMBOL(mmc_start_areq);
  654. /**
  655. * mmc_wait_for_req - start a request and wait for completion
  656. * @host: MMC host to start command
  657. * @mrq: MMC request to start
  658. *
  659. * Start a new MMC custom command request for a host, and wait
  660. * for the command to complete. In the case of 'cap_cmd_during_tfr'
  661. * requests, the transfer is ongoing and the caller can issue further
  662. * commands that do not use the data lines, and then wait by calling
  663. * mmc_wait_for_req_done().
  664. * Does not attempt to parse the response.
  665. */
  666. void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
  667. {
  668. __mmc_start_req(host, mrq);
  669. if (!mrq->cap_cmd_during_tfr)
  670. mmc_wait_for_req_done(host, mrq);
  671. }
  672. EXPORT_SYMBOL(mmc_wait_for_req);
  673. /**
  674. * mmc_wait_for_cmd - start a command and wait for completion
  675. * @host: MMC host to start command
  676. * @cmd: MMC command to start
  677. * @retries: maximum number of retries
  678. *
  679. * Start a new MMC command for a host, and wait for the command
  680. * to complete. Return any error that occurred while the command
  681. * was executing. Do not attempt to parse the response.
  682. */
  683. int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
  684. {
  685. struct mmc_request mrq = {};
  686. WARN_ON(!host->claimed);
  687. memset(cmd->resp, 0, sizeof(cmd->resp));
  688. cmd->retries = retries;
  689. mrq.cmd = cmd;
  690. cmd->data = NULL;
  691. mmc_wait_for_req(host, &mrq);
  692. return cmd->error;
  693. }
  694. EXPORT_SYMBOL(mmc_wait_for_cmd);
  695. /**
  696. * mmc_set_data_timeout - set the timeout for a data command
  697. * @data: data phase for command
  698. * @card: the MMC card associated with the data transfer
  699. *
  700. * Computes the data timeout parameters according to the
  701. * correct algorithm given the card type.
  702. */
  703. void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
  704. {
  705. unsigned int mult;
  706. /*
  707. * SDIO cards only define an upper 1 s limit on access.
  708. */
  709. if (mmc_card_sdio(card)) {
  710. data->timeout_ns = 1000000000;
  711. data->timeout_clks = 0;
  712. return;
  713. }
  714. /*
  715. * SD cards use a 100 multiplier rather than 10
  716. */
  717. mult = mmc_card_sd(card) ? 100 : 10;
  718. /*
  719. * Scale up the multiplier (and therefore the timeout) by
  720. * the r2w factor for writes.
  721. */
  722. if (data->flags & MMC_DATA_WRITE)
  723. mult <<= card->csd.r2w_factor;
  724. data->timeout_ns = card->csd.taac_ns * mult;
  725. data->timeout_clks = card->csd.taac_clks * mult;
  726. /*
  727. * SD cards also have an upper limit on the timeout.
  728. */
  729. if (mmc_card_sd(card)) {
  730. unsigned int timeout_us, limit_us;
  731. timeout_us = data->timeout_ns / 1000;
  732. if (card->host->ios.clock)
  733. timeout_us += data->timeout_clks * 1000 /
  734. (card->host->ios.clock / 1000);
  735. if (data->flags & MMC_DATA_WRITE)
  736. /*
  737. * The MMC spec "It is strongly recommended
  738. * for hosts to implement more than 500ms
  739. * timeout value even if the card indicates
  740. * the 250ms maximum busy length." Even the
  741. * previous value of 300ms is known to be
  742. * insufficient for some cards.
  743. */
  744. limit_us = 3000000;
  745. else
  746. limit_us = 100000;
  747. /*
  748. * SDHC cards always use these fixed values.
  749. */
  750. if (timeout_us > limit_us) {
  751. data->timeout_ns = limit_us * 1000;
  752. data->timeout_clks = 0;
  753. }
  754. /* assign limit value if invalid */
  755. if (timeout_us == 0)
  756. data->timeout_ns = limit_us * 1000;
  757. }
  758. /*
  759. * Some cards require longer data read timeout than indicated in CSD.
  760. * Address this by setting the read timeout to a "reasonably high"
  761. * value. For the cards tested, 600ms has proven enough. If necessary,
  762. * this value can be increased if other problematic cards require this.
  763. */
  764. if (mmc_card_long_read_time(card) && data->flags & MMC_DATA_READ) {
  765. data->timeout_ns = 600000000;
  766. data->timeout_clks = 0;
  767. }
  768. /*
  769. * Some cards need very high timeouts if driven in SPI mode.
  770. * The worst observed timeout was 900ms after writing a
  771. * continuous stream of data until the internal logic
  772. * overflowed.
  773. */
  774. if (mmc_host_is_spi(card->host)) {
  775. if (data->flags & MMC_DATA_WRITE) {
  776. if (data->timeout_ns < 1000000000)
  777. data->timeout_ns = 1000000000; /* 1s */
  778. } else {
  779. if (data->timeout_ns < 100000000)
  780. data->timeout_ns = 100000000; /* 100ms */
  781. }
  782. }
  783. }
  784. EXPORT_SYMBOL(mmc_set_data_timeout);
  785. /**
  786. * mmc_align_data_size - pads a transfer size to a more optimal value
  787. * @card: the MMC card associated with the data transfer
  788. * @sz: original transfer size
  789. *
  790. * Pads the original data size with a number of extra bytes in
  791. * order to avoid controller bugs and/or performance hits
  792. * (e.g. some controllers revert to PIO for certain sizes).
  793. *
  794. * Returns the improved size, which might be unmodified.
  795. *
  796. * Note that this function is only relevant when issuing a
  797. * single scatter gather entry.
  798. */
  799. unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
  800. {
  801. /*
  802. * FIXME: We don't have a system for the controller to tell
  803. * the core about its problems yet, so for now we just 32-bit
  804. * align the size.
  805. */
  806. sz = ((sz + 3) / 4) * 4;
  807. return sz;
  808. }
  809. EXPORT_SYMBOL(mmc_align_data_size);
  810. /*
  811. * Allow claiming an already claimed host if the context is the same or there is
  812. * no context but the task is the same.
  813. */
  814. static inline bool mmc_ctx_matches(struct mmc_host *host, struct mmc_ctx *ctx,
  815. struct task_struct *task)
  816. {
  817. return host->claimer == ctx ||
  818. (!ctx && task && host->claimer->task == task);
  819. }
  820. static inline void mmc_ctx_set_claimer(struct mmc_host *host,
  821. struct mmc_ctx *ctx,
  822. struct task_struct *task)
  823. {
  824. if (!host->claimer) {
  825. if (ctx)
  826. host->claimer = ctx;
  827. else
  828. host->claimer = &host->default_ctx;
  829. }
  830. if (task)
  831. host->claimer->task = task;
  832. }
  833. /**
  834. * __mmc_claim_host - exclusively claim a host
  835. * @host: mmc host to claim
  836. * @ctx: context that claims the host or NULL in which case the default
  837. * context will be used
  838. * @abort: whether or not the operation should be aborted
  839. *
  840. * Claim a host for a set of operations. If @abort is non null and
  841. * dereference a non-zero value then this will return prematurely with
  842. * that non-zero value without acquiring the lock. Returns zero
  843. * with the lock held otherwise.
  844. */
  845. int __mmc_claim_host(struct mmc_host *host, struct mmc_ctx *ctx,
  846. atomic_t *abort)
  847. {
  848. struct task_struct *task = ctx ? NULL : current;
  849. DECLARE_WAITQUEUE(wait, current);
  850. unsigned long flags;
  851. int stop;
  852. bool pm = false;
  853. might_sleep();
  854. add_wait_queue(&host->wq, &wait);
  855. spin_lock_irqsave(&host->lock, flags);
  856. while (1) {
  857. set_current_state(TASK_UNINTERRUPTIBLE);
  858. stop = abort ? atomic_read(abort) : 0;
  859. if (stop || !host->claimed || mmc_ctx_matches(host, ctx, task))
  860. break;
  861. spin_unlock_irqrestore(&host->lock, flags);
  862. schedule();
  863. spin_lock_irqsave(&host->lock, flags);
  864. }
  865. set_current_state(TASK_RUNNING);
  866. if (!stop) {
  867. host->claimed = 1;
  868. mmc_ctx_set_claimer(host, ctx, task);
  869. host->claim_cnt += 1;
  870. if (host->claim_cnt == 1)
  871. pm = true;
  872. } else
  873. wake_up(&host->wq);
  874. spin_unlock_irqrestore(&host->lock, flags);
  875. remove_wait_queue(&host->wq, &wait);
  876. if (pm)
  877. pm_runtime_get_sync(mmc_dev(host));
  878. return stop;
  879. }
  880. EXPORT_SYMBOL(__mmc_claim_host);
  881. /**
  882. * mmc_release_host - release a host
  883. * @host: mmc host to release
  884. *
  885. * Release a MMC host, allowing others to claim the host
  886. * for their operations.
  887. */
  888. void mmc_release_host(struct mmc_host *host)
  889. {
  890. unsigned long flags;
  891. WARN_ON(!host->claimed);
  892. spin_lock_irqsave(&host->lock, flags);
  893. if (--host->claim_cnt) {
  894. /* Release for nested claim */
  895. spin_unlock_irqrestore(&host->lock, flags);
  896. } else {
  897. host->claimed = 0;
  898. host->claimer->task = NULL;
  899. host->claimer = NULL;
  900. spin_unlock_irqrestore(&host->lock, flags);
  901. wake_up(&host->wq);
  902. pm_runtime_mark_last_busy(mmc_dev(host));
  903. pm_runtime_put_autosuspend(mmc_dev(host));
  904. }
  905. }
  906. EXPORT_SYMBOL(mmc_release_host);
  907. /*
  908. * This is a helper function, which fetches a runtime pm reference for the
  909. * card device and also claims the host.
  910. */
  911. void mmc_get_card(struct mmc_card *card, struct mmc_ctx *ctx)
  912. {
  913. pm_runtime_get_sync(&card->dev);
  914. __mmc_claim_host(card->host, ctx, NULL);
  915. }
  916. EXPORT_SYMBOL(mmc_get_card);
  917. /*
  918. * This is a helper function, which releases the host and drops the runtime
  919. * pm reference for the card device.
  920. */
  921. void mmc_put_card(struct mmc_card *card, struct mmc_ctx *ctx)
  922. {
  923. struct mmc_host *host = card->host;
  924. WARN_ON(ctx && host->claimer != ctx);
  925. mmc_release_host(host);
  926. pm_runtime_mark_last_busy(&card->dev);
  927. pm_runtime_put_autosuspend(&card->dev);
  928. }
  929. EXPORT_SYMBOL(mmc_put_card);
  930. /*
  931. * Internal function that does the actual ios call to the host driver,
  932. * optionally printing some debug output.
  933. */
  934. static inline void mmc_set_ios(struct mmc_host *host)
  935. {
  936. struct mmc_ios *ios = &host->ios;
  937. pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
  938. "width %u timing %u\n",
  939. mmc_hostname(host), ios->clock, ios->bus_mode,
  940. ios->power_mode, ios->chip_select, ios->vdd,
  941. 1 << ios->bus_width, ios->timing);
  942. host->ops->set_ios(host, ios);
  943. }
  944. /*
  945. * Control chip select pin on a host.
  946. */
  947. void mmc_set_chip_select(struct mmc_host *host, int mode)
  948. {
  949. host->ios.chip_select = mode;
  950. mmc_set_ios(host);
  951. }
  952. /*
  953. * Sets the host clock to the highest possible frequency that
  954. * is below "hz".
  955. */
  956. void mmc_set_clock(struct mmc_host *host, unsigned int hz)
  957. {
  958. WARN_ON(hz && hz < host->f_min);
  959. if (hz > host->f_max)
  960. hz = host->f_max;
  961. host->ios.clock = hz;
  962. mmc_set_ios(host);
  963. }
  964. int mmc_execute_tuning(struct mmc_card *card)
  965. {
  966. struct mmc_host *host = card->host;
  967. u32 opcode;
  968. int err;
  969. if (!host->ops->execute_tuning)
  970. return 0;
  971. if (host->cqe_on)
  972. host->cqe_ops->cqe_off(host);
  973. if (mmc_card_mmc(card))
  974. opcode = MMC_SEND_TUNING_BLOCK_HS200;
  975. else
  976. opcode = MMC_SEND_TUNING_BLOCK;
  977. err = host->ops->execute_tuning(host, opcode);
  978. if (err)
  979. pr_err("%s: tuning execution failed: %d\n",
  980. mmc_hostname(host), err);
  981. else
  982. mmc_retune_enable(host);
  983. return err;
  984. }
  985. /*
  986. * Change the bus mode (open drain/push-pull) of a host.
  987. */
  988. void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
  989. {
  990. host->ios.bus_mode = mode;
  991. mmc_set_ios(host);
  992. }
  993. /*
  994. * Change data bus width of a host.
  995. */
  996. void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
  997. {
  998. host->ios.bus_width = width;
  999. mmc_set_ios(host);
  1000. }
  1001. /*
  1002. * Set initial state after a power cycle or a hw_reset.
  1003. */
  1004. void mmc_set_initial_state(struct mmc_host *host)
  1005. {
  1006. if (host->cqe_on)
  1007. host->cqe_ops->cqe_off(host);
  1008. mmc_retune_disable(host);
  1009. if (mmc_host_is_spi(host))
  1010. host->ios.chip_select = MMC_CS_HIGH;
  1011. else
  1012. host->ios.chip_select = MMC_CS_DONTCARE;
  1013. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  1014. host->ios.bus_width = MMC_BUS_WIDTH_1;
  1015. host->ios.timing = MMC_TIMING_LEGACY;
  1016. host->ios.drv_type = 0;
  1017. host->ios.enhanced_strobe = false;
  1018. /*
  1019. * Make sure we are in non-enhanced strobe mode before we
  1020. * actually enable it in ext_csd.
  1021. */
  1022. if ((host->caps2 & MMC_CAP2_HS400_ES) &&
  1023. host->ops->hs400_enhanced_strobe)
  1024. host->ops->hs400_enhanced_strobe(host, &host->ios);
  1025. mmc_set_ios(host);
  1026. }
  1027. /**
  1028. * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
  1029. * @vdd: voltage (mV)
  1030. * @low_bits: prefer low bits in boundary cases
  1031. *
  1032. * This function returns the OCR bit number according to the provided @vdd
  1033. * value. If conversion is not possible a negative errno value returned.
  1034. *
  1035. * Depending on the @low_bits flag the function prefers low or high OCR bits
  1036. * on boundary voltages. For example,
  1037. * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
  1038. * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
  1039. *
  1040. * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
  1041. */
  1042. static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
  1043. {
  1044. const int max_bit = ilog2(MMC_VDD_35_36);
  1045. int bit;
  1046. if (vdd < 1650 || vdd > 3600)
  1047. return -EINVAL;
  1048. if (vdd >= 1650 && vdd <= 1950)
  1049. return ilog2(MMC_VDD_165_195);
  1050. if (low_bits)
  1051. vdd -= 1;
  1052. /* Base 2000 mV, step 100 mV, bit's base 8. */
  1053. bit = (vdd - 2000) / 100 + 8;
  1054. if (bit > max_bit)
  1055. return max_bit;
  1056. return bit;
  1057. }
  1058. /**
  1059. * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
  1060. * @vdd_min: minimum voltage value (mV)
  1061. * @vdd_max: maximum voltage value (mV)
  1062. *
  1063. * This function returns the OCR mask bits according to the provided @vdd_min
  1064. * and @vdd_max values. If conversion is not possible the function returns 0.
  1065. *
  1066. * Notes wrt boundary cases:
  1067. * This function sets the OCR bits for all boundary voltages, for example
  1068. * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
  1069. * MMC_VDD_34_35 mask.
  1070. */
  1071. u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
  1072. {
  1073. u32 mask = 0;
  1074. if (vdd_max < vdd_min)
  1075. return 0;
  1076. /* Prefer high bits for the boundary vdd_max values. */
  1077. vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
  1078. if (vdd_max < 0)
  1079. return 0;
  1080. /* Prefer low bits for the boundary vdd_min values. */
  1081. vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
  1082. if (vdd_min < 0)
  1083. return 0;
  1084. /* Fill the mask, from max bit to min bit. */
  1085. while (vdd_max >= vdd_min)
  1086. mask |= 1 << vdd_max--;
  1087. return mask;
  1088. }
  1089. EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
  1090. #ifdef CONFIG_OF
  1091. /**
  1092. * mmc_of_parse_voltage - return mask of supported voltages
  1093. * @np: The device node need to be parsed.
  1094. * @mask: mask of voltages available for MMC/SD/SDIO
  1095. *
  1096. * Parse the "voltage-ranges" DT property, returning zero if it is not
  1097. * found, negative errno if the voltage-range specification is invalid,
  1098. * or one if the voltage-range is specified and successfully parsed.
  1099. */
  1100. int mmc_of_parse_voltage(struct device_node *np, u32 *mask)
  1101. {
  1102. const u32 *voltage_ranges;
  1103. int num_ranges, i;
  1104. voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges);
  1105. num_ranges = num_ranges / sizeof(*voltage_ranges) / 2;
  1106. if (!voltage_ranges) {
  1107. pr_debug("%pOF: voltage-ranges unspecified\n", np);
  1108. return 0;
  1109. }
  1110. if (!num_ranges) {
  1111. pr_err("%pOF: voltage-ranges empty\n", np);
  1112. return -EINVAL;
  1113. }
  1114. for (i = 0; i < num_ranges; i++) {
  1115. const int j = i * 2;
  1116. u32 ocr_mask;
  1117. ocr_mask = mmc_vddrange_to_ocrmask(
  1118. be32_to_cpu(voltage_ranges[j]),
  1119. be32_to_cpu(voltage_ranges[j + 1]));
  1120. if (!ocr_mask) {
  1121. pr_err("%pOF: voltage-range #%d is invalid\n",
  1122. np, i);
  1123. return -EINVAL;
  1124. }
  1125. *mask |= ocr_mask;
  1126. }
  1127. return 1;
  1128. }
  1129. EXPORT_SYMBOL(mmc_of_parse_voltage);
  1130. #endif /* CONFIG_OF */
  1131. static int mmc_of_get_func_num(struct device_node *node)
  1132. {
  1133. u32 reg;
  1134. int ret;
  1135. ret = of_property_read_u32(node, "reg", &reg);
  1136. if (ret < 0)
  1137. return ret;
  1138. return reg;
  1139. }
  1140. struct device_node *mmc_of_find_child_device(struct mmc_host *host,
  1141. unsigned func_num)
  1142. {
  1143. struct device_node *node;
  1144. if (!host->parent || !host->parent->of_node)
  1145. return NULL;
  1146. for_each_child_of_node(host->parent->of_node, node) {
  1147. if (mmc_of_get_func_num(node) == func_num)
  1148. return node;
  1149. }
  1150. return NULL;
  1151. }
  1152. #ifdef CONFIG_REGULATOR
  1153. /**
  1154. * mmc_ocrbitnum_to_vdd - Convert a OCR bit number to its voltage
  1155. * @vdd_bit: OCR bit number
  1156. * @min_uV: minimum voltage value (mV)
  1157. * @max_uV: maximum voltage value (mV)
  1158. *
  1159. * This function returns the voltage range according to the provided OCR
  1160. * bit number. If conversion is not possible a negative errno value returned.
  1161. */
  1162. static int mmc_ocrbitnum_to_vdd(int vdd_bit, int *min_uV, int *max_uV)
  1163. {
  1164. int tmp;
  1165. if (!vdd_bit)
  1166. return -EINVAL;
  1167. /*
  1168. * REVISIT mmc_vddrange_to_ocrmask() may have set some
  1169. * bits this regulator doesn't quite support ... don't
  1170. * be too picky, most cards and regulators are OK with
  1171. * a 0.1V range goof (it's a small error percentage).
  1172. */
  1173. tmp = vdd_bit - ilog2(MMC_VDD_165_195);
  1174. if (tmp == 0) {
  1175. *min_uV = 1650 * 1000;
  1176. *max_uV = 1950 * 1000;
  1177. } else {
  1178. *min_uV = 1900 * 1000 + tmp * 100 * 1000;
  1179. *max_uV = *min_uV + 100 * 1000;
  1180. }
  1181. return 0;
  1182. }
  1183. /**
  1184. * mmc_regulator_get_ocrmask - return mask of supported voltages
  1185. * @supply: regulator to use
  1186. *
  1187. * This returns either a negative errno, or a mask of voltages that
  1188. * can be provided to MMC/SD/SDIO devices using the specified voltage
  1189. * regulator. This would normally be called before registering the
  1190. * MMC host adapter.
  1191. */
  1192. int mmc_regulator_get_ocrmask(struct regulator *supply)
  1193. {
  1194. int result = 0;
  1195. int count;
  1196. int i;
  1197. int vdd_uV;
  1198. int vdd_mV;
  1199. count = regulator_count_voltages(supply);
  1200. if (count < 0)
  1201. return count;
  1202. for (i = 0; i < count; i++) {
  1203. vdd_uV = regulator_list_voltage(supply, i);
  1204. if (vdd_uV <= 0)
  1205. continue;
  1206. vdd_mV = vdd_uV / 1000;
  1207. result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
  1208. }
  1209. if (!result) {
  1210. vdd_uV = regulator_get_voltage(supply);
  1211. if (vdd_uV <= 0)
  1212. return vdd_uV;
  1213. vdd_mV = vdd_uV / 1000;
  1214. result = mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
  1215. }
  1216. return result;
  1217. }
  1218. EXPORT_SYMBOL_GPL(mmc_regulator_get_ocrmask);
  1219. /**
  1220. * mmc_regulator_set_ocr - set regulator to match host->ios voltage
  1221. * @mmc: the host to regulate
  1222. * @supply: regulator to use
  1223. * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
  1224. *
  1225. * Returns zero on success, else negative errno.
  1226. *
  1227. * MMC host drivers may use this to enable or disable a regulator using
  1228. * a particular supply voltage. This would normally be called from the
  1229. * set_ios() method.
  1230. */
  1231. int mmc_regulator_set_ocr(struct mmc_host *mmc,
  1232. struct regulator *supply,
  1233. unsigned short vdd_bit)
  1234. {
  1235. int result = 0;
  1236. int min_uV, max_uV;
  1237. if (vdd_bit) {
  1238. mmc_ocrbitnum_to_vdd(vdd_bit, &min_uV, &max_uV);
  1239. result = regulator_set_voltage(supply, min_uV, max_uV);
  1240. if (result == 0 && !mmc->regulator_enabled) {
  1241. result = regulator_enable(supply);
  1242. if (!result)
  1243. mmc->regulator_enabled = true;
  1244. }
  1245. } else if (mmc->regulator_enabled) {
  1246. result = regulator_disable(supply);
  1247. if (result == 0)
  1248. mmc->regulator_enabled = false;
  1249. }
  1250. if (result)
  1251. dev_err(mmc_dev(mmc),
  1252. "could not set regulator OCR (%d)\n", result);
  1253. return result;
  1254. }
  1255. EXPORT_SYMBOL_GPL(mmc_regulator_set_ocr);
  1256. static int mmc_regulator_set_voltage_if_supported(struct regulator *regulator,
  1257. int min_uV, int target_uV,
  1258. int max_uV)
  1259. {
  1260. /*
  1261. * Check if supported first to avoid errors since we may try several
  1262. * signal levels during power up and don't want to show errors.
  1263. */
  1264. if (!regulator_is_supported_voltage(regulator, min_uV, max_uV))
  1265. return -EINVAL;
  1266. return regulator_set_voltage_triplet(regulator, min_uV, target_uV,
  1267. max_uV);
  1268. }
  1269. /**
  1270. * mmc_regulator_set_vqmmc - Set VQMMC as per the ios
  1271. *
  1272. * For 3.3V signaling, we try to match VQMMC to VMMC as closely as possible.
  1273. * That will match the behavior of old boards where VQMMC and VMMC were supplied
  1274. * by the same supply. The Bus Operating conditions for 3.3V signaling in the
  1275. * SD card spec also define VQMMC in terms of VMMC.
  1276. * If this is not possible we'll try the full 2.7-3.6V of the spec.
  1277. *
  1278. * For 1.2V and 1.8V signaling we'll try to get as close as possible to the
  1279. * requested voltage. This is definitely a good idea for UHS where there's a
  1280. * separate regulator on the card that's trying to make 1.8V and it's best if
  1281. * we match.
  1282. *
  1283. * This function is expected to be used by a controller's
  1284. * start_signal_voltage_switch() function.
  1285. */
  1286. int mmc_regulator_set_vqmmc(struct mmc_host *mmc, struct mmc_ios *ios)
  1287. {
  1288. struct device *dev = mmc_dev(mmc);
  1289. int ret, volt, min_uV, max_uV;
  1290. /* If no vqmmc supply then we can't change the voltage */
  1291. if (IS_ERR(mmc->supply.vqmmc))
  1292. return -EINVAL;
  1293. switch (ios->signal_voltage) {
  1294. case MMC_SIGNAL_VOLTAGE_120:
  1295. return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
  1296. 1100000, 1200000, 1300000);
  1297. case MMC_SIGNAL_VOLTAGE_180:
  1298. return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
  1299. 1700000, 1800000, 1950000);
  1300. case MMC_SIGNAL_VOLTAGE_330:
  1301. ret = mmc_ocrbitnum_to_vdd(mmc->ios.vdd, &volt, &max_uV);
  1302. if (ret < 0)
  1303. return ret;
  1304. dev_dbg(dev, "%s: found vmmc voltage range of %d-%duV\n",
  1305. __func__, volt, max_uV);
  1306. min_uV = max(volt - 300000, 2700000);
  1307. max_uV = min(max_uV + 200000, 3600000);
  1308. /*
  1309. * Due to a limitation in the current implementation of
  1310. * regulator_set_voltage_triplet() which is taking the lowest
  1311. * voltage possible if below the target, search for a suitable
  1312. * voltage in two steps and try to stay close to vmmc
  1313. * with a 0.3V tolerance at first.
  1314. */
  1315. if (!mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
  1316. min_uV, volt, max_uV))
  1317. return 0;
  1318. return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
  1319. 2700000, volt, 3600000);
  1320. default:
  1321. return -EINVAL;
  1322. }
  1323. }
  1324. EXPORT_SYMBOL_GPL(mmc_regulator_set_vqmmc);
  1325. #endif /* CONFIG_REGULATOR */
  1326. /**
  1327. * mmc_regulator_get_supply - try to get VMMC and VQMMC regulators for a host
  1328. * @mmc: the host to regulate
  1329. *
  1330. * Returns 0 or errno. errno should be handled, it is either a critical error
  1331. * or -EPROBE_DEFER. 0 means no critical error but it does not mean all
  1332. * regulators have been found because they all are optional. If you require
  1333. * certain regulators, you need to check separately in your driver if they got
  1334. * populated after calling this function.
  1335. */
  1336. int mmc_regulator_get_supply(struct mmc_host *mmc)
  1337. {
  1338. struct device *dev = mmc_dev(mmc);
  1339. int ret;
  1340. mmc->supply.vmmc = devm_regulator_get_optional(dev, "vmmc");
  1341. mmc->supply.vqmmc = devm_regulator_get_optional(dev, "vqmmc");
  1342. if (IS_ERR(mmc->supply.vmmc)) {
  1343. if (PTR_ERR(mmc->supply.vmmc) == -EPROBE_DEFER)
  1344. return -EPROBE_DEFER;
  1345. dev_dbg(dev, "No vmmc regulator found\n");
  1346. } else {
  1347. ret = mmc_regulator_get_ocrmask(mmc->supply.vmmc);
  1348. if (ret > 0)
  1349. mmc->ocr_avail = ret;
  1350. else
  1351. dev_warn(dev, "Failed getting OCR mask: %d\n", ret);
  1352. }
  1353. if (IS_ERR(mmc->supply.vqmmc)) {
  1354. if (PTR_ERR(mmc->supply.vqmmc) == -EPROBE_DEFER)
  1355. return -EPROBE_DEFER;
  1356. dev_dbg(dev, "No vqmmc regulator found\n");
  1357. }
  1358. return 0;
  1359. }
  1360. EXPORT_SYMBOL_GPL(mmc_regulator_get_supply);
  1361. /*
  1362. * Mask off any voltages we don't support and select
  1363. * the lowest voltage
  1364. */
  1365. u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
  1366. {
  1367. int bit;
  1368. /*
  1369. * Sanity check the voltages that the card claims to
  1370. * support.
  1371. */
  1372. if (ocr & 0x7F) {
  1373. dev_warn(mmc_dev(host),
  1374. "card claims to support voltages below defined range\n");
  1375. ocr &= ~0x7F;
  1376. }
  1377. ocr &= host->ocr_avail;
  1378. if (!ocr) {
  1379. dev_warn(mmc_dev(host), "no support for card's volts\n");
  1380. return 0;
  1381. }
  1382. if (host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) {
  1383. bit = ffs(ocr) - 1;
  1384. ocr &= 3 << bit;
  1385. mmc_power_cycle(host, ocr);
  1386. } else {
  1387. bit = fls(ocr) - 1;
  1388. ocr &= 3 << bit;
  1389. if (bit != host->ios.vdd)
  1390. dev_warn(mmc_dev(host), "exceeding card's volts\n");
  1391. }
  1392. return ocr;
  1393. }
  1394. int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage)
  1395. {
  1396. int err = 0;
  1397. int old_signal_voltage = host->ios.signal_voltage;
  1398. host->ios.signal_voltage = signal_voltage;
  1399. if (host->ops->start_signal_voltage_switch)
  1400. err = host->ops->start_signal_voltage_switch(host, &host->ios);
  1401. if (err)
  1402. host->ios.signal_voltage = old_signal_voltage;
  1403. return err;
  1404. }
  1405. int mmc_host_set_uhs_voltage(struct mmc_host *host)
  1406. {
  1407. u32 clock;
  1408. /*
  1409. * During a signal voltage level switch, the clock must be gated
  1410. * for 5 ms according to the SD spec
  1411. */
  1412. clock = host->ios.clock;
  1413. host->ios.clock = 0;
  1414. mmc_set_ios(host);
  1415. if (mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180))
  1416. return -EAGAIN;
  1417. /* Keep clock gated for at least 10 ms, though spec only says 5 ms */
  1418. mmc_delay(10);
  1419. host->ios.clock = clock;
  1420. mmc_set_ios(host);
  1421. return 0;
  1422. }
  1423. int mmc_set_uhs_voltage(struct mmc_host *host, u32 ocr)
  1424. {
  1425. struct mmc_command cmd = {};
  1426. int err = 0;
  1427. /*
  1428. * If we cannot switch voltages, return failure so the caller
  1429. * can continue without UHS mode
  1430. */
  1431. if (!host->ops->start_signal_voltage_switch)
  1432. return -EPERM;
  1433. if (!host->ops->card_busy)
  1434. pr_warn("%s: cannot verify signal voltage switch\n",
  1435. mmc_hostname(host));
  1436. cmd.opcode = SD_SWITCH_VOLTAGE;
  1437. cmd.arg = 0;
  1438. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1439. err = mmc_wait_for_cmd(host, &cmd, 0);
  1440. if (err)
  1441. return err;
  1442. if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
  1443. return -EIO;
  1444. /*
  1445. * The card should drive cmd and dat[0:3] low immediately
  1446. * after the response of cmd11, but wait 1 ms to be sure
  1447. */
  1448. mmc_delay(1);
  1449. if (host->ops->card_busy && !host->ops->card_busy(host)) {
  1450. err = -EAGAIN;
  1451. goto power_cycle;
  1452. }
  1453. if (mmc_host_set_uhs_voltage(host)) {
  1454. /*
  1455. * Voltages may not have been switched, but we've already
  1456. * sent CMD11, so a power cycle is required anyway
  1457. */
  1458. err = -EAGAIN;
  1459. goto power_cycle;
  1460. }
  1461. /* Wait for at least 1 ms according to spec */
  1462. mmc_delay(1);
  1463. /*
  1464. * Failure to switch is indicated by the card holding
  1465. * dat[0:3] low
  1466. */
  1467. if (host->ops->card_busy && host->ops->card_busy(host))
  1468. err = -EAGAIN;
  1469. power_cycle:
  1470. if (err) {
  1471. pr_debug("%s: Signal voltage switch failed, "
  1472. "power cycling card\n", mmc_hostname(host));
  1473. mmc_power_cycle(host, ocr);
  1474. }
  1475. return err;
  1476. }
  1477. /*
  1478. * Select timing parameters for host.
  1479. */
  1480. void mmc_set_timing(struct mmc_host *host, unsigned int timing)
  1481. {
  1482. host->ios.timing = timing;
  1483. mmc_set_ios(host);
  1484. }
  1485. /*
  1486. * Select appropriate driver type for host.
  1487. */
  1488. void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
  1489. {
  1490. host->ios.drv_type = drv_type;
  1491. mmc_set_ios(host);
  1492. }
  1493. int mmc_select_drive_strength(struct mmc_card *card, unsigned int max_dtr,
  1494. int card_drv_type, int *drv_type)
  1495. {
  1496. struct mmc_host *host = card->host;
  1497. int host_drv_type = SD_DRIVER_TYPE_B;
  1498. *drv_type = 0;
  1499. if (!host->ops->select_drive_strength)
  1500. return 0;
  1501. /* Use SD definition of driver strength for hosts */
  1502. if (host->caps & MMC_CAP_DRIVER_TYPE_A)
  1503. host_drv_type |= SD_DRIVER_TYPE_A;
  1504. if (host->caps & MMC_CAP_DRIVER_TYPE_C)
  1505. host_drv_type |= SD_DRIVER_TYPE_C;
  1506. if (host->caps & MMC_CAP_DRIVER_TYPE_D)
  1507. host_drv_type |= SD_DRIVER_TYPE_D;
  1508. /*
  1509. * The drive strength that the hardware can support
  1510. * depends on the board design. Pass the appropriate
  1511. * information and let the hardware specific code
  1512. * return what is possible given the options
  1513. */
  1514. return host->ops->select_drive_strength(card, max_dtr,
  1515. host_drv_type,
  1516. card_drv_type,
  1517. drv_type);
  1518. }
  1519. /*
  1520. * Apply power to the MMC stack. This is a two-stage process.
  1521. * First, we enable power to the card without the clock running.
  1522. * We then wait a bit for the power to stabilise. Finally,
  1523. * enable the bus drivers and clock to the card.
  1524. *
  1525. * We must _NOT_ enable the clock prior to power stablising.
  1526. *
  1527. * If a host does all the power sequencing itself, ignore the
  1528. * initial MMC_POWER_UP stage.
  1529. */
  1530. void mmc_power_up(struct mmc_host *host, u32 ocr)
  1531. {
  1532. if (host->ios.power_mode == MMC_POWER_ON)
  1533. return;
  1534. mmc_pwrseq_pre_power_on(host);
  1535. host->ios.vdd = fls(ocr) - 1;
  1536. host->ios.power_mode = MMC_POWER_UP;
  1537. /* Set initial state and call mmc_set_ios */
  1538. mmc_set_initial_state(host);
  1539. /* Try to set signal voltage to 3.3V but fall back to 1.8v or 1.2v */
  1540. if (!mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330))
  1541. dev_dbg(mmc_dev(host), "Initial signal voltage of 3.3v\n");
  1542. else if (!mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180))
  1543. dev_dbg(mmc_dev(host), "Initial signal voltage of 1.8v\n");
  1544. else if (!mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120))
  1545. dev_dbg(mmc_dev(host), "Initial signal voltage of 1.2v\n");
  1546. /*
  1547. * This delay should be sufficient to allow the power supply
  1548. * to reach the minimum voltage.
  1549. */
  1550. mmc_delay(10);
  1551. mmc_pwrseq_post_power_on(host);
  1552. host->ios.clock = host->f_init;
  1553. host->ios.power_mode = MMC_POWER_ON;
  1554. mmc_set_ios(host);
  1555. /*
  1556. * This delay must be at least 74 clock sizes, or 1 ms, or the
  1557. * time required to reach a stable voltage.
  1558. */
  1559. mmc_delay(10);
  1560. }
  1561. void mmc_power_off(struct mmc_host *host)
  1562. {
  1563. if (host->ios.power_mode == MMC_POWER_OFF)
  1564. return;
  1565. mmc_pwrseq_power_off(host);
  1566. host->ios.clock = 0;
  1567. host->ios.vdd = 0;
  1568. host->ios.power_mode = MMC_POWER_OFF;
  1569. /* Set initial state and call mmc_set_ios */
  1570. mmc_set_initial_state(host);
  1571. /*
  1572. * Some configurations, such as the 802.11 SDIO card in the OLPC
  1573. * XO-1.5, require a short delay after poweroff before the card
  1574. * can be successfully turned on again.
  1575. */
  1576. mmc_delay(1);
  1577. }
  1578. void mmc_power_cycle(struct mmc_host *host, u32 ocr)
  1579. {
  1580. mmc_power_off(host);
  1581. /* Wait at least 1 ms according to SD spec */
  1582. mmc_delay(1);
  1583. mmc_power_up(host, ocr);
  1584. }
  1585. /*
  1586. * Cleanup when the last reference to the bus operator is dropped.
  1587. */
  1588. static void __mmc_release_bus(struct mmc_host *host)
  1589. {
  1590. WARN_ON(!host->bus_dead);
  1591. host->bus_ops = NULL;
  1592. }
  1593. /*
  1594. * Increase reference count of bus operator
  1595. */
  1596. static inline void mmc_bus_get(struct mmc_host *host)
  1597. {
  1598. unsigned long flags;
  1599. spin_lock_irqsave(&host->lock, flags);
  1600. host->bus_refs++;
  1601. spin_unlock_irqrestore(&host->lock, flags);
  1602. }
  1603. /*
  1604. * Decrease reference count of bus operator and free it if
  1605. * it is the last reference.
  1606. */
  1607. static inline void mmc_bus_put(struct mmc_host *host)
  1608. {
  1609. unsigned long flags;
  1610. spin_lock_irqsave(&host->lock, flags);
  1611. host->bus_refs--;
  1612. if ((host->bus_refs == 0) && host->bus_ops)
  1613. __mmc_release_bus(host);
  1614. spin_unlock_irqrestore(&host->lock, flags);
  1615. }
  1616. /*
  1617. * Assign a mmc bus handler to a host. Only one bus handler may control a
  1618. * host at any given time.
  1619. */
  1620. void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
  1621. {
  1622. unsigned long flags;
  1623. WARN_ON(!host->claimed);
  1624. spin_lock_irqsave(&host->lock, flags);
  1625. WARN_ON(host->bus_ops);
  1626. WARN_ON(host->bus_refs);
  1627. host->bus_ops = ops;
  1628. host->bus_refs = 1;
  1629. host->bus_dead = 0;
  1630. spin_unlock_irqrestore(&host->lock, flags);
  1631. }
  1632. /*
  1633. * Remove the current bus handler from a host.
  1634. */
  1635. void mmc_detach_bus(struct mmc_host *host)
  1636. {
  1637. unsigned long flags;
  1638. WARN_ON(!host->claimed);
  1639. WARN_ON(!host->bus_ops);
  1640. spin_lock_irqsave(&host->lock, flags);
  1641. host->bus_dead = 1;
  1642. spin_unlock_irqrestore(&host->lock, flags);
  1643. mmc_bus_put(host);
  1644. }
  1645. static void _mmc_detect_change(struct mmc_host *host, unsigned long delay,
  1646. bool cd_irq)
  1647. {
  1648. /*
  1649. * If the device is configured as wakeup, we prevent a new sleep for
  1650. * 5 s to give provision for user space to consume the event.
  1651. */
  1652. if (cd_irq && !(host->caps & MMC_CAP_NEEDS_POLL) &&
  1653. device_can_wakeup(mmc_dev(host)))
  1654. pm_wakeup_event(mmc_dev(host), 5000);
  1655. host->detect_change = 1;
  1656. mmc_schedule_delayed_work(&host->detect, delay);
  1657. }
  1658. /**
  1659. * mmc_detect_change - process change of state on a MMC socket
  1660. * @host: host which changed state.
  1661. * @delay: optional delay to wait before detection (jiffies)
  1662. *
  1663. * MMC drivers should call this when they detect a card has been
  1664. * inserted or removed. The MMC layer will confirm that any
  1665. * present card is still functional, and initialize any newly
  1666. * inserted.
  1667. */
  1668. void mmc_detect_change(struct mmc_host *host, unsigned long delay)
  1669. {
  1670. _mmc_detect_change(host, delay, true);
  1671. }
  1672. EXPORT_SYMBOL(mmc_detect_change);
  1673. void mmc_init_erase(struct mmc_card *card)
  1674. {
  1675. unsigned int sz;
  1676. if (is_power_of_2(card->erase_size))
  1677. card->erase_shift = ffs(card->erase_size) - 1;
  1678. else
  1679. card->erase_shift = 0;
  1680. /*
  1681. * It is possible to erase an arbitrarily large area of an SD or MMC
  1682. * card. That is not desirable because it can take a long time
  1683. * (minutes) potentially delaying more important I/O, and also the
  1684. * timeout calculations become increasingly hugely over-estimated.
  1685. * Consequently, 'pref_erase' is defined as a guide to limit erases
  1686. * to that size and alignment.
  1687. *
  1688. * For SD cards that define Allocation Unit size, limit erases to one
  1689. * Allocation Unit at a time.
  1690. * For MMC, have a stab at ai good value and for modern cards it will
  1691. * end up being 4MiB. Note that if the value is too small, it can end
  1692. * up taking longer to erase. Also note, erase_size is already set to
  1693. * High Capacity Erase Size if available when this function is called.
  1694. */
  1695. if (mmc_card_sd(card) && card->ssr.au) {
  1696. card->pref_erase = card->ssr.au;
  1697. card->erase_shift = ffs(card->ssr.au) - 1;
  1698. } else if (card->erase_size) {
  1699. sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
  1700. if (sz < 128)
  1701. card->pref_erase = 512 * 1024 / 512;
  1702. else if (sz < 512)
  1703. card->pref_erase = 1024 * 1024 / 512;
  1704. else if (sz < 1024)
  1705. card->pref_erase = 2 * 1024 * 1024 / 512;
  1706. else
  1707. card->pref_erase = 4 * 1024 * 1024 / 512;
  1708. if (card->pref_erase < card->erase_size)
  1709. card->pref_erase = card->erase_size;
  1710. else {
  1711. sz = card->pref_erase % card->erase_size;
  1712. if (sz)
  1713. card->pref_erase += card->erase_size - sz;
  1714. }
  1715. } else
  1716. card->pref_erase = 0;
  1717. }
  1718. static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
  1719. unsigned int arg, unsigned int qty)
  1720. {
  1721. unsigned int erase_timeout;
  1722. if (arg == MMC_DISCARD_ARG ||
  1723. (arg == MMC_TRIM_ARG && card->ext_csd.rev >= 6)) {
  1724. erase_timeout = card->ext_csd.trim_timeout;
  1725. } else if (card->ext_csd.erase_group_def & 1) {
  1726. /* High Capacity Erase Group Size uses HC timeouts */
  1727. if (arg == MMC_TRIM_ARG)
  1728. erase_timeout = card->ext_csd.trim_timeout;
  1729. else
  1730. erase_timeout = card->ext_csd.hc_erase_timeout;
  1731. } else {
  1732. /* CSD Erase Group Size uses write timeout */
  1733. unsigned int mult = (10 << card->csd.r2w_factor);
  1734. unsigned int timeout_clks = card->csd.taac_clks * mult;
  1735. unsigned int timeout_us;
  1736. /* Avoid overflow: e.g. taac_ns=80000000 mult=1280 */
  1737. if (card->csd.taac_ns < 1000000)
  1738. timeout_us = (card->csd.taac_ns * mult) / 1000;
  1739. else
  1740. timeout_us = (card->csd.taac_ns / 1000) * mult;
  1741. /*
  1742. * ios.clock is only a target. The real clock rate might be
  1743. * less but not that much less, so fudge it by multiplying by 2.
  1744. */
  1745. timeout_clks <<= 1;
  1746. timeout_us += (timeout_clks * 1000) /
  1747. (card->host->ios.clock / 1000);
  1748. erase_timeout = timeout_us / 1000;
  1749. /*
  1750. * Theoretically, the calculation could underflow so round up
  1751. * to 1ms in that case.
  1752. */
  1753. if (!erase_timeout)
  1754. erase_timeout = 1;
  1755. }
  1756. /* Multiplier for secure operations */
  1757. if (arg & MMC_SECURE_ARGS) {
  1758. if (arg == MMC_SECURE_ERASE_ARG)
  1759. erase_timeout *= card->ext_csd.sec_erase_mult;
  1760. else
  1761. erase_timeout *= card->ext_csd.sec_trim_mult;
  1762. }
  1763. erase_timeout *= qty;
  1764. /*
  1765. * Ensure at least a 1 second timeout for SPI as per
  1766. * 'mmc_set_data_timeout()'
  1767. */
  1768. if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
  1769. erase_timeout = 1000;
  1770. return erase_timeout;
  1771. }
  1772. static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
  1773. unsigned int arg,
  1774. unsigned int qty)
  1775. {
  1776. unsigned int erase_timeout;
  1777. if (card->ssr.erase_timeout) {
  1778. /* Erase timeout specified in SD Status Register (SSR) */
  1779. erase_timeout = card->ssr.erase_timeout * qty +
  1780. card->ssr.erase_offset;
  1781. } else {
  1782. /*
  1783. * Erase timeout not specified in SD Status Register (SSR) so
  1784. * use 250ms per write block.
  1785. */
  1786. erase_timeout = 250 * qty;
  1787. }
  1788. /* Must not be less than 1 second */
  1789. if (erase_timeout < 1000)
  1790. erase_timeout = 1000;
  1791. return erase_timeout;
  1792. }
  1793. static unsigned int mmc_erase_timeout(struct mmc_card *card,
  1794. unsigned int arg,
  1795. unsigned int qty)
  1796. {
  1797. if (mmc_card_sd(card))
  1798. return mmc_sd_erase_timeout(card, arg, qty);
  1799. else
  1800. return mmc_mmc_erase_timeout(card, arg, qty);
  1801. }
  1802. static int mmc_do_erase(struct mmc_card *card, unsigned int from,
  1803. unsigned int to, unsigned int arg)
  1804. {
  1805. struct mmc_command cmd = {};
  1806. unsigned int qty = 0, busy_timeout = 0;
  1807. bool use_r1b_resp = false;
  1808. unsigned long timeout;
  1809. int err;
  1810. mmc_retune_hold(card->host);
  1811. /*
  1812. * qty is used to calculate the erase timeout which depends on how many
  1813. * erase groups (or allocation units in SD terminology) are affected.
  1814. * We count erasing part of an erase group as one erase group.
  1815. * For SD, the allocation units are always a power of 2. For MMC, the
  1816. * erase group size is almost certainly also power of 2, but it does not
  1817. * seem to insist on that in the JEDEC standard, so we fall back to
  1818. * division in that case. SD may not specify an allocation unit size,
  1819. * in which case the timeout is based on the number of write blocks.
  1820. *
  1821. * Note that the timeout for secure trim 2 will only be correct if the
  1822. * number of erase groups specified is the same as the total of all
  1823. * preceding secure trim 1 commands. Since the power may have been
  1824. * lost since the secure trim 1 commands occurred, it is generally
  1825. * impossible to calculate the secure trim 2 timeout correctly.
  1826. */
  1827. if (card->erase_shift)
  1828. qty += ((to >> card->erase_shift) -
  1829. (from >> card->erase_shift)) + 1;
  1830. else if (mmc_card_sd(card))
  1831. qty += to - from + 1;
  1832. else
  1833. qty += ((to / card->erase_size) -
  1834. (from / card->erase_size)) + 1;
  1835. if (!mmc_card_blockaddr(card)) {
  1836. from <<= 9;
  1837. to <<= 9;
  1838. }
  1839. if (mmc_card_sd(card))
  1840. cmd.opcode = SD_ERASE_WR_BLK_START;
  1841. else
  1842. cmd.opcode = MMC_ERASE_GROUP_START;
  1843. cmd.arg = from;
  1844. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1845. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1846. if (err) {
  1847. pr_err("mmc_erase: group start error %d, "
  1848. "status %#x\n", err, cmd.resp[0]);
  1849. err = -EIO;
  1850. goto out;
  1851. }
  1852. memset(&cmd, 0, sizeof(struct mmc_command));
  1853. if (mmc_card_sd(card))
  1854. cmd.opcode = SD_ERASE_WR_BLK_END;
  1855. else
  1856. cmd.opcode = MMC_ERASE_GROUP_END;
  1857. cmd.arg = to;
  1858. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1859. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1860. if (err) {
  1861. pr_err("mmc_erase: group end error %d, status %#x\n",
  1862. err, cmd.resp[0]);
  1863. err = -EIO;
  1864. goto out;
  1865. }
  1866. memset(&cmd, 0, sizeof(struct mmc_command));
  1867. cmd.opcode = MMC_ERASE;
  1868. cmd.arg = arg;
  1869. busy_timeout = mmc_erase_timeout(card, arg, qty);
  1870. /*
  1871. * If the host controller supports busy signalling and the timeout for
  1872. * the erase operation does not exceed the max_busy_timeout, we should
  1873. * use R1B response. Or we need to prevent the host from doing hw busy
  1874. * detection, which is done by converting to a R1 response instead.
  1875. */
  1876. if (card->host->max_busy_timeout &&
  1877. busy_timeout > card->host->max_busy_timeout) {
  1878. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1879. } else {
  1880. cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  1881. cmd.busy_timeout = busy_timeout;
  1882. use_r1b_resp = true;
  1883. }
  1884. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1885. if (err) {
  1886. pr_err("mmc_erase: erase error %d, status %#x\n",
  1887. err, cmd.resp[0]);
  1888. err = -EIO;
  1889. goto out;
  1890. }
  1891. if (mmc_host_is_spi(card->host))
  1892. goto out;
  1893. /*
  1894. * In case of when R1B + MMC_CAP_WAIT_WHILE_BUSY is used, the polling
  1895. * shall be avoided.
  1896. */
  1897. if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp)
  1898. goto out;
  1899. timeout = jiffies + msecs_to_jiffies(busy_timeout);
  1900. do {
  1901. memset(&cmd, 0, sizeof(struct mmc_command));
  1902. cmd.opcode = MMC_SEND_STATUS;
  1903. cmd.arg = card->rca << 16;
  1904. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1905. /* Do not retry else we can't see errors */
  1906. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1907. if (err || (cmd.resp[0] & 0xFDF92000)) {
  1908. pr_err("error %d requesting status %#x\n",
  1909. err, cmd.resp[0]);
  1910. err = -EIO;
  1911. goto out;
  1912. }
  1913. /* Timeout if the device never becomes ready for data and
  1914. * never leaves the program state.
  1915. */
  1916. if (time_after(jiffies, timeout)) {
  1917. pr_err("%s: Card stuck in programming state! %s\n",
  1918. mmc_hostname(card->host), __func__);
  1919. err = -EIO;
  1920. goto out;
  1921. }
  1922. } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
  1923. (R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG));
  1924. out:
  1925. mmc_retune_release(card->host);
  1926. return err;
  1927. }
  1928. static unsigned int mmc_align_erase_size(struct mmc_card *card,
  1929. unsigned int *from,
  1930. unsigned int *to,
  1931. unsigned int nr)
  1932. {
  1933. unsigned int from_new = *from, nr_new = nr, rem;
  1934. /*
  1935. * When the 'card->erase_size' is power of 2, we can use round_up/down()
  1936. * to align the erase size efficiently.
  1937. */
  1938. if (is_power_of_2(card->erase_size)) {
  1939. unsigned int temp = from_new;
  1940. from_new = round_up(temp, card->erase_size);
  1941. rem = from_new - temp;
  1942. if (nr_new > rem)
  1943. nr_new -= rem;
  1944. else
  1945. return 0;
  1946. nr_new = round_down(nr_new, card->erase_size);
  1947. } else {
  1948. rem = from_new % card->erase_size;
  1949. if (rem) {
  1950. rem = card->erase_size - rem;
  1951. from_new += rem;
  1952. if (nr_new > rem)
  1953. nr_new -= rem;
  1954. else
  1955. return 0;
  1956. }
  1957. rem = nr_new % card->erase_size;
  1958. if (rem)
  1959. nr_new -= rem;
  1960. }
  1961. if (nr_new == 0)
  1962. return 0;
  1963. *to = from_new + nr_new;
  1964. *from = from_new;
  1965. return nr_new;
  1966. }
  1967. /**
  1968. * mmc_erase - erase sectors.
  1969. * @card: card to erase
  1970. * @from: first sector to erase
  1971. * @nr: number of sectors to erase
  1972. * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
  1973. *
  1974. * Caller must claim host before calling this function.
  1975. */
  1976. int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
  1977. unsigned int arg)
  1978. {
  1979. unsigned int rem, to = from + nr;
  1980. int err;
  1981. if (!(card->host->caps & MMC_CAP_ERASE) ||
  1982. !(card->csd.cmdclass & CCC_ERASE))
  1983. return -EOPNOTSUPP;
  1984. if (!card->erase_size)
  1985. return -EOPNOTSUPP;
  1986. if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
  1987. return -EOPNOTSUPP;
  1988. if ((arg & MMC_SECURE_ARGS) &&
  1989. !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
  1990. return -EOPNOTSUPP;
  1991. if ((arg & MMC_TRIM_ARGS) &&
  1992. !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
  1993. return -EOPNOTSUPP;
  1994. if (arg == MMC_SECURE_ERASE_ARG) {
  1995. if (from % card->erase_size || nr % card->erase_size)
  1996. return -EINVAL;
  1997. }
  1998. if (arg == MMC_ERASE_ARG)
  1999. nr = mmc_align_erase_size(card, &from, &to, nr);
  2000. if (nr == 0)
  2001. return 0;
  2002. if (to <= from)
  2003. return -EINVAL;
  2004. /* 'from' and 'to' are inclusive */
  2005. to -= 1;
  2006. /*
  2007. * Special case where only one erase-group fits in the timeout budget:
  2008. * If the region crosses an erase-group boundary on this particular
  2009. * case, we will be trimming more than one erase-group which, does not
  2010. * fit in the timeout budget of the controller, so we need to split it
  2011. * and call mmc_do_erase() twice if necessary. This special case is
  2012. * identified by the card->eg_boundary flag.
  2013. */
  2014. rem = card->erase_size - (from % card->erase_size);
  2015. if ((arg & MMC_TRIM_ARGS) && (card->eg_boundary) && (nr > rem)) {
  2016. err = mmc_do_erase(card, from, from + rem - 1, arg);
  2017. from += rem;
  2018. if ((err) || (to <= from))
  2019. return err;
  2020. }
  2021. return mmc_do_erase(card, from, to, arg);
  2022. }
  2023. EXPORT_SYMBOL(mmc_erase);
  2024. int mmc_can_erase(struct mmc_card *card)
  2025. {
  2026. if ((card->host->caps & MMC_CAP_ERASE) &&
  2027. (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
  2028. return 1;
  2029. return 0;
  2030. }
  2031. EXPORT_SYMBOL(mmc_can_erase);
  2032. int mmc_can_trim(struct mmc_card *card)
  2033. {
  2034. if ((card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN) &&
  2035. (!(card->quirks & MMC_QUIRK_TRIM_BROKEN)))
  2036. return 1;
  2037. return 0;
  2038. }
  2039. EXPORT_SYMBOL(mmc_can_trim);
  2040. int mmc_can_discard(struct mmc_card *card)
  2041. {
  2042. /*
  2043. * As there's no way to detect the discard support bit at v4.5
  2044. * use the s/w feature support filed.
  2045. */
  2046. if (card->ext_csd.feature_support & MMC_DISCARD_FEATURE)
  2047. return 1;
  2048. return 0;
  2049. }
  2050. EXPORT_SYMBOL(mmc_can_discard);
  2051. int mmc_can_sanitize(struct mmc_card *card)
  2052. {
  2053. if (!mmc_can_trim(card) && !mmc_can_erase(card))
  2054. return 0;
  2055. if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE)
  2056. return 1;
  2057. return 0;
  2058. }
  2059. EXPORT_SYMBOL(mmc_can_sanitize);
  2060. int mmc_can_secure_erase_trim(struct mmc_card *card)
  2061. {
  2062. if ((card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN) &&
  2063. !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
  2064. return 1;
  2065. return 0;
  2066. }
  2067. EXPORT_SYMBOL(mmc_can_secure_erase_trim);
  2068. int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
  2069. unsigned int nr)
  2070. {
  2071. if (!card->erase_size)
  2072. return 0;
  2073. if (from % card->erase_size || nr % card->erase_size)
  2074. return 0;
  2075. return 1;
  2076. }
  2077. EXPORT_SYMBOL(mmc_erase_group_aligned);
  2078. static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
  2079. unsigned int arg)
  2080. {
  2081. struct mmc_host *host = card->host;
  2082. unsigned int max_discard, x, y, qty = 0, max_qty, min_qty, timeout;
  2083. unsigned int last_timeout = 0;
  2084. unsigned int max_busy_timeout = host->max_busy_timeout ?
  2085. host->max_busy_timeout : MMC_ERASE_TIMEOUT_MS;
  2086. if (card->erase_shift) {
  2087. max_qty = UINT_MAX >> card->erase_shift;
  2088. min_qty = card->pref_erase >> card->erase_shift;
  2089. } else if (mmc_card_sd(card)) {
  2090. max_qty = UINT_MAX;
  2091. min_qty = card->pref_erase;
  2092. } else {
  2093. max_qty = UINT_MAX / card->erase_size;
  2094. min_qty = card->pref_erase / card->erase_size;
  2095. }
  2096. /*
  2097. * We should not only use 'host->max_busy_timeout' as the limitation
  2098. * when deciding the max discard sectors. We should set a balance value
  2099. * to improve the erase speed, and it can not get too long timeout at
  2100. * the same time.
  2101. *
  2102. * Here we set 'card->pref_erase' as the minimal discard sectors no
  2103. * matter what size of 'host->max_busy_timeout', but if the
  2104. * 'host->max_busy_timeout' is large enough for more discard sectors,
  2105. * then we can continue to increase the max discard sectors until we
  2106. * get a balance value. In cases when the 'host->max_busy_timeout'
  2107. * isn't specified, use the default max erase timeout.
  2108. */
  2109. do {
  2110. y = 0;
  2111. for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
  2112. timeout = mmc_erase_timeout(card, arg, qty + x);
  2113. if (qty + x > min_qty && timeout > max_busy_timeout)
  2114. break;
  2115. if (timeout < last_timeout)
  2116. break;
  2117. last_timeout = timeout;
  2118. y = x;
  2119. }
  2120. qty += y;
  2121. } while (y);
  2122. if (!qty)
  2123. return 0;
  2124. /*
  2125. * When specifying a sector range to trim, chances are we might cross
  2126. * an erase-group boundary even if the amount of sectors is less than
  2127. * one erase-group.
  2128. * If we can only fit one erase-group in the controller timeout budget,
  2129. * we have to care that erase-group boundaries are not crossed by a
  2130. * single trim operation. We flag that special case with "eg_boundary".
  2131. * In all other cases we can just decrement qty and pretend that we
  2132. * always touch (qty + 1) erase-groups as a simple optimization.
  2133. */
  2134. if (qty == 1)
  2135. card->eg_boundary = 1;
  2136. else
  2137. qty--;
  2138. /* Convert qty to sectors */
  2139. if (card->erase_shift)
  2140. max_discard = qty << card->erase_shift;
  2141. else if (mmc_card_sd(card))
  2142. max_discard = qty + 1;
  2143. else
  2144. max_discard = qty * card->erase_size;
  2145. return max_discard;
  2146. }
  2147. unsigned int mmc_calc_max_discard(struct mmc_card *card)
  2148. {
  2149. struct mmc_host *host = card->host;
  2150. unsigned int max_discard, max_trim;
  2151. /*
  2152. * Without erase_group_def set, MMC erase timeout depends on clock
  2153. * frequence which can change. In that case, the best choice is
  2154. * just the preferred erase size.
  2155. */
  2156. if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
  2157. return card->pref_erase;
  2158. max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
  2159. if (mmc_can_trim(card)) {
  2160. max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
  2161. if (max_trim < max_discard)
  2162. max_discard = max_trim;
  2163. } else if (max_discard < card->erase_size) {
  2164. max_discard = 0;
  2165. }
  2166. pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
  2167. mmc_hostname(host), max_discard, host->max_busy_timeout ?
  2168. host->max_busy_timeout : MMC_ERASE_TIMEOUT_MS);
  2169. return max_discard;
  2170. }
  2171. EXPORT_SYMBOL(mmc_calc_max_discard);
  2172. bool mmc_card_is_blockaddr(struct mmc_card *card)
  2173. {
  2174. return card ? mmc_card_blockaddr(card) : false;
  2175. }
  2176. EXPORT_SYMBOL(mmc_card_is_blockaddr);
  2177. int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
  2178. {
  2179. struct mmc_command cmd = {};
  2180. if (mmc_card_blockaddr(card) || mmc_card_ddr52(card) ||
  2181. mmc_card_hs400(card) || mmc_card_hs400es(card))
  2182. return 0;
  2183. cmd.opcode = MMC_SET_BLOCKLEN;
  2184. cmd.arg = blocklen;
  2185. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  2186. return mmc_wait_for_cmd(card->host, &cmd, 5);
  2187. }
  2188. EXPORT_SYMBOL(mmc_set_blocklen);
  2189. int mmc_set_blockcount(struct mmc_card *card, unsigned int blockcount,
  2190. bool is_rel_write)
  2191. {
  2192. struct mmc_command cmd = {};
  2193. cmd.opcode = MMC_SET_BLOCK_COUNT;
  2194. cmd.arg = blockcount & 0x0000FFFF;
  2195. if (is_rel_write)
  2196. cmd.arg |= 1 << 31;
  2197. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  2198. return mmc_wait_for_cmd(card->host, &cmd, 5);
  2199. }
  2200. EXPORT_SYMBOL(mmc_set_blockcount);
  2201. static void mmc_hw_reset_for_init(struct mmc_host *host)
  2202. {
  2203. mmc_pwrseq_reset(host);
  2204. if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
  2205. return;
  2206. host->ops->hw_reset(host);
  2207. }
  2208. int mmc_hw_reset(struct mmc_host *host)
  2209. {
  2210. int ret;
  2211. if (!host->card)
  2212. return -EINVAL;
  2213. mmc_bus_get(host);
  2214. if (!host->bus_ops || host->bus_dead || !host->bus_ops->reset) {
  2215. mmc_bus_put(host);
  2216. return -EOPNOTSUPP;
  2217. }
  2218. ret = host->bus_ops->reset(host);
  2219. mmc_bus_put(host);
  2220. if (ret)
  2221. pr_warn("%s: tried to reset card, got error %d\n",
  2222. mmc_hostname(host), ret);
  2223. return ret;
  2224. }
  2225. EXPORT_SYMBOL(mmc_hw_reset);
  2226. static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
  2227. {
  2228. host->f_init = freq;
  2229. pr_debug("%s: %s: trying to init card at %u Hz\n",
  2230. mmc_hostname(host), __func__, host->f_init);
  2231. mmc_power_up(host, host->ocr_avail);
  2232. /*
  2233. * Some eMMCs (with VCCQ always on) may not be reset after power up, so
  2234. * do a hardware reset if possible.
  2235. */
  2236. mmc_hw_reset_for_init(host);
  2237. /*
  2238. * sdio_reset sends CMD52 to reset card. Since we do not know
  2239. * if the card is being re-initialized, just send it. CMD52
  2240. * should be ignored by SD/eMMC cards.
  2241. * Skip it if we already know that we do not support SDIO commands
  2242. */
  2243. if (!(host->caps2 & MMC_CAP2_NO_SDIO))
  2244. sdio_reset(host);
  2245. mmc_go_idle(host);
  2246. if (!(host->caps2 & MMC_CAP2_NO_SD))
  2247. mmc_send_if_cond(host, host->ocr_avail);
  2248. /* Order's important: probe SDIO, then SD, then MMC */
  2249. if (!(host->caps2 & MMC_CAP2_NO_SDIO))
  2250. if (!mmc_attach_sdio(host))
  2251. return 0;
  2252. if (!(host->caps2 & MMC_CAP2_NO_SD))
  2253. if (!mmc_attach_sd(host))
  2254. return 0;
  2255. if (!(host->caps2 & MMC_CAP2_NO_MMC))
  2256. if (!mmc_attach_mmc(host))
  2257. return 0;
  2258. mmc_power_off(host);
  2259. return -EIO;
  2260. }
  2261. int _mmc_detect_card_removed(struct mmc_host *host)
  2262. {
  2263. int ret;
  2264. if (!host->card || mmc_card_removed(host->card))
  2265. return 1;
  2266. ret = host->bus_ops->alive(host);
  2267. /*
  2268. * Card detect status and alive check may be out of sync if card is
  2269. * removed slowly, when card detect switch changes while card/slot
  2270. * pads are still contacted in hardware (refer to "SD Card Mechanical
  2271. * Addendum, Appendix C: Card Detection Switch"). So reschedule a
  2272. * detect work 200ms later for this case.
  2273. */
  2274. if (!ret && host->ops->get_cd && !host->ops->get_cd(host)) {
  2275. mmc_detect_change(host, msecs_to_jiffies(200));
  2276. pr_debug("%s: card removed too slowly\n", mmc_hostname(host));
  2277. }
  2278. if (ret) {
  2279. mmc_card_set_removed(host->card);
  2280. pr_debug("%s: card remove detected\n", mmc_hostname(host));
  2281. }
  2282. return ret;
  2283. }
  2284. int mmc_detect_card_removed(struct mmc_host *host)
  2285. {
  2286. struct mmc_card *card = host->card;
  2287. int ret;
  2288. WARN_ON(!host->claimed);
  2289. if (!card)
  2290. return 1;
  2291. if (!mmc_card_is_removable(host))
  2292. return 0;
  2293. ret = mmc_card_removed(card);
  2294. /*
  2295. * The card will be considered unchanged unless we have been asked to
  2296. * detect a change or host requires polling to provide card detection.
  2297. */
  2298. if (!host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL))
  2299. return ret;
  2300. host->detect_change = 0;
  2301. if (!ret) {
  2302. ret = _mmc_detect_card_removed(host);
  2303. if (ret && (host->caps & MMC_CAP_NEEDS_POLL)) {
  2304. /*
  2305. * Schedule a detect work as soon as possible to let a
  2306. * rescan handle the card removal.
  2307. */
  2308. cancel_delayed_work(&host->detect);
  2309. _mmc_detect_change(host, 0, false);
  2310. }
  2311. }
  2312. return ret;
  2313. }
  2314. EXPORT_SYMBOL(mmc_detect_card_removed);
  2315. void mmc_rescan(struct work_struct *work)
  2316. {
  2317. struct mmc_host *host =
  2318. container_of(work, struct mmc_host, detect.work);
  2319. int i;
  2320. if (host->rescan_disable)
  2321. return;
  2322. /* If there is a non-removable card registered, only scan once */
  2323. if (!mmc_card_is_removable(host) && host->rescan_entered)
  2324. return;
  2325. host->rescan_entered = 1;
  2326. if (host->trigger_card_event && host->ops->card_event) {
  2327. mmc_claim_host(host);
  2328. host->ops->card_event(host);
  2329. mmc_release_host(host);
  2330. host->trigger_card_event = false;
  2331. }
  2332. mmc_bus_get(host);
  2333. /*
  2334. * if there is a _removable_ card registered, check whether it is
  2335. * still present
  2336. */
  2337. if (host->bus_ops && !host->bus_dead && mmc_card_is_removable(host))
  2338. host->bus_ops->detect(host);
  2339. host->detect_change = 0;
  2340. /*
  2341. * Let mmc_bus_put() free the bus/bus_ops if we've found that
  2342. * the card is no longer present.
  2343. */
  2344. mmc_bus_put(host);
  2345. mmc_bus_get(host);
  2346. /* if there still is a card present, stop here */
  2347. if (host->bus_ops != NULL) {
  2348. mmc_bus_put(host);
  2349. goto out;
  2350. }
  2351. /*
  2352. * Only we can add a new handler, so it's safe to
  2353. * release the lock here.
  2354. */
  2355. mmc_bus_put(host);
  2356. mmc_claim_host(host);
  2357. if (mmc_card_is_removable(host) && host->ops->get_cd &&
  2358. host->ops->get_cd(host) == 0) {
  2359. mmc_power_off(host);
  2360. mmc_release_host(host);
  2361. goto out;
  2362. }
  2363. for (i = 0; i < ARRAY_SIZE(freqs); i++) {
  2364. if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
  2365. break;
  2366. if (freqs[i] <= host->f_min)
  2367. break;
  2368. }
  2369. mmc_release_host(host);
  2370. out:
  2371. if (host->caps & MMC_CAP_NEEDS_POLL)
  2372. mmc_schedule_delayed_work(&host->detect, HZ);
  2373. }
  2374. void mmc_start_host(struct mmc_host *host)
  2375. {
  2376. host->f_init = max(freqs[0], host->f_min);
  2377. host->rescan_disable = 0;
  2378. host->ios.power_mode = MMC_POWER_UNDEFINED;
  2379. if (!(host->caps2 & MMC_CAP2_NO_PRESCAN_POWERUP)) {
  2380. mmc_claim_host(host);
  2381. mmc_power_up(host, host->ocr_avail);
  2382. mmc_release_host(host);
  2383. }
  2384. mmc_gpiod_request_cd_irq(host);
  2385. _mmc_detect_change(host, 0, false);
  2386. }
  2387. void mmc_stop_host(struct mmc_host *host)
  2388. {
  2389. if (host->slot.cd_irq >= 0) {
  2390. if (host->slot.cd_wake_enabled)
  2391. disable_irq_wake(host->slot.cd_irq);
  2392. disable_irq(host->slot.cd_irq);
  2393. }
  2394. host->rescan_disable = 1;
  2395. cancel_delayed_work_sync(&host->detect);
  2396. /* clear pm flags now and let card drivers set them as needed */
  2397. host->pm_flags = 0;
  2398. mmc_bus_get(host);
  2399. if (host->bus_ops && !host->bus_dead) {
  2400. /* Calling bus_ops->remove() with a claimed host can deadlock */
  2401. host->bus_ops->remove(host);
  2402. mmc_claim_host(host);
  2403. mmc_detach_bus(host);
  2404. mmc_power_off(host);
  2405. mmc_release_host(host);
  2406. mmc_bus_put(host);
  2407. return;
  2408. }
  2409. mmc_bus_put(host);
  2410. mmc_claim_host(host);
  2411. mmc_power_off(host);
  2412. mmc_release_host(host);
  2413. }
  2414. int mmc_power_save_host(struct mmc_host *host)
  2415. {
  2416. int ret = 0;
  2417. pr_debug("%s: %s: powering down\n", mmc_hostname(host), __func__);
  2418. mmc_bus_get(host);
  2419. if (!host->bus_ops || host->bus_dead) {
  2420. mmc_bus_put(host);
  2421. return -EINVAL;
  2422. }
  2423. if (host->bus_ops->power_save)
  2424. ret = host->bus_ops->power_save(host);
  2425. mmc_bus_put(host);
  2426. mmc_power_off(host);
  2427. return ret;
  2428. }
  2429. EXPORT_SYMBOL(mmc_power_save_host);
  2430. int mmc_power_restore_host(struct mmc_host *host)
  2431. {
  2432. int ret;
  2433. pr_debug("%s: %s: powering up\n", mmc_hostname(host), __func__);
  2434. mmc_bus_get(host);
  2435. if (!host->bus_ops || host->bus_dead) {
  2436. mmc_bus_put(host);
  2437. return -EINVAL;
  2438. }
  2439. mmc_power_up(host, host->card->ocr);
  2440. ret = host->bus_ops->power_restore(host);
  2441. mmc_bus_put(host);
  2442. return ret;
  2443. }
  2444. EXPORT_SYMBOL(mmc_power_restore_host);
  2445. #ifdef CONFIG_PM_SLEEP
  2446. /* Do the card removal on suspend if card is assumed removeable
  2447. * Do that in pm notifier while userspace isn't yet frozen, so we will be able
  2448. to sync the card.
  2449. */
  2450. static int mmc_pm_notify(struct notifier_block *notify_block,
  2451. unsigned long mode, void *unused)
  2452. {
  2453. struct mmc_host *host = container_of(
  2454. notify_block, struct mmc_host, pm_notify);
  2455. unsigned long flags;
  2456. int err = 0;
  2457. switch (mode) {
  2458. case PM_HIBERNATION_PREPARE:
  2459. case PM_SUSPEND_PREPARE:
  2460. case PM_RESTORE_PREPARE:
  2461. spin_lock_irqsave(&host->lock, flags);
  2462. host->rescan_disable = 1;
  2463. spin_unlock_irqrestore(&host->lock, flags);
  2464. cancel_delayed_work_sync(&host->detect);
  2465. if (!host->bus_ops)
  2466. break;
  2467. /* Validate prerequisites for suspend */
  2468. if (host->bus_ops->pre_suspend)
  2469. err = host->bus_ops->pre_suspend(host);
  2470. if (!err)
  2471. break;
  2472. /* Calling bus_ops->remove() with a claimed host can deadlock */
  2473. host->bus_ops->remove(host);
  2474. mmc_claim_host(host);
  2475. mmc_detach_bus(host);
  2476. mmc_power_off(host);
  2477. mmc_release_host(host);
  2478. host->pm_flags = 0;
  2479. break;
  2480. case PM_POST_SUSPEND:
  2481. case PM_POST_HIBERNATION:
  2482. case PM_POST_RESTORE:
  2483. spin_lock_irqsave(&host->lock, flags);
  2484. host->rescan_disable = 0;
  2485. spin_unlock_irqrestore(&host->lock, flags);
  2486. _mmc_detect_change(host, 0, false);
  2487. }
  2488. return 0;
  2489. }
  2490. void mmc_register_pm_notifier(struct mmc_host *host)
  2491. {
  2492. host->pm_notify.notifier_call = mmc_pm_notify;
  2493. register_pm_notifier(&host->pm_notify);
  2494. }
  2495. void mmc_unregister_pm_notifier(struct mmc_host *host)
  2496. {
  2497. unregister_pm_notifier(&host->pm_notify);
  2498. }
  2499. #endif
  2500. /**
  2501. * mmc_init_context_info() - init synchronization context
  2502. * @host: mmc host
  2503. *
  2504. * Init struct context_info needed to implement asynchronous
  2505. * request mechanism, used by mmc core, host driver and mmc requests
  2506. * supplier.
  2507. */
  2508. void mmc_init_context_info(struct mmc_host *host)
  2509. {
  2510. host->context_info.is_new_req = false;
  2511. host->context_info.is_done_rcv = false;
  2512. host->context_info.is_waiting_last_req = false;
  2513. init_waitqueue_head(&host->context_info.wait);
  2514. }
  2515. static int __init mmc_init(void)
  2516. {
  2517. int ret;
  2518. ret = mmc_register_bus();
  2519. if (ret)
  2520. return ret;
  2521. ret = mmc_register_host_class();
  2522. if (ret)
  2523. goto unregister_bus;
  2524. ret = sdio_register_bus();
  2525. if (ret)
  2526. goto unregister_host_class;
  2527. return 0;
  2528. unregister_host_class:
  2529. mmc_unregister_host_class();
  2530. unregister_bus:
  2531. mmc_unregister_bus();
  2532. return ret;
  2533. }
  2534. static void __exit mmc_exit(void)
  2535. {
  2536. sdio_unregister_bus();
  2537. mmc_unregister_host_class();
  2538. mmc_unregister_bus();
  2539. }
  2540. subsys_initcall(mmc_init);
  2541. module_exit(mmc_exit);
  2542. MODULE_LICENSE("GPL");