core.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  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/mmc/card.h>
  26. #include <linux/mmc/host.h>
  27. #include <linux/mmc/mmc.h>
  28. #include <linux/mmc/sd.h>
  29. #include "core.h"
  30. #include "bus.h"
  31. #include "host.h"
  32. #include "sdio_bus.h"
  33. #include "mmc_ops.h"
  34. #include "sd_ops.h"
  35. #include "sdio_ops.h"
  36. static struct workqueue_struct *workqueue;
  37. /*
  38. * Enabling software CRCs on the data blocks can be a significant (30%)
  39. * performance cost, and for other reasons may not always be desired.
  40. * So we allow it it to be disabled.
  41. */
  42. int use_spi_crc = 1;
  43. module_param(use_spi_crc, bool, 0);
  44. /*
  45. * We normally treat cards as removed during suspend if they are not
  46. * known to be on a non-removable bus, to avoid the risk of writing
  47. * back data to a different card after resume. Allow this to be
  48. * overridden if necessary.
  49. */
  50. #ifdef CONFIG_MMC_UNSAFE_RESUME
  51. int mmc_assume_removable;
  52. #else
  53. int mmc_assume_removable = 1;
  54. #endif
  55. module_param_named(removable, mmc_assume_removable, bool, 0644);
  56. MODULE_PARM_DESC(
  57. removable,
  58. "MMC/SD cards are removable and may be removed during suspend");
  59. /*
  60. * Internal function. Schedule delayed work in the MMC work queue.
  61. */
  62. static int mmc_schedule_delayed_work(struct delayed_work *work,
  63. unsigned long delay)
  64. {
  65. return queue_delayed_work(workqueue, work, delay);
  66. }
  67. /*
  68. * Internal function. Flush all scheduled work from the MMC work queue.
  69. */
  70. static void mmc_flush_scheduled_work(void)
  71. {
  72. flush_workqueue(workqueue);
  73. }
  74. /**
  75. * mmc_request_done - finish processing an MMC request
  76. * @host: MMC host which completed request
  77. * @mrq: MMC request which request
  78. *
  79. * MMC drivers should call this function when they have completed
  80. * their processing of a request.
  81. */
  82. void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
  83. {
  84. struct mmc_command *cmd = mrq->cmd;
  85. int err = cmd->error;
  86. if (err && cmd->retries && mmc_host_is_spi(host)) {
  87. if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
  88. cmd->retries = 0;
  89. }
  90. if (err && cmd->retries) {
  91. pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
  92. mmc_hostname(host), cmd->opcode, err);
  93. cmd->retries--;
  94. cmd->error = 0;
  95. host->ops->request(host, mrq);
  96. } else {
  97. led_trigger_event(host->led, LED_OFF);
  98. pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
  99. mmc_hostname(host), cmd->opcode, err,
  100. cmd->resp[0], cmd->resp[1],
  101. cmd->resp[2], cmd->resp[3]);
  102. if (mrq->data) {
  103. pr_debug("%s: %d bytes transferred: %d\n",
  104. mmc_hostname(host),
  105. mrq->data->bytes_xfered, mrq->data->error);
  106. }
  107. if (mrq->stop) {
  108. pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
  109. mmc_hostname(host), mrq->stop->opcode,
  110. mrq->stop->error,
  111. mrq->stop->resp[0], mrq->stop->resp[1],
  112. mrq->stop->resp[2], mrq->stop->resp[3]);
  113. }
  114. if (mrq->done)
  115. mrq->done(mrq);
  116. }
  117. }
  118. EXPORT_SYMBOL(mmc_request_done);
  119. static void
  120. mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
  121. {
  122. #ifdef CONFIG_MMC_DEBUG
  123. unsigned int i, sz;
  124. struct scatterlist *sg;
  125. #endif
  126. pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
  127. mmc_hostname(host), mrq->cmd->opcode,
  128. mrq->cmd->arg, mrq->cmd->flags);
  129. if (mrq->data) {
  130. pr_debug("%s: blksz %d blocks %d flags %08x "
  131. "tsac %d ms nsac %d\n",
  132. mmc_hostname(host), mrq->data->blksz,
  133. mrq->data->blocks, mrq->data->flags,
  134. mrq->data->timeout_ns / 1000000,
  135. mrq->data->timeout_clks);
  136. }
  137. if (mrq->stop) {
  138. pr_debug("%s: CMD%u arg %08x flags %08x\n",
  139. mmc_hostname(host), mrq->stop->opcode,
  140. mrq->stop->arg, mrq->stop->flags);
  141. }
  142. WARN_ON(!host->claimed);
  143. led_trigger_event(host->led, LED_FULL);
  144. mrq->cmd->error = 0;
  145. mrq->cmd->mrq = mrq;
  146. if (mrq->data) {
  147. BUG_ON(mrq->data->blksz > host->max_blk_size);
  148. BUG_ON(mrq->data->blocks > host->max_blk_count);
  149. BUG_ON(mrq->data->blocks * mrq->data->blksz >
  150. host->max_req_size);
  151. #ifdef CONFIG_MMC_DEBUG
  152. sz = 0;
  153. for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
  154. sz += sg->length;
  155. BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
  156. #endif
  157. mrq->cmd->data = mrq->data;
  158. mrq->data->error = 0;
  159. mrq->data->mrq = mrq;
  160. if (mrq->stop) {
  161. mrq->data->stop = mrq->stop;
  162. mrq->stop->error = 0;
  163. mrq->stop->mrq = mrq;
  164. }
  165. }
  166. host->ops->request(host, mrq);
  167. }
  168. static void mmc_wait_done(struct mmc_request *mrq)
  169. {
  170. complete(mrq->done_data);
  171. }
  172. /**
  173. * mmc_wait_for_req - start a request and wait for completion
  174. * @host: MMC host to start command
  175. * @mrq: MMC request to start
  176. *
  177. * Start a new MMC custom command request for a host, and wait
  178. * for the command to complete. Does not attempt to parse the
  179. * response.
  180. */
  181. void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
  182. {
  183. DECLARE_COMPLETION_ONSTACK(complete);
  184. mrq->done_data = &complete;
  185. mrq->done = mmc_wait_done;
  186. mmc_start_request(host, mrq);
  187. wait_for_completion(&complete);
  188. }
  189. EXPORT_SYMBOL(mmc_wait_for_req);
  190. /**
  191. * mmc_wait_for_cmd - start a command and wait for completion
  192. * @host: MMC host to start command
  193. * @cmd: MMC command to start
  194. * @retries: maximum number of retries
  195. *
  196. * Start a new MMC command for a host, and wait for the command
  197. * to complete. Return any error that occurred while the command
  198. * was executing. Do not attempt to parse the response.
  199. */
  200. int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
  201. {
  202. struct mmc_request mrq;
  203. WARN_ON(!host->claimed);
  204. memset(&mrq, 0, sizeof(struct mmc_request));
  205. memset(cmd->resp, 0, sizeof(cmd->resp));
  206. cmd->retries = retries;
  207. mrq.cmd = cmd;
  208. cmd->data = NULL;
  209. mmc_wait_for_req(host, &mrq);
  210. return cmd->error;
  211. }
  212. EXPORT_SYMBOL(mmc_wait_for_cmd);
  213. /**
  214. * mmc_set_data_timeout - set the timeout for a data command
  215. * @data: data phase for command
  216. * @card: the MMC card associated with the data transfer
  217. *
  218. * Computes the data timeout parameters according to the
  219. * correct algorithm given the card type.
  220. */
  221. void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
  222. {
  223. unsigned int mult;
  224. /*
  225. * SDIO cards only define an upper 1 s limit on access.
  226. */
  227. if (mmc_card_sdio(card)) {
  228. data->timeout_ns = 1000000000;
  229. data->timeout_clks = 0;
  230. return;
  231. }
  232. /*
  233. * SD cards use a 100 multiplier rather than 10
  234. */
  235. mult = mmc_card_sd(card) ? 100 : 10;
  236. /*
  237. * Scale up the multiplier (and therefore the timeout) by
  238. * the r2w factor for writes.
  239. */
  240. if (data->flags & MMC_DATA_WRITE)
  241. mult <<= card->csd.r2w_factor;
  242. data->timeout_ns = card->csd.tacc_ns * mult;
  243. data->timeout_clks = card->csd.tacc_clks * mult;
  244. /*
  245. * SD cards also have an upper limit on the timeout.
  246. */
  247. if (mmc_card_sd(card)) {
  248. unsigned int timeout_us, limit_us;
  249. timeout_us = data->timeout_ns / 1000;
  250. timeout_us += data->timeout_clks * 1000 /
  251. (card->host->ios.clock / 1000);
  252. if (data->flags & MMC_DATA_WRITE)
  253. /*
  254. * The limit is really 250 ms, but that is
  255. * insufficient for some crappy cards.
  256. */
  257. limit_us = 300000;
  258. else
  259. limit_us = 100000;
  260. /*
  261. * SDHC cards always use these fixed values.
  262. */
  263. if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
  264. data->timeout_ns = limit_us * 1000;
  265. data->timeout_clks = 0;
  266. }
  267. }
  268. /*
  269. * Some cards need very high timeouts if driven in SPI mode.
  270. * The worst observed timeout was 900ms after writing a
  271. * continuous stream of data until the internal logic
  272. * overflowed.
  273. */
  274. if (mmc_host_is_spi(card->host)) {
  275. if (data->flags & MMC_DATA_WRITE) {
  276. if (data->timeout_ns < 1000000000)
  277. data->timeout_ns = 1000000000; /* 1s */
  278. } else {
  279. if (data->timeout_ns < 100000000)
  280. data->timeout_ns = 100000000; /* 100ms */
  281. }
  282. }
  283. }
  284. EXPORT_SYMBOL(mmc_set_data_timeout);
  285. /**
  286. * mmc_align_data_size - pads a transfer size to a more optimal value
  287. * @card: the MMC card associated with the data transfer
  288. * @sz: original transfer size
  289. *
  290. * Pads the original data size with a number of extra bytes in
  291. * order to avoid controller bugs and/or performance hits
  292. * (e.g. some controllers revert to PIO for certain sizes).
  293. *
  294. * Returns the improved size, which might be unmodified.
  295. *
  296. * Note that this function is only relevant when issuing a
  297. * single scatter gather entry.
  298. */
  299. unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
  300. {
  301. /*
  302. * FIXME: We don't have a system for the controller to tell
  303. * the core about its problems yet, so for now we just 32-bit
  304. * align the size.
  305. */
  306. sz = ((sz + 3) / 4) * 4;
  307. return sz;
  308. }
  309. EXPORT_SYMBOL(mmc_align_data_size);
  310. /**
  311. * mmc_host_enable - enable a host.
  312. * @host: mmc host to enable
  313. *
  314. * Hosts that support power saving can use the 'enable' and 'disable'
  315. * methods to exit and enter power saving states. For more information
  316. * see comments for struct mmc_host_ops.
  317. */
  318. int mmc_host_enable(struct mmc_host *host)
  319. {
  320. if (!(host->caps & MMC_CAP_DISABLE))
  321. return 0;
  322. if (host->en_dis_recurs)
  323. return 0;
  324. if (host->nesting_cnt++)
  325. return 0;
  326. cancel_delayed_work_sync(&host->disable);
  327. if (host->enabled)
  328. return 0;
  329. if (host->ops->enable) {
  330. int err;
  331. host->en_dis_recurs = 1;
  332. err = host->ops->enable(host);
  333. host->en_dis_recurs = 0;
  334. if (err) {
  335. pr_debug("%s: enable error %d\n",
  336. mmc_hostname(host), err);
  337. return err;
  338. }
  339. }
  340. host->enabled = 1;
  341. return 0;
  342. }
  343. EXPORT_SYMBOL(mmc_host_enable);
  344. static int mmc_host_do_disable(struct mmc_host *host, int lazy)
  345. {
  346. if (host->ops->disable) {
  347. int err;
  348. host->en_dis_recurs = 1;
  349. err = host->ops->disable(host, lazy);
  350. host->en_dis_recurs = 0;
  351. if (err < 0) {
  352. pr_debug("%s: disable error %d\n",
  353. mmc_hostname(host), err);
  354. return err;
  355. }
  356. if (err > 0) {
  357. unsigned long delay = msecs_to_jiffies(err);
  358. mmc_schedule_delayed_work(&host->disable, delay);
  359. }
  360. }
  361. host->enabled = 0;
  362. return 0;
  363. }
  364. /**
  365. * mmc_host_disable - disable a host.
  366. * @host: mmc host to disable
  367. *
  368. * Hosts that support power saving can use the 'enable' and 'disable'
  369. * methods to exit and enter power saving states. For more information
  370. * see comments for struct mmc_host_ops.
  371. */
  372. int mmc_host_disable(struct mmc_host *host)
  373. {
  374. int err;
  375. if (!(host->caps & MMC_CAP_DISABLE))
  376. return 0;
  377. if (host->en_dis_recurs)
  378. return 0;
  379. if (--host->nesting_cnt)
  380. return 0;
  381. if (!host->enabled)
  382. return 0;
  383. err = mmc_host_do_disable(host, 0);
  384. return err;
  385. }
  386. EXPORT_SYMBOL(mmc_host_disable);
  387. /**
  388. * __mmc_claim_host - exclusively claim a host
  389. * @host: mmc host to claim
  390. * @abort: whether or not the operation should be aborted
  391. *
  392. * Claim a host for a set of operations. If @abort is non null and
  393. * dereference a non-zero value then this will return prematurely with
  394. * that non-zero value without acquiring the lock. Returns zero
  395. * with the lock held otherwise.
  396. */
  397. int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
  398. {
  399. DECLARE_WAITQUEUE(wait, current);
  400. unsigned long flags;
  401. int stop;
  402. might_sleep();
  403. add_wait_queue(&host->wq, &wait);
  404. spin_lock_irqsave(&host->lock, flags);
  405. while (1) {
  406. set_current_state(TASK_UNINTERRUPTIBLE);
  407. stop = abort ? atomic_read(abort) : 0;
  408. if (stop || !host->claimed || host->claimer == current)
  409. break;
  410. spin_unlock_irqrestore(&host->lock, flags);
  411. schedule();
  412. spin_lock_irqsave(&host->lock, flags);
  413. }
  414. set_current_state(TASK_RUNNING);
  415. if (!stop) {
  416. host->claimed = 1;
  417. host->claimer = current;
  418. host->claim_cnt += 1;
  419. } else
  420. wake_up(&host->wq);
  421. spin_unlock_irqrestore(&host->lock, flags);
  422. remove_wait_queue(&host->wq, &wait);
  423. if (!stop)
  424. mmc_host_enable(host);
  425. return stop;
  426. }
  427. EXPORT_SYMBOL(__mmc_claim_host);
  428. /**
  429. * mmc_try_claim_host - try exclusively to claim a host
  430. * @host: mmc host to claim
  431. *
  432. * Returns %1 if the host is claimed, %0 otherwise.
  433. */
  434. int mmc_try_claim_host(struct mmc_host *host)
  435. {
  436. int claimed_host = 0;
  437. unsigned long flags;
  438. spin_lock_irqsave(&host->lock, flags);
  439. if (!host->claimed || host->claimer == current) {
  440. host->claimed = 1;
  441. host->claimer = current;
  442. host->claim_cnt += 1;
  443. claimed_host = 1;
  444. }
  445. spin_unlock_irqrestore(&host->lock, flags);
  446. return claimed_host;
  447. }
  448. EXPORT_SYMBOL(mmc_try_claim_host);
  449. static void mmc_do_release_host(struct mmc_host *host)
  450. {
  451. unsigned long flags;
  452. spin_lock_irqsave(&host->lock, flags);
  453. if (--host->claim_cnt) {
  454. /* Release for nested claim */
  455. spin_unlock_irqrestore(&host->lock, flags);
  456. } else {
  457. host->claimed = 0;
  458. host->claimer = NULL;
  459. spin_unlock_irqrestore(&host->lock, flags);
  460. wake_up(&host->wq);
  461. }
  462. }
  463. void mmc_host_deeper_disable(struct work_struct *work)
  464. {
  465. struct mmc_host *host =
  466. container_of(work, struct mmc_host, disable.work);
  467. /* If the host is claimed then we do not want to disable it anymore */
  468. if (!mmc_try_claim_host(host))
  469. return;
  470. mmc_host_do_disable(host, 1);
  471. mmc_do_release_host(host);
  472. }
  473. /**
  474. * mmc_host_lazy_disable - lazily disable a host.
  475. * @host: mmc host to disable
  476. *
  477. * Hosts that support power saving can use the 'enable' and 'disable'
  478. * methods to exit and enter power saving states. For more information
  479. * see comments for struct mmc_host_ops.
  480. */
  481. int mmc_host_lazy_disable(struct mmc_host *host)
  482. {
  483. if (!(host->caps & MMC_CAP_DISABLE))
  484. return 0;
  485. if (host->en_dis_recurs)
  486. return 0;
  487. if (--host->nesting_cnt)
  488. return 0;
  489. if (!host->enabled)
  490. return 0;
  491. if (host->disable_delay) {
  492. mmc_schedule_delayed_work(&host->disable,
  493. msecs_to_jiffies(host->disable_delay));
  494. return 0;
  495. } else
  496. return mmc_host_do_disable(host, 1);
  497. }
  498. EXPORT_SYMBOL(mmc_host_lazy_disable);
  499. /**
  500. * mmc_release_host - release a host
  501. * @host: mmc host to release
  502. *
  503. * Release a MMC host, allowing others to claim the host
  504. * for their operations.
  505. */
  506. void mmc_release_host(struct mmc_host *host)
  507. {
  508. WARN_ON(!host->claimed);
  509. mmc_host_lazy_disable(host);
  510. mmc_do_release_host(host);
  511. }
  512. EXPORT_SYMBOL(mmc_release_host);
  513. /*
  514. * Internal function that does the actual ios call to the host driver,
  515. * optionally printing some debug output.
  516. */
  517. static inline void mmc_set_ios(struct mmc_host *host)
  518. {
  519. struct mmc_ios *ios = &host->ios;
  520. pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
  521. "width %u timing %u\n",
  522. mmc_hostname(host), ios->clock, ios->bus_mode,
  523. ios->power_mode, ios->chip_select, ios->vdd,
  524. ios->bus_width, ios->timing);
  525. host->ops->set_ios(host, ios);
  526. }
  527. /*
  528. * Control chip select pin on a host.
  529. */
  530. void mmc_set_chip_select(struct mmc_host *host, int mode)
  531. {
  532. host->ios.chip_select = mode;
  533. mmc_set_ios(host);
  534. }
  535. /*
  536. * Sets the host clock to the highest possible frequency that
  537. * is below "hz".
  538. */
  539. void mmc_set_clock(struct mmc_host *host, unsigned int hz)
  540. {
  541. WARN_ON(hz < host->f_min);
  542. if (hz > host->f_max)
  543. hz = host->f_max;
  544. host->ios.clock = hz;
  545. mmc_set_ios(host);
  546. }
  547. /*
  548. * Change the bus mode (open drain/push-pull) of a host.
  549. */
  550. void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
  551. {
  552. host->ios.bus_mode = mode;
  553. mmc_set_ios(host);
  554. }
  555. /*
  556. * Change data bus width of a host.
  557. */
  558. void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
  559. {
  560. host->ios.bus_width = width;
  561. mmc_set_ios(host);
  562. }
  563. /**
  564. * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
  565. * @vdd: voltage (mV)
  566. * @low_bits: prefer low bits in boundary cases
  567. *
  568. * This function returns the OCR bit number according to the provided @vdd
  569. * value. If conversion is not possible a negative errno value returned.
  570. *
  571. * Depending on the @low_bits flag the function prefers low or high OCR bits
  572. * on boundary voltages. For example,
  573. * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
  574. * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
  575. *
  576. * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
  577. */
  578. static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
  579. {
  580. const int max_bit = ilog2(MMC_VDD_35_36);
  581. int bit;
  582. if (vdd < 1650 || vdd > 3600)
  583. return -EINVAL;
  584. if (vdd >= 1650 && vdd <= 1950)
  585. return ilog2(MMC_VDD_165_195);
  586. if (low_bits)
  587. vdd -= 1;
  588. /* Base 2000 mV, step 100 mV, bit's base 8. */
  589. bit = (vdd - 2000) / 100 + 8;
  590. if (bit > max_bit)
  591. return max_bit;
  592. return bit;
  593. }
  594. /**
  595. * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
  596. * @vdd_min: minimum voltage value (mV)
  597. * @vdd_max: maximum voltage value (mV)
  598. *
  599. * This function returns the OCR mask bits according to the provided @vdd_min
  600. * and @vdd_max values. If conversion is not possible the function returns 0.
  601. *
  602. * Notes wrt boundary cases:
  603. * This function sets the OCR bits for all boundary voltages, for example
  604. * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
  605. * MMC_VDD_34_35 mask.
  606. */
  607. u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
  608. {
  609. u32 mask = 0;
  610. if (vdd_max < vdd_min)
  611. return 0;
  612. /* Prefer high bits for the boundary vdd_max values. */
  613. vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
  614. if (vdd_max < 0)
  615. return 0;
  616. /* Prefer low bits for the boundary vdd_min values. */
  617. vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
  618. if (vdd_min < 0)
  619. return 0;
  620. /* Fill the mask, from max bit to min bit. */
  621. while (vdd_max >= vdd_min)
  622. mask |= 1 << vdd_max--;
  623. return mask;
  624. }
  625. EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
  626. #ifdef CONFIG_REGULATOR
  627. /**
  628. * mmc_regulator_get_ocrmask - return mask of supported voltages
  629. * @supply: regulator to use
  630. *
  631. * This returns either a negative errno, or a mask of voltages that
  632. * can be provided to MMC/SD/SDIO devices using the specified voltage
  633. * regulator. This would normally be called before registering the
  634. * MMC host adapter.
  635. */
  636. int mmc_regulator_get_ocrmask(struct regulator *supply)
  637. {
  638. int result = 0;
  639. int count;
  640. int i;
  641. count = regulator_count_voltages(supply);
  642. if (count < 0)
  643. return count;
  644. for (i = 0; i < count; i++) {
  645. int vdd_uV;
  646. int vdd_mV;
  647. vdd_uV = regulator_list_voltage(supply, i);
  648. if (vdd_uV <= 0)
  649. continue;
  650. vdd_mV = vdd_uV / 1000;
  651. result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
  652. }
  653. return result;
  654. }
  655. EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
  656. /**
  657. * mmc_regulator_set_ocr - set regulator to match host->ios voltage
  658. * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
  659. * @supply: regulator to use
  660. *
  661. * Returns zero on success, else negative errno.
  662. *
  663. * MMC host drivers may use this to enable or disable a regulator using
  664. * a particular supply voltage. This would normally be called from the
  665. * set_ios() method.
  666. */
  667. int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit)
  668. {
  669. int result = 0;
  670. int min_uV, max_uV;
  671. int enabled;
  672. enabled = regulator_is_enabled(supply);
  673. if (enabled < 0)
  674. return enabled;
  675. if (vdd_bit) {
  676. int tmp;
  677. int voltage;
  678. /* REVISIT mmc_vddrange_to_ocrmask() may have set some
  679. * bits this regulator doesn't quite support ... don't
  680. * be too picky, most cards and regulators are OK with
  681. * a 0.1V range goof (it's a small error percentage).
  682. */
  683. tmp = vdd_bit - ilog2(MMC_VDD_165_195);
  684. if (tmp == 0) {
  685. min_uV = 1650 * 1000;
  686. max_uV = 1950 * 1000;
  687. } else {
  688. min_uV = 1900 * 1000 + tmp * 100 * 1000;
  689. max_uV = min_uV + 100 * 1000;
  690. }
  691. /* avoid needless changes to this voltage; the regulator
  692. * might not allow this operation
  693. */
  694. voltage = regulator_get_voltage(supply);
  695. if (voltage < 0)
  696. result = voltage;
  697. else if (voltage < min_uV || voltage > max_uV)
  698. result = regulator_set_voltage(supply, min_uV, max_uV);
  699. else
  700. result = 0;
  701. if (result == 0 && !enabled)
  702. result = regulator_enable(supply);
  703. } else if (enabled) {
  704. result = regulator_disable(supply);
  705. }
  706. return result;
  707. }
  708. EXPORT_SYMBOL(mmc_regulator_set_ocr);
  709. #endif
  710. /*
  711. * Mask off any voltages we don't support and select
  712. * the lowest voltage
  713. */
  714. u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
  715. {
  716. int bit;
  717. ocr &= host->ocr_avail;
  718. bit = ffs(ocr);
  719. if (bit) {
  720. bit -= 1;
  721. ocr &= 3 << bit;
  722. host->ios.vdd = bit;
  723. mmc_set_ios(host);
  724. } else {
  725. pr_warning("%s: host doesn't support card's voltages\n",
  726. mmc_hostname(host));
  727. ocr = 0;
  728. }
  729. return ocr;
  730. }
  731. /*
  732. * Select timing parameters for host.
  733. */
  734. void mmc_set_timing(struct mmc_host *host, unsigned int timing)
  735. {
  736. host->ios.timing = timing;
  737. mmc_set_ios(host);
  738. }
  739. /*
  740. * Apply power to the MMC stack. This is a two-stage process.
  741. * First, we enable power to the card without the clock running.
  742. * We then wait a bit for the power to stabilise. Finally,
  743. * enable the bus drivers and clock to the card.
  744. *
  745. * We must _NOT_ enable the clock prior to power stablising.
  746. *
  747. * If a host does all the power sequencing itself, ignore the
  748. * initial MMC_POWER_UP stage.
  749. */
  750. static void mmc_power_up(struct mmc_host *host)
  751. {
  752. int bit;
  753. /* If ocr is set, we use it */
  754. if (host->ocr)
  755. bit = ffs(host->ocr) - 1;
  756. else
  757. bit = fls(host->ocr_avail) - 1;
  758. host->ios.vdd = bit;
  759. if (mmc_host_is_spi(host)) {
  760. host->ios.chip_select = MMC_CS_HIGH;
  761. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  762. } else {
  763. host->ios.chip_select = MMC_CS_DONTCARE;
  764. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  765. }
  766. host->ios.power_mode = MMC_POWER_UP;
  767. host->ios.bus_width = MMC_BUS_WIDTH_1;
  768. host->ios.timing = MMC_TIMING_LEGACY;
  769. mmc_set_ios(host);
  770. /*
  771. * This delay should be sufficient to allow the power supply
  772. * to reach the minimum voltage.
  773. */
  774. mmc_delay(10);
  775. if (host->f_min > 400000) {
  776. pr_warning("%s: Minimum clock frequency too high for "
  777. "identification mode\n", mmc_hostname(host));
  778. host->ios.clock = host->f_min;
  779. } else
  780. host->ios.clock = 400000;
  781. host->ios.power_mode = MMC_POWER_ON;
  782. mmc_set_ios(host);
  783. /*
  784. * This delay must be at least 74 clock sizes, or 1 ms, or the
  785. * time required to reach a stable voltage.
  786. */
  787. mmc_delay(10);
  788. }
  789. static void mmc_power_off(struct mmc_host *host)
  790. {
  791. host->ios.clock = 0;
  792. host->ios.vdd = 0;
  793. if (!mmc_host_is_spi(host)) {
  794. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  795. host->ios.chip_select = MMC_CS_DONTCARE;
  796. }
  797. host->ios.power_mode = MMC_POWER_OFF;
  798. host->ios.bus_width = MMC_BUS_WIDTH_1;
  799. host->ios.timing = MMC_TIMING_LEGACY;
  800. mmc_set_ios(host);
  801. }
  802. /*
  803. * Cleanup when the last reference to the bus operator is dropped.
  804. */
  805. static void __mmc_release_bus(struct mmc_host *host)
  806. {
  807. BUG_ON(!host);
  808. BUG_ON(host->bus_refs);
  809. BUG_ON(!host->bus_dead);
  810. host->bus_ops = NULL;
  811. }
  812. /*
  813. * Increase reference count of bus operator
  814. */
  815. static inline void mmc_bus_get(struct mmc_host *host)
  816. {
  817. unsigned long flags;
  818. spin_lock_irqsave(&host->lock, flags);
  819. host->bus_refs++;
  820. spin_unlock_irqrestore(&host->lock, flags);
  821. }
  822. /*
  823. * Decrease reference count of bus operator and free it if
  824. * it is the last reference.
  825. */
  826. static inline void mmc_bus_put(struct mmc_host *host)
  827. {
  828. unsigned long flags;
  829. spin_lock_irqsave(&host->lock, flags);
  830. host->bus_refs--;
  831. if ((host->bus_refs == 0) && host->bus_ops)
  832. __mmc_release_bus(host);
  833. spin_unlock_irqrestore(&host->lock, flags);
  834. }
  835. /*
  836. * Assign a mmc bus handler to a host. Only one bus handler may control a
  837. * host at any given time.
  838. */
  839. void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
  840. {
  841. unsigned long flags;
  842. BUG_ON(!host);
  843. BUG_ON(!ops);
  844. WARN_ON(!host->claimed);
  845. spin_lock_irqsave(&host->lock, flags);
  846. BUG_ON(host->bus_ops);
  847. BUG_ON(host->bus_refs);
  848. host->bus_ops = ops;
  849. host->bus_refs = 1;
  850. host->bus_dead = 0;
  851. spin_unlock_irqrestore(&host->lock, flags);
  852. }
  853. /*
  854. * Remove the current bus handler from a host. Assumes that there are
  855. * no interesting cards left, so the bus is powered down.
  856. */
  857. void mmc_detach_bus(struct mmc_host *host)
  858. {
  859. unsigned long flags;
  860. BUG_ON(!host);
  861. WARN_ON(!host->claimed);
  862. WARN_ON(!host->bus_ops);
  863. spin_lock_irqsave(&host->lock, flags);
  864. host->bus_dead = 1;
  865. spin_unlock_irqrestore(&host->lock, flags);
  866. mmc_power_off(host);
  867. mmc_bus_put(host);
  868. }
  869. /**
  870. * mmc_detect_change - process change of state on a MMC socket
  871. * @host: host which changed state.
  872. * @delay: optional delay to wait before detection (jiffies)
  873. *
  874. * MMC drivers should call this when they detect a card has been
  875. * inserted or removed. The MMC layer will confirm that any
  876. * present card is still functional, and initialize any newly
  877. * inserted.
  878. */
  879. void mmc_detect_change(struct mmc_host *host, unsigned long delay)
  880. {
  881. #ifdef CONFIG_MMC_DEBUG
  882. unsigned long flags;
  883. spin_lock_irqsave(&host->lock, flags);
  884. WARN_ON(host->removed);
  885. spin_unlock_irqrestore(&host->lock, flags);
  886. #endif
  887. mmc_schedule_delayed_work(&host->detect, delay);
  888. }
  889. EXPORT_SYMBOL(mmc_detect_change);
  890. void mmc_rescan(struct work_struct *work)
  891. {
  892. struct mmc_host *host =
  893. container_of(work, struct mmc_host, detect.work);
  894. u32 ocr;
  895. int err;
  896. mmc_bus_get(host);
  897. /* if there is a card registered, check whether it is still present */
  898. if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
  899. host->bus_ops->detect(host);
  900. mmc_bus_put(host);
  901. mmc_bus_get(host);
  902. /* if there still is a card present, stop here */
  903. if (host->bus_ops != NULL) {
  904. mmc_bus_put(host);
  905. goto out;
  906. }
  907. /* detect a newly inserted card */
  908. /*
  909. * Only we can add a new handler, so it's safe to
  910. * release the lock here.
  911. */
  912. mmc_bus_put(host);
  913. if (host->ops->get_cd && host->ops->get_cd(host) == 0)
  914. goto out;
  915. mmc_claim_host(host);
  916. mmc_power_up(host);
  917. mmc_go_idle(host);
  918. mmc_send_if_cond(host, host->ocr_avail);
  919. /*
  920. * First we search for SDIO...
  921. */
  922. err = mmc_send_io_op_cond(host, 0, &ocr);
  923. if (!err) {
  924. if (mmc_attach_sdio(host, ocr))
  925. mmc_power_off(host);
  926. goto out;
  927. }
  928. /*
  929. * ...then normal SD...
  930. */
  931. err = mmc_send_app_op_cond(host, 0, &ocr);
  932. if (!err) {
  933. if (mmc_attach_sd(host, ocr))
  934. mmc_power_off(host);
  935. goto out;
  936. }
  937. /*
  938. * ...and finally MMC.
  939. */
  940. err = mmc_send_op_cond(host, 0, &ocr);
  941. if (!err) {
  942. if (mmc_attach_mmc(host, ocr))
  943. mmc_power_off(host);
  944. goto out;
  945. }
  946. mmc_release_host(host);
  947. mmc_power_off(host);
  948. out:
  949. if (host->caps & MMC_CAP_NEEDS_POLL)
  950. mmc_schedule_delayed_work(&host->detect, HZ);
  951. }
  952. void mmc_start_host(struct mmc_host *host)
  953. {
  954. mmc_power_off(host);
  955. mmc_detect_change(host, 0);
  956. }
  957. void mmc_stop_host(struct mmc_host *host)
  958. {
  959. #ifdef CONFIG_MMC_DEBUG
  960. unsigned long flags;
  961. spin_lock_irqsave(&host->lock, flags);
  962. host->removed = 1;
  963. spin_unlock_irqrestore(&host->lock, flags);
  964. #endif
  965. if (host->caps & MMC_CAP_DISABLE)
  966. cancel_delayed_work(&host->disable);
  967. cancel_delayed_work(&host->detect);
  968. mmc_flush_scheduled_work();
  969. mmc_bus_get(host);
  970. if (host->bus_ops && !host->bus_dead) {
  971. if (host->bus_ops->remove)
  972. host->bus_ops->remove(host);
  973. mmc_claim_host(host);
  974. mmc_detach_bus(host);
  975. mmc_release_host(host);
  976. mmc_bus_put(host);
  977. return;
  978. }
  979. mmc_bus_put(host);
  980. BUG_ON(host->card);
  981. mmc_power_off(host);
  982. }
  983. void mmc_power_save_host(struct mmc_host *host)
  984. {
  985. mmc_bus_get(host);
  986. if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
  987. mmc_bus_put(host);
  988. return;
  989. }
  990. if (host->bus_ops->power_save)
  991. host->bus_ops->power_save(host);
  992. mmc_bus_put(host);
  993. mmc_power_off(host);
  994. }
  995. EXPORT_SYMBOL(mmc_power_save_host);
  996. void mmc_power_restore_host(struct mmc_host *host)
  997. {
  998. mmc_bus_get(host);
  999. if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
  1000. mmc_bus_put(host);
  1001. return;
  1002. }
  1003. mmc_power_up(host);
  1004. host->bus_ops->power_restore(host);
  1005. mmc_bus_put(host);
  1006. }
  1007. EXPORT_SYMBOL(mmc_power_restore_host);
  1008. int mmc_card_awake(struct mmc_host *host)
  1009. {
  1010. int err = -ENOSYS;
  1011. mmc_bus_get(host);
  1012. if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
  1013. err = host->bus_ops->awake(host);
  1014. mmc_bus_put(host);
  1015. return err;
  1016. }
  1017. EXPORT_SYMBOL(mmc_card_awake);
  1018. int mmc_card_sleep(struct mmc_host *host)
  1019. {
  1020. int err = -ENOSYS;
  1021. mmc_bus_get(host);
  1022. if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
  1023. err = host->bus_ops->sleep(host);
  1024. mmc_bus_put(host);
  1025. return err;
  1026. }
  1027. EXPORT_SYMBOL(mmc_card_sleep);
  1028. int mmc_card_can_sleep(struct mmc_host *host)
  1029. {
  1030. struct mmc_card *card = host->card;
  1031. if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
  1032. return 1;
  1033. return 0;
  1034. }
  1035. EXPORT_SYMBOL(mmc_card_can_sleep);
  1036. #ifdef CONFIG_PM
  1037. /**
  1038. * mmc_suspend_host - suspend a host
  1039. * @host: mmc host
  1040. * @state: suspend mode (PM_SUSPEND_xxx)
  1041. */
  1042. int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
  1043. {
  1044. int err = 0;
  1045. if (host->caps & MMC_CAP_DISABLE)
  1046. cancel_delayed_work(&host->disable);
  1047. cancel_delayed_work(&host->detect);
  1048. mmc_flush_scheduled_work();
  1049. mmc_bus_get(host);
  1050. if (host->bus_ops && !host->bus_dead) {
  1051. if (host->bus_ops->suspend)
  1052. err = host->bus_ops->suspend(host);
  1053. if (err == -ENOSYS || !host->bus_ops->resume) {
  1054. /*
  1055. * We simply "remove" the card in this case.
  1056. * It will be redetected on resume.
  1057. */
  1058. if (host->bus_ops->remove)
  1059. host->bus_ops->remove(host);
  1060. mmc_claim_host(host);
  1061. mmc_detach_bus(host);
  1062. mmc_release_host(host);
  1063. err = 0;
  1064. }
  1065. }
  1066. mmc_bus_put(host);
  1067. if (!err)
  1068. mmc_power_off(host);
  1069. return err;
  1070. }
  1071. EXPORT_SYMBOL(mmc_suspend_host);
  1072. /**
  1073. * mmc_resume_host - resume a previously suspended host
  1074. * @host: mmc host
  1075. */
  1076. int mmc_resume_host(struct mmc_host *host)
  1077. {
  1078. int err = 0;
  1079. mmc_bus_get(host);
  1080. if (host->bus_ops && !host->bus_dead) {
  1081. mmc_power_up(host);
  1082. mmc_select_voltage(host, host->ocr);
  1083. BUG_ON(!host->bus_ops->resume);
  1084. err = host->bus_ops->resume(host);
  1085. if (err) {
  1086. printk(KERN_WARNING "%s: error %d during resume "
  1087. "(card was removed?)\n",
  1088. mmc_hostname(host), err);
  1089. if (host->bus_ops->remove)
  1090. host->bus_ops->remove(host);
  1091. mmc_claim_host(host);
  1092. mmc_detach_bus(host);
  1093. mmc_release_host(host);
  1094. /* no need to bother upper layers */
  1095. err = 0;
  1096. }
  1097. }
  1098. mmc_bus_put(host);
  1099. /*
  1100. * We add a slight delay here so that resume can progress
  1101. * in parallel.
  1102. */
  1103. mmc_detect_change(host, 1);
  1104. return err;
  1105. }
  1106. EXPORT_SYMBOL(mmc_resume_host);
  1107. #endif
  1108. static int __init mmc_init(void)
  1109. {
  1110. int ret;
  1111. workqueue = create_singlethread_workqueue("kmmcd");
  1112. if (!workqueue)
  1113. return -ENOMEM;
  1114. ret = mmc_register_bus();
  1115. if (ret)
  1116. goto destroy_workqueue;
  1117. ret = mmc_register_host_class();
  1118. if (ret)
  1119. goto unregister_bus;
  1120. ret = sdio_register_bus();
  1121. if (ret)
  1122. goto unregister_host_class;
  1123. return 0;
  1124. unregister_host_class:
  1125. mmc_unregister_host_class();
  1126. unregister_bus:
  1127. mmc_unregister_bus();
  1128. destroy_workqueue:
  1129. destroy_workqueue(workqueue);
  1130. return ret;
  1131. }
  1132. static void __exit mmc_exit(void)
  1133. {
  1134. sdio_unregister_bus();
  1135. mmc_unregister_host_class();
  1136. mmc_unregister_bus();
  1137. destroy_workqueue(workqueue);
  1138. }
  1139. subsys_initcall(mmc_init);
  1140. module_exit(mmc_exit);
  1141. MODULE_LICENSE("GPL");