core.c 61 KB

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