core.c 72 KB

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