core.c 53 KB

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