core.c 72 KB

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