core.c 59 KB

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