core.c 44 KB

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