core.c 76 KB

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