core.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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. EXPORT_SYMBOL(mmc_set_data_timeout);
  255. /**
  256. * mmc_align_data_size - pads a transfer size to a more optimal value
  257. * @card: the MMC card associated with the data transfer
  258. * @sz: original transfer size
  259. *
  260. * Pads the original data size with a number of extra bytes in
  261. * order to avoid controller bugs and/or performance hits
  262. * (e.g. some controllers revert to PIO for certain sizes).
  263. *
  264. * Returns the improved size, which might be unmodified.
  265. *
  266. * Note that this function is only relevant when issuing a
  267. * single scatter gather entry.
  268. */
  269. unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
  270. {
  271. /*
  272. * FIXME: We don't have a system for the controller to tell
  273. * the core about its problems yet, so for now we just 32-bit
  274. * align the size.
  275. */
  276. sz = ((sz + 3) / 4) * 4;
  277. return sz;
  278. }
  279. EXPORT_SYMBOL(mmc_align_data_size);
  280. /**
  281. * __mmc_claim_host - exclusively claim a host
  282. * @host: mmc host to claim
  283. * @abort: whether or not the operation should be aborted
  284. *
  285. * Claim a host for a set of operations. If @abort is non null and
  286. * dereference a non-zero value then this will return prematurely with
  287. * that non-zero value without acquiring the lock. Returns zero
  288. * with the lock held otherwise.
  289. */
  290. int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
  291. {
  292. DECLARE_WAITQUEUE(wait, current);
  293. unsigned long flags;
  294. int stop;
  295. might_sleep();
  296. add_wait_queue(&host->wq, &wait);
  297. spin_lock_irqsave(&host->lock, flags);
  298. while (1) {
  299. set_current_state(TASK_UNINTERRUPTIBLE);
  300. stop = abort ? atomic_read(abort) : 0;
  301. if (stop || !host->claimed)
  302. break;
  303. spin_unlock_irqrestore(&host->lock, flags);
  304. schedule();
  305. spin_lock_irqsave(&host->lock, flags);
  306. }
  307. set_current_state(TASK_RUNNING);
  308. if (!stop)
  309. host->claimed = 1;
  310. else
  311. wake_up(&host->wq);
  312. spin_unlock_irqrestore(&host->lock, flags);
  313. remove_wait_queue(&host->wq, &wait);
  314. return stop;
  315. }
  316. EXPORT_SYMBOL(__mmc_claim_host);
  317. /**
  318. * mmc_release_host - release a host
  319. * @host: mmc host to release
  320. *
  321. * Release a MMC host, allowing others to claim the host
  322. * for their operations.
  323. */
  324. void mmc_release_host(struct mmc_host *host)
  325. {
  326. unsigned long flags;
  327. WARN_ON(!host->claimed);
  328. spin_lock_irqsave(&host->lock, flags);
  329. host->claimed = 0;
  330. spin_unlock_irqrestore(&host->lock, flags);
  331. wake_up(&host->wq);
  332. }
  333. EXPORT_SYMBOL(mmc_release_host);
  334. /*
  335. * Internal function that does the actual ios call to the host driver,
  336. * optionally printing some debug output.
  337. */
  338. static inline void mmc_set_ios(struct mmc_host *host)
  339. {
  340. struct mmc_ios *ios = &host->ios;
  341. pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
  342. "width %u timing %u\n",
  343. mmc_hostname(host), ios->clock, ios->bus_mode,
  344. ios->power_mode, ios->chip_select, ios->vdd,
  345. ios->bus_width, ios->timing);
  346. host->ops->set_ios(host, ios);
  347. }
  348. /*
  349. * Control chip select pin on a host.
  350. */
  351. void mmc_set_chip_select(struct mmc_host *host, int mode)
  352. {
  353. host->ios.chip_select = mode;
  354. mmc_set_ios(host);
  355. }
  356. /*
  357. * Sets the host clock to the highest possible frequency that
  358. * is below "hz".
  359. */
  360. void mmc_set_clock(struct mmc_host *host, unsigned int hz)
  361. {
  362. WARN_ON(hz < host->f_min);
  363. if (hz > host->f_max)
  364. hz = host->f_max;
  365. host->ios.clock = hz;
  366. mmc_set_ios(host);
  367. }
  368. /*
  369. * Change the bus mode (open drain/push-pull) of a host.
  370. */
  371. void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
  372. {
  373. host->ios.bus_mode = mode;
  374. mmc_set_ios(host);
  375. }
  376. /*
  377. * Change data bus width of a host.
  378. */
  379. void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
  380. {
  381. host->ios.bus_width = width;
  382. mmc_set_ios(host);
  383. }
  384. /**
  385. * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
  386. * @vdd: voltage (mV)
  387. * @low_bits: prefer low bits in boundary cases
  388. *
  389. * This function returns the OCR bit number according to the provided @vdd
  390. * value. If conversion is not possible a negative errno value returned.
  391. *
  392. * Depending on the @low_bits flag the function prefers low or high OCR bits
  393. * on boundary voltages. For example,
  394. * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
  395. * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
  396. *
  397. * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
  398. */
  399. static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
  400. {
  401. const int max_bit = ilog2(MMC_VDD_35_36);
  402. int bit;
  403. if (vdd < 1650 || vdd > 3600)
  404. return -EINVAL;
  405. if (vdd >= 1650 && vdd <= 1950)
  406. return ilog2(MMC_VDD_165_195);
  407. if (low_bits)
  408. vdd -= 1;
  409. /* Base 2000 mV, step 100 mV, bit's base 8. */
  410. bit = (vdd - 2000) / 100 + 8;
  411. if (bit > max_bit)
  412. return max_bit;
  413. return bit;
  414. }
  415. /**
  416. * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
  417. * @vdd_min: minimum voltage value (mV)
  418. * @vdd_max: maximum voltage value (mV)
  419. *
  420. * This function returns the OCR mask bits according to the provided @vdd_min
  421. * and @vdd_max values. If conversion is not possible the function returns 0.
  422. *
  423. * Notes wrt boundary cases:
  424. * This function sets the OCR bits for all boundary voltages, for example
  425. * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
  426. * MMC_VDD_34_35 mask.
  427. */
  428. u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
  429. {
  430. u32 mask = 0;
  431. if (vdd_max < vdd_min)
  432. return 0;
  433. /* Prefer high bits for the boundary vdd_max values. */
  434. vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
  435. if (vdd_max < 0)
  436. return 0;
  437. /* Prefer low bits for the boundary vdd_min values. */
  438. vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
  439. if (vdd_min < 0)
  440. return 0;
  441. /* Fill the mask, from max bit to min bit. */
  442. while (vdd_max >= vdd_min)
  443. mask |= 1 << vdd_max--;
  444. return mask;
  445. }
  446. EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
  447. #ifdef CONFIG_REGULATOR
  448. /**
  449. * mmc_regulator_get_ocrmask - return mask of supported voltages
  450. * @supply: regulator to use
  451. *
  452. * This returns either a negative errno, or a mask of voltages that
  453. * can be provided to MMC/SD/SDIO devices using the specified voltage
  454. * regulator. This would normally be called before registering the
  455. * MMC host adapter.
  456. */
  457. int mmc_regulator_get_ocrmask(struct regulator *supply)
  458. {
  459. int result = 0;
  460. int count;
  461. int i;
  462. count = regulator_count_voltages(supply);
  463. if (count < 0)
  464. return count;
  465. for (i = 0; i < count; i++) {
  466. int vdd_uV;
  467. int vdd_mV;
  468. vdd_uV = regulator_list_voltage(supply, i);
  469. if (vdd_uV <= 0)
  470. continue;
  471. vdd_mV = vdd_uV / 1000;
  472. result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
  473. }
  474. return result;
  475. }
  476. EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
  477. /**
  478. * mmc_regulator_set_ocr - set regulator to match host->ios voltage
  479. * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
  480. * @supply: regulator to use
  481. *
  482. * Returns zero on success, else negative errno.
  483. *
  484. * MMC host drivers may use this to enable or disable a regulator using
  485. * a particular supply voltage. This would normally be called from the
  486. * set_ios() method.
  487. */
  488. int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit)
  489. {
  490. int result = 0;
  491. int min_uV, max_uV;
  492. int enabled;
  493. enabled = regulator_is_enabled(supply);
  494. if (enabled < 0)
  495. return enabled;
  496. if (vdd_bit) {
  497. int tmp;
  498. int voltage;
  499. /* REVISIT mmc_vddrange_to_ocrmask() may have set some
  500. * bits this regulator doesn't quite support ... don't
  501. * be too picky, most cards and regulators are OK with
  502. * a 0.1V range goof (it's a small error percentage).
  503. */
  504. tmp = vdd_bit - ilog2(MMC_VDD_165_195);
  505. if (tmp == 0) {
  506. min_uV = 1650 * 1000;
  507. max_uV = 1950 * 1000;
  508. } else {
  509. min_uV = 1900 * 1000 + tmp * 100 * 1000;
  510. max_uV = min_uV + 100 * 1000;
  511. }
  512. /* avoid needless changes to this voltage; the regulator
  513. * might not allow this operation
  514. */
  515. voltage = regulator_get_voltage(supply);
  516. if (voltage < 0)
  517. result = voltage;
  518. else if (voltage < min_uV || voltage > max_uV)
  519. result = regulator_set_voltage(supply, min_uV, max_uV);
  520. else
  521. result = 0;
  522. if (result == 0 && !enabled)
  523. result = regulator_enable(supply);
  524. } else if (enabled) {
  525. result = regulator_disable(supply);
  526. }
  527. return result;
  528. }
  529. EXPORT_SYMBOL(mmc_regulator_set_ocr);
  530. #endif
  531. /*
  532. * Mask off any voltages we don't support and select
  533. * the lowest voltage
  534. */
  535. u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
  536. {
  537. int bit;
  538. ocr &= host->ocr_avail;
  539. bit = ffs(ocr);
  540. if (bit) {
  541. bit -= 1;
  542. ocr &= 3 << bit;
  543. host->ios.vdd = bit;
  544. mmc_set_ios(host);
  545. } else {
  546. pr_warning("%s: host doesn't support card's voltages\n",
  547. mmc_hostname(host));
  548. ocr = 0;
  549. }
  550. return ocr;
  551. }
  552. /*
  553. * Select timing parameters for host.
  554. */
  555. void mmc_set_timing(struct mmc_host *host, unsigned int timing)
  556. {
  557. host->ios.timing = timing;
  558. mmc_set_ios(host);
  559. }
  560. /*
  561. * Apply power to the MMC stack. This is a two-stage process.
  562. * First, we enable power to the card without the clock running.
  563. * We then wait a bit for the power to stabilise. Finally,
  564. * enable the bus drivers and clock to the card.
  565. *
  566. * We must _NOT_ enable the clock prior to power stablising.
  567. *
  568. * If a host does all the power sequencing itself, ignore the
  569. * initial MMC_POWER_UP stage.
  570. */
  571. static void mmc_power_up(struct mmc_host *host)
  572. {
  573. int bit = fls(host->ocr_avail) - 1;
  574. host->ios.vdd = bit;
  575. if (mmc_host_is_spi(host)) {
  576. host->ios.chip_select = MMC_CS_HIGH;
  577. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  578. } else {
  579. host->ios.chip_select = MMC_CS_DONTCARE;
  580. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  581. }
  582. host->ios.power_mode = MMC_POWER_UP;
  583. host->ios.bus_width = MMC_BUS_WIDTH_1;
  584. host->ios.timing = MMC_TIMING_LEGACY;
  585. mmc_set_ios(host);
  586. /*
  587. * This delay should be sufficient to allow the power supply
  588. * to reach the minimum voltage.
  589. */
  590. mmc_delay(2);
  591. host->ios.clock = host->f_min;
  592. host->ios.power_mode = MMC_POWER_ON;
  593. mmc_set_ios(host);
  594. /*
  595. * This delay must be at least 74 clock sizes, or 1 ms, or the
  596. * time required to reach a stable voltage.
  597. */
  598. mmc_delay(2);
  599. }
  600. static void mmc_power_off(struct mmc_host *host)
  601. {
  602. host->ios.clock = 0;
  603. host->ios.vdd = 0;
  604. if (!mmc_host_is_spi(host)) {
  605. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  606. host->ios.chip_select = MMC_CS_DONTCARE;
  607. }
  608. host->ios.power_mode = MMC_POWER_OFF;
  609. host->ios.bus_width = MMC_BUS_WIDTH_1;
  610. host->ios.timing = MMC_TIMING_LEGACY;
  611. mmc_set_ios(host);
  612. }
  613. /*
  614. * Cleanup when the last reference to the bus operator is dropped.
  615. */
  616. static void __mmc_release_bus(struct mmc_host *host)
  617. {
  618. BUG_ON(!host);
  619. BUG_ON(host->bus_refs);
  620. BUG_ON(!host->bus_dead);
  621. host->bus_ops = NULL;
  622. }
  623. /*
  624. * Increase reference count of bus operator
  625. */
  626. static inline void mmc_bus_get(struct mmc_host *host)
  627. {
  628. unsigned long flags;
  629. spin_lock_irqsave(&host->lock, flags);
  630. host->bus_refs++;
  631. spin_unlock_irqrestore(&host->lock, flags);
  632. }
  633. /*
  634. * Decrease reference count of bus operator and free it if
  635. * it is the last reference.
  636. */
  637. static inline void mmc_bus_put(struct mmc_host *host)
  638. {
  639. unsigned long flags;
  640. spin_lock_irqsave(&host->lock, flags);
  641. host->bus_refs--;
  642. if ((host->bus_refs == 0) && host->bus_ops)
  643. __mmc_release_bus(host);
  644. spin_unlock_irqrestore(&host->lock, flags);
  645. }
  646. /*
  647. * Assign a mmc bus handler to a host. Only one bus handler may control a
  648. * host at any given time.
  649. */
  650. void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
  651. {
  652. unsigned long flags;
  653. BUG_ON(!host);
  654. BUG_ON(!ops);
  655. WARN_ON(!host->claimed);
  656. spin_lock_irqsave(&host->lock, flags);
  657. BUG_ON(host->bus_ops);
  658. BUG_ON(host->bus_refs);
  659. host->bus_ops = ops;
  660. host->bus_refs = 1;
  661. host->bus_dead = 0;
  662. spin_unlock_irqrestore(&host->lock, flags);
  663. }
  664. /*
  665. * Remove the current bus handler from a host. Assumes that there are
  666. * no interesting cards left, so the bus is powered down.
  667. */
  668. void mmc_detach_bus(struct mmc_host *host)
  669. {
  670. unsigned long flags;
  671. BUG_ON(!host);
  672. WARN_ON(!host->claimed);
  673. WARN_ON(!host->bus_ops);
  674. spin_lock_irqsave(&host->lock, flags);
  675. host->bus_dead = 1;
  676. spin_unlock_irqrestore(&host->lock, flags);
  677. mmc_power_off(host);
  678. mmc_bus_put(host);
  679. }
  680. /**
  681. * mmc_detect_change - process change of state on a MMC socket
  682. * @host: host which changed state.
  683. * @delay: optional delay to wait before detection (jiffies)
  684. *
  685. * MMC drivers should call this when they detect a card has been
  686. * inserted or removed. The MMC layer will confirm that any
  687. * present card is still functional, and initialize any newly
  688. * inserted.
  689. */
  690. void mmc_detect_change(struct mmc_host *host, unsigned long delay)
  691. {
  692. #ifdef CONFIG_MMC_DEBUG
  693. unsigned long flags;
  694. spin_lock_irqsave(&host->lock, flags);
  695. WARN_ON(host->removed);
  696. spin_unlock_irqrestore(&host->lock, flags);
  697. #endif
  698. mmc_schedule_delayed_work(&host->detect, delay);
  699. }
  700. EXPORT_SYMBOL(mmc_detect_change);
  701. void mmc_rescan(struct work_struct *work)
  702. {
  703. struct mmc_host *host =
  704. container_of(work, struct mmc_host, detect.work);
  705. u32 ocr;
  706. int err;
  707. mmc_bus_get(host);
  708. if (host->bus_ops == NULL) {
  709. /*
  710. * Only we can add a new handler, so it's safe to
  711. * release the lock here.
  712. */
  713. mmc_bus_put(host);
  714. if (host->ops->get_cd && host->ops->get_cd(host) == 0)
  715. goto out;
  716. mmc_claim_host(host);
  717. mmc_power_up(host);
  718. mmc_go_idle(host);
  719. mmc_send_if_cond(host, host->ocr_avail);
  720. /*
  721. * First we search for SDIO...
  722. */
  723. err = mmc_send_io_op_cond(host, 0, &ocr);
  724. if (!err) {
  725. if (mmc_attach_sdio(host, ocr))
  726. mmc_power_off(host);
  727. goto out;
  728. }
  729. /*
  730. * ...then normal SD...
  731. */
  732. err = mmc_send_app_op_cond(host, 0, &ocr);
  733. if (!err) {
  734. if (mmc_attach_sd(host, ocr))
  735. mmc_power_off(host);
  736. goto out;
  737. }
  738. /*
  739. * ...and finally MMC.
  740. */
  741. err = mmc_send_op_cond(host, 0, &ocr);
  742. if (!err) {
  743. if (mmc_attach_mmc(host, ocr))
  744. mmc_power_off(host);
  745. goto out;
  746. }
  747. mmc_release_host(host);
  748. mmc_power_off(host);
  749. } else {
  750. if (host->bus_ops->detect && !host->bus_dead)
  751. host->bus_ops->detect(host);
  752. mmc_bus_put(host);
  753. }
  754. out:
  755. if (host->caps & MMC_CAP_NEEDS_POLL)
  756. mmc_schedule_delayed_work(&host->detect, HZ);
  757. }
  758. void mmc_start_host(struct mmc_host *host)
  759. {
  760. mmc_power_off(host);
  761. mmc_detect_change(host, 0);
  762. }
  763. void mmc_stop_host(struct mmc_host *host)
  764. {
  765. #ifdef CONFIG_MMC_DEBUG
  766. unsigned long flags;
  767. spin_lock_irqsave(&host->lock, flags);
  768. host->removed = 1;
  769. spin_unlock_irqrestore(&host->lock, flags);
  770. #endif
  771. mmc_flush_scheduled_work();
  772. mmc_bus_get(host);
  773. if (host->bus_ops && !host->bus_dead) {
  774. if (host->bus_ops->remove)
  775. host->bus_ops->remove(host);
  776. mmc_claim_host(host);
  777. mmc_detach_bus(host);
  778. mmc_release_host(host);
  779. }
  780. mmc_bus_put(host);
  781. BUG_ON(host->card);
  782. mmc_power_off(host);
  783. }
  784. #ifdef CONFIG_PM
  785. /**
  786. * mmc_suspend_host - suspend a host
  787. * @host: mmc host
  788. * @state: suspend mode (PM_SUSPEND_xxx)
  789. */
  790. int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
  791. {
  792. mmc_flush_scheduled_work();
  793. mmc_bus_get(host);
  794. if (host->bus_ops && !host->bus_dead) {
  795. if (host->bus_ops->suspend)
  796. host->bus_ops->suspend(host);
  797. if (!host->bus_ops->resume) {
  798. if (host->bus_ops->remove)
  799. host->bus_ops->remove(host);
  800. mmc_claim_host(host);
  801. mmc_detach_bus(host);
  802. mmc_release_host(host);
  803. }
  804. }
  805. mmc_bus_put(host);
  806. mmc_power_off(host);
  807. return 0;
  808. }
  809. EXPORT_SYMBOL(mmc_suspend_host);
  810. /**
  811. * mmc_resume_host - resume a previously suspended host
  812. * @host: mmc host
  813. */
  814. int mmc_resume_host(struct mmc_host *host)
  815. {
  816. mmc_bus_get(host);
  817. if (host->bus_ops && !host->bus_dead) {
  818. mmc_power_up(host);
  819. BUG_ON(!host->bus_ops->resume);
  820. host->bus_ops->resume(host);
  821. }
  822. mmc_bus_put(host);
  823. /*
  824. * We add a slight delay here so that resume can progress
  825. * in parallel.
  826. */
  827. mmc_detect_change(host, 1);
  828. return 0;
  829. }
  830. EXPORT_SYMBOL(mmc_resume_host);
  831. #endif
  832. static int __init mmc_init(void)
  833. {
  834. int ret;
  835. workqueue = create_singlethread_workqueue("kmmcd");
  836. if (!workqueue)
  837. return -ENOMEM;
  838. ret = mmc_register_bus();
  839. if (ret)
  840. goto destroy_workqueue;
  841. ret = mmc_register_host_class();
  842. if (ret)
  843. goto unregister_bus;
  844. ret = sdio_register_bus();
  845. if (ret)
  846. goto unregister_host_class;
  847. return 0;
  848. unregister_host_class:
  849. mmc_unregister_host_class();
  850. unregister_bus:
  851. mmc_unregister_bus();
  852. destroy_workqueue:
  853. destroy_workqueue(workqueue);
  854. return ret;
  855. }
  856. static void __exit mmc_exit(void)
  857. {
  858. sdio_unregister_bus();
  859. mmc_unregister_host_class();
  860. mmc_unregister_bus();
  861. destroy_workqueue(workqueue);
  862. }
  863. subsys_initcall(mmc_init);
  864. module_exit(mmc_exit);
  865. MODULE_LICENSE("GPL");