core.c 73 KB

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