2 * linux/drivers/mmc/mmc.c
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 #include <linux/config.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/completion.h>
15 #include <linux/device.h>
16 #include <linux/delay.h>
17 #include <linux/pagemap.h>
18 #include <linux/err.h>
20 #include <linux/mmc/card.h>
21 #include <linux/mmc/host.h>
22 #include <linux/mmc/protocol.h>
26 #ifdef CONFIG_MMC_DEBUG
27 #define DBG(x...) printk(KERN_DEBUG x)
29 #define DBG(x...) do { } while (0)
35 * OCR Bit positions to 10s of Vdd mV.
37 static const unsigned short mmc_ocr_bit_to_vdd[] = {
38 150, 155, 160, 165, 170, 180, 190, 200,
39 210, 220, 230, 240, 250, 260, 270, 280,
40 290, 300, 310, 320, 330, 340, 350, 360
43 static const unsigned int tran_exp[] = {
44 10000, 100000, 1000000, 10000000,
48 static const unsigned char tran_mant[] = {
49 0, 10, 12, 13, 15, 20, 25, 30,
50 35, 40, 45, 50, 55, 60, 70, 80,
53 static const unsigned int tacc_exp[] = {
54 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
57 static const unsigned int tacc_mant[] = {
58 0, 10, 12, 13, 15, 20, 25, 30,
59 35, 40, 45, 50, 55, 60, 70, 80,
64 * mmc_request_done - finish processing an MMC command
65 * @host: MMC host which completed command
66 * @mrq: MMC request which completed
68 * MMC drivers should call this function when they have completed
69 * their processing of a command. This should be called before the
70 * data part of the command has completed.
72 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
74 struct mmc_command *cmd = mrq->cmd;
75 int err = mrq->cmd->error;
76 DBG("MMC: req done (%02x): %d: %08x %08x %08x %08x\n", cmd->opcode,
77 err, cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
79 if (err && cmd->retries) {
82 host->ops->request(host, mrq);
83 } else if (mrq->done) {
88 EXPORT_SYMBOL(mmc_request_done);
91 * mmc_start_request - start a command on a host
92 * @host: MMC host to start command on
93 * @mrq: MMC request to start
95 * Queue a command on the specified host. We expect the
96 * caller to be holding the host lock with interrupts disabled.
99 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
101 DBG("MMC: starting cmd %02x arg %08x flags %08x\n",
102 mrq->cmd->opcode, mrq->cmd->arg, mrq->cmd->flags);
104 WARN_ON(host->card_busy == NULL);
109 mrq->cmd->data = mrq->data;
110 mrq->data->error = 0;
111 mrq->data->mrq = mrq;
113 mrq->data->stop = mrq->stop;
114 mrq->stop->error = 0;
115 mrq->stop->mrq = mrq;
118 host->ops->request(host, mrq);
121 EXPORT_SYMBOL(mmc_start_request);
123 static void mmc_wait_done(struct mmc_request *mrq)
125 complete(mrq->done_data);
128 int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
130 DECLARE_COMPLETION(complete);
132 mrq->done_data = &complete;
133 mrq->done = mmc_wait_done;
135 mmc_start_request(host, mrq);
137 wait_for_completion(&complete);
142 EXPORT_SYMBOL(mmc_wait_for_req);
145 * mmc_wait_for_cmd - start a command and wait for completion
146 * @host: MMC host to start command
147 * @cmd: MMC command to start
148 * @retries: maximum number of retries
150 * Start a new MMC command for a host, and wait for the command
151 * to complete. Return any error that occurred while the command
152 * was executing. Do not attempt to parse the response.
154 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
156 struct mmc_request mrq;
158 BUG_ON(host->card_busy == NULL);
160 memset(&mrq, 0, sizeof(struct mmc_request));
162 memset(cmd->resp, 0, sizeof(cmd->resp));
163 cmd->retries = retries;
168 mmc_wait_for_req(host, &mrq);
173 EXPORT_SYMBOL(mmc_wait_for_cmd);
176 * mmc_wait_for_app_cmd - start an application command and wait for
178 * @host: MMC host to start command
179 * @rca: RCA to send MMC_APP_CMD to
180 * @cmd: MMC command to start
181 * @retries: maximum number of retries
183 * Sends a MMC_APP_CMD, checks the card response, sends the command
184 * in the parameter and waits for it to complete. Return any error
185 * that occurred while the command was executing. Do not attempt to
186 * parse the response.
188 int mmc_wait_for_app_cmd(struct mmc_host *host, unsigned int rca,
189 struct mmc_command *cmd, int retries)
191 struct mmc_request mrq;
192 struct mmc_command appcmd;
196 BUG_ON(host->card_busy == NULL);
199 err = MMC_ERR_INVALID;
202 * We have to resend MMC_APP_CMD for each attempt so
203 * we cannot use the retries field in mmc_command.
205 for (i = 0;i <= retries;i++) {
206 memset(&mrq, 0, sizeof(struct mmc_request));
208 appcmd.opcode = MMC_APP_CMD;
209 appcmd.arg = rca << 16;
210 appcmd.flags = MMC_RSP_R1;
212 memset(appcmd.resp, 0, sizeof(appcmd.resp));
218 mmc_wait_for_req(host, &mrq);
225 /* Check that card supported application commands */
226 if (!(appcmd.resp[0] & R1_APP_CMD))
227 return MMC_ERR_FAILED;
229 memset(&mrq, 0, sizeof(struct mmc_request));
231 memset(cmd->resp, 0, sizeof(cmd->resp));
237 mmc_wait_for_req(host, &mrq);
240 if (cmd->error == MMC_ERR_NONE)
247 EXPORT_SYMBOL(mmc_wait_for_app_cmd);
250 * __mmc_claim_host - exclusively claim a host
251 * @host: mmc host to claim
252 * @card: mmc card to claim host for
254 * Claim a host for a set of operations. If a valid card
255 * is passed and this wasn't the last card selected, select
256 * the card before returning.
258 * Note: you should use mmc_card_claim_host or mmc_claim_host.
260 int __mmc_claim_host(struct mmc_host *host, struct mmc_card *card)
262 DECLARE_WAITQUEUE(wait, current);
266 add_wait_queue(&host->wq, &wait);
267 spin_lock_irqsave(&host->lock, flags);
269 set_current_state(TASK_UNINTERRUPTIBLE);
270 if (host->card_busy == NULL)
272 spin_unlock_irqrestore(&host->lock, flags);
274 spin_lock_irqsave(&host->lock, flags);
276 set_current_state(TASK_RUNNING);
277 host->card_busy = card;
278 spin_unlock_irqrestore(&host->lock, flags);
279 remove_wait_queue(&host->wq, &wait);
281 if (card != (void *)-1 && host->card_selected != card) {
282 struct mmc_command cmd;
284 host->card_selected = card;
286 cmd.opcode = MMC_SELECT_CARD;
287 cmd.arg = card->rca << 16;
288 cmd.flags = MMC_RSP_R1;
290 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
296 EXPORT_SYMBOL(__mmc_claim_host);
299 * mmc_release_host - release a host
300 * @host: mmc host to release
302 * Release a MMC host, allowing others to claim the host
303 * for their operations.
305 void mmc_release_host(struct mmc_host *host)
309 BUG_ON(host->card_busy == NULL);
311 spin_lock_irqsave(&host->lock, flags);
312 host->card_busy = NULL;
313 spin_unlock_irqrestore(&host->lock, flags);
318 EXPORT_SYMBOL(mmc_release_host);
321 * Ensure that no card is selected.
323 static void mmc_deselect_cards(struct mmc_host *host)
325 struct mmc_command cmd;
327 if (host->card_selected) {
328 host->card_selected = NULL;
330 cmd.opcode = MMC_SELECT_CARD;
332 cmd.flags = MMC_RSP_NONE;
334 mmc_wait_for_cmd(host, &cmd, 0);
339 static inline void mmc_delay(unsigned int ms)
341 if (ms < HZ / 1000) {
345 msleep_interruptible (ms);
350 * Mask off any voltages we don't support and select
353 static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
357 ocr &= host->ocr_avail;
366 host->ops->set_ios(host, &host->ios);
374 #define UNSTUFF_BITS(resp,start,size) \
376 const int __size = size; \
377 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
378 const int __off = 3 - ((start) / 32); \
379 const int __shft = (start) & 31; \
382 __res = resp[__off] >> __shft; \
383 if (__size + __shft > 32) \
384 __res |= resp[__off-1] << ((32 - __shft) % 32); \
389 * Given the decoded CSD structure, decode the raw CID to our CID structure.
391 static void mmc_decode_cid(struct mmc_card *card)
393 u32 *resp = card->raw_cid;
395 memset(&card->cid, 0, sizeof(struct mmc_cid));
397 if (mmc_card_sd(card)) {
399 * SD doesn't currently have a version field so we will
400 * have to assume we can parse this.
402 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
403 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
404 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
405 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
406 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
407 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
408 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
409 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
410 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
411 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
412 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
413 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
415 card->cid.year += 2000; /* SD cards year offset */
419 * The selection of the format here is based upon published
420 * specs from sandisk and from what people have reported.
422 switch (card->csd.mmca_vsn) {
423 case 0: /* MMC v1.0 - v1.2 */
424 case 1: /* MMC v1.4 */
425 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
426 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
427 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
428 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
429 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
430 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
431 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
432 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
433 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
434 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
435 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
436 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
437 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
440 case 2: /* MMC v2.0 - v2.2 */
441 case 3: /* MMC v3.1 - v3.3 */
442 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
443 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
444 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
445 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
446 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
447 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
448 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
449 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
450 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
451 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
452 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
456 printk("%s: card has unknown MMCA version %d\n",
457 mmc_hostname(card->host), card->csd.mmca_vsn);
458 mmc_card_set_bad(card);
465 * Given a 128-bit response, decode to our card CSD structure.
467 static void mmc_decode_csd(struct mmc_card *card)
469 struct mmc_csd *csd = &card->csd;
470 unsigned int e, m, csd_struct;
471 u32 *resp = card->raw_csd;
473 if (mmc_card_sd(card)) {
474 csd_struct = UNSTUFF_BITS(resp, 126, 2);
475 if (csd_struct != 0) {
476 printk("%s: unrecognised CSD structure version %d\n",
477 mmc_hostname(card->host), csd_struct);
478 mmc_card_set_bad(card);
482 m = UNSTUFF_BITS(resp, 115, 4);
483 e = UNSTUFF_BITS(resp, 112, 3);
484 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
485 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
487 m = UNSTUFF_BITS(resp, 99, 4);
488 e = UNSTUFF_BITS(resp, 96, 3);
489 csd->max_dtr = tran_exp[e] * tran_mant[m];
490 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
492 e = UNSTUFF_BITS(resp, 47, 3);
493 m = UNSTUFF_BITS(resp, 62, 12);
494 csd->capacity = (1 + m) << (e + 2);
496 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
500 * We only understand CSD structure v1.1 and v1.2.
501 * v1.2 has extra information in bits 15, 11 and 10.
503 csd_struct = UNSTUFF_BITS(resp, 126, 2);
504 if (csd_struct != 1 && csd_struct != 2) {
505 printk("%s: unrecognised CSD structure version %d\n",
506 mmc_hostname(card->host), csd_struct);
507 mmc_card_set_bad(card);
511 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
512 m = UNSTUFF_BITS(resp, 115, 4);
513 e = UNSTUFF_BITS(resp, 112, 3);
514 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
515 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
517 m = UNSTUFF_BITS(resp, 99, 4);
518 e = UNSTUFF_BITS(resp, 96, 3);
519 csd->max_dtr = tran_exp[e] * tran_mant[m];
520 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
522 e = UNSTUFF_BITS(resp, 47, 3);
523 m = UNSTUFF_BITS(resp, 62, 12);
524 csd->capacity = (1 + m) << (e + 2);
526 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
531 * Locate a MMC card on this MMC host given a raw CID.
533 static struct mmc_card *mmc_find_card(struct mmc_host *host, u32 *raw_cid)
535 struct mmc_card *card;
537 list_for_each_entry(card, &host->cards, node) {
538 if (memcmp(card->raw_cid, raw_cid, sizeof(card->raw_cid)) == 0)
545 * Allocate a new MMC card, and assign a unique RCA.
547 static struct mmc_card *
548 mmc_alloc_card(struct mmc_host *host, u32 *raw_cid, unsigned int *frca)
550 struct mmc_card *card, *c;
551 unsigned int rca = *frca;
553 card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL);
555 return ERR_PTR(-ENOMEM);
557 mmc_init_card(card, host);
558 memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid));
561 list_for_each_entry(c, &host->cards, node)
575 * Tell attached cards to go to IDLE state
577 static void mmc_idle_cards(struct mmc_host *host)
579 struct mmc_command cmd;
581 host->ios.chip_select = MMC_CS_HIGH;
582 host->ops->set_ios(host, &host->ios);
586 cmd.opcode = MMC_GO_IDLE_STATE;
588 cmd.flags = MMC_RSP_NONE;
590 mmc_wait_for_cmd(host, &cmd, 0);
594 host->ios.chip_select = MMC_CS_DONTCARE;
595 host->ops->set_ios(host, &host->ios);
601 * Apply power to the MMC stack.
603 static void mmc_power_up(struct mmc_host *host)
605 int bit = fls(host->ocr_avail) - 1;
608 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
609 host->ios.chip_select = MMC_CS_DONTCARE;
610 host->ios.power_mode = MMC_POWER_UP;
611 host->ops->set_ios(host, &host->ios);
615 host->ios.clock = host->f_min;
616 host->ios.power_mode = MMC_POWER_ON;
617 host->ops->set_ios(host, &host->ios);
622 static void mmc_power_off(struct mmc_host *host)
626 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
627 host->ios.chip_select = MMC_CS_DONTCARE;
628 host->ios.power_mode = MMC_POWER_OFF;
629 host->ops->set_ios(host, &host->ios);
632 static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
634 struct mmc_command cmd;
637 cmd.opcode = MMC_SEND_OP_COND;
639 cmd.flags = MMC_RSP_R3;
641 for (i = 100; i; i--) {
642 err = mmc_wait_for_cmd(host, &cmd, 0);
643 if (err != MMC_ERR_NONE)
646 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
649 err = MMC_ERR_TIMEOUT;
660 static int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
662 struct mmc_command cmd;
665 cmd.opcode = SD_APP_OP_COND;
667 cmd.flags = MMC_RSP_R3;
669 for (i = 100; i; i--) {
670 err = mmc_wait_for_app_cmd(host, 0, &cmd, CMD_RETRIES);
671 if (err != MMC_ERR_NONE)
674 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
677 err = MMC_ERR_TIMEOUT;
689 * Discover cards by requesting their CID. If this command
690 * times out, it is not an error; there are no further cards
691 * to be discovered. Add new cards to the list.
693 * Create a mmc_card entry for each discovered card, assigning
694 * it an RCA, and save the raw CID for decoding later.
696 static void mmc_discover_cards(struct mmc_host *host)
698 struct mmc_card *card;
699 unsigned int first_rca = 1, err;
702 struct mmc_command cmd;
704 cmd.opcode = MMC_ALL_SEND_CID;
706 cmd.flags = MMC_RSP_R2;
708 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
709 if (err == MMC_ERR_TIMEOUT) {
713 if (err != MMC_ERR_NONE) {
714 printk(KERN_ERR "%s: error requesting CID: %d\n",
715 mmc_hostname(host), err);
719 card = mmc_find_card(host, cmd.resp);
721 card = mmc_alloc_card(host, cmd.resp, &first_rca);
726 list_add(&card->node, &host->cards);
729 card->state &= ~MMC_STATE_DEAD;
731 if (host->mode == MMC_MODE_SD) {
732 mmc_card_set_sd(card);
734 cmd.opcode = SD_SEND_RELATIVE_ADDR;
736 cmd.flags = MMC_RSP_R1;
738 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
739 if (err != MMC_ERR_NONE)
740 mmc_card_set_dead(card);
742 card->rca = cmd.resp[0] >> 16;
745 cmd.opcode = MMC_SET_RELATIVE_ADDR;
746 cmd.arg = card->rca << 16;
747 cmd.flags = MMC_RSP_R1;
749 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
750 if (err != MMC_ERR_NONE)
751 mmc_card_set_dead(card);
756 static void mmc_read_csds(struct mmc_host *host)
758 struct mmc_card *card;
760 list_for_each_entry(card, &host->cards, node) {
761 struct mmc_command cmd;
764 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
767 cmd.opcode = MMC_SEND_CSD;
768 cmd.arg = card->rca << 16;
769 cmd.flags = MMC_RSP_R2;
771 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
772 if (err != MMC_ERR_NONE) {
773 mmc_card_set_dead(card);
777 memcpy(card->raw_csd, cmd.resp, sizeof(card->raw_csd));
779 mmc_decode_csd(card);
780 mmc_decode_cid(card);
784 static unsigned int mmc_calculate_clock(struct mmc_host *host)
786 struct mmc_card *card;
787 unsigned int max_dtr = host->f_max;
789 list_for_each_entry(card, &host->cards, node)
790 if (!mmc_card_dead(card) && max_dtr > card->csd.max_dtr)
791 max_dtr = card->csd.max_dtr;
793 DBG("MMC: selected %d.%03dMHz transfer rate\n",
794 max_dtr / 1000000, (max_dtr / 1000) % 1000);
800 * Check whether cards we already know about are still present.
801 * We do this by requesting status, and checking whether a card
804 * A request for status does not cause a state change in data
807 static void mmc_check_cards(struct mmc_host *host)
809 struct list_head *l, *n;
811 mmc_deselect_cards(host);
813 list_for_each_safe(l, n, &host->cards) {
814 struct mmc_card *card = mmc_list_to_card(l);
815 struct mmc_command cmd;
818 cmd.opcode = MMC_SEND_STATUS;
819 cmd.arg = card->rca << 16;
820 cmd.flags = MMC_RSP_R1;
822 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
823 if (err == MMC_ERR_NONE)
826 mmc_card_set_dead(card);
830 static void mmc_setup(struct mmc_host *host)
832 if (host->ios.power_mode != MMC_POWER_ON) {
836 host->mode = MMC_MODE_MMC;
839 mmc_idle_cards(host);
841 err = mmc_send_op_cond(host, 0, &ocr);
844 * If we fail to detect any cards then try
845 * searching for SD cards.
847 if (err != MMC_ERR_NONE)
849 err = mmc_send_app_op_cond(host, 0, &ocr);
850 if (err != MMC_ERR_NONE)
853 host->mode = MMC_MODE_SD;
856 host->ocr = mmc_select_voltage(host, ocr);
859 * Since we're changing the OCR value, we seem to
860 * need to tell some cards to go back to the idle
861 * state. We wait 1ms to give cards time to
865 mmc_idle_cards(host);
867 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
868 host->ios.clock = host->f_min;
869 host->ops->set_ios(host, &host->ios);
872 * We should remember the OCR mask from the existing
873 * cards, and detect the new cards OCR mask, combine
874 * the two and re-select the VDD. However, if we do
875 * change VDD, we should do an idle, and then do a
876 * full re-initialisation. We would need to notify
877 * drivers so that they can re-setup the cards as
878 * well, while keeping their queues at bay.
880 * For the moment, we take the easy way out - if the
881 * new cards don't like our currently selected VDD,
882 * they drop off the bus.
890 * Send the selected OCR multiple times... until the cards
891 * all get the idea that they should be ready for CMD2.
892 * (My SanDisk card seems to need this.)
894 if (host->mode == MMC_MODE_SD)
895 mmc_send_app_op_cond(host, host->ocr, NULL);
897 mmc_send_op_cond(host, host->ocr, NULL);
899 mmc_discover_cards(host);
902 * Ok, now switch to push-pull mode.
904 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
905 host->ops->set_ios(host, &host->ios);
912 * mmc_detect_change - process change of state on a MMC socket
913 * @host: host which changed state.
915 * All we know is that card(s) have been inserted or removed
916 * from the socket(s). We don't know which socket or cards.
918 void mmc_detect_change(struct mmc_host *host)
920 schedule_work(&host->detect);
923 EXPORT_SYMBOL(mmc_detect_change);
926 static void mmc_rescan(void *data)
928 struct mmc_host *host = data;
929 struct list_head *l, *n;
931 mmc_claim_host(host);
933 if (host->ios.power_mode == MMC_POWER_ON)
934 mmc_check_cards(host);
938 if (!list_empty(&host->cards)) {
940 * (Re-)calculate the fastest clock rate which the
941 * attached cards and the host support.
943 host->ios.clock = mmc_calculate_clock(host);
944 host->ops->set_ios(host, &host->ios);
947 mmc_release_host(host);
949 list_for_each_safe(l, n, &host->cards) {
950 struct mmc_card *card = mmc_list_to_card(l);
953 * If this is a new and good card, register it.
955 if (!mmc_card_present(card) && !mmc_card_dead(card)) {
956 if (mmc_register_card(card))
957 mmc_card_set_dead(card);
959 mmc_card_set_present(card);
963 * If this card is dead, destroy it.
965 if (mmc_card_dead(card)) {
966 list_del(&card->node);
967 mmc_remove_card(card);
972 * If we discover that there are no cards on the
973 * bus, turn off the clock and power down.
975 if (list_empty(&host->cards))
981 * mmc_alloc_host - initialise the per-host structure.
982 * @extra: sizeof private data structure
983 * @dev: pointer to host device model structure
985 * Initialise the per-host structure.
987 struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
989 struct mmc_host *host;
991 host = mmc_alloc_host_sysfs(extra, dev);
993 spin_lock_init(&host->lock);
994 init_waitqueue_head(&host->wq);
995 INIT_LIST_HEAD(&host->cards);
996 INIT_WORK(&host->detect, mmc_rescan, host);
999 * By default, hosts do not support SGIO or large requests.
1000 * They have to set these according to their abilities.
1002 host->max_hw_segs = 1;
1003 host->max_phys_segs = 1;
1004 host->max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
1005 host->max_seg_size = PAGE_CACHE_SIZE;
1011 EXPORT_SYMBOL(mmc_alloc_host);
1014 * mmc_add_host - initialise host hardware
1017 int mmc_add_host(struct mmc_host *host)
1021 ret = mmc_add_host_sysfs(host);
1023 mmc_power_off(host);
1024 mmc_detect_change(host);
1030 EXPORT_SYMBOL(mmc_add_host);
1033 * mmc_remove_host - remove host hardware
1036 * Unregister and remove all cards associated with this host,
1037 * and power down the MMC bus.
1039 void mmc_remove_host(struct mmc_host *host)
1041 struct list_head *l, *n;
1043 list_for_each_safe(l, n, &host->cards) {
1044 struct mmc_card *card = mmc_list_to_card(l);
1046 mmc_remove_card(card);
1049 mmc_power_off(host);
1050 mmc_remove_host_sysfs(host);
1053 EXPORT_SYMBOL(mmc_remove_host);
1056 * mmc_free_host - free the host structure
1059 * Free the host once all references to it have been dropped.
1061 void mmc_free_host(struct mmc_host *host)
1063 flush_scheduled_work();
1064 mmc_free_host_sysfs(host);
1067 EXPORT_SYMBOL(mmc_free_host);
1072 * mmc_suspend_host - suspend a host
1074 * @state: suspend mode (PM_SUSPEND_xxx)
1076 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1078 mmc_claim_host(host);
1079 mmc_deselect_cards(host);
1080 mmc_power_off(host);
1081 mmc_release_host(host);
1086 EXPORT_SYMBOL(mmc_suspend_host);
1089 * mmc_resume_host - resume a previously suspended host
1092 int mmc_resume_host(struct mmc_host *host)
1094 mmc_detect_change(host);
1099 EXPORT_SYMBOL(mmc_resume_host);
1103 MODULE_LICENSE("GPL");