core.c 59 KB

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