core.c 67 KB

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