core.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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_host_enable - enable a host.
  297. * @host: mmc host to enable
  298. *
  299. * Hosts that support power saving can use the 'enable' and 'disable'
  300. * methods to exit and enter power saving states. For more information
  301. * see comments for struct mmc_host_ops.
  302. */
  303. int mmc_host_enable(struct mmc_host *host)
  304. {
  305. if (!(host->caps & MMC_CAP_DISABLE))
  306. return 0;
  307. if (host->en_dis_recurs)
  308. return 0;
  309. if (host->nesting_cnt++)
  310. return 0;
  311. cancel_delayed_work_sync(&host->disable);
  312. if (host->enabled)
  313. return 0;
  314. if (host->ops->enable) {
  315. int err;
  316. host->en_dis_recurs = 1;
  317. err = host->ops->enable(host);
  318. host->en_dis_recurs = 0;
  319. if (err) {
  320. pr_debug("%s: enable error %d\n",
  321. mmc_hostname(host), err);
  322. return err;
  323. }
  324. }
  325. host->enabled = 1;
  326. return 0;
  327. }
  328. EXPORT_SYMBOL(mmc_host_enable);
  329. static int mmc_host_do_disable(struct mmc_host *host, int lazy)
  330. {
  331. if (host->ops->disable) {
  332. int err;
  333. host->en_dis_recurs = 1;
  334. err = host->ops->disable(host, lazy);
  335. host->en_dis_recurs = 0;
  336. if (err < 0) {
  337. pr_debug("%s: disable error %d\n",
  338. mmc_hostname(host), err);
  339. return err;
  340. }
  341. if (err > 0) {
  342. unsigned long delay = msecs_to_jiffies(err);
  343. mmc_schedule_delayed_work(&host->disable, delay);
  344. }
  345. }
  346. host->enabled = 0;
  347. return 0;
  348. }
  349. /**
  350. * mmc_host_disable - disable a host.
  351. * @host: mmc host to disable
  352. *
  353. * Hosts that support power saving can use the 'enable' and 'disable'
  354. * methods to exit and enter power saving states. For more information
  355. * see comments for struct mmc_host_ops.
  356. */
  357. int mmc_host_disable(struct mmc_host *host)
  358. {
  359. int err;
  360. if (!(host->caps & MMC_CAP_DISABLE))
  361. return 0;
  362. if (host->en_dis_recurs)
  363. return 0;
  364. if (--host->nesting_cnt)
  365. return 0;
  366. if (!host->enabled)
  367. return 0;
  368. err = mmc_host_do_disable(host, 0);
  369. return err;
  370. }
  371. EXPORT_SYMBOL(mmc_host_disable);
  372. /**
  373. * __mmc_claim_host - exclusively claim a host
  374. * @host: mmc host to claim
  375. * @abort: whether or not the operation should be aborted
  376. *
  377. * Claim a host for a set of operations. If @abort is non null and
  378. * dereference a non-zero value then this will return prematurely with
  379. * that non-zero value without acquiring the lock. Returns zero
  380. * with the lock held otherwise.
  381. */
  382. int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
  383. {
  384. DECLARE_WAITQUEUE(wait, current);
  385. unsigned long flags;
  386. int stop;
  387. might_sleep();
  388. add_wait_queue(&host->wq, &wait);
  389. spin_lock_irqsave(&host->lock, flags);
  390. while (1) {
  391. set_current_state(TASK_UNINTERRUPTIBLE);
  392. stop = abort ? atomic_read(abort) : 0;
  393. if (stop || !host->claimed)
  394. break;
  395. spin_unlock_irqrestore(&host->lock, flags);
  396. schedule();
  397. spin_lock_irqsave(&host->lock, flags);
  398. }
  399. set_current_state(TASK_RUNNING);
  400. if (!stop)
  401. host->claimed = 1;
  402. else
  403. wake_up(&host->wq);
  404. spin_unlock_irqrestore(&host->lock, flags);
  405. remove_wait_queue(&host->wq, &wait);
  406. if (!stop)
  407. mmc_host_enable(host);
  408. return stop;
  409. }
  410. EXPORT_SYMBOL(__mmc_claim_host);
  411. static int mmc_try_claim_host(struct mmc_host *host)
  412. {
  413. int claimed_host = 0;
  414. unsigned long flags;
  415. spin_lock_irqsave(&host->lock, flags);
  416. if (!host->claimed) {
  417. host->claimed = 1;
  418. claimed_host = 1;
  419. }
  420. spin_unlock_irqrestore(&host->lock, flags);
  421. return claimed_host;
  422. }
  423. static void mmc_do_release_host(struct mmc_host *host)
  424. {
  425. unsigned long flags;
  426. spin_lock_irqsave(&host->lock, flags);
  427. host->claimed = 0;
  428. spin_unlock_irqrestore(&host->lock, flags);
  429. wake_up(&host->wq);
  430. }
  431. void mmc_host_deeper_disable(struct work_struct *work)
  432. {
  433. struct mmc_host *host =
  434. container_of(work, struct mmc_host, disable.work);
  435. /* If the host is claimed then we do not want to disable it anymore */
  436. if (!mmc_try_claim_host(host))
  437. return;
  438. mmc_host_do_disable(host, 1);
  439. mmc_do_release_host(host);
  440. }
  441. /**
  442. * mmc_host_lazy_disable - lazily disable a host.
  443. * @host: mmc host to disable
  444. *
  445. * Hosts that support power saving can use the 'enable' and 'disable'
  446. * methods to exit and enter power saving states. For more information
  447. * see comments for struct mmc_host_ops.
  448. */
  449. int mmc_host_lazy_disable(struct mmc_host *host)
  450. {
  451. if (!(host->caps & MMC_CAP_DISABLE))
  452. return 0;
  453. if (host->en_dis_recurs)
  454. return 0;
  455. if (--host->nesting_cnt)
  456. return 0;
  457. if (!host->enabled)
  458. return 0;
  459. if (host->disable_delay) {
  460. mmc_schedule_delayed_work(&host->disable,
  461. msecs_to_jiffies(host->disable_delay));
  462. return 0;
  463. } else
  464. return mmc_host_do_disable(host, 1);
  465. }
  466. EXPORT_SYMBOL(mmc_host_lazy_disable);
  467. /**
  468. * mmc_release_host - release a host
  469. * @host: mmc host to release
  470. *
  471. * Release a MMC host, allowing others to claim the host
  472. * for their operations.
  473. */
  474. void mmc_release_host(struct mmc_host *host)
  475. {
  476. WARN_ON(!host->claimed);
  477. mmc_host_lazy_disable(host);
  478. mmc_do_release_host(host);
  479. }
  480. EXPORT_SYMBOL(mmc_release_host);
  481. /*
  482. * Internal function that does the actual ios call to the host driver,
  483. * optionally printing some debug output.
  484. */
  485. static inline void mmc_set_ios(struct mmc_host *host)
  486. {
  487. struct mmc_ios *ios = &host->ios;
  488. pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
  489. "width %u timing %u\n",
  490. mmc_hostname(host), ios->clock, ios->bus_mode,
  491. ios->power_mode, ios->chip_select, ios->vdd,
  492. ios->bus_width, ios->timing);
  493. host->ops->set_ios(host, ios);
  494. }
  495. /*
  496. * Control chip select pin on a host.
  497. */
  498. void mmc_set_chip_select(struct mmc_host *host, int mode)
  499. {
  500. host->ios.chip_select = mode;
  501. mmc_set_ios(host);
  502. }
  503. /*
  504. * Sets the host clock to the highest possible frequency that
  505. * is below "hz".
  506. */
  507. void mmc_set_clock(struct mmc_host *host, unsigned int hz)
  508. {
  509. WARN_ON(hz < host->f_min);
  510. if (hz > host->f_max)
  511. hz = host->f_max;
  512. host->ios.clock = hz;
  513. mmc_set_ios(host);
  514. }
  515. /*
  516. * Change the bus mode (open drain/push-pull) of a host.
  517. */
  518. void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
  519. {
  520. host->ios.bus_mode = mode;
  521. mmc_set_ios(host);
  522. }
  523. /*
  524. * Change data bus width of a host.
  525. */
  526. void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
  527. {
  528. host->ios.bus_width = width;
  529. mmc_set_ios(host);
  530. }
  531. /**
  532. * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
  533. * @vdd: voltage (mV)
  534. * @low_bits: prefer low bits in boundary cases
  535. *
  536. * This function returns the OCR bit number according to the provided @vdd
  537. * value. If conversion is not possible a negative errno value returned.
  538. *
  539. * Depending on the @low_bits flag the function prefers low or high OCR bits
  540. * on boundary voltages. For example,
  541. * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
  542. * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
  543. *
  544. * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
  545. */
  546. static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
  547. {
  548. const int max_bit = ilog2(MMC_VDD_35_36);
  549. int bit;
  550. if (vdd < 1650 || vdd > 3600)
  551. return -EINVAL;
  552. if (vdd >= 1650 && vdd <= 1950)
  553. return ilog2(MMC_VDD_165_195);
  554. if (low_bits)
  555. vdd -= 1;
  556. /* Base 2000 mV, step 100 mV, bit's base 8. */
  557. bit = (vdd - 2000) / 100 + 8;
  558. if (bit > max_bit)
  559. return max_bit;
  560. return bit;
  561. }
  562. /**
  563. * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
  564. * @vdd_min: minimum voltage value (mV)
  565. * @vdd_max: maximum voltage value (mV)
  566. *
  567. * This function returns the OCR mask bits according to the provided @vdd_min
  568. * and @vdd_max values. If conversion is not possible the function returns 0.
  569. *
  570. * Notes wrt boundary cases:
  571. * This function sets the OCR bits for all boundary voltages, for example
  572. * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
  573. * MMC_VDD_34_35 mask.
  574. */
  575. u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
  576. {
  577. u32 mask = 0;
  578. if (vdd_max < vdd_min)
  579. return 0;
  580. /* Prefer high bits for the boundary vdd_max values. */
  581. vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
  582. if (vdd_max < 0)
  583. return 0;
  584. /* Prefer low bits for the boundary vdd_min values. */
  585. vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
  586. if (vdd_min < 0)
  587. return 0;
  588. /* Fill the mask, from max bit to min bit. */
  589. while (vdd_max >= vdd_min)
  590. mask |= 1 << vdd_max--;
  591. return mask;
  592. }
  593. EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
  594. #ifdef CONFIG_REGULATOR
  595. /**
  596. * mmc_regulator_get_ocrmask - return mask of supported voltages
  597. * @supply: regulator to use
  598. *
  599. * This returns either a negative errno, or a mask of voltages that
  600. * can be provided to MMC/SD/SDIO devices using the specified voltage
  601. * regulator. This would normally be called before registering the
  602. * MMC host adapter.
  603. */
  604. int mmc_regulator_get_ocrmask(struct regulator *supply)
  605. {
  606. int result = 0;
  607. int count;
  608. int i;
  609. count = regulator_count_voltages(supply);
  610. if (count < 0)
  611. return count;
  612. for (i = 0; i < count; i++) {
  613. int vdd_uV;
  614. int vdd_mV;
  615. vdd_uV = regulator_list_voltage(supply, i);
  616. if (vdd_uV <= 0)
  617. continue;
  618. vdd_mV = vdd_uV / 1000;
  619. result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
  620. }
  621. return result;
  622. }
  623. EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
  624. /**
  625. * mmc_regulator_set_ocr - set regulator to match host->ios voltage
  626. * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
  627. * @supply: regulator to use
  628. *
  629. * Returns zero on success, else negative errno.
  630. *
  631. * MMC host drivers may use this to enable or disable a regulator using
  632. * a particular supply voltage. This would normally be called from the
  633. * set_ios() method.
  634. */
  635. int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit)
  636. {
  637. int result = 0;
  638. int min_uV, max_uV;
  639. int enabled;
  640. enabled = regulator_is_enabled(supply);
  641. if (enabled < 0)
  642. return enabled;
  643. if (vdd_bit) {
  644. int tmp;
  645. int voltage;
  646. /* REVISIT mmc_vddrange_to_ocrmask() may have set some
  647. * bits this regulator doesn't quite support ... don't
  648. * be too picky, most cards and regulators are OK with
  649. * a 0.1V range goof (it's a small error percentage).
  650. */
  651. tmp = vdd_bit - ilog2(MMC_VDD_165_195);
  652. if (tmp == 0) {
  653. min_uV = 1650 * 1000;
  654. max_uV = 1950 * 1000;
  655. } else {
  656. min_uV = 1900 * 1000 + tmp * 100 * 1000;
  657. max_uV = min_uV + 100 * 1000;
  658. }
  659. /* avoid needless changes to this voltage; the regulator
  660. * might not allow this operation
  661. */
  662. voltage = regulator_get_voltage(supply);
  663. if (voltage < 0)
  664. result = voltage;
  665. else if (voltage < min_uV || voltage > max_uV)
  666. result = regulator_set_voltage(supply, min_uV, max_uV);
  667. else
  668. result = 0;
  669. if (result == 0 && !enabled)
  670. result = regulator_enable(supply);
  671. } else if (enabled) {
  672. result = regulator_disable(supply);
  673. }
  674. return result;
  675. }
  676. EXPORT_SYMBOL(mmc_regulator_set_ocr);
  677. #endif
  678. /*
  679. * Mask off any voltages we don't support and select
  680. * the lowest voltage
  681. */
  682. u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
  683. {
  684. int bit;
  685. ocr &= host->ocr_avail;
  686. bit = ffs(ocr);
  687. if (bit) {
  688. bit -= 1;
  689. ocr &= 3 << bit;
  690. host->ios.vdd = bit;
  691. mmc_set_ios(host);
  692. } else {
  693. pr_warning("%s: host doesn't support card's voltages\n",
  694. mmc_hostname(host));
  695. ocr = 0;
  696. }
  697. return ocr;
  698. }
  699. /*
  700. * Select timing parameters for host.
  701. */
  702. void mmc_set_timing(struct mmc_host *host, unsigned int timing)
  703. {
  704. host->ios.timing = timing;
  705. mmc_set_ios(host);
  706. }
  707. /*
  708. * Apply power to the MMC stack. This is a two-stage process.
  709. * First, we enable power to the card without the clock running.
  710. * We then wait a bit for the power to stabilise. Finally,
  711. * enable the bus drivers and clock to the card.
  712. *
  713. * We must _NOT_ enable the clock prior to power stablising.
  714. *
  715. * If a host does all the power sequencing itself, ignore the
  716. * initial MMC_POWER_UP stage.
  717. */
  718. static void mmc_power_up(struct mmc_host *host)
  719. {
  720. int bit;
  721. /* If ocr is set, we use it */
  722. if (host->ocr)
  723. bit = ffs(host->ocr) - 1;
  724. else
  725. bit = fls(host->ocr_avail) - 1;
  726. host->ios.vdd = bit;
  727. if (mmc_host_is_spi(host)) {
  728. host->ios.chip_select = MMC_CS_HIGH;
  729. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  730. } else {
  731. host->ios.chip_select = MMC_CS_DONTCARE;
  732. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  733. }
  734. host->ios.power_mode = MMC_POWER_UP;
  735. host->ios.bus_width = MMC_BUS_WIDTH_1;
  736. host->ios.timing = MMC_TIMING_LEGACY;
  737. mmc_set_ios(host);
  738. /*
  739. * This delay should be sufficient to allow the power supply
  740. * to reach the minimum voltage.
  741. */
  742. mmc_delay(10);
  743. if (host->f_min > 400000) {
  744. pr_warning("%s: Minimum clock frequency too high for "
  745. "identification mode\n", mmc_hostname(host));
  746. host->ios.clock = host->f_min;
  747. } else
  748. host->ios.clock = 400000;
  749. host->ios.power_mode = MMC_POWER_ON;
  750. mmc_set_ios(host);
  751. /*
  752. * This delay must be at least 74 clock sizes, or 1 ms, or the
  753. * time required to reach a stable voltage.
  754. */
  755. mmc_delay(10);
  756. }
  757. static void mmc_power_off(struct mmc_host *host)
  758. {
  759. host->ios.clock = 0;
  760. host->ios.vdd = 0;
  761. if (!mmc_host_is_spi(host)) {
  762. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  763. host->ios.chip_select = MMC_CS_DONTCARE;
  764. }
  765. host->ios.power_mode = MMC_POWER_OFF;
  766. host->ios.bus_width = MMC_BUS_WIDTH_1;
  767. host->ios.timing = MMC_TIMING_LEGACY;
  768. mmc_set_ios(host);
  769. }
  770. /*
  771. * Cleanup when the last reference to the bus operator is dropped.
  772. */
  773. static void __mmc_release_bus(struct mmc_host *host)
  774. {
  775. BUG_ON(!host);
  776. BUG_ON(host->bus_refs);
  777. BUG_ON(!host->bus_dead);
  778. host->bus_ops = NULL;
  779. }
  780. /*
  781. * Increase reference count of bus operator
  782. */
  783. static inline void mmc_bus_get(struct mmc_host *host)
  784. {
  785. unsigned long flags;
  786. spin_lock_irqsave(&host->lock, flags);
  787. host->bus_refs++;
  788. spin_unlock_irqrestore(&host->lock, flags);
  789. }
  790. /*
  791. * Decrease reference count of bus operator and free it if
  792. * it is the last reference.
  793. */
  794. static inline void mmc_bus_put(struct mmc_host *host)
  795. {
  796. unsigned long flags;
  797. spin_lock_irqsave(&host->lock, flags);
  798. host->bus_refs--;
  799. if ((host->bus_refs == 0) && host->bus_ops)
  800. __mmc_release_bus(host);
  801. spin_unlock_irqrestore(&host->lock, flags);
  802. }
  803. /*
  804. * Assign a mmc bus handler to a host. Only one bus handler may control a
  805. * host at any given time.
  806. */
  807. void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
  808. {
  809. unsigned long flags;
  810. BUG_ON(!host);
  811. BUG_ON(!ops);
  812. WARN_ON(!host->claimed);
  813. spin_lock_irqsave(&host->lock, flags);
  814. BUG_ON(host->bus_ops);
  815. BUG_ON(host->bus_refs);
  816. host->bus_ops = ops;
  817. host->bus_refs = 1;
  818. host->bus_dead = 0;
  819. spin_unlock_irqrestore(&host->lock, flags);
  820. }
  821. /*
  822. * Remove the current bus handler from a host. Assumes that there are
  823. * no interesting cards left, so the bus is powered down.
  824. */
  825. void mmc_detach_bus(struct mmc_host *host)
  826. {
  827. unsigned long flags;
  828. BUG_ON(!host);
  829. WARN_ON(!host->claimed);
  830. WARN_ON(!host->bus_ops);
  831. spin_lock_irqsave(&host->lock, flags);
  832. host->bus_dead = 1;
  833. spin_unlock_irqrestore(&host->lock, flags);
  834. mmc_power_off(host);
  835. mmc_bus_put(host);
  836. }
  837. /**
  838. * mmc_detect_change - process change of state on a MMC socket
  839. * @host: host which changed state.
  840. * @delay: optional delay to wait before detection (jiffies)
  841. *
  842. * MMC drivers should call this when they detect a card has been
  843. * inserted or removed. The MMC layer will confirm that any
  844. * present card is still functional, and initialize any newly
  845. * inserted.
  846. */
  847. void mmc_detect_change(struct mmc_host *host, unsigned long delay)
  848. {
  849. #ifdef CONFIG_MMC_DEBUG
  850. unsigned long flags;
  851. spin_lock_irqsave(&host->lock, flags);
  852. WARN_ON(host->removed);
  853. spin_unlock_irqrestore(&host->lock, flags);
  854. #endif
  855. mmc_schedule_delayed_work(&host->detect, delay);
  856. }
  857. EXPORT_SYMBOL(mmc_detect_change);
  858. void mmc_rescan(struct work_struct *work)
  859. {
  860. struct mmc_host *host =
  861. container_of(work, struct mmc_host, detect.work);
  862. u32 ocr;
  863. int err;
  864. mmc_bus_get(host);
  865. /* if there is a card registered, check whether it is still present */
  866. if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
  867. host->bus_ops->detect(host);
  868. mmc_bus_put(host);
  869. mmc_bus_get(host);
  870. /* if there still is a card present, stop here */
  871. if (host->bus_ops != NULL) {
  872. mmc_bus_put(host);
  873. goto out;
  874. }
  875. /* detect a newly inserted card */
  876. /*
  877. * Only we can add a new handler, so it's safe to
  878. * release the lock here.
  879. */
  880. mmc_bus_put(host);
  881. if (host->ops->get_cd && host->ops->get_cd(host) == 0)
  882. goto out;
  883. mmc_claim_host(host);
  884. mmc_power_up(host);
  885. mmc_go_idle(host);
  886. mmc_send_if_cond(host, host->ocr_avail);
  887. /*
  888. * First we search for SDIO...
  889. */
  890. err = mmc_send_io_op_cond(host, 0, &ocr);
  891. if (!err) {
  892. if (mmc_attach_sdio(host, ocr))
  893. mmc_power_off(host);
  894. goto out;
  895. }
  896. /*
  897. * ...then normal SD...
  898. */
  899. err = mmc_send_app_op_cond(host, 0, &ocr);
  900. if (!err) {
  901. if (mmc_attach_sd(host, ocr))
  902. mmc_power_off(host);
  903. goto out;
  904. }
  905. /*
  906. * ...and finally MMC.
  907. */
  908. err = mmc_send_op_cond(host, 0, &ocr);
  909. if (!err) {
  910. if (mmc_attach_mmc(host, ocr))
  911. mmc_power_off(host);
  912. goto out;
  913. }
  914. mmc_release_host(host);
  915. mmc_power_off(host);
  916. out:
  917. if (host->caps & MMC_CAP_NEEDS_POLL)
  918. mmc_schedule_delayed_work(&host->detect, HZ);
  919. }
  920. void mmc_start_host(struct mmc_host *host)
  921. {
  922. mmc_power_off(host);
  923. mmc_detect_change(host, 0);
  924. }
  925. void mmc_stop_host(struct mmc_host *host)
  926. {
  927. #ifdef CONFIG_MMC_DEBUG
  928. unsigned long flags;
  929. spin_lock_irqsave(&host->lock, flags);
  930. host->removed = 1;
  931. spin_unlock_irqrestore(&host->lock, flags);
  932. #endif
  933. if (host->caps & MMC_CAP_DISABLE)
  934. cancel_delayed_work(&host->disable);
  935. cancel_delayed_work(&host->detect);
  936. mmc_flush_scheduled_work();
  937. mmc_bus_get(host);
  938. if (host->bus_ops && !host->bus_dead) {
  939. if (host->bus_ops->remove)
  940. host->bus_ops->remove(host);
  941. mmc_claim_host(host);
  942. mmc_detach_bus(host);
  943. mmc_release_host(host);
  944. }
  945. mmc_bus_put(host);
  946. BUG_ON(host->card);
  947. mmc_power_off(host);
  948. }
  949. #ifdef CONFIG_PM
  950. /**
  951. * mmc_suspend_host - suspend a host
  952. * @host: mmc host
  953. * @state: suspend mode (PM_SUSPEND_xxx)
  954. */
  955. int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
  956. {
  957. if (host->caps & MMC_CAP_DISABLE)
  958. cancel_delayed_work(&host->disable);
  959. cancel_delayed_work(&host->detect);
  960. mmc_flush_scheduled_work();
  961. mmc_bus_get(host);
  962. if (host->bus_ops && !host->bus_dead) {
  963. if (host->bus_ops->suspend)
  964. host->bus_ops->suspend(host);
  965. if (!host->bus_ops->resume) {
  966. if (host->bus_ops->remove)
  967. host->bus_ops->remove(host);
  968. mmc_claim_host(host);
  969. mmc_detach_bus(host);
  970. mmc_release_host(host);
  971. }
  972. }
  973. mmc_bus_put(host);
  974. mmc_power_off(host);
  975. return 0;
  976. }
  977. EXPORT_SYMBOL(mmc_suspend_host);
  978. /**
  979. * mmc_resume_host - resume a previously suspended host
  980. * @host: mmc host
  981. */
  982. int mmc_resume_host(struct mmc_host *host)
  983. {
  984. mmc_bus_get(host);
  985. if (host->bus_ops && !host->bus_dead) {
  986. mmc_power_up(host);
  987. mmc_select_voltage(host, host->ocr);
  988. BUG_ON(!host->bus_ops->resume);
  989. host->bus_ops->resume(host);
  990. }
  991. mmc_bus_put(host);
  992. /*
  993. * We add a slight delay here so that resume can progress
  994. * in parallel.
  995. */
  996. mmc_detect_change(host, 1);
  997. return 0;
  998. }
  999. EXPORT_SYMBOL(mmc_resume_host);
  1000. #endif
  1001. static int __init mmc_init(void)
  1002. {
  1003. int ret;
  1004. workqueue = create_singlethread_workqueue("kmmcd");
  1005. if (!workqueue)
  1006. return -ENOMEM;
  1007. ret = mmc_register_bus();
  1008. if (ret)
  1009. goto destroy_workqueue;
  1010. ret = mmc_register_host_class();
  1011. if (ret)
  1012. goto unregister_bus;
  1013. ret = sdio_register_bus();
  1014. if (ret)
  1015. goto unregister_host_class;
  1016. return 0;
  1017. unregister_host_class:
  1018. mmc_unregister_host_class();
  1019. unregister_bus:
  1020. mmc_unregister_bus();
  1021. destroy_workqueue:
  1022. destroy_workqueue(workqueue);
  1023. return ret;
  1024. }
  1025. static void __exit mmc_exit(void)
  1026. {
  1027. sdio_unregister_bus();
  1028. mmc_unregister_host_class();
  1029. mmc_unregister_bus();
  1030. destroy_workqueue(workqueue);
  1031. }
  1032. subsys_initcall(mmc_init);
  1033. module_exit(mmc_exit);
  1034. MODULE_LICENSE("GPL");