core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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-2007 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 <asm/scatterlist.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/mmc/card.h>
  24. #include <linux/mmc/host.h>
  25. #include <linux/mmc/mmc.h>
  26. #include <linux/mmc/sd.h>
  27. #include "core.h"
  28. #include "bus.h"
  29. #include "host.h"
  30. #include "mmc_ops.h"
  31. #include "sd_ops.h"
  32. extern int mmc_attach_mmc(struct mmc_host *host, u32 ocr);
  33. extern int mmc_attach_sd(struct mmc_host *host, u32 ocr);
  34. static struct workqueue_struct *workqueue;
  35. /*
  36. * Internal function. Schedule delayed work in the MMC work queue.
  37. */
  38. static int mmc_schedule_delayed_work(struct delayed_work *work,
  39. unsigned long delay)
  40. {
  41. return queue_delayed_work(workqueue, work, delay);
  42. }
  43. /*
  44. * Internal function. Flush all scheduled work from the MMC work queue.
  45. */
  46. static void mmc_flush_scheduled_work(void)
  47. {
  48. flush_workqueue(workqueue);
  49. }
  50. /**
  51. * mmc_request_done - finish processing an MMC request
  52. * @host: MMC host which completed request
  53. * @mrq: MMC request which request
  54. *
  55. * MMC drivers should call this function when they have completed
  56. * their processing of a request.
  57. */
  58. void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
  59. {
  60. struct mmc_command *cmd = mrq->cmd;
  61. int err = cmd->error;
  62. pr_debug("%s: req done (CMD%u): %d/%d/%d: %08x %08x %08x %08x\n",
  63. mmc_hostname(host), cmd->opcode, err,
  64. mrq->data ? mrq->data->error : 0,
  65. mrq->stop ? mrq->stop->error : 0,
  66. cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
  67. if (err && cmd->retries) {
  68. cmd->retries--;
  69. cmd->error = 0;
  70. host->ops->request(host, mrq);
  71. } else if (mrq->done) {
  72. mrq->done(mrq);
  73. }
  74. }
  75. EXPORT_SYMBOL(mmc_request_done);
  76. /**
  77. * mmc_start_request - start a command on a host
  78. * @host: MMC host to start command on
  79. * @mrq: MMC request to start
  80. *
  81. * Queue a command on the specified host. We expect the
  82. * caller to be holding the host lock with interrupts disabled.
  83. */
  84. void
  85. mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
  86. {
  87. #ifdef CONFIG_MMC_DEBUG
  88. unsigned int i, sz;
  89. #endif
  90. pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
  91. mmc_hostname(host), mrq->cmd->opcode,
  92. mrq->cmd->arg, mrq->cmd->flags);
  93. WARN_ON(!host->claimed);
  94. mrq->cmd->error = 0;
  95. mrq->cmd->mrq = mrq;
  96. if (mrq->data) {
  97. BUG_ON(mrq->data->blksz > host->max_blk_size);
  98. BUG_ON(mrq->data->blocks > host->max_blk_count);
  99. BUG_ON(mrq->data->blocks * mrq->data->blksz >
  100. host->max_req_size);
  101. #ifdef CONFIG_MMC_DEBUG
  102. sz = 0;
  103. for (i = 0;i < mrq->data->sg_len;i++)
  104. sz += mrq->data->sg[i].length;
  105. BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
  106. #endif
  107. mrq->cmd->data = mrq->data;
  108. mrq->data->error = 0;
  109. mrq->data->mrq = mrq;
  110. if (mrq->stop) {
  111. mrq->data->stop = mrq->stop;
  112. mrq->stop->error = 0;
  113. mrq->stop->mrq = mrq;
  114. }
  115. }
  116. host->ops->request(host, mrq);
  117. }
  118. EXPORT_SYMBOL(mmc_start_request);
  119. static void mmc_wait_done(struct mmc_request *mrq)
  120. {
  121. complete(mrq->done_data);
  122. }
  123. int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
  124. {
  125. DECLARE_COMPLETION_ONSTACK(complete);
  126. mrq->done_data = &complete;
  127. mrq->done = mmc_wait_done;
  128. mmc_start_request(host, mrq);
  129. wait_for_completion(&complete);
  130. return 0;
  131. }
  132. EXPORT_SYMBOL(mmc_wait_for_req);
  133. /**
  134. * mmc_wait_for_cmd - start a command and wait for completion
  135. * @host: MMC host to start command
  136. * @cmd: MMC command to start
  137. * @retries: maximum number of retries
  138. *
  139. * Start a new MMC command for a host, and wait for the command
  140. * to complete. Return any error that occurred while the command
  141. * was executing. Do not attempt to parse the response.
  142. */
  143. int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
  144. {
  145. struct mmc_request mrq;
  146. BUG_ON(!host->claimed);
  147. memset(&mrq, 0, sizeof(struct mmc_request));
  148. memset(cmd->resp, 0, sizeof(cmd->resp));
  149. cmd->retries = retries;
  150. mrq.cmd = cmd;
  151. cmd->data = NULL;
  152. mmc_wait_for_req(host, &mrq);
  153. return cmd->error;
  154. }
  155. EXPORT_SYMBOL(mmc_wait_for_cmd);
  156. /**
  157. * mmc_set_data_timeout - set the timeout for a data command
  158. * @data: data phase for command
  159. * @card: the MMC card associated with the data transfer
  160. * @write: flag to differentiate reads from writes
  161. */
  162. void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
  163. int write)
  164. {
  165. unsigned int mult;
  166. /*
  167. * SD cards use a 100 multiplier rather than 10
  168. */
  169. mult = mmc_card_sd(card) ? 100 : 10;
  170. /*
  171. * Scale up the multiplier (and therefore the timeout) by
  172. * the r2w factor for writes.
  173. */
  174. if (write)
  175. mult <<= card->csd.r2w_factor;
  176. data->timeout_ns = card->csd.tacc_ns * mult;
  177. data->timeout_clks = card->csd.tacc_clks * mult;
  178. /*
  179. * SD cards also have an upper limit on the timeout.
  180. */
  181. if (mmc_card_sd(card)) {
  182. unsigned int timeout_us, limit_us;
  183. timeout_us = data->timeout_ns / 1000;
  184. timeout_us += data->timeout_clks * 1000 /
  185. (card->host->ios.clock / 1000);
  186. if (write)
  187. limit_us = 250000;
  188. else
  189. limit_us = 100000;
  190. /*
  191. * SDHC cards always use these fixed values.
  192. */
  193. if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
  194. data->timeout_ns = limit_us * 1000;
  195. data->timeout_clks = 0;
  196. }
  197. }
  198. }
  199. EXPORT_SYMBOL(mmc_set_data_timeout);
  200. /**
  201. * __mmc_claim_host - exclusively claim a host
  202. * @host: mmc host to claim
  203. * @card: mmc card to claim host for
  204. *
  205. * Claim a host for a set of operations. If a valid card
  206. * is passed and this wasn't the last card selected, select
  207. * the card before returning.
  208. *
  209. * Note: you should use mmc_card_claim_host or mmc_claim_host.
  210. */
  211. void mmc_claim_host(struct mmc_host *host)
  212. {
  213. DECLARE_WAITQUEUE(wait, current);
  214. unsigned long flags;
  215. add_wait_queue(&host->wq, &wait);
  216. spin_lock_irqsave(&host->lock, flags);
  217. while (1) {
  218. set_current_state(TASK_UNINTERRUPTIBLE);
  219. if (!host->claimed)
  220. break;
  221. spin_unlock_irqrestore(&host->lock, flags);
  222. schedule();
  223. spin_lock_irqsave(&host->lock, flags);
  224. }
  225. set_current_state(TASK_RUNNING);
  226. host->claimed = 1;
  227. spin_unlock_irqrestore(&host->lock, flags);
  228. remove_wait_queue(&host->wq, &wait);
  229. }
  230. EXPORT_SYMBOL(mmc_claim_host);
  231. /**
  232. * mmc_release_host - release a host
  233. * @host: mmc host to release
  234. *
  235. * Release a MMC host, allowing others to claim the host
  236. * for their operations.
  237. */
  238. void mmc_release_host(struct mmc_host *host)
  239. {
  240. unsigned long flags;
  241. BUG_ON(!host->claimed);
  242. spin_lock_irqsave(&host->lock, flags);
  243. host->claimed = 0;
  244. spin_unlock_irqrestore(&host->lock, flags);
  245. wake_up(&host->wq);
  246. }
  247. EXPORT_SYMBOL(mmc_release_host);
  248. /*
  249. * Internal function that does the actual ios call to the host driver,
  250. * optionally printing some debug output.
  251. */
  252. static inline void mmc_set_ios(struct mmc_host *host)
  253. {
  254. struct mmc_ios *ios = &host->ios;
  255. pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
  256. "width %u timing %u\n",
  257. mmc_hostname(host), ios->clock, ios->bus_mode,
  258. ios->power_mode, ios->chip_select, ios->vdd,
  259. ios->bus_width, ios->timing);
  260. host->ops->set_ios(host, ios);
  261. }
  262. /*
  263. * Control chip select pin on a host.
  264. */
  265. void mmc_set_chip_select(struct mmc_host *host, int mode)
  266. {
  267. host->ios.chip_select = mode;
  268. mmc_set_ios(host);
  269. }
  270. /*
  271. * Sets the host clock to the highest possible frequency that
  272. * is below "hz".
  273. */
  274. void mmc_set_clock(struct mmc_host *host, unsigned int hz)
  275. {
  276. WARN_ON(hz < host->f_min);
  277. if (hz > host->f_max)
  278. hz = host->f_max;
  279. host->ios.clock = hz;
  280. mmc_set_ios(host);
  281. }
  282. /*
  283. * Change the bus mode (open drain/push-pull) of a host.
  284. */
  285. void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
  286. {
  287. host->ios.bus_mode = mode;
  288. mmc_set_ios(host);
  289. }
  290. /*
  291. * Change data bus width of a host.
  292. */
  293. void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
  294. {
  295. host->ios.bus_width = width;
  296. mmc_set_ios(host);
  297. }
  298. /*
  299. * Mask off any voltages we don't support and select
  300. * the lowest voltage
  301. */
  302. u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
  303. {
  304. int bit;
  305. ocr &= host->ocr_avail;
  306. bit = ffs(ocr);
  307. if (bit) {
  308. bit -= 1;
  309. ocr &= 3 << bit;
  310. host->ios.vdd = bit;
  311. mmc_set_ios(host);
  312. } else {
  313. ocr = 0;
  314. }
  315. return ocr;
  316. }
  317. /*
  318. * Select timing parameters for host.
  319. */
  320. void mmc_set_timing(struct mmc_host *host, unsigned int timing)
  321. {
  322. host->ios.timing = timing;
  323. mmc_set_ios(host);
  324. }
  325. /*
  326. * Apply power to the MMC stack. This is a two-stage process.
  327. * First, we enable power to the card without the clock running.
  328. * We then wait a bit for the power to stabilise. Finally,
  329. * enable the bus drivers and clock to the card.
  330. *
  331. * We must _NOT_ enable the clock prior to power stablising.
  332. *
  333. * If a host does all the power sequencing itself, ignore the
  334. * initial MMC_POWER_UP stage.
  335. */
  336. static void mmc_power_up(struct mmc_host *host)
  337. {
  338. int bit = fls(host->ocr_avail) - 1;
  339. host->ios.vdd = bit;
  340. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  341. host->ios.chip_select = MMC_CS_DONTCARE;
  342. host->ios.power_mode = MMC_POWER_UP;
  343. host->ios.bus_width = MMC_BUS_WIDTH_1;
  344. host->ios.timing = MMC_TIMING_LEGACY;
  345. mmc_set_ios(host);
  346. mmc_delay(1);
  347. host->ios.clock = host->f_min;
  348. host->ios.power_mode = MMC_POWER_ON;
  349. mmc_set_ios(host);
  350. mmc_delay(2);
  351. }
  352. static void mmc_power_off(struct mmc_host *host)
  353. {
  354. host->ios.clock = 0;
  355. host->ios.vdd = 0;
  356. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  357. host->ios.chip_select = MMC_CS_DONTCARE;
  358. host->ios.power_mode = MMC_POWER_OFF;
  359. host->ios.bus_width = MMC_BUS_WIDTH_1;
  360. host->ios.timing = MMC_TIMING_LEGACY;
  361. mmc_set_ios(host);
  362. }
  363. /*
  364. * Assign a mmc bus handler to a host. Only one bus handler may control a
  365. * host at any given time.
  366. */
  367. void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
  368. {
  369. unsigned long flags;
  370. BUG_ON(!host);
  371. BUG_ON(!ops);
  372. BUG_ON(!host->claimed);
  373. spin_lock_irqsave(&host->lock, flags);
  374. BUG_ON(host->bus_ops);
  375. BUG_ON(host->bus_refs);
  376. host->bus_ops = ops;
  377. host->bus_refs = 1;
  378. host->bus_dead = 0;
  379. spin_unlock_irqrestore(&host->lock, flags);
  380. }
  381. /*
  382. * Remove the current bus handler from a host. Assumes that there are
  383. * no interesting cards left, so the bus is powered down.
  384. */
  385. void mmc_detach_bus(struct mmc_host *host)
  386. {
  387. unsigned long flags;
  388. BUG_ON(!host);
  389. BUG_ON(!host->claimed);
  390. BUG_ON(!host->bus_ops);
  391. spin_lock_irqsave(&host->lock, flags);
  392. host->bus_dead = 1;
  393. spin_unlock_irqrestore(&host->lock, flags);
  394. mmc_power_off(host);
  395. mmc_bus_put(host);
  396. }
  397. /*
  398. * Cleanup when the last reference to the bus operator is dropped.
  399. */
  400. void __mmc_release_bus(struct mmc_host *host)
  401. {
  402. BUG_ON(!host);
  403. BUG_ON(host->bus_refs);
  404. BUG_ON(!host->bus_dead);
  405. host->bus_ops = NULL;
  406. }
  407. /**
  408. * mmc_detect_change - process change of state on a MMC socket
  409. * @host: host which changed state.
  410. * @delay: optional delay to wait before detection (jiffies)
  411. *
  412. * All we know is that card(s) have been inserted or removed
  413. * from the socket(s). We don't know which socket or cards.
  414. */
  415. void mmc_detect_change(struct mmc_host *host, unsigned long delay)
  416. {
  417. #ifdef CONFIG_MMC_DEBUG
  418. unsigned long flags;
  419. spin_lock_irqsave(&host->lock, flags);
  420. BUG_ON(host->removed);
  421. spin_unlock_irqrestore(&host->lock, flags);
  422. #endif
  423. mmc_schedule_delayed_work(&host->detect, delay);
  424. }
  425. EXPORT_SYMBOL(mmc_detect_change);
  426. void mmc_rescan(struct work_struct *work)
  427. {
  428. struct mmc_host *host =
  429. container_of(work, struct mmc_host, detect.work);
  430. u32 ocr;
  431. int err;
  432. mmc_bus_get(host);
  433. if (host->bus_ops == NULL) {
  434. /*
  435. * Only we can add a new handler, so it's safe to
  436. * release the lock here.
  437. */
  438. mmc_bus_put(host);
  439. mmc_claim_host(host);
  440. mmc_power_up(host);
  441. mmc_go_idle(host);
  442. mmc_send_if_cond(host, host->ocr_avail);
  443. err = mmc_send_app_op_cond(host, 0, &ocr);
  444. if (err == MMC_ERR_NONE) {
  445. if (mmc_attach_sd(host, ocr))
  446. mmc_power_off(host);
  447. } else {
  448. /*
  449. * If we fail to detect any SD cards then try
  450. * searching for MMC cards.
  451. */
  452. err = mmc_send_op_cond(host, 0, &ocr);
  453. if (err == MMC_ERR_NONE) {
  454. if (mmc_attach_mmc(host, ocr))
  455. mmc_power_off(host);
  456. } else {
  457. mmc_power_off(host);
  458. mmc_release_host(host);
  459. }
  460. }
  461. } else {
  462. if (host->bus_ops->detect && !host->bus_dead)
  463. host->bus_ops->detect(host);
  464. mmc_bus_put(host);
  465. }
  466. }
  467. void mmc_start_host(struct mmc_host *host)
  468. {
  469. mmc_power_off(host);
  470. mmc_detect_change(host, 0);
  471. }
  472. void mmc_stop_host(struct mmc_host *host)
  473. {
  474. #ifdef CONFIG_MMC_DEBUG
  475. unsigned long flags;
  476. spin_lock_irqsave(&host->lock, flags);
  477. host->removed = 1;
  478. spin_unlock_irqrestore(&host->lock, flags);
  479. #endif
  480. mmc_flush_scheduled_work();
  481. mmc_bus_get(host);
  482. if (host->bus_ops && !host->bus_dead) {
  483. if (host->bus_ops->remove)
  484. host->bus_ops->remove(host);
  485. mmc_claim_host(host);
  486. mmc_detach_bus(host);
  487. mmc_release_host(host);
  488. }
  489. mmc_bus_put(host);
  490. BUG_ON(host->card);
  491. mmc_power_off(host);
  492. }
  493. #ifdef CONFIG_PM
  494. /**
  495. * mmc_suspend_host - suspend a host
  496. * @host: mmc host
  497. * @state: suspend mode (PM_SUSPEND_xxx)
  498. */
  499. int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
  500. {
  501. mmc_flush_scheduled_work();
  502. mmc_bus_get(host);
  503. if (host->bus_ops && !host->bus_dead) {
  504. if (host->bus_ops->suspend)
  505. host->bus_ops->suspend(host);
  506. if (!host->bus_ops->resume) {
  507. if (host->bus_ops->remove)
  508. host->bus_ops->remove(host);
  509. mmc_claim_host(host);
  510. mmc_detach_bus(host);
  511. mmc_release_host(host);
  512. }
  513. }
  514. mmc_bus_put(host);
  515. mmc_power_off(host);
  516. return 0;
  517. }
  518. EXPORT_SYMBOL(mmc_suspend_host);
  519. /**
  520. * mmc_resume_host - resume a previously suspended host
  521. * @host: mmc host
  522. */
  523. int mmc_resume_host(struct mmc_host *host)
  524. {
  525. mmc_bus_get(host);
  526. if (host->bus_ops && !host->bus_dead) {
  527. mmc_power_up(host);
  528. BUG_ON(!host->bus_ops->resume);
  529. host->bus_ops->resume(host);
  530. }
  531. mmc_bus_put(host);
  532. /*
  533. * We add a slight delay here so that resume can progress
  534. * in parallel.
  535. */
  536. mmc_detect_change(host, 1);
  537. return 0;
  538. }
  539. EXPORT_SYMBOL(mmc_resume_host);
  540. #endif
  541. static int __init mmc_init(void)
  542. {
  543. int ret;
  544. workqueue = create_singlethread_workqueue("kmmcd");
  545. if (!workqueue)
  546. return -ENOMEM;
  547. ret = mmc_register_bus();
  548. if (ret == 0) {
  549. ret = mmc_register_host_class();
  550. if (ret)
  551. mmc_unregister_bus();
  552. }
  553. return ret;
  554. }
  555. static void __exit mmc_exit(void)
  556. {
  557. mmc_unregister_host_class();
  558. mmc_unregister_bus();
  559. destroy_workqueue(workqueue);
  560. }
  561. module_init(mmc_init);
  562. module_exit(mmc_exit);
  563. MODULE_LICENSE("GPL");