core.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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 there is a card registered, check whether it is still present */
  724. if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
  725. host->bus_ops->detect(host);
  726. mmc_bus_put(host);
  727. mmc_bus_get(host);
  728. /* if there still is a card present, stop here */
  729. if (host->bus_ops != NULL) {
  730. mmc_bus_put(host);
  731. goto out;
  732. }
  733. /* detect a newly inserted card */
  734. /*
  735. * Only we can add a new handler, so it's safe to
  736. * release the lock here.
  737. */
  738. mmc_bus_put(host);
  739. if (host->ops->get_cd && host->ops->get_cd(host) == 0)
  740. goto out;
  741. mmc_claim_host(host);
  742. mmc_power_up(host);
  743. mmc_go_idle(host);
  744. mmc_send_if_cond(host, host->ocr_avail);
  745. /*
  746. * First we search for SDIO...
  747. */
  748. err = mmc_send_io_op_cond(host, 0, &ocr);
  749. if (!err) {
  750. if (mmc_attach_sdio(host, ocr))
  751. mmc_power_off(host);
  752. goto out;
  753. }
  754. /*
  755. * ...then normal SD...
  756. */
  757. err = mmc_send_app_op_cond(host, 0, &ocr);
  758. if (!err) {
  759. if (mmc_attach_sd(host, ocr))
  760. mmc_power_off(host);
  761. goto out;
  762. }
  763. /*
  764. * ...and finally MMC.
  765. */
  766. err = mmc_send_op_cond(host, 0, &ocr);
  767. if (!err) {
  768. if (mmc_attach_mmc(host, ocr))
  769. mmc_power_off(host);
  770. goto out;
  771. }
  772. mmc_release_host(host);
  773. mmc_power_off(host);
  774. out:
  775. if (host->caps & MMC_CAP_NEEDS_POLL)
  776. mmc_schedule_delayed_work(&host->detect, HZ);
  777. }
  778. void mmc_start_host(struct mmc_host *host)
  779. {
  780. mmc_power_off(host);
  781. mmc_detect_change(host, 0);
  782. }
  783. void mmc_stop_host(struct mmc_host *host)
  784. {
  785. #ifdef CONFIG_MMC_DEBUG
  786. unsigned long flags;
  787. spin_lock_irqsave(&host->lock, flags);
  788. host->removed = 1;
  789. spin_unlock_irqrestore(&host->lock, flags);
  790. #endif
  791. cancel_delayed_work(&host->detect);
  792. mmc_flush_scheduled_work();
  793. mmc_bus_get(host);
  794. if (host->bus_ops && !host->bus_dead) {
  795. if (host->bus_ops->remove)
  796. host->bus_ops->remove(host);
  797. mmc_claim_host(host);
  798. mmc_detach_bus(host);
  799. mmc_release_host(host);
  800. }
  801. mmc_bus_put(host);
  802. BUG_ON(host->card);
  803. mmc_power_off(host);
  804. }
  805. #ifdef CONFIG_PM
  806. /**
  807. * mmc_suspend_host - suspend a host
  808. * @host: mmc host
  809. * @state: suspend mode (PM_SUSPEND_xxx)
  810. */
  811. int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
  812. {
  813. cancel_delayed_work(&host->detect);
  814. mmc_flush_scheduled_work();
  815. mmc_bus_get(host);
  816. if (host->bus_ops && !host->bus_dead) {
  817. if (host->bus_ops->suspend)
  818. host->bus_ops->suspend(host);
  819. if (!host->bus_ops->resume) {
  820. if (host->bus_ops->remove)
  821. host->bus_ops->remove(host);
  822. mmc_claim_host(host);
  823. mmc_detach_bus(host);
  824. mmc_release_host(host);
  825. }
  826. }
  827. mmc_bus_put(host);
  828. mmc_power_off(host);
  829. return 0;
  830. }
  831. EXPORT_SYMBOL(mmc_suspend_host);
  832. /**
  833. * mmc_resume_host - resume a previously suspended host
  834. * @host: mmc host
  835. */
  836. int mmc_resume_host(struct mmc_host *host)
  837. {
  838. mmc_bus_get(host);
  839. if (host->bus_ops && !host->bus_dead) {
  840. mmc_power_up(host);
  841. mmc_select_voltage(host, host->ocr);
  842. BUG_ON(!host->bus_ops->resume);
  843. host->bus_ops->resume(host);
  844. }
  845. mmc_bus_put(host);
  846. /*
  847. * We add a slight delay here so that resume can progress
  848. * in parallel.
  849. */
  850. mmc_detect_change(host, 1);
  851. return 0;
  852. }
  853. EXPORT_SYMBOL(mmc_resume_host);
  854. #endif
  855. static int __init mmc_init(void)
  856. {
  857. int ret;
  858. workqueue = create_singlethread_workqueue("kmmcd");
  859. if (!workqueue)
  860. return -ENOMEM;
  861. ret = mmc_register_bus();
  862. if (ret)
  863. goto destroy_workqueue;
  864. ret = mmc_register_host_class();
  865. if (ret)
  866. goto unregister_bus;
  867. ret = sdio_register_bus();
  868. if (ret)
  869. goto unregister_host_class;
  870. return 0;
  871. unregister_host_class:
  872. mmc_unregister_host_class();
  873. unregister_bus:
  874. mmc_unregister_bus();
  875. destroy_workqueue:
  876. destroy_workqueue(workqueue);
  877. return ret;
  878. }
  879. static void __exit mmc_exit(void)
  880. {
  881. sdio_unregister_bus();
  882. mmc_unregister_host_class();
  883. mmc_unregister_bus();
  884. destroy_workqueue(workqueue);
  885. }
  886. subsys_initcall(mmc_init);
  887. module_exit(mmc_exit);
  888. MODULE_LICENSE("GPL");