core.c 76 KB

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