core.c 21 KB

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