core.c 15 KB

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