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