core.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178
  1. /*
  2. * linux/drivers/mmc/core/core.c
  3. *
  4. * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
  5. * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
  6. * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
  7. * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/completion.h>
  17. #include <linux/device.h>
  18. #include <linux/delay.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/err.h>
  21. #include <linux/leds.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/log2.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/suspend.h>
  27. #include <linux/fault-inject.h>
  28. #include <linux/random.h>
  29. #include <linux/mmc/card.h>
  30. #include <linux/mmc/host.h>
  31. #include <linux/mmc/mmc.h>
  32. #include <linux/mmc/sd.h>
  33. #include "core.h"
  34. #include "bus.h"
  35. #include "host.h"
  36. #include "sdio_bus.h"
  37. #include "mmc_ops.h"
  38. #include "sd_ops.h"
  39. #include "sdio_ops.h"
  40. static struct workqueue_struct *workqueue;
  41. /*
  42. * Enabling software CRCs on the data blocks can be a significant (30%)
  43. * performance cost, and for other reasons may not always be desired.
  44. * So we allow it it to be disabled.
  45. */
  46. int use_spi_crc = 1;
  47. module_param(use_spi_crc, bool, 0);
  48. /*
  49. * We normally treat cards as removed during suspend if they are not
  50. * known to be on a non-removable bus, to avoid the risk of writing
  51. * back data to a different card after resume. Allow this to be
  52. * overridden if necessary.
  53. */
  54. #ifdef CONFIG_MMC_UNSAFE_RESUME
  55. int mmc_assume_removable;
  56. #else
  57. int mmc_assume_removable = 1;
  58. #endif
  59. EXPORT_SYMBOL(mmc_assume_removable);
  60. module_param_named(removable, mmc_assume_removable, bool, 0644);
  61. MODULE_PARM_DESC(
  62. removable,
  63. "MMC/SD cards are removable and may be removed during suspend");
  64. /*
  65. * Internal function. Schedule delayed work in the MMC work queue.
  66. */
  67. static int mmc_schedule_delayed_work(struct delayed_work *work,
  68. unsigned long delay)
  69. {
  70. return queue_delayed_work(workqueue, work, delay);
  71. }
  72. /*
  73. * Internal function. Flush all scheduled work from the MMC work queue.
  74. */
  75. static void mmc_flush_scheduled_work(void)
  76. {
  77. flush_workqueue(workqueue);
  78. }
  79. #ifdef CONFIG_FAIL_MMC_REQUEST
  80. /*
  81. * Internal function. Inject random data errors.
  82. * If mmc_data is NULL no errors are injected.
  83. */
  84. static void mmc_should_fail_request(struct mmc_host *host,
  85. struct mmc_request *mrq)
  86. {
  87. struct mmc_command *cmd = mrq->cmd;
  88. struct mmc_data *data = mrq->data;
  89. static const int data_errors[] = {
  90. -ETIMEDOUT,
  91. -EILSEQ,
  92. -EIO,
  93. };
  94. if (!data)
  95. return;
  96. if (cmd->error || data->error ||
  97. !should_fail(&host->fail_mmc_request, data->blksz * data->blocks))
  98. return;
  99. data->error = data_errors[random32() % ARRAY_SIZE(data_errors)];
  100. data->bytes_xfered = (random32() % (data->bytes_xfered >> 9)) << 9;
  101. }
  102. #else /* CONFIG_FAIL_MMC_REQUEST */
  103. static inline void mmc_should_fail_request(struct mmc_host *host,
  104. struct mmc_request *mrq)
  105. {
  106. }
  107. #endif /* CONFIG_FAIL_MMC_REQUEST */
  108. /**
  109. * mmc_request_done - finish processing an MMC request
  110. * @host: MMC host which completed request
  111. * @mrq: MMC request which request
  112. *
  113. * MMC drivers should call this function when they have completed
  114. * their processing of a request.
  115. */
  116. void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
  117. {
  118. struct mmc_command *cmd = mrq->cmd;
  119. int err = cmd->error;
  120. if (err && cmd->retries && mmc_host_is_spi(host)) {
  121. if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
  122. cmd->retries = 0;
  123. }
  124. if (err && cmd->retries) {
  125. pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
  126. mmc_hostname(host), cmd->opcode, err);
  127. cmd->retries--;
  128. cmd->error = 0;
  129. host->ops->request(host, mrq);
  130. } else {
  131. mmc_should_fail_request(host, mrq);
  132. led_trigger_event(host->led, LED_OFF);
  133. pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
  134. mmc_hostname(host), cmd->opcode, err,
  135. cmd->resp[0], cmd->resp[1],
  136. cmd->resp[2], cmd->resp[3]);
  137. if (mrq->data) {
  138. pr_debug("%s: %d bytes transferred: %d\n",
  139. mmc_hostname(host),
  140. mrq->data->bytes_xfered, mrq->data->error);
  141. }
  142. if (mrq->stop) {
  143. pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
  144. mmc_hostname(host), mrq->stop->opcode,
  145. mrq->stop->error,
  146. mrq->stop->resp[0], mrq->stop->resp[1],
  147. mrq->stop->resp[2], mrq->stop->resp[3]);
  148. }
  149. if (mrq->done)
  150. mrq->done(mrq);
  151. mmc_host_clk_release(host);
  152. }
  153. }
  154. EXPORT_SYMBOL(mmc_request_done);
  155. static void
  156. mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
  157. {
  158. #ifdef CONFIG_MMC_DEBUG
  159. unsigned int i, sz;
  160. struct scatterlist *sg;
  161. #endif
  162. pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
  163. mmc_hostname(host), mrq->cmd->opcode,
  164. mrq->cmd->arg, mrq->cmd->flags);
  165. if (mrq->data) {
  166. pr_debug("%s: blksz %d blocks %d flags %08x "
  167. "tsac %d ms nsac %d\n",
  168. mmc_hostname(host), mrq->data->blksz,
  169. mrq->data->blocks, mrq->data->flags,
  170. mrq->data->timeout_ns / 1000000,
  171. mrq->data->timeout_clks);
  172. }
  173. if (mrq->stop) {
  174. pr_debug("%s: CMD%u arg %08x flags %08x\n",
  175. mmc_hostname(host), mrq->stop->opcode,
  176. mrq->stop->arg, mrq->stop->flags);
  177. }
  178. WARN_ON(!host->claimed);
  179. mrq->cmd->error = 0;
  180. mrq->cmd->mrq = mrq;
  181. if (mrq->data) {
  182. BUG_ON(mrq->data->blksz > host->max_blk_size);
  183. BUG_ON(mrq->data->blocks > host->max_blk_count);
  184. BUG_ON(mrq->data->blocks * mrq->data->blksz >
  185. host->max_req_size);
  186. #ifdef CONFIG_MMC_DEBUG
  187. sz = 0;
  188. for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
  189. sz += sg->length;
  190. BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
  191. #endif
  192. mrq->cmd->data = mrq->data;
  193. mrq->data->error = 0;
  194. mrq->data->mrq = mrq;
  195. if (mrq->stop) {
  196. mrq->data->stop = mrq->stop;
  197. mrq->stop->error = 0;
  198. mrq->stop->mrq = mrq;
  199. }
  200. }
  201. mmc_host_clk_hold(host);
  202. led_trigger_event(host->led, LED_FULL);
  203. host->ops->request(host, mrq);
  204. }
  205. static void mmc_wait_done(struct mmc_request *mrq)
  206. {
  207. complete(&mrq->completion);
  208. }
  209. static void __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq)
  210. {
  211. init_completion(&mrq->completion);
  212. mrq->done = mmc_wait_done;
  213. mmc_start_request(host, mrq);
  214. }
  215. static void mmc_wait_for_req_done(struct mmc_host *host,
  216. struct mmc_request *mrq)
  217. {
  218. wait_for_completion(&mrq->completion);
  219. }
  220. /**
  221. * mmc_pre_req - Prepare for a new request
  222. * @host: MMC host to prepare command
  223. * @mrq: MMC request to prepare for
  224. * @is_first_req: true if there is no previous started request
  225. * that may run in parellel to this call, otherwise false
  226. *
  227. * mmc_pre_req() is called in prior to mmc_start_req() to let
  228. * host prepare for the new request. Preparation of a request may be
  229. * performed while another request is running on the host.
  230. */
  231. static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq,
  232. bool is_first_req)
  233. {
  234. if (host->ops->pre_req)
  235. host->ops->pre_req(host, mrq, is_first_req);
  236. }
  237. /**
  238. * mmc_post_req - Post process a completed request
  239. * @host: MMC host to post process command
  240. * @mrq: MMC request to post process for
  241. * @err: Error, if non zero, clean up any resources made in pre_req
  242. *
  243. * Let the host post process a completed request. Post processing of
  244. * a request may be performed while another reuqest is running.
  245. */
  246. static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq,
  247. int err)
  248. {
  249. if (host->ops->post_req)
  250. host->ops->post_req(host, mrq, err);
  251. }
  252. /**
  253. * mmc_start_req - start a non-blocking request
  254. * @host: MMC host to start command
  255. * @areq: async request to start
  256. * @error: out parameter returns 0 for success, otherwise non zero
  257. *
  258. * Start a new MMC custom command request for a host.
  259. * If there is on ongoing async request wait for completion
  260. * of that request and start the new one and return.
  261. * Does not wait for the new request to complete.
  262. *
  263. * Returns the completed request, NULL in case of none completed.
  264. * Wait for the an ongoing request (previoulsy started) to complete and
  265. * return the completed request. If there is no ongoing request, NULL
  266. * is returned without waiting. NULL is not an error condition.
  267. */
  268. struct mmc_async_req *mmc_start_req(struct mmc_host *host,
  269. struct mmc_async_req *areq, int *error)
  270. {
  271. int err = 0;
  272. struct mmc_async_req *data = host->areq;
  273. /* Prepare a new request */
  274. if (areq)
  275. mmc_pre_req(host, areq->mrq, !host->areq);
  276. if (host->areq) {
  277. mmc_wait_for_req_done(host, host->areq->mrq);
  278. err = host->areq->err_check(host->card, host->areq);
  279. if (err) {
  280. /* post process the completed failed request */
  281. mmc_post_req(host, host->areq->mrq, 0);
  282. if (areq)
  283. /*
  284. * Cancel the new prepared request, because
  285. * it can't run until the failed
  286. * request has been properly handled.
  287. */
  288. mmc_post_req(host, areq->mrq, -EINVAL);
  289. host->areq = NULL;
  290. goto out;
  291. }
  292. }
  293. if (areq)
  294. __mmc_start_req(host, areq->mrq);
  295. if (host->areq)
  296. mmc_post_req(host, host->areq->mrq, 0);
  297. host->areq = areq;
  298. out:
  299. if (error)
  300. *error = err;
  301. return data;
  302. }
  303. EXPORT_SYMBOL(mmc_start_req);
  304. /**
  305. * mmc_wait_for_req - start a request and wait for completion
  306. * @host: MMC host to start command
  307. * @mrq: MMC request to start
  308. *
  309. * Start a new MMC custom command request for a host, and wait
  310. * for the command to complete. Does not attempt to parse the
  311. * response.
  312. */
  313. void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
  314. {
  315. __mmc_start_req(host, mrq);
  316. mmc_wait_for_req_done(host, mrq);
  317. }
  318. EXPORT_SYMBOL(mmc_wait_for_req);
  319. /**
  320. * mmc_wait_for_cmd - start a command and wait for completion
  321. * @host: MMC host to start command
  322. * @cmd: MMC command to start
  323. * @retries: maximum number of retries
  324. *
  325. * Start a new MMC command for a host, and wait for the command
  326. * to complete. Return any error that occurred while the command
  327. * was executing. Do not attempt to parse the response.
  328. */
  329. int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
  330. {
  331. struct mmc_request mrq = {NULL};
  332. WARN_ON(!host->claimed);
  333. memset(cmd->resp, 0, sizeof(cmd->resp));
  334. cmd->retries = retries;
  335. mrq.cmd = cmd;
  336. cmd->data = NULL;
  337. mmc_wait_for_req(host, &mrq);
  338. return cmd->error;
  339. }
  340. EXPORT_SYMBOL(mmc_wait_for_cmd);
  341. /**
  342. * mmc_set_data_timeout - set the timeout for a data command
  343. * @data: data phase for command
  344. * @card: the MMC card associated with the data transfer
  345. *
  346. * Computes the data timeout parameters according to the
  347. * correct algorithm given the card type.
  348. */
  349. void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
  350. {
  351. unsigned int mult;
  352. /*
  353. * SDIO cards only define an upper 1 s limit on access.
  354. */
  355. if (mmc_card_sdio(card)) {
  356. data->timeout_ns = 1000000000;
  357. data->timeout_clks = 0;
  358. return;
  359. }
  360. /*
  361. * SD cards use a 100 multiplier rather than 10
  362. */
  363. mult = mmc_card_sd(card) ? 100 : 10;
  364. /*
  365. * Scale up the multiplier (and therefore the timeout) by
  366. * the r2w factor for writes.
  367. */
  368. if (data->flags & MMC_DATA_WRITE)
  369. mult <<= card->csd.r2w_factor;
  370. data->timeout_ns = card->csd.tacc_ns * mult;
  371. data->timeout_clks = card->csd.tacc_clks * mult;
  372. /*
  373. * SD cards also have an upper limit on the timeout.
  374. */
  375. if (mmc_card_sd(card)) {
  376. unsigned int timeout_us, limit_us;
  377. timeout_us = data->timeout_ns / 1000;
  378. if (mmc_host_clk_rate(card->host))
  379. timeout_us += data->timeout_clks * 1000 /
  380. (mmc_host_clk_rate(card->host) / 1000);
  381. if (data->flags & MMC_DATA_WRITE)
  382. /*
  383. * The limit is really 250 ms, but that is
  384. * insufficient for some crappy cards.
  385. */
  386. limit_us = 300000;
  387. else
  388. limit_us = 100000;
  389. /*
  390. * SDHC cards always use these fixed values.
  391. */
  392. if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
  393. data->timeout_ns = limit_us * 1000;
  394. data->timeout_clks = 0;
  395. }
  396. }
  397. /*
  398. * Some cards need very high timeouts if driven in SPI mode.
  399. * The worst observed timeout was 900ms after writing a
  400. * continuous stream of data until the internal logic
  401. * overflowed.
  402. */
  403. if (mmc_host_is_spi(card->host)) {
  404. if (data->flags & MMC_DATA_WRITE) {
  405. if (data->timeout_ns < 1000000000)
  406. data->timeout_ns = 1000000000; /* 1s */
  407. } else {
  408. if (data->timeout_ns < 100000000)
  409. data->timeout_ns = 100000000; /* 100ms */
  410. }
  411. }
  412. }
  413. EXPORT_SYMBOL(mmc_set_data_timeout);
  414. /**
  415. * mmc_align_data_size - pads a transfer size to a more optimal value
  416. * @card: the MMC card associated with the data transfer
  417. * @sz: original transfer size
  418. *
  419. * Pads the original data size with a number of extra bytes in
  420. * order to avoid controller bugs and/or performance hits
  421. * (e.g. some controllers revert to PIO for certain sizes).
  422. *
  423. * Returns the improved size, which might be unmodified.
  424. *
  425. * Note that this function is only relevant when issuing a
  426. * single scatter gather entry.
  427. */
  428. unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
  429. {
  430. /*
  431. * FIXME: We don't have a system for the controller to tell
  432. * the core about its problems yet, so for now we just 32-bit
  433. * align the size.
  434. */
  435. sz = ((sz + 3) / 4) * 4;
  436. return sz;
  437. }
  438. EXPORT_SYMBOL(mmc_align_data_size);
  439. /**
  440. * mmc_host_enable - enable a host.
  441. * @host: mmc host to enable
  442. *
  443. * Hosts that support power saving can use the 'enable' and 'disable'
  444. * methods to exit and enter power saving states. For more information
  445. * see comments for struct mmc_host_ops.
  446. */
  447. int mmc_host_enable(struct mmc_host *host)
  448. {
  449. if (!(host->caps & MMC_CAP_DISABLE))
  450. return 0;
  451. if (host->en_dis_recurs)
  452. return 0;
  453. if (host->nesting_cnt++)
  454. return 0;
  455. cancel_delayed_work_sync(&host->disable);
  456. if (host->enabled)
  457. return 0;
  458. if (host->ops->enable) {
  459. int err;
  460. host->en_dis_recurs = 1;
  461. err = host->ops->enable(host);
  462. host->en_dis_recurs = 0;
  463. if (err) {
  464. pr_debug("%s: enable error %d\n",
  465. mmc_hostname(host), err);
  466. return err;
  467. }
  468. }
  469. host->enabled = 1;
  470. return 0;
  471. }
  472. EXPORT_SYMBOL(mmc_host_enable);
  473. static int mmc_host_do_disable(struct mmc_host *host, int lazy)
  474. {
  475. if (host->ops->disable) {
  476. int err;
  477. host->en_dis_recurs = 1;
  478. err = host->ops->disable(host, lazy);
  479. host->en_dis_recurs = 0;
  480. if (err < 0) {
  481. pr_debug("%s: disable error %d\n",
  482. mmc_hostname(host), err);
  483. return err;
  484. }
  485. if (err > 0) {
  486. unsigned long delay = msecs_to_jiffies(err);
  487. mmc_schedule_delayed_work(&host->disable, delay);
  488. }
  489. }
  490. host->enabled = 0;
  491. return 0;
  492. }
  493. /**
  494. * mmc_host_disable - disable a host.
  495. * @host: mmc host to disable
  496. *
  497. * Hosts that support power saving can use the 'enable' and 'disable'
  498. * methods to exit and enter power saving states. For more information
  499. * see comments for struct mmc_host_ops.
  500. */
  501. int mmc_host_disable(struct mmc_host *host)
  502. {
  503. int err;
  504. if (!(host->caps & MMC_CAP_DISABLE))
  505. return 0;
  506. if (host->en_dis_recurs)
  507. return 0;
  508. if (--host->nesting_cnt)
  509. return 0;
  510. if (!host->enabled)
  511. return 0;
  512. err = mmc_host_do_disable(host, 0);
  513. return err;
  514. }
  515. EXPORT_SYMBOL(mmc_host_disable);
  516. /**
  517. * __mmc_claim_host - exclusively claim a host
  518. * @host: mmc host to claim
  519. * @abort: whether or not the operation should be aborted
  520. *
  521. * Claim a host for a set of operations. If @abort is non null and
  522. * dereference a non-zero value then this will return prematurely with
  523. * that non-zero value without acquiring the lock. Returns zero
  524. * with the lock held otherwise.
  525. */
  526. int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
  527. {
  528. DECLARE_WAITQUEUE(wait, current);
  529. unsigned long flags;
  530. int stop;
  531. might_sleep();
  532. add_wait_queue(&host->wq, &wait);
  533. spin_lock_irqsave(&host->lock, flags);
  534. while (1) {
  535. set_current_state(TASK_UNINTERRUPTIBLE);
  536. stop = abort ? atomic_read(abort) : 0;
  537. if (stop || !host->claimed || host->claimer == current)
  538. break;
  539. spin_unlock_irqrestore(&host->lock, flags);
  540. schedule();
  541. spin_lock_irqsave(&host->lock, flags);
  542. }
  543. set_current_state(TASK_RUNNING);
  544. if (!stop) {
  545. host->claimed = 1;
  546. host->claimer = current;
  547. host->claim_cnt += 1;
  548. } else
  549. wake_up(&host->wq);
  550. spin_unlock_irqrestore(&host->lock, flags);
  551. remove_wait_queue(&host->wq, &wait);
  552. if (!stop)
  553. mmc_host_enable(host);
  554. return stop;
  555. }
  556. EXPORT_SYMBOL(__mmc_claim_host);
  557. /**
  558. * mmc_try_claim_host - try exclusively to claim a host
  559. * @host: mmc host to claim
  560. *
  561. * Returns %1 if the host is claimed, %0 otherwise.
  562. */
  563. int mmc_try_claim_host(struct mmc_host *host)
  564. {
  565. int claimed_host = 0;
  566. unsigned long flags;
  567. spin_lock_irqsave(&host->lock, flags);
  568. if (!host->claimed || host->claimer == current) {
  569. host->claimed = 1;
  570. host->claimer = current;
  571. host->claim_cnt += 1;
  572. claimed_host = 1;
  573. }
  574. spin_unlock_irqrestore(&host->lock, flags);
  575. return claimed_host;
  576. }
  577. EXPORT_SYMBOL(mmc_try_claim_host);
  578. /**
  579. * mmc_do_release_host - release a claimed host
  580. * @host: mmc host to release
  581. *
  582. * If you successfully claimed a host, this function will
  583. * release it again.
  584. */
  585. void mmc_do_release_host(struct mmc_host *host)
  586. {
  587. unsigned long flags;
  588. spin_lock_irqsave(&host->lock, flags);
  589. if (--host->claim_cnt) {
  590. /* Release for nested claim */
  591. spin_unlock_irqrestore(&host->lock, flags);
  592. } else {
  593. host->claimed = 0;
  594. host->claimer = NULL;
  595. spin_unlock_irqrestore(&host->lock, flags);
  596. wake_up(&host->wq);
  597. }
  598. }
  599. EXPORT_SYMBOL(mmc_do_release_host);
  600. void mmc_host_deeper_disable(struct work_struct *work)
  601. {
  602. struct mmc_host *host =
  603. container_of(work, struct mmc_host, disable.work);
  604. /* If the host is claimed then we do not want to disable it anymore */
  605. if (!mmc_try_claim_host(host))
  606. return;
  607. mmc_host_do_disable(host, 1);
  608. mmc_do_release_host(host);
  609. }
  610. /**
  611. * mmc_host_lazy_disable - lazily disable a host.
  612. * @host: mmc host to disable
  613. *
  614. * Hosts that support power saving can use the 'enable' and 'disable'
  615. * methods to exit and enter power saving states. For more information
  616. * see comments for struct mmc_host_ops.
  617. */
  618. int mmc_host_lazy_disable(struct mmc_host *host)
  619. {
  620. if (!(host->caps & MMC_CAP_DISABLE))
  621. return 0;
  622. if (host->en_dis_recurs)
  623. return 0;
  624. if (--host->nesting_cnt)
  625. return 0;
  626. if (!host->enabled)
  627. return 0;
  628. if (host->disable_delay) {
  629. mmc_schedule_delayed_work(&host->disable,
  630. msecs_to_jiffies(host->disable_delay));
  631. return 0;
  632. } else
  633. return mmc_host_do_disable(host, 1);
  634. }
  635. EXPORT_SYMBOL(mmc_host_lazy_disable);
  636. /**
  637. * mmc_release_host - release a host
  638. * @host: mmc host to release
  639. *
  640. * Release a MMC host, allowing others to claim the host
  641. * for their operations.
  642. */
  643. void mmc_release_host(struct mmc_host *host)
  644. {
  645. WARN_ON(!host->claimed);
  646. mmc_host_lazy_disable(host);
  647. mmc_do_release_host(host);
  648. }
  649. EXPORT_SYMBOL(mmc_release_host);
  650. /*
  651. * Internal function that does the actual ios call to the host driver,
  652. * optionally printing some debug output.
  653. */
  654. static inline void mmc_set_ios(struct mmc_host *host)
  655. {
  656. struct mmc_ios *ios = &host->ios;
  657. pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
  658. "width %u timing %u\n",
  659. mmc_hostname(host), ios->clock, ios->bus_mode,
  660. ios->power_mode, ios->chip_select, ios->vdd,
  661. ios->bus_width, ios->timing);
  662. if (ios->clock > 0)
  663. mmc_set_ungated(host);
  664. host->ops->set_ios(host, ios);
  665. }
  666. /*
  667. * Control chip select pin on a host.
  668. */
  669. void mmc_set_chip_select(struct mmc_host *host, int mode)
  670. {
  671. mmc_host_clk_hold(host);
  672. host->ios.chip_select = mode;
  673. mmc_set_ios(host);
  674. mmc_host_clk_release(host);
  675. }
  676. /*
  677. * Sets the host clock to the highest possible frequency that
  678. * is below "hz".
  679. */
  680. static void __mmc_set_clock(struct mmc_host *host, unsigned int hz)
  681. {
  682. WARN_ON(hz < host->f_min);
  683. if (hz > host->f_max)
  684. hz = host->f_max;
  685. host->ios.clock = hz;
  686. mmc_set_ios(host);
  687. }
  688. void mmc_set_clock(struct mmc_host *host, unsigned int hz)
  689. {
  690. mmc_host_clk_hold(host);
  691. __mmc_set_clock(host, hz);
  692. mmc_host_clk_release(host);
  693. }
  694. #ifdef CONFIG_MMC_CLKGATE
  695. /*
  696. * This gates the clock by setting it to 0 Hz.
  697. */
  698. void mmc_gate_clock(struct mmc_host *host)
  699. {
  700. unsigned long flags;
  701. spin_lock_irqsave(&host->clk_lock, flags);
  702. host->clk_old = host->ios.clock;
  703. host->ios.clock = 0;
  704. host->clk_gated = true;
  705. spin_unlock_irqrestore(&host->clk_lock, flags);
  706. mmc_set_ios(host);
  707. }
  708. /*
  709. * This restores the clock from gating by using the cached
  710. * clock value.
  711. */
  712. void mmc_ungate_clock(struct mmc_host *host)
  713. {
  714. /*
  715. * We should previously have gated the clock, so the clock shall
  716. * be 0 here! The clock may however be 0 during initialization,
  717. * when some request operations are performed before setting
  718. * the frequency. When ungate is requested in that situation
  719. * we just ignore the call.
  720. */
  721. if (host->clk_old) {
  722. BUG_ON(host->ios.clock);
  723. /* This call will also set host->clk_gated to false */
  724. __mmc_set_clock(host, host->clk_old);
  725. }
  726. }
  727. void mmc_set_ungated(struct mmc_host *host)
  728. {
  729. unsigned long flags;
  730. /*
  731. * We've been given a new frequency while the clock is gated,
  732. * so make sure we regard this as ungating it.
  733. */
  734. spin_lock_irqsave(&host->clk_lock, flags);
  735. host->clk_gated = false;
  736. spin_unlock_irqrestore(&host->clk_lock, flags);
  737. }
  738. #else
  739. void mmc_set_ungated(struct mmc_host *host)
  740. {
  741. }
  742. #endif
  743. /*
  744. * Change the bus mode (open drain/push-pull) of a host.
  745. */
  746. void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
  747. {
  748. mmc_host_clk_hold(host);
  749. host->ios.bus_mode = mode;
  750. mmc_set_ios(host);
  751. mmc_host_clk_release(host);
  752. }
  753. /*
  754. * Change data bus width of a host.
  755. */
  756. void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
  757. {
  758. mmc_host_clk_hold(host);
  759. host->ios.bus_width = width;
  760. mmc_set_ios(host);
  761. mmc_host_clk_release(host);
  762. }
  763. /**
  764. * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
  765. * @vdd: voltage (mV)
  766. * @low_bits: prefer low bits in boundary cases
  767. *
  768. * This function returns the OCR bit number according to the provided @vdd
  769. * value. If conversion is not possible a negative errno value returned.
  770. *
  771. * Depending on the @low_bits flag the function prefers low or high OCR bits
  772. * on boundary voltages. For example,
  773. * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
  774. * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
  775. *
  776. * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
  777. */
  778. static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
  779. {
  780. const int max_bit = ilog2(MMC_VDD_35_36);
  781. int bit;
  782. if (vdd < 1650 || vdd > 3600)
  783. return -EINVAL;
  784. if (vdd >= 1650 && vdd <= 1950)
  785. return ilog2(MMC_VDD_165_195);
  786. if (low_bits)
  787. vdd -= 1;
  788. /* Base 2000 mV, step 100 mV, bit's base 8. */
  789. bit = (vdd - 2000) / 100 + 8;
  790. if (bit > max_bit)
  791. return max_bit;
  792. return bit;
  793. }
  794. /**
  795. * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
  796. * @vdd_min: minimum voltage value (mV)
  797. * @vdd_max: maximum voltage value (mV)
  798. *
  799. * This function returns the OCR mask bits according to the provided @vdd_min
  800. * and @vdd_max values. If conversion is not possible the function returns 0.
  801. *
  802. * Notes wrt boundary cases:
  803. * This function sets the OCR bits for all boundary voltages, for example
  804. * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
  805. * MMC_VDD_34_35 mask.
  806. */
  807. u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
  808. {
  809. u32 mask = 0;
  810. if (vdd_max < vdd_min)
  811. return 0;
  812. /* Prefer high bits for the boundary vdd_max values. */
  813. vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
  814. if (vdd_max < 0)
  815. return 0;
  816. /* Prefer low bits for the boundary vdd_min values. */
  817. vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
  818. if (vdd_min < 0)
  819. return 0;
  820. /* Fill the mask, from max bit to min bit. */
  821. while (vdd_max >= vdd_min)
  822. mask |= 1 << vdd_max--;
  823. return mask;
  824. }
  825. EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
  826. #ifdef CONFIG_REGULATOR
  827. /**
  828. * mmc_regulator_get_ocrmask - return mask of supported voltages
  829. * @supply: regulator to use
  830. *
  831. * This returns either a negative errno, or a mask of voltages that
  832. * can be provided to MMC/SD/SDIO devices using the specified voltage
  833. * regulator. This would normally be called before registering the
  834. * MMC host adapter.
  835. */
  836. int mmc_regulator_get_ocrmask(struct regulator *supply)
  837. {
  838. int result = 0;
  839. int count;
  840. int i;
  841. count = regulator_count_voltages(supply);
  842. if (count < 0)
  843. return count;
  844. for (i = 0; i < count; i++) {
  845. int vdd_uV;
  846. int vdd_mV;
  847. vdd_uV = regulator_list_voltage(supply, i);
  848. if (vdd_uV <= 0)
  849. continue;
  850. vdd_mV = vdd_uV / 1000;
  851. result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
  852. }
  853. return result;
  854. }
  855. EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
  856. /**
  857. * mmc_regulator_set_ocr - set regulator to match host->ios voltage
  858. * @mmc: the host to regulate
  859. * @supply: regulator to use
  860. * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
  861. *
  862. * Returns zero on success, else negative errno.
  863. *
  864. * MMC host drivers may use this to enable or disable a regulator using
  865. * a particular supply voltage. This would normally be called from the
  866. * set_ios() method.
  867. */
  868. int mmc_regulator_set_ocr(struct mmc_host *mmc,
  869. struct regulator *supply,
  870. unsigned short vdd_bit)
  871. {
  872. int result = 0;
  873. int min_uV, max_uV;
  874. if (vdd_bit) {
  875. int tmp;
  876. int voltage;
  877. /* REVISIT mmc_vddrange_to_ocrmask() may have set some
  878. * bits this regulator doesn't quite support ... don't
  879. * be too picky, most cards and regulators are OK with
  880. * a 0.1V range goof (it's a small error percentage).
  881. */
  882. tmp = vdd_bit - ilog2(MMC_VDD_165_195);
  883. if (tmp == 0) {
  884. min_uV = 1650 * 1000;
  885. max_uV = 1950 * 1000;
  886. } else {
  887. min_uV = 1900 * 1000 + tmp * 100 * 1000;
  888. max_uV = min_uV + 100 * 1000;
  889. }
  890. /* avoid needless changes to this voltage; the regulator
  891. * might not allow this operation
  892. */
  893. voltage = regulator_get_voltage(supply);
  894. if (voltage < 0)
  895. result = voltage;
  896. else if (voltage < min_uV || voltage > max_uV)
  897. result = regulator_set_voltage(supply, min_uV, max_uV);
  898. else
  899. result = 0;
  900. if (result == 0 && !mmc->regulator_enabled) {
  901. result = regulator_enable(supply);
  902. if (!result)
  903. mmc->regulator_enabled = true;
  904. }
  905. } else if (mmc->regulator_enabled) {
  906. result = regulator_disable(supply);
  907. if (result == 0)
  908. mmc->regulator_enabled = false;
  909. }
  910. if (result)
  911. dev_err(mmc_dev(mmc),
  912. "could not set regulator OCR (%d)\n", result);
  913. return result;
  914. }
  915. EXPORT_SYMBOL(mmc_regulator_set_ocr);
  916. #endif /* CONFIG_REGULATOR */
  917. /*
  918. * Mask off any voltages we don't support and select
  919. * the lowest voltage
  920. */
  921. u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
  922. {
  923. int bit;
  924. ocr &= host->ocr_avail;
  925. bit = ffs(ocr);
  926. if (bit) {
  927. bit -= 1;
  928. ocr &= 3 << bit;
  929. mmc_host_clk_hold(host);
  930. host->ios.vdd = bit;
  931. mmc_set_ios(host);
  932. mmc_host_clk_release(host);
  933. } else {
  934. pr_warning("%s: host doesn't support card's voltages\n",
  935. mmc_hostname(host));
  936. ocr = 0;
  937. }
  938. return ocr;
  939. }
  940. int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, bool cmd11)
  941. {
  942. struct mmc_command cmd = {0};
  943. int err = 0;
  944. BUG_ON(!host);
  945. /*
  946. * Send CMD11 only if the request is to switch the card to
  947. * 1.8V signalling.
  948. */
  949. if ((signal_voltage != MMC_SIGNAL_VOLTAGE_330) && cmd11) {
  950. cmd.opcode = SD_SWITCH_VOLTAGE;
  951. cmd.arg = 0;
  952. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  953. err = mmc_wait_for_cmd(host, &cmd, 0);
  954. if (err)
  955. return err;
  956. if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
  957. return -EIO;
  958. }
  959. host->ios.signal_voltage = signal_voltage;
  960. if (host->ops->start_signal_voltage_switch)
  961. err = host->ops->start_signal_voltage_switch(host, &host->ios);
  962. return err;
  963. }
  964. /*
  965. * Select timing parameters for host.
  966. */
  967. void mmc_set_timing(struct mmc_host *host, unsigned int timing)
  968. {
  969. mmc_host_clk_hold(host);
  970. host->ios.timing = timing;
  971. mmc_set_ios(host);
  972. mmc_host_clk_release(host);
  973. }
  974. /*
  975. * Select appropriate driver type for host.
  976. */
  977. void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
  978. {
  979. mmc_host_clk_hold(host);
  980. host->ios.drv_type = drv_type;
  981. mmc_set_ios(host);
  982. mmc_host_clk_release(host);
  983. }
  984. /*
  985. * Apply power to the MMC stack. This is a two-stage process.
  986. * First, we enable power to the card without the clock running.
  987. * We then wait a bit for the power to stabilise. Finally,
  988. * enable the bus drivers and clock to the card.
  989. *
  990. * We must _NOT_ enable the clock prior to power stablising.
  991. *
  992. * If a host does all the power sequencing itself, ignore the
  993. * initial MMC_POWER_UP stage.
  994. */
  995. static void mmc_power_up(struct mmc_host *host)
  996. {
  997. int bit;
  998. mmc_host_clk_hold(host);
  999. /* If ocr is set, we use it */
  1000. if (host->ocr)
  1001. bit = ffs(host->ocr) - 1;
  1002. else
  1003. bit = fls(host->ocr_avail) - 1;
  1004. host->ios.vdd = bit;
  1005. if (mmc_host_is_spi(host))
  1006. host->ios.chip_select = MMC_CS_HIGH;
  1007. else
  1008. host->ios.chip_select = MMC_CS_DONTCARE;
  1009. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  1010. host->ios.power_mode = MMC_POWER_UP;
  1011. host->ios.bus_width = MMC_BUS_WIDTH_1;
  1012. host->ios.timing = MMC_TIMING_LEGACY;
  1013. mmc_set_ios(host);
  1014. /*
  1015. * This delay should be sufficient to allow the power supply
  1016. * to reach the minimum voltage.
  1017. */
  1018. mmc_delay(10);
  1019. host->ios.clock = host->f_init;
  1020. host->ios.power_mode = MMC_POWER_ON;
  1021. mmc_set_ios(host);
  1022. /*
  1023. * This delay must be at least 74 clock sizes, or 1 ms, or the
  1024. * time required to reach a stable voltage.
  1025. */
  1026. mmc_delay(10);
  1027. mmc_host_clk_release(host);
  1028. }
  1029. void mmc_power_off(struct mmc_host *host)
  1030. {
  1031. mmc_host_clk_hold(host);
  1032. host->ios.clock = 0;
  1033. host->ios.vdd = 0;
  1034. /*
  1035. * Reset ocr mask to be the highest possible voltage supported for
  1036. * this mmc host. This value will be used at next power up.
  1037. */
  1038. host->ocr = 1 << (fls(host->ocr_avail) - 1);
  1039. if (!mmc_host_is_spi(host)) {
  1040. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  1041. host->ios.chip_select = MMC_CS_DONTCARE;
  1042. }
  1043. host->ios.power_mode = MMC_POWER_OFF;
  1044. host->ios.bus_width = MMC_BUS_WIDTH_1;
  1045. host->ios.timing = MMC_TIMING_LEGACY;
  1046. mmc_set_ios(host);
  1047. /*
  1048. * Some configurations, such as the 802.11 SDIO card in the OLPC
  1049. * XO-1.5, require a short delay after poweroff before the card
  1050. * can be successfully turned on again.
  1051. */
  1052. mmc_delay(1);
  1053. mmc_host_clk_release(host);
  1054. }
  1055. /*
  1056. * Cleanup when the last reference to the bus operator is dropped.
  1057. */
  1058. static void __mmc_release_bus(struct mmc_host *host)
  1059. {
  1060. BUG_ON(!host);
  1061. BUG_ON(host->bus_refs);
  1062. BUG_ON(!host->bus_dead);
  1063. host->bus_ops = NULL;
  1064. }
  1065. /*
  1066. * Increase reference count of bus operator
  1067. */
  1068. static inline void mmc_bus_get(struct mmc_host *host)
  1069. {
  1070. unsigned long flags;
  1071. spin_lock_irqsave(&host->lock, flags);
  1072. host->bus_refs++;
  1073. spin_unlock_irqrestore(&host->lock, flags);
  1074. }
  1075. /*
  1076. * Decrease reference count of bus operator and free it if
  1077. * it is the last reference.
  1078. */
  1079. static inline void mmc_bus_put(struct mmc_host *host)
  1080. {
  1081. unsigned long flags;
  1082. spin_lock_irqsave(&host->lock, flags);
  1083. host->bus_refs--;
  1084. if ((host->bus_refs == 0) && host->bus_ops)
  1085. __mmc_release_bus(host);
  1086. spin_unlock_irqrestore(&host->lock, flags);
  1087. }
  1088. /*
  1089. * Assign a mmc bus handler to a host. Only one bus handler may control a
  1090. * host at any given time.
  1091. */
  1092. void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
  1093. {
  1094. unsigned long flags;
  1095. BUG_ON(!host);
  1096. BUG_ON(!ops);
  1097. WARN_ON(!host->claimed);
  1098. spin_lock_irqsave(&host->lock, flags);
  1099. BUG_ON(host->bus_ops);
  1100. BUG_ON(host->bus_refs);
  1101. host->bus_ops = ops;
  1102. host->bus_refs = 1;
  1103. host->bus_dead = 0;
  1104. spin_unlock_irqrestore(&host->lock, flags);
  1105. }
  1106. /*
  1107. * Remove the current bus handler from a host.
  1108. */
  1109. void mmc_detach_bus(struct mmc_host *host)
  1110. {
  1111. unsigned long flags;
  1112. BUG_ON(!host);
  1113. WARN_ON(!host->claimed);
  1114. WARN_ON(!host->bus_ops);
  1115. spin_lock_irqsave(&host->lock, flags);
  1116. host->bus_dead = 1;
  1117. spin_unlock_irqrestore(&host->lock, flags);
  1118. mmc_bus_put(host);
  1119. }
  1120. /**
  1121. * mmc_detect_change - process change of state on a MMC socket
  1122. * @host: host which changed state.
  1123. * @delay: optional delay to wait before detection (jiffies)
  1124. *
  1125. * MMC drivers should call this when they detect a card has been
  1126. * inserted or removed. The MMC layer will confirm that any
  1127. * present card is still functional, and initialize any newly
  1128. * inserted.
  1129. */
  1130. void mmc_detect_change(struct mmc_host *host, unsigned long delay)
  1131. {
  1132. #ifdef CONFIG_MMC_DEBUG
  1133. unsigned long flags;
  1134. spin_lock_irqsave(&host->lock, flags);
  1135. WARN_ON(host->removed);
  1136. spin_unlock_irqrestore(&host->lock, flags);
  1137. #endif
  1138. mmc_schedule_delayed_work(&host->detect, delay);
  1139. }
  1140. EXPORT_SYMBOL(mmc_detect_change);
  1141. void mmc_init_erase(struct mmc_card *card)
  1142. {
  1143. unsigned int sz;
  1144. if (is_power_of_2(card->erase_size))
  1145. card->erase_shift = ffs(card->erase_size) - 1;
  1146. else
  1147. card->erase_shift = 0;
  1148. /*
  1149. * It is possible to erase an arbitrarily large area of an SD or MMC
  1150. * card. That is not desirable because it can take a long time
  1151. * (minutes) potentially delaying more important I/O, and also the
  1152. * timeout calculations become increasingly hugely over-estimated.
  1153. * Consequently, 'pref_erase' is defined as a guide to limit erases
  1154. * to that size and alignment.
  1155. *
  1156. * For SD cards that define Allocation Unit size, limit erases to one
  1157. * Allocation Unit at a time. For MMC cards that define High Capacity
  1158. * Erase Size, whether it is switched on or not, limit to that size.
  1159. * Otherwise just have a stab at a good value. For modern cards it
  1160. * will end up being 4MiB. Note that if the value is too small, it
  1161. * can end up taking longer to erase.
  1162. */
  1163. if (mmc_card_sd(card) && card->ssr.au) {
  1164. card->pref_erase = card->ssr.au;
  1165. card->erase_shift = ffs(card->ssr.au) - 1;
  1166. } else if (card->ext_csd.hc_erase_size) {
  1167. card->pref_erase = card->ext_csd.hc_erase_size;
  1168. } else {
  1169. sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
  1170. if (sz < 128)
  1171. card->pref_erase = 512 * 1024 / 512;
  1172. else if (sz < 512)
  1173. card->pref_erase = 1024 * 1024 / 512;
  1174. else if (sz < 1024)
  1175. card->pref_erase = 2 * 1024 * 1024 / 512;
  1176. else
  1177. card->pref_erase = 4 * 1024 * 1024 / 512;
  1178. if (card->pref_erase < card->erase_size)
  1179. card->pref_erase = card->erase_size;
  1180. else {
  1181. sz = card->pref_erase % card->erase_size;
  1182. if (sz)
  1183. card->pref_erase += card->erase_size - sz;
  1184. }
  1185. }
  1186. }
  1187. static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
  1188. unsigned int arg, unsigned int qty)
  1189. {
  1190. unsigned int erase_timeout;
  1191. if (card->ext_csd.erase_group_def & 1) {
  1192. /* High Capacity Erase Group Size uses HC timeouts */
  1193. if (arg == MMC_TRIM_ARG)
  1194. erase_timeout = card->ext_csd.trim_timeout;
  1195. else
  1196. erase_timeout = card->ext_csd.hc_erase_timeout;
  1197. } else {
  1198. /* CSD Erase Group Size uses write timeout */
  1199. unsigned int mult = (10 << card->csd.r2w_factor);
  1200. unsigned int timeout_clks = card->csd.tacc_clks * mult;
  1201. unsigned int timeout_us;
  1202. /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
  1203. if (card->csd.tacc_ns < 1000000)
  1204. timeout_us = (card->csd.tacc_ns * mult) / 1000;
  1205. else
  1206. timeout_us = (card->csd.tacc_ns / 1000) * mult;
  1207. /*
  1208. * ios.clock is only a target. The real clock rate might be
  1209. * less but not that much less, so fudge it by multiplying by 2.
  1210. */
  1211. timeout_clks <<= 1;
  1212. timeout_us += (timeout_clks * 1000) /
  1213. (mmc_host_clk_rate(card->host) / 1000);
  1214. erase_timeout = timeout_us / 1000;
  1215. /*
  1216. * Theoretically, the calculation could underflow so round up
  1217. * to 1ms in that case.
  1218. */
  1219. if (!erase_timeout)
  1220. erase_timeout = 1;
  1221. }
  1222. /* Multiplier for secure operations */
  1223. if (arg & MMC_SECURE_ARGS) {
  1224. if (arg == MMC_SECURE_ERASE_ARG)
  1225. erase_timeout *= card->ext_csd.sec_erase_mult;
  1226. else
  1227. erase_timeout *= card->ext_csd.sec_trim_mult;
  1228. }
  1229. erase_timeout *= qty;
  1230. /*
  1231. * Ensure at least a 1 second timeout for SPI as per
  1232. * 'mmc_set_data_timeout()'
  1233. */
  1234. if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
  1235. erase_timeout = 1000;
  1236. return erase_timeout;
  1237. }
  1238. static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
  1239. unsigned int arg,
  1240. unsigned int qty)
  1241. {
  1242. unsigned int erase_timeout;
  1243. if (card->ssr.erase_timeout) {
  1244. /* Erase timeout specified in SD Status Register (SSR) */
  1245. erase_timeout = card->ssr.erase_timeout * qty +
  1246. card->ssr.erase_offset;
  1247. } else {
  1248. /*
  1249. * Erase timeout not specified in SD Status Register (SSR) so
  1250. * use 250ms per write block.
  1251. */
  1252. erase_timeout = 250 * qty;
  1253. }
  1254. /* Must not be less than 1 second */
  1255. if (erase_timeout < 1000)
  1256. erase_timeout = 1000;
  1257. return erase_timeout;
  1258. }
  1259. static unsigned int mmc_erase_timeout(struct mmc_card *card,
  1260. unsigned int arg,
  1261. unsigned int qty)
  1262. {
  1263. if (mmc_card_sd(card))
  1264. return mmc_sd_erase_timeout(card, arg, qty);
  1265. else
  1266. return mmc_mmc_erase_timeout(card, arg, qty);
  1267. }
  1268. static int mmc_do_erase(struct mmc_card *card, unsigned int from,
  1269. unsigned int to, unsigned int arg)
  1270. {
  1271. struct mmc_command cmd = {0};
  1272. unsigned int qty = 0;
  1273. int err;
  1274. /*
  1275. * qty is used to calculate the erase timeout which depends on how many
  1276. * erase groups (or allocation units in SD terminology) are affected.
  1277. * We count erasing part of an erase group as one erase group.
  1278. * For SD, the allocation units are always a power of 2. For MMC, the
  1279. * erase group size is almost certainly also power of 2, but it does not
  1280. * seem to insist on that in the JEDEC standard, so we fall back to
  1281. * division in that case. SD may not specify an allocation unit size,
  1282. * in which case the timeout is based on the number of write blocks.
  1283. *
  1284. * Note that the timeout for secure trim 2 will only be correct if the
  1285. * number of erase groups specified is the same as the total of all
  1286. * preceding secure trim 1 commands. Since the power may have been
  1287. * lost since the secure trim 1 commands occurred, it is generally
  1288. * impossible to calculate the secure trim 2 timeout correctly.
  1289. */
  1290. if (card->erase_shift)
  1291. qty += ((to >> card->erase_shift) -
  1292. (from >> card->erase_shift)) + 1;
  1293. else if (mmc_card_sd(card))
  1294. qty += to - from + 1;
  1295. else
  1296. qty += ((to / card->erase_size) -
  1297. (from / card->erase_size)) + 1;
  1298. if (!mmc_card_blockaddr(card)) {
  1299. from <<= 9;
  1300. to <<= 9;
  1301. }
  1302. if (mmc_card_sd(card))
  1303. cmd.opcode = SD_ERASE_WR_BLK_START;
  1304. else
  1305. cmd.opcode = MMC_ERASE_GROUP_START;
  1306. cmd.arg = from;
  1307. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1308. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1309. if (err) {
  1310. printk(KERN_ERR "mmc_erase: group start error %d, "
  1311. "status %#x\n", err, cmd.resp[0]);
  1312. err = -EINVAL;
  1313. goto out;
  1314. }
  1315. memset(&cmd, 0, sizeof(struct mmc_command));
  1316. if (mmc_card_sd(card))
  1317. cmd.opcode = SD_ERASE_WR_BLK_END;
  1318. else
  1319. cmd.opcode = MMC_ERASE_GROUP_END;
  1320. cmd.arg = to;
  1321. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1322. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1323. if (err) {
  1324. printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
  1325. err, cmd.resp[0]);
  1326. err = -EINVAL;
  1327. goto out;
  1328. }
  1329. memset(&cmd, 0, sizeof(struct mmc_command));
  1330. cmd.opcode = MMC_ERASE;
  1331. cmd.arg = arg;
  1332. cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  1333. cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
  1334. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1335. if (err) {
  1336. printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
  1337. err, cmd.resp[0]);
  1338. err = -EIO;
  1339. goto out;
  1340. }
  1341. if (mmc_host_is_spi(card->host))
  1342. goto out;
  1343. do {
  1344. memset(&cmd, 0, sizeof(struct mmc_command));
  1345. cmd.opcode = MMC_SEND_STATUS;
  1346. cmd.arg = card->rca << 16;
  1347. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1348. /* Do not retry else we can't see errors */
  1349. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1350. if (err || (cmd.resp[0] & 0xFDF92000)) {
  1351. printk(KERN_ERR "error %d requesting status %#x\n",
  1352. err, cmd.resp[0]);
  1353. err = -EIO;
  1354. goto out;
  1355. }
  1356. } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
  1357. R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG);
  1358. out:
  1359. return err;
  1360. }
  1361. /**
  1362. * mmc_erase - erase sectors.
  1363. * @card: card to erase
  1364. * @from: first sector to erase
  1365. * @nr: number of sectors to erase
  1366. * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
  1367. *
  1368. * Caller must claim host before calling this function.
  1369. */
  1370. int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
  1371. unsigned int arg)
  1372. {
  1373. unsigned int rem, to = from + nr;
  1374. if (!(card->host->caps & MMC_CAP_ERASE) ||
  1375. !(card->csd.cmdclass & CCC_ERASE))
  1376. return -EOPNOTSUPP;
  1377. if (!card->erase_size)
  1378. return -EOPNOTSUPP;
  1379. if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
  1380. return -EOPNOTSUPP;
  1381. if ((arg & MMC_SECURE_ARGS) &&
  1382. !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
  1383. return -EOPNOTSUPP;
  1384. if ((arg & MMC_TRIM_ARGS) &&
  1385. !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
  1386. return -EOPNOTSUPP;
  1387. if (arg == MMC_SECURE_ERASE_ARG) {
  1388. if (from % card->erase_size || nr % card->erase_size)
  1389. return -EINVAL;
  1390. }
  1391. if (arg == MMC_ERASE_ARG) {
  1392. rem = from % card->erase_size;
  1393. if (rem) {
  1394. rem = card->erase_size - rem;
  1395. from += rem;
  1396. if (nr > rem)
  1397. nr -= rem;
  1398. else
  1399. return 0;
  1400. }
  1401. rem = nr % card->erase_size;
  1402. if (rem)
  1403. nr -= rem;
  1404. }
  1405. if (nr == 0)
  1406. return 0;
  1407. to = from + nr;
  1408. if (to <= from)
  1409. return -EINVAL;
  1410. /* 'from' and 'to' are inclusive */
  1411. to -= 1;
  1412. return mmc_do_erase(card, from, to, arg);
  1413. }
  1414. EXPORT_SYMBOL(mmc_erase);
  1415. int mmc_can_erase(struct mmc_card *card)
  1416. {
  1417. if ((card->host->caps & MMC_CAP_ERASE) &&
  1418. (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
  1419. return 1;
  1420. return 0;
  1421. }
  1422. EXPORT_SYMBOL(mmc_can_erase);
  1423. int mmc_can_trim(struct mmc_card *card)
  1424. {
  1425. if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
  1426. return 1;
  1427. return 0;
  1428. }
  1429. EXPORT_SYMBOL(mmc_can_trim);
  1430. int mmc_can_secure_erase_trim(struct mmc_card *card)
  1431. {
  1432. if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
  1433. return 1;
  1434. return 0;
  1435. }
  1436. EXPORT_SYMBOL(mmc_can_secure_erase_trim);
  1437. int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
  1438. unsigned int nr)
  1439. {
  1440. if (!card->erase_size)
  1441. return 0;
  1442. if (from % card->erase_size || nr % card->erase_size)
  1443. return 0;
  1444. return 1;
  1445. }
  1446. EXPORT_SYMBOL(mmc_erase_group_aligned);
  1447. static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
  1448. unsigned int arg)
  1449. {
  1450. struct mmc_host *host = card->host;
  1451. unsigned int max_discard, x, y, qty = 0, max_qty, timeout;
  1452. unsigned int last_timeout = 0;
  1453. if (card->erase_shift)
  1454. max_qty = UINT_MAX >> card->erase_shift;
  1455. else if (mmc_card_sd(card))
  1456. max_qty = UINT_MAX;
  1457. else
  1458. max_qty = UINT_MAX / card->erase_size;
  1459. /* Find the largest qty with an OK timeout */
  1460. do {
  1461. y = 0;
  1462. for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
  1463. timeout = mmc_erase_timeout(card, arg, qty + x);
  1464. if (timeout > host->max_discard_to)
  1465. break;
  1466. if (timeout < last_timeout)
  1467. break;
  1468. last_timeout = timeout;
  1469. y = x;
  1470. }
  1471. qty += y;
  1472. } while (y);
  1473. if (!qty)
  1474. return 0;
  1475. if (qty == 1)
  1476. return 1;
  1477. /* Convert qty to sectors */
  1478. if (card->erase_shift)
  1479. max_discard = --qty << card->erase_shift;
  1480. else if (mmc_card_sd(card))
  1481. max_discard = qty;
  1482. else
  1483. max_discard = --qty * card->erase_size;
  1484. return max_discard;
  1485. }
  1486. unsigned int mmc_calc_max_discard(struct mmc_card *card)
  1487. {
  1488. struct mmc_host *host = card->host;
  1489. unsigned int max_discard, max_trim;
  1490. if (!host->max_discard_to)
  1491. return UINT_MAX;
  1492. /*
  1493. * Without erase_group_def set, MMC erase timeout depends on clock
  1494. * frequence which can change. In that case, the best choice is
  1495. * just the preferred erase size.
  1496. */
  1497. if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
  1498. return card->pref_erase;
  1499. max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
  1500. if (mmc_can_trim(card)) {
  1501. max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
  1502. if (max_trim < max_discard)
  1503. max_discard = max_trim;
  1504. } else if (max_discard < card->erase_size) {
  1505. max_discard = 0;
  1506. }
  1507. pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
  1508. mmc_hostname(host), max_discard, host->max_discard_to);
  1509. return max_discard;
  1510. }
  1511. EXPORT_SYMBOL(mmc_calc_max_discard);
  1512. int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
  1513. {
  1514. struct mmc_command cmd = {0};
  1515. if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
  1516. return 0;
  1517. cmd.opcode = MMC_SET_BLOCKLEN;
  1518. cmd.arg = blocklen;
  1519. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1520. return mmc_wait_for_cmd(card->host, &cmd, 5);
  1521. }
  1522. EXPORT_SYMBOL(mmc_set_blocklen);
  1523. static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
  1524. {
  1525. host->f_init = freq;
  1526. #ifdef CONFIG_MMC_DEBUG
  1527. pr_info("%s: %s: trying to init card at %u Hz\n",
  1528. mmc_hostname(host), __func__, host->f_init);
  1529. #endif
  1530. mmc_power_up(host);
  1531. /*
  1532. * sdio_reset sends CMD52 to reset card. Since we do not know
  1533. * if the card is being re-initialized, just send it. CMD52
  1534. * should be ignored by SD/eMMC cards.
  1535. */
  1536. sdio_reset(host);
  1537. mmc_go_idle(host);
  1538. mmc_send_if_cond(host, host->ocr_avail);
  1539. /* Order's important: probe SDIO, then SD, then MMC */
  1540. if (!mmc_attach_sdio(host))
  1541. return 0;
  1542. if (!mmc_attach_sd(host))
  1543. return 0;
  1544. if (!mmc_attach_mmc(host))
  1545. return 0;
  1546. mmc_power_off(host);
  1547. return -EIO;
  1548. }
  1549. void mmc_rescan(struct work_struct *work)
  1550. {
  1551. static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
  1552. struct mmc_host *host =
  1553. container_of(work, struct mmc_host, detect.work);
  1554. int i;
  1555. if (host->rescan_disable)
  1556. return;
  1557. mmc_bus_get(host);
  1558. /*
  1559. * if there is a _removable_ card registered, check whether it is
  1560. * still present
  1561. */
  1562. if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
  1563. && !(host->caps & MMC_CAP_NONREMOVABLE))
  1564. host->bus_ops->detect(host);
  1565. /*
  1566. * Let mmc_bus_put() free the bus/bus_ops if we've found that
  1567. * the card is no longer present.
  1568. */
  1569. mmc_bus_put(host);
  1570. mmc_bus_get(host);
  1571. /* if there still is a card present, stop here */
  1572. if (host->bus_ops != NULL) {
  1573. mmc_bus_put(host);
  1574. goto out;
  1575. }
  1576. /*
  1577. * Only we can add a new handler, so it's safe to
  1578. * release the lock here.
  1579. */
  1580. mmc_bus_put(host);
  1581. if (host->ops->get_cd && host->ops->get_cd(host) == 0)
  1582. goto out;
  1583. mmc_claim_host(host);
  1584. for (i = 0; i < ARRAY_SIZE(freqs); i++) {
  1585. if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
  1586. break;
  1587. if (freqs[i] <= host->f_min)
  1588. break;
  1589. }
  1590. mmc_release_host(host);
  1591. out:
  1592. if (host->caps & MMC_CAP_NEEDS_POLL)
  1593. mmc_schedule_delayed_work(&host->detect, HZ);
  1594. }
  1595. void mmc_start_host(struct mmc_host *host)
  1596. {
  1597. mmc_power_off(host);
  1598. mmc_detect_change(host, 0);
  1599. }
  1600. void mmc_stop_host(struct mmc_host *host)
  1601. {
  1602. #ifdef CONFIG_MMC_DEBUG
  1603. unsigned long flags;
  1604. spin_lock_irqsave(&host->lock, flags);
  1605. host->removed = 1;
  1606. spin_unlock_irqrestore(&host->lock, flags);
  1607. #endif
  1608. if (host->caps & MMC_CAP_DISABLE)
  1609. cancel_delayed_work(&host->disable);
  1610. cancel_delayed_work_sync(&host->detect);
  1611. mmc_flush_scheduled_work();
  1612. /* clear pm flags now and let card drivers set them as needed */
  1613. host->pm_flags = 0;
  1614. mmc_bus_get(host);
  1615. if (host->bus_ops && !host->bus_dead) {
  1616. if (host->bus_ops->remove)
  1617. host->bus_ops->remove(host);
  1618. mmc_claim_host(host);
  1619. mmc_detach_bus(host);
  1620. mmc_power_off(host);
  1621. mmc_release_host(host);
  1622. mmc_bus_put(host);
  1623. return;
  1624. }
  1625. mmc_bus_put(host);
  1626. BUG_ON(host->card);
  1627. mmc_power_off(host);
  1628. }
  1629. int mmc_power_save_host(struct mmc_host *host)
  1630. {
  1631. int ret = 0;
  1632. #ifdef CONFIG_MMC_DEBUG
  1633. pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
  1634. #endif
  1635. mmc_bus_get(host);
  1636. if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
  1637. mmc_bus_put(host);
  1638. return -EINVAL;
  1639. }
  1640. if (host->bus_ops->power_save)
  1641. ret = host->bus_ops->power_save(host);
  1642. mmc_bus_put(host);
  1643. mmc_power_off(host);
  1644. return ret;
  1645. }
  1646. EXPORT_SYMBOL(mmc_power_save_host);
  1647. int mmc_power_restore_host(struct mmc_host *host)
  1648. {
  1649. int ret;
  1650. #ifdef CONFIG_MMC_DEBUG
  1651. pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
  1652. #endif
  1653. mmc_bus_get(host);
  1654. if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
  1655. mmc_bus_put(host);
  1656. return -EINVAL;
  1657. }
  1658. mmc_power_up(host);
  1659. ret = host->bus_ops->power_restore(host);
  1660. mmc_bus_put(host);
  1661. return ret;
  1662. }
  1663. EXPORT_SYMBOL(mmc_power_restore_host);
  1664. int mmc_card_awake(struct mmc_host *host)
  1665. {
  1666. int err = -ENOSYS;
  1667. mmc_bus_get(host);
  1668. if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
  1669. err = host->bus_ops->awake(host);
  1670. mmc_bus_put(host);
  1671. return err;
  1672. }
  1673. EXPORT_SYMBOL(mmc_card_awake);
  1674. int mmc_card_sleep(struct mmc_host *host)
  1675. {
  1676. int err = -ENOSYS;
  1677. mmc_bus_get(host);
  1678. if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
  1679. err = host->bus_ops->sleep(host);
  1680. mmc_bus_put(host);
  1681. return err;
  1682. }
  1683. EXPORT_SYMBOL(mmc_card_sleep);
  1684. int mmc_card_can_sleep(struct mmc_host *host)
  1685. {
  1686. struct mmc_card *card = host->card;
  1687. if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
  1688. return 1;
  1689. return 0;
  1690. }
  1691. EXPORT_SYMBOL(mmc_card_can_sleep);
  1692. #ifdef CONFIG_PM
  1693. /**
  1694. * mmc_suspend_host - suspend a host
  1695. * @host: mmc host
  1696. */
  1697. int mmc_suspend_host(struct mmc_host *host)
  1698. {
  1699. int err = 0;
  1700. if (host->caps & MMC_CAP_DISABLE)
  1701. cancel_delayed_work(&host->disable);
  1702. cancel_delayed_work(&host->detect);
  1703. mmc_flush_scheduled_work();
  1704. mmc_bus_get(host);
  1705. if (host->bus_ops && !host->bus_dead) {
  1706. if (host->bus_ops->suspend)
  1707. err = host->bus_ops->suspend(host);
  1708. if (err == -ENOSYS || !host->bus_ops->resume) {
  1709. /*
  1710. * We simply "remove" the card in this case.
  1711. * It will be redetected on resume.
  1712. */
  1713. if (host->bus_ops->remove)
  1714. host->bus_ops->remove(host);
  1715. mmc_claim_host(host);
  1716. mmc_detach_bus(host);
  1717. mmc_power_off(host);
  1718. mmc_release_host(host);
  1719. host->pm_flags = 0;
  1720. err = 0;
  1721. }
  1722. }
  1723. mmc_bus_put(host);
  1724. if (!err && !mmc_card_keep_power(host))
  1725. mmc_power_off(host);
  1726. return err;
  1727. }
  1728. EXPORT_SYMBOL(mmc_suspend_host);
  1729. /**
  1730. * mmc_resume_host - resume a previously suspended host
  1731. * @host: mmc host
  1732. */
  1733. int mmc_resume_host(struct mmc_host *host)
  1734. {
  1735. int err = 0;
  1736. mmc_bus_get(host);
  1737. if (host->bus_ops && !host->bus_dead) {
  1738. if (!mmc_card_keep_power(host)) {
  1739. mmc_power_up(host);
  1740. mmc_select_voltage(host, host->ocr);
  1741. /*
  1742. * Tell runtime PM core we just powered up the card,
  1743. * since it still believes the card is powered off.
  1744. * Note that currently runtime PM is only enabled
  1745. * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
  1746. */
  1747. if (mmc_card_sdio(host->card) &&
  1748. (host->caps & MMC_CAP_POWER_OFF_CARD)) {
  1749. pm_runtime_disable(&host->card->dev);
  1750. pm_runtime_set_active(&host->card->dev);
  1751. pm_runtime_enable(&host->card->dev);
  1752. }
  1753. }
  1754. BUG_ON(!host->bus_ops->resume);
  1755. err = host->bus_ops->resume(host);
  1756. if (err) {
  1757. printk(KERN_WARNING "%s: error %d during resume "
  1758. "(card was removed?)\n",
  1759. mmc_hostname(host), err);
  1760. err = 0;
  1761. }
  1762. }
  1763. host->pm_flags &= ~MMC_PM_KEEP_POWER;
  1764. mmc_bus_put(host);
  1765. return err;
  1766. }
  1767. EXPORT_SYMBOL(mmc_resume_host);
  1768. /* Do the card removal on suspend if card is assumed removeable
  1769. * Do that in pm notifier while userspace isn't yet frozen, so we will be able
  1770. to sync the card.
  1771. */
  1772. int mmc_pm_notify(struct notifier_block *notify_block,
  1773. unsigned long mode, void *unused)
  1774. {
  1775. struct mmc_host *host = container_of(
  1776. notify_block, struct mmc_host, pm_notify);
  1777. unsigned long flags;
  1778. switch (mode) {
  1779. case PM_HIBERNATION_PREPARE:
  1780. case PM_SUSPEND_PREPARE:
  1781. spin_lock_irqsave(&host->lock, flags);
  1782. host->rescan_disable = 1;
  1783. spin_unlock_irqrestore(&host->lock, flags);
  1784. cancel_delayed_work_sync(&host->detect);
  1785. if (!host->bus_ops || host->bus_ops->suspend)
  1786. break;
  1787. mmc_claim_host(host);
  1788. if (host->bus_ops->remove)
  1789. host->bus_ops->remove(host);
  1790. mmc_detach_bus(host);
  1791. mmc_power_off(host);
  1792. mmc_release_host(host);
  1793. host->pm_flags = 0;
  1794. break;
  1795. case PM_POST_SUSPEND:
  1796. case PM_POST_HIBERNATION:
  1797. case PM_POST_RESTORE:
  1798. spin_lock_irqsave(&host->lock, flags);
  1799. host->rescan_disable = 0;
  1800. spin_unlock_irqrestore(&host->lock, flags);
  1801. mmc_detect_change(host, 0);
  1802. }
  1803. return 0;
  1804. }
  1805. #endif
  1806. static int __init mmc_init(void)
  1807. {
  1808. int ret;
  1809. workqueue = alloc_ordered_workqueue("kmmcd", 0);
  1810. if (!workqueue)
  1811. return -ENOMEM;
  1812. ret = mmc_register_bus();
  1813. if (ret)
  1814. goto destroy_workqueue;
  1815. ret = mmc_register_host_class();
  1816. if (ret)
  1817. goto unregister_bus;
  1818. ret = sdio_register_bus();
  1819. if (ret)
  1820. goto unregister_host_class;
  1821. return 0;
  1822. unregister_host_class:
  1823. mmc_unregister_host_class();
  1824. unregister_bus:
  1825. mmc_unregister_bus();
  1826. destroy_workqueue:
  1827. destroy_workqueue(workqueue);
  1828. return ret;
  1829. }
  1830. static void __exit mmc_exit(void)
  1831. {
  1832. sdio_unregister_bus();
  1833. mmc_unregister_host_class();
  1834. mmc_unregister_bus();
  1835. destroy_workqueue(workqueue);
  1836. }
  1837. subsys_initcall(mmc_init);
  1838. module_exit(mmc_exit);
  1839. MODULE_LICENSE("GPL");