core.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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/mmc/card.h>
  24. #include <linux/mmc/host.h>
  25. #include <linux/mmc/mmc.h>
  26. #include <linux/mmc/sd.h>
  27. #include "core.h"
  28. #include "bus.h"
  29. #include "host.h"
  30. #include "sdio_bus.h"
  31. #include "mmc_ops.h"
  32. #include "sd_ops.h"
  33. #include "sdio_ops.h"
  34. static struct workqueue_struct *workqueue;
  35. /*
  36. * Enabling software CRCs on the data blocks can be a significant (30%)
  37. * performance cost, and for other reasons may not always be desired.
  38. * So we allow it it to be disabled.
  39. */
  40. int use_spi_crc = 1;
  41. module_param(use_spi_crc, bool, 0);
  42. /*
  43. * Internal function. Schedule delayed work in the MMC work queue.
  44. */
  45. static int mmc_schedule_delayed_work(struct delayed_work *work,
  46. unsigned long delay)
  47. {
  48. return queue_delayed_work(workqueue, work, delay);
  49. }
  50. /*
  51. * Internal function. Flush all scheduled work from the MMC work queue.
  52. */
  53. static void mmc_flush_scheduled_work(void)
  54. {
  55. flush_workqueue(workqueue);
  56. }
  57. /**
  58. * mmc_request_done - finish processing an MMC request
  59. * @host: MMC host which completed request
  60. * @mrq: MMC request which request
  61. *
  62. * MMC drivers should call this function when they have completed
  63. * their processing of a request.
  64. */
  65. void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
  66. {
  67. struct mmc_command *cmd = mrq->cmd;
  68. int err = cmd->error;
  69. if (err && cmd->retries && mmc_host_is_spi(host)) {
  70. if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
  71. cmd->retries = 0;
  72. }
  73. if (err && cmd->retries) {
  74. pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
  75. mmc_hostname(host), cmd->opcode, err);
  76. cmd->retries--;
  77. cmd->error = 0;
  78. host->ops->request(host, mrq);
  79. } else {
  80. led_trigger_event(host->led, LED_OFF);
  81. pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
  82. mmc_hostname(host), cmd->opcode, err,
  83. cmd->resp[0], cmd->resp[1],
  84. cmd->resp[2], cmd->resp[3]);
  85. if (mrq->data) {
  86. pr_debug("%s: %d bytes transferred: %d\n",
  87. mmc_hostname(host),
  88. mrq->data->bytes_xfered, mrq->data->error);
  89. }
  90. if (mrq->stop) {
  91. pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
  92. mmc_hostname(host), mrq->stop->opcode,
  93. mrq->stop->error,
  94. mrq->stop->resp[0], mrq->stop->resp[1],
  95. mrq->stop->resp[2], mrq->stop->resp[3]);
  96. }
  97. if (mrq->done)
  98. mrq->done(mrq);
  99. }
  100. }
  101. EXPORT_SYMBOL(mmc_request_done);
  102. static void
  103. mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
  104. {
  105. #ifdef CONFIG_MMC_DEBUG
  106. unsigned int i, sz;
  107. struct scatterlist *sg;
  108. #endif
  109. pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
  110. mmc_hostname(host), mrq->cmd->opcode,
  111. mrq->cmd->arg, mrq->cmd->flags);
  112. if (mrq->data) {
  113. pr_debug("%s: blksz %d blocks %d flags %08x "
  114. "tsac %d ms nsac %d\n",
  115. mmc_hostname(host), mrq->data->blksz,
  116. mrq->data->blocks, mrq->data->flags,
  117. mrq->data->timeout_ns / 1000000,
  118. mrq->data->timeout_clks);
  119. }
  120. if (mrq->stop) {
  121. pr_debug("%s: CMD%u arg %08x flags %08x\n",
  122. mmc_hostname(host), mrq->stop->opcode,
  123. mrq->stop->arg, mrq->stop->flags);
  124. }
  125. WARN_ON(!host->claimed);
  126. led_trigger_event(host->led, LED_FULL);
  127. mrq->cmd->error = 0;
  128. mrq->cmd->mrq = mrq;
  129. if (mrq->data) {
  130. BUG_ON(mrq->data->blksz > host->max_blk_size);
  131. BUG_ON(mrq->data->blocks > host->max_blk_count);
  132. BUG_ON(mrq->data->blocks * mrq->data->blksz >
  133. host->max_req_size);
  134. #ifdef CONFIG_MMC_DEBUG
  135. sz = 0;
  136. for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
  137. sz += sg->length;
  138. BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
  139. #endif
  140. mrq->cmd->data = mrq->data;
  141. mrq->data->error = 0;
  142. mrq->data->mrq = mrq;
  143. if (mrq->stop) {
  144. mrq->data->stop = mrq->stop;
  145. mrq->stop->error = 0;
  146. mrq->stop->mrq = mrq;
  147. }
  148. }
  149. host->ops->request(host, mrq);
  150. }
  151. static void mmc_wait_done(struct mmc_request *mrq)
  152. {
  153. complete(mrq->done_data);
  154. }
  155. /**
  156. * mmc_wait_for_req - start a request and wait for completion
  157. * @host: MMC host to start command
  158. * @mrq: MMC request to start
  159. *
  160. * Start a new MMC custom command request for a host, and wait
  161. * for the command to complete. Does not attempt to parse the
  162. * response.
  163. */
  164. void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
  165. {
  166. DECLARE_COMPLETION_ONSTACK(complete);
  167. mrq->done_data = &complete;
  168. mrq->done = mmc_wait_done;
  169. mmc_start_request(host, mrq);
  170. wait_for_completion(&complete);
  171. }
  172. EXPORT_SYMBOL(mmc_wait_for_req);
  173. /**
  174. * mmc_wait_for_cmd - start a command and wait for completion
  175. * @host: MMC host to start command
  176. * @cmd: MMC command to start
  177. * @retries: maximum number of retries
  178. *
  179. * Start a new MMC command for a host, and wait for the command
  180. * to complete. Return any error that occurred while the command
  181. * was executing. Do not attempt to parse the response.
  182. */
  183. int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
  184. {
  185. struct mmc_request mrq;
  186. WARN_ON(!host->claimed);
  187. memset(&mrq, 0, sizeof(struct mmc_request));
  188. memset(cmd->resp, 0, sizeof(cmd->resp));
  189. cmd->retries = retries;
  190. mrq.cmd = cmd;
  191. cmd->data = NULL;
  192. mmc_wait_for_req(host, &mrq);
  193. return cmd->error;
  194. }
  195. EXPORT_SYMBOL(mmc_wait_for_cmd);
  196. /**
  197. * mmc_set_data_timeout - set the timeout for a data command
  198. * @data: data phase for command
  199. * @card: the MMC card associated with the data transfer
  200. *
  201. * Computes the data timeout parameters according to the
  202. * correct algorithm given the card type.
  203. */
  204. void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
  205. {
  206. unsigned int mult;
  207. /*
  208. * SDIO cards only define an upper 1 s limit on access.
  209. */
  210. if (mmc_card_sdio(card)) {
  211. data->timeout_ns = 1000000000;
  212. data->timeout_clks = 0;
  213. return;
  214. }
  215. /*
  216. * SD cards use a 100 multiplier rather than 10
  217. */
  218. mult = mmc_card_sd(card) ? 100 : 10;
  219. /*
  220. * Scale up the multiplier (and therefore the timeout) by
  221. * the r2w factor for writes.
  222. */
  223. if (data->flags & MMC_DATA_WRITE)
  224. mult <<= card->csd.r2w_factor;
  225. data->timeout_ns = card->csd.tacc_ns * mult;
  226. data->timeout_clks = card->csd.tacc_clks * mult;
  227. /*
  228. * SD cards also have an upper limit on the timeout.
  229. */
  230. if (mmc_card_sd(card)) {
  231. unsigned int timeout_us, limit_us;
  232. timeout_us = data->timeout_ns / 1000;
  233. timeout_us += data->timeout_clks * 1000 /
  234. (card->host->ios.clock / 1000);
  235. if (data->flags & MMC_DATA_WRITE)
  236. /*
  237. * The limit is really 250 ms, but that is
  238. * insufficient for some crappy cards.
  239. */
  240. limit_us = 300000;
  241. else
  242. limit_us = 100000;
  243. /*
  244. * SDHC cards always use these fixed values.
  245. */
  246. if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
  247. data->timeout_ns = limit_us * 1000;
  248. data->timeout_clks = 0;
  249. }
  250. }
  251. }
  252. EXPORT_SYMBOL(mmc_set_data_timeout);
  253. /**
  254. * mmc_align_data_size - pads a transfer size to a more optimal value
  255. * @card: the MMC card associated with the data transfer
  256. * @sz: original transfer size
  257. *
  258. * Pads the original data size with a number of extra bytes in
  259. * order to avoid controller bugs and/or performance hits
  260. * (e.g. some controllers revert to PIO for certain sizes).
  261. *
  262. * Returns the improved size, which might be unmodified.
  263. *
  264. * Note that this function is only relevant when issuing a
  265. * single scatter gather entry.
  266. */
  267. unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
  268. {
  269. /*
  270. * FIXME: We don't have a system for the controller to tell
  271. * the core about its problems yet, so for now we just 32-bit
  272. * align the size.
  273. */
  274. sz = ((sz + 3) / 4) * 4;
  275. return sz;
  276. }
  277. EXPORT_SYMBOL(mmc_align_data_size);
  278. /**
  279. * __mmc_claim_host - exclusively claim a host
  280. * @host: mmc host to claim
  281. * @abort: whether or not the operation should be aborted
  282. *
  283. * Claim a host for a set of operations. If @abort is non null and
  284. * dereference a non-zero value then this will return prematurely with
  285. * that non-zero value without acquiring the lock. Returns zero
  286. * with the lock held otherwise.
  287. */
  288. int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
  289. {
  290. DECLARE_WAITQUEUE(wait, current);
  291. unsigned long flags;
  292. int stop;
  293. might_sleep();
  294. add_wait_queue(&host->wq, &wait);
  295. spin_lock_irqsave(&host->lock, flags);
  296. while (1) {
  297. set_current_state(TASK_UNINTERRUPTIBLE);
  298. stop = abort ? atomic_read(abort) : 0;
  299. if (stop || !host->claimed)
  300. break;
  301. spin_unlock_irqrestore(&host->lock, flags);
  302. schedule();
  303. spin_lock_irqsave(&host->lock, flags);
  304. }
  305. set_current_state(TASK_RUNNING);
  306. if (!stop)
  307. host->claimed = 1;
  308. else
  309. wake_up(&host->wq);
  310. spin_unlock_irqrestore(&host->lock, flags);
  311. remove_wait_queue(&host->wq, &wait);
  312. return stop;
  313. }
  314. EXPORT_SYMBOL(__mmc_claim_host);
  315. /**
  316. * mmc_release_host - release a host
  317. * @host: mmc host to release
  318. *
  319. * Release a MMC host, allowing others to claim the host
  320. * for their operations.
  321. */
  322. void mmc_release_host(struct mmc_host *host)
  323. {
  324. unsigned long flags;
  325. WARN_ON(!host->claimed);
  326. spin_lock_irqsave(&host->lock, flags);
  327. host->claimed = 0;
  328. spin_unlock_irqrestore(&host->lock, flags);
  329. wake_up(&host->wq);
  330. }
  331. EXPORT_SYMBOL(mmc_release_host);
  332. /*
  333. * Internal function that does the actual ios call to the host driver,
  334. * optionally printing some debug output.
  335. */
  336. static inline void mmc_set_ios(struct mmc_host *host)
  337. {
  338. struct mmc_ios *ios = &host->ios;
  339. pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
  340. "width %u timing %u\n",
  341. mmc_hostname(host), ios->clock, ios->bus_mode,
  342. ios->power_mode, ios->chip_select, ios->vdd,
  343. ios->bus_width, ios->timing);
  344. host->ops->set_ios(host, ios);
  345. }
  346. /*
  347. * Control chip select pin on a host.
  348. */
  349. void mmc_set_chip_select(struct mmc_host *host, int mode)
  350. {
  351. host->ios.chip_select = mode;
  352. mmc_set_ios(host);
  353. }
  354. /*
  355. * Sets the host clock to the highest possible frequency that
  356. * is below "hz".
  357. */
  358. void mmc_set_clock(struct mmc_host *host, unsigned int hz)
  359. {
  360. WARN_ON(hz < host->f_min);
  361. if (hz > host->f_max)
  362. hz = host->f_max;
  363. host->ios.clock = hz;
  364. mmc_set_ios(host);
  365. }
  366. /*
  367. * Change the bus mode (open drain/push-pull) of a host.
  368. */
  369. void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
  370. {
  371. host->ios.bus_mode = mode;
  372. mmc_set_ios(host);
  373. }
  374. /*
  375. * Change data bus width of a host.
  376. */
  377. void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
  378. {
  379. host->ios.bus_width = width;
  380. mmc_set_ios(host);
  381. }
  382. /*
  383. * Mask off any voltages we don't support and select
  384. * the lowest voltage
  385. */
  386. u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
  387. {
  388. int bit;
  389. ocr &= host->ocr_avail;
  390. bit = ffs(ocr);
  391. if (bit) {
  392. bit -= 1;
  393. ocr &= 3 << bit;
  394. host->ios.vdd = bit;
  395. mmc_set_ios(host);
  396. } else {
  397. ocr = 0;
  398. }
  399. return ocr;
  400. }
  401. /*
  402. * Select timing parameters for host.
  403. */
  404. void mmc_set_timing(struct mmc_host *host, unsigned int timing)
  405. {
  406. host->ios.timing = timing;
  407. mmc_set_ios(host);
  408. }
  409. /*
  410. * Apply power to the MMC stack. This is a two-stage process.
  411. * First, we enable power to the card without the clock running.
  412. * We then wait a bit for the power to stabilise. Finally,
  413. * enable the bus drivers and clock to the card.
  414. *
  415. * We must _NOT_ enable the clock prior to power stablising.
  416. *
  417. * If a host does all the power sequencing itself, ignore the
  418. * initial MMC_POWER_UP stage.
  419. */
  420. static void mmc_power_up(struct mmc_host *host)
  421. {
  422. int bit = fls(host->ocr_avail) - 1;
  423. host->ios.vdd = bit;
  424. if (mmc_host_is_spi(host)) {
  425. host->ios.chip_select = MMC_CS_HIGH;
  426. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  427. } else {
  428. host->ios.chip_select = MMC_CS_DONTCARE;
  429. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  430. }
  431. host->ios.power_mode = MMC_POWER_UP;
  432. host->ios.bus_width = MMC_BUS_WIDTH_1;
  433. host->ios.timing = MMC_TIMING_LEGACY;
  434. mmc_set_ios(host);
  435. /*
  436. * This delay should be sufficient to allow the power supply
  437. * to reach the minimum voltage.
  438. */
  439. mmc_delay(2);
  440. host->ios.clock = host->f_min;
  441. host->ios.power_mode = MMC_POWER_ON;
  442. mmc_set_ios(host);
  443. /*
  444. * This delay must be at least 74 clock sizes, or 1 ms, or the
  445. * time required to reach a stable voltage.
  446. */
  447. mmc_delay(2);
  448. }
  449. static void mmc_power_off(struct mmc_host *host)
  450. {
  451. host->ios.clock = 0;
  452. host->ios.vdd = 0;
  453. if (!mmc_host_is_spi(host)) {
  454. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  455. host->ios.chip_select = MMC_CS_DONTCARE;
  456. }
  457. host->ios.power_mode = MMC_POWER_OFF;
  458. host->ios.bus_width = MMC_BUS_WIDTH_1;
  459. host->ios.timing = MMC_TIMING_LEGACY;
  460. mmc_set_ios(host);
  461. }
  462. /*
  463. * Cleanup when the last reference to the bus operator is dropped.
  464. */
  465. static void __mmc_release_bus(struct mmc_host *host)
  466. {
  467. BUG_ON(!host);
  468. BUG_ON(host->bus_refs);
  469. BUG_ON(!host->bus_dead);
  470. host->bus_ops = NULL;
  471. }
  472. /*
  473. * Increase reference count of bus operator
  474. */
  475. static inline void mmc_bus_get(struct mmc_host *host)
  476. {
  477. unsigned long flags;
  478. spin_lock_irqsave(&host->lock, flags);
  479. host->bus_refs++;
  480. spin_unlock_irqrestore(&host->lock, flags);
  481. }
  482. /*
  483. * Decrease reference count of bus operator and free it if
  484. * it is the last reference.
  485. */
  486. static inline void mmc_bus_put(struct mmc_host *host)
  487. {
  488. unsigned long flags;
  489. spin_lock_irqsave(&host->lock, flags);
  490. host->bus_refs--;
  491. if ((host->bus_refs == 0) && host->bus_ops)
  492. __mmc_release_bus(host);
  493. spin_unlock_irqrestore(&host->lock, flags);
  494. }
  495. /*
  496. * Assign a mmc bus handler to a host. Only one bus handler may control a
  497. * host at any given time.
  498. */
  499. void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
  500. {
  501. unsigned long flags;
  502. BUG_ON(!host);
  503. BUG_ON(!ops);
  504. WARN_ON(!host->claimed);
  505. spin_lock_irqsave(&host->lock, flags);
  506. BUG_ON(host->bus_ops);
  507. BUG_ON(host->bus_refs);
  508. host->bus_ops = ops;
  509. host->bus_refs = 1;
  510. host->bus_dead = 0;
  511. spin_unlock_irqrestore(&host->lock, flags);
  512. }
  513. /*
  514. * Remove the current bus handler from a host. Assumes that there are
  515. * no interesting cards left, so the bus is powered down.
  516. */
  517. void mmc_detach_bus(struct mmc_host *host)
  518. {
  519. unsigned long flags;
  520. BUG_ON(!host);
  521. WARN_ON(!host->claimed);
  522. WARN_ON(!host->bus_ops);
  523. spin_lock_irqsave(&host->lock, flags);
  524. host->bus_dead = 1;
  525. spin_unlock_irqrestore(&host->lock, flags);
  526. mmc_power_off(host);
  527. mmc_bus_put(host);
  528. }
  529. /**
  530. * mmc_detect_change - process change of state on a MMC socket
  531. * @host: host which changed state.
  532. * @delay: optional delay to wait before detection (jiffies)
  533. *
  534. * MMC drivers should call this when they detect a card has been
  535. * inserted or removed. The MMC layer will confirm that any
  536. * present card is still functional, and initialize any newly
  537. * inserted.
  538. */
  539. void mmc_detect_change(struct mmc_host *host, unsigned long delay)
  540. {
  541. #ifdef CONFIG_MMC_DEBUG
  542. unsigned long flags;
  543. spin_lock_irqsave(&host->lock, flags);
  544. WARN_ON(host->removed);
  545. spin_unlock_irqrestore(&host->lock, flags);
  546. #endif
  547. mmc_schedule_delayed_work(&host->detect, delay);
  548. }
  549. EXPORT_SYMBOL(mmc_detect_change);
  550. void mmc_rescan(struct work_struct *work)
  551. {
  552. struct mmc_host *host =
  553. container_of(work, struct mmc_host, detect.work);
  554. u32 ocr;
  555. int err;
  556. mmc_bus_get(host);
  557. if (host->bus_ops == NULL) {
  558. /*
  559. * Only we can add a new handler, so it's safe to
  560. * release the lock here.
  561. */
  562. mmc_bus_put(host);
  563. if (host->ops->get_cd && host->ops->get_cd(host) == 0)
  564. goto out;
  565. mmc_claim_host(host);
  566. mmc_power_up(host);
  567. mmc_go_idle(host);
  568. mmc_send_if_cond(host, host->ocr_avail);
  569. /*
  570. * First we search for SDIO...
  571. */
  572. err = mmc_send_io_op_cond(host, 0, &ocr);
  573. if (!err) {
  574. if (mmc_attach_sdio(host, ocr))
  575. mmc_power_off(host);
  576. goto out;
  577. }
  578. /*
  579. * ...then normal SD...
  580. */
  581. err = mmc_send_app_op_cond(host, 0, &ocr);
  582. if (!err) {
  583. if (mmc_attach_sd(host, ocr))
  584. mmc_power_off(host);
  585. goto out;
  586. }
  587. /*
  588. * ...and finally MMC.
  589. */
  590. err = mmc_send_op_cond(host, 0, &ocr);
  591. if (!err) {
  592. if (mmc_attach_mmc(host, ocr))
  593. mmc_power_off(host);
  594. goto out;
  595. }
  596. mmc_release_host(host);
  597. mmc_power_off(host);
  598. } else {
  599. if (host->bus_ops->detect && !host->bus_dead)
  600. host->bus_ops->detect(host);
  601. mmc_bus_put(host);
  602. }
  603. out:
  604. if (host->caps & MMC_CAP_NEEDS_POLL)
  605. mmc_schedule_delayed_work(&host->detect, HZ);
  606. }
  607. void mmc_start_host(struct mmc_host *host)
  608. {
  609. mmc_power_off(host);
  610. mmc_detect_change(host, 0);
  611. }
  612. void mmc_stop_host(struct mmc_host *host)
  613. {
  614. #ifdef CONFIG_MMC_DEBUG
  615. unsigned long flags;
  616. spin_lock_irqsave(&host->lock, flags);
  617. host->removed = 1;
  618. spin_unlock_irqrestore(&host->lock, flags);
  619. #endif
  620. mmc_flush_scheduled_work();
  621. mmc_bus_get(host);
  622. if (host->bus_ops && !host->bus_dead) {
  623. if (host->bus_ops->remove)
  624. host->bus_ops->remove(host);
  625. mmc_claim_host(host);
  626. mmc_detach_bus(host);
  627. mmc_release_host(host);
  628. }
  629. mmc_bus_put(host);
  630. BUG_ON(host->card);
  631. mmc_power_off(host);
  632. }
  633. #ifdef CONFIG_PM
  634. /**
  635. * mmc_suspend_host - suspend a host
  636. * @host: mmc host
  637. * @state: suspend mode (PM_SUSPEND_xxx)
  638. */
  639. int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
  640. {
  641. mmc_flush_scheduled_work();
  642. mmc_bus_get(host);
  643. if (host->bus_ops && !host->bus_dead) {
  644. if (host->bus_ops->suspend)
  645. host->bus_ops->suspend(host);
  646. if (!host->bus_ops->resume) {
  647. if (host->bus_ops->remove)
  648. host->bus_ops->remove(host);
  649. mmc_claim_host(host);
  650. mmc_detach_bus(host);
  651. mmc_release_host(host);
  652. }
  653. }
  654. mmc_bus_put(host);
  655. mmc_power_off(host);
  656. return 0;
  657. }
  658. EXPORT_SYMBOL(mmc_suspend_host);
  659. /**
  660. * mmc_resume_host - resume a previously suspended host
  661. * @host: mmc host
  662. */
  663. int mmc_resume_host(struct mmc_host *host)
  664. {
  665. mmc_bus_get(host);
  666. if (host->bus_ops && !host->bus_dead) {
  667. mmc_power_up(host);
  668. BUG_ON(!host->bus_ops->resume);
  669. host->bus_ops->resume(host);
  670. }
  671. mmc_bus_put(host);
  672. /*
  673. * We add a slight delay here so that resume can progress
  674. * in parallel.
  675. */
  676. mmc_detect_change(host, 1);
  677. return 0;
  678. }
  679. EXPORT_SYMBOL(mmc_resume_host);
  680. #endif
  681. static int __init mmc_init(void)
  682. {
  683. int ret;
  684. workqueue = create_singlethread_workqueue("kmmcd");
  685. if (!workqueue)
  686. return -ENOMEM;
  687. ret = mmc_register_bus();
  688. if (ret)
  689. goto destroy_workqueue;
  690. ret = mmc_register_host_class();
  691. if (ret)
  692. goto unregister_bus;
  693. ret = sdio_register_bus();
  694. if (ret)
  695. goto unregister_host_class;
  696. return 0;
  697. unregister_host_class:
  698. mmc_unregister_host_class();
  699. unregister_bus:
  700. mmc_unregister_bus();
  701. destroy_workqueue:
  702. destroy_workqueue(workqueue);
  703. return ret;
  704. }
  705. static void __exit mmc_exit(void)
  706. {
  707. sdio_unregister_bus();
  708. mmc_unregister_host_class();
  709. mmc_unregister_bus();
  710. destroy_workqueue(workqueue);
  711. }
  712. subsys_initcall(mmc_init);
  713. module_exit(mmc_exit);
  714. MODULE_LICENSE("GPL");