core.c 71 KB

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