core.c 15 KB

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