core.c 59 KB

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