core.c 66 KB

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