core.c 71 KB

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