core.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  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. sdio_reset(host);
  918. mmc_go_idle(host);
  919. mmc_send_if_cond(host, host->ocr_avail);
  920. /*
  921. * First we search for SDIO...
  922. */
  923. err = mmc_send_io_op_cond(host, 0, &ocr);
  924. if (!err) {
  925. if (mmc_attach_sdio(host, ocr))
  926. mmc_power_off(host);
  927. goto out;
  928. }
  929. /*
  930. * ...then normal SD...
  931. */
  932. err = mmc_send_app_op_cond(host, 0, &ocr);
  933. if (!err) {
  934. if (mmc_attach_sd(host, ocr))
  935. mmc_power_off(host);
  936. goto out;
  937. }
  938. /*
  939. * ...and finally MMC.
  940. */
  941. err = mmc_send_op_cond(host, 0, &ocr);
  942. if (!err) {
  943. if (mmc_attach_mmc(host, ocr))
  944. mmc_power_off(host);
  945. goto out;
  946. }
  947. mmc_release_host(host);
  948. mmc_power_off(host);
  949. out:
  950. if (host->caps & MMC_CAP_NEEDS_POLL)
  951. mmc_schedule_delayed_work(&host->detect, HZ);
  952. }
  953. void mmc_start_host(struct mmc_host *host)
  954. {
  955. mmc_power_off(host);
  956. mmc_detect_change(host, 0);
  957. }
  958. void mmc_stop_host(struct mmc_host *host)
  959. {
  960. #ifdef CONFIG_MMC_DEBUG
  961. unsigned long flags;
  962. spin_lock_irqsave(&host->lock, flags);
  963. host->removed = 1;
  964. spin_unlock_irqrestore(&host->lock, flags);
  965. #endif
  966. if (host->caps & MMC_CAP_DISABLE)
  967. cancel_delayed_work(&host->disable);
  968. cancel_delayed_work(&host->detect);
  969. mmc_flush_scheduled_work();
  970. /* clear pm flags now and let card drivers set them as needed */
  971. host->pm_flags = 0;
  972. mmc_bus_get(host);
  973. if (host->bus_ops && !host->bus_dead) {
  974. if (host->bus_ops->remove)
  975. host->bus_ops->remove(host);
  976. mmc_claim_host(host);
  977. mmc_detach_bus(host);
  978. mmc_release_host(host);
  979. mmc_bus_put(host);
  980. return;
  981. }
  982. mmc_bus_put(host);
  983. BUG_ON(host->card);
  984. mmc_power_off(host);
  985. }
  986. void mmc_power_save_host(struct mmc_host *host)
  987. {
  988. mmc_bus_get(host);
  989. if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
  990. mmc_bus_put(host);
  991. return;
  992. }
  993. if (host->bus_ops->power_save)
  994. host->bus_ops->power_save(host);
  995. mmc_bus_put(host);
  996. mmc_power_off(host);
  997. }
  998. EXPORT_SYMBOL(mmc_power_save_host);
  999. void mmc_power_restore_host(struct mmc_host *host)
  1000. {
  1001. mmc_bus_get(host);
  1002. if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
  1003. mmc_bus_put(host);
  1004. return;
  1005. }
  1006. mmc_power_up(host);
  1007. host->bus_ops->power_restore(host);
  1008. mmc_bus_put(host);
  1009. }
  1010. EXPORT_SYMBOL(mmc_power_restore_host);
  1011. int mmc_card_awake(struct mmc_host *host)
  1012. {
  1013. int err = -ENOSYS;
  1014. mmc_bus_get(host);
  1015. if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
  1016. err = host->bus_ops->awake(host);
  1017. mmc_bus_put(host);
  1018. return err;
  1019. }
  1020. EXPORT_SYMBOL(mmc_card_awake);
  1021. int mmc_card_sleep(struct mmc_host *host)
  1022. {
  1023. int err = -ENOSYS;
  1024. mmc_bus_get(host);
  1025. if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
  1026. err = host->bus_ops->sleep(host);
  1027. mmc_bus_put(host);
  1028. return err;
  1029. }
  1030. EXPORT_SYMBOL(mmc_card_sleep);
  1031. int mmc_card_can_sleep(struct mmc_host *host)
  1032. {
  1033. struct mmc_card *card = host->card;
  1034. if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
  1035. return 1;
  1036. return 0;
  1037. }
  1038. EXPORT_SYMBOL(mmc_card_can_sleep);
  1039. #ifdef CONFIG_PM
  1040. /**
  1041. * mmc_suspend_host - suspend a host
  1042. * @host: mmc host
  1043. * @state: suspend mode (PM_SUSPEND_xxx)
  1044. */
  1045. int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
  1046. {
  1047. int err = 0;
  1048. if (host->caps & MMC_CAP_DISABLE)
  1049. cancel_delayed_work(&host->disable);
  1050. cancel_delayed_work(&host->detect);
  1051. mmc_flush_scheduled_work();
  1052. mmc_bus_get(host);
  1053. if (host->bus_ops && !host->bus_dead) {
  1054. if (host->bus_ops->suspend)
  1055. err = host->bus_ops->suspend(host);
  1056. if (err == -ENOSYS || !host->bus_ops->resume) {
  1057. /*
  1058. * We simply "remove" the card in this case.
  1059. * It will be redetected on resume.
  1060. */
  1061. if (host->bus_ops->remove)
  1062. host->bus_ops->remove(host);
  1063. mmc_claim_host(host);
  1064. mmc_detach_bus(host);
  1065. mmc_release_host(host);
  1066. host->pm_flags = 0;
  1067. err = 0;
  1068. }
  1069. }
  1070. mmc_bus_put(host);
  1071. if (!err && !(host->pm_flags & MMC_PM_KEEP_POWER))
  1072. mmc_power_off(host);
  1073. return err;
  1074. }
  1075. EXPORT_SYMBOL(mmc_suspend_host);
  1076. /**
  1077. * mmc_resume_host - resume a previously suspended host
  1078. * @host: mmc host
  1079. */
  1080. int mmc_resume_host(struct mmc_host *host)
  1081. {
  1082. int err = 0;
  1083. mmc_bus_get(host);
  1084. if (host->bus_ops && !host->bus_dead) {
  1085. if (!(host->pm_flags & MMC_PM_KEEP_POWER)) {
  1086. mmc_power_up(host);
  1087. mmc_select_voltage(host, host->ocr);
  1088. }
  1089. BUG_ON(!host->bus_ops->resume);
  1090. err = host->bus_ops->resume(host);
  1091. if (err) {
  1092. printk(KERN_WARNING "%s: error %d during resume "
  1093. "(card was removed?)\n",
  1094. mmc_hostname(host), err);
  1095. if (host->bus_ops->remove)
  1096. host->bus_ops->remove(host);
  1097. mmc_claim_host(host);
  1098. mmc_detach_bus(host);
  1099. mmc_release_host(host);
  1100. /* no need to bother upper layers */
  1101. err = 0;
  1102. }
  1103. }
  1104. mmc_bus_put(host);
  1105. /*
  1106. * We add a slight delay here so that resume can progress
  1107. * in parallel.
  1108. */
  1109. mmc_detect_change(host, 1);
  1110. return err;
  1111. }
  1112. EXPORT_SYMBOL(mmc_resume_host);
  1113. #endif
  1114. static int __init mmc_init(void)
  1115. {
  1116. int ret;
  1117. workqueue = create_singlethread_workqueue("kmmcd");
  1118. if (!workqueue)
  1119. return -ENOMEM;
  1120. ret = mmc_register_bus();
  1121. if (ret)
  1122. goto destroy_workqueue;
  1123. ret = mmc_register_host_class();
  1124. if (ret)
  1125. goto unregister_bus;
  1126. ret = sdio_register_bus();
  1127. if (ret)
  1128. goto unregister_host_class;
  1129. return 0;
  1130. unregister_host_class:
  1131. mmc_unregister_host_class();
  1132. unregister_bus:
  1133. mmc_unregister_bus();
  1134. destroy_workqueue:
  1135. destroy_workqueue(workqueue);
  1136. return ret;
  1137. }
  1138. static void __exit mmc_exit(void)
  1139. {
  1140. sdio_unregister_bus();
  1141. mmc_unregister_host_class();
  1142. mmc_unregister_bus();
  1143. destroy_workqueue(workqueue);
  1144. }
  1145. subsys_initcall(mmc_init);
  1146. module_exit(mmc_exit);
  1147. MODULE_LICENSE("GPL");