core.c 69 KB

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