core.c 24 KB

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