core.c 71 KB

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