core.c 65 KB

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