core.c 51 KB

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