core.c 76 KB

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