core.c 71 KB

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