2 * linux/drivers/mmc/mmc.c
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
6 * SD support Copyright (C) 2005 Pierre Ossman, All Rights Reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 #include <linux/config.h>
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>
24 #include <linux/mmc/card.h>
25 #include <linux/mmc/host.h>
26 #include <linux/mmc/protocol.h>
33 * OCR Bit positions to 10s of Vdd mV.
35 static const unsigned short mmc_ocr_bit_to_vdd[] = {
36 150, 155, 160, 165, 170, 180, 190, 200,
37 210, 220, 230, 240, 250, 260, 270, 280,
38 290, 300, 310, 320, 330, 340, 350, 360
41 static const unsigned int tran_exp[] = {
42 10000, 100000, 1000000, 10000000,
46 static const unsigned char tran_mant[] = {
47 0, 10, 12, 13, 15, 20, 25, 30,
48 35, 40, 45, 50, 55, 60, 70, 80,
51 static const unsigned int tacc_exp[] = {
52 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
55 static const unsigned int tacc_mant[] = {
56 0, 10, 12, 13, 15, 20, 25, 30,
57 35, 40, 45, 50, 55, 60, 70, 80,
62 * mmc_request_done - finish processing an MMC command
63 * @host: MMC host which completed command
64 * @mrq: MMC request which completed
66 * MMC drivers should call this function when they have completed
67 * their processing of a command. This should be called before the
68 * data part of the command has completed.
70 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
72 struct mmc_command *cmd = mrq->cmd;
73 int err = mrq->cmd->error;
74 pr_debug("MMC: req done (%02x): %d: %08x %08x %08x %08x\n",
75 cmd->opcode, err, cmd->resp[0], cmd->resp[1],
76 cmd->resp[2], cmd->resp[3]);
78 if (err && cmd->retries) {
81 host->ops->request(host, mrq);
82 } else if (mrq->done) {
87 EXPORT_SYMBOL(mmc_request_done);
90 * mmc_start_request - start a command on a host
91 * @host: MMC host to start command on
92 * @mrq: MMC request to start
94 * Queue a command on the specified host. We expect the
95 * caller to be holding the host lock with interrupts disabled.
98 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
100 pr_debug("MMC: starting cmd %02x arg %08x flags %08x\n",
101 mrq->cmd->opcode, mrq->cmd->arg, mrq->cmd->flags);
103 WARN_ON(host->card_busy == NULL);
108 mrq->cmd->data = mrq->data;
109 mrq->data->error = 0;
110 mrq->data->mrq = mrq;
112 mrq->data->stop = mrq->stop;
113 mrq->stop->error = 0;
114 mrq->stop->mrq = mrq;
117 host->ops->request(host, mrq);
120 EXPORT_SYMBOL(mmc_start_request);
122 static void mmc_wait_done(struct mmc_request *mrq)
124 complete(mrq->done_data);
127 int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
129 DECLARE_COMPLETION(complete);
131 mrq->done_data = &complete;
132 mrq->done = mmc_wait_done;
134 mmc_start_request(host, mrq);
136 wait_for_completion(&complete);
141 EXPORT_SYMBOL(mmc_wait_for_req);
144 * mmc_wait_for_cmd - start a command and wait for completion
145 * @host: MMC host to start command
146 * @cmd: MMC command to start
147 * @retries: maximum number of retries
149 * Start a new MMC command for a host, and wait for the command
150 * to complete. Return any error that occurred while the command
151 * was executing. Do not attempt to parse the response.
153 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
155 struct mmc_request mrq;
157 BUG_ON(host->card_busy == NULL);
159 memset(&mrq, 0, sizeof(struct mmc_request));
161 memset(cmd->resp, 0, sizeof(cmd->resp));
162 cmd->retries = retries;
167 mmc_wait_for_req(host, &mrq);
172 EXPORT_SYMBOL(mmc_wait_for_cmd);
175 * mmc_wait_for_app_cmd - start an application command and wait for
177 * @host: MMC host to start command
178 * @rca: RCA to send MMC_APP_CMD to
179 * @cmd: MMC command to start
180 * @retries: maximum number of retries
182 * Sends a MMC_APP_CMD, checks the card response, sends the command
183 * in the parameter and waits for it to complete. Return any error
184 * that occurred while the command was executing. Do not attempt to
185 * parse the response.
187 int mmc_wait_for_app_cmd(struct mmc_host *host, unsigned int rca,
188 struct mmc_command *cmd, int retries)
190 struct mmc_request mrq;
191 struct mmc_command appcmd;
195 BUG_ON(host->card_busy == NULL);
198 err = MMC_ERR_INVALID;
201 * We have to resend MMC_APP_CMD for each attempt so
202 * we cannot use the retries field in mmc_command.
204 for (i = 0;i <= retries;i++) {
205 memset(&mrq, 0, sizeof(struct mmc_request));
207 appcmd.opcode = MMC_APP_CMD;
208 appcmd.arg = rca << 16;
209 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
211 memset(appcmd.resp, 0, sizeof(appcmd.resp));
217 mmc_wait_for_req(host, &mrq);
224 /* Check that card supported application commands */
225 if (!(appcmd.resp[0] & R1_APP_CMD))
226 return MMC_ERR_FAILED;
228 memset(&mrq, 0, sizeof(struct mmc_request));
230 memset(cmd->resp, 0, sizeof(cmd->resp));
236 mmc_wait_for_req(host, &mrq);
239 if (cmd->error == MMC_ERR_NONE)
246 EXPORT_SYMBOL(mmc_wait_for_app_cmd);
248 static int mmc_select_card(struct mmc_host *host, struct mmc_card *card);
251 * __mmc_claim_host - exclusively claim a host
252 * @host: mmc host to claim
253 * @card: mmc card to claim host for
255 * Claim a host for a set of operations. If a valid card
256 * is passed and this wasn't the last card selected, select
257 * the card before returning.
259 * Note: you should use mmc_card_claim_host or mmc_claim_host.
261 int __mmc_claim_host(struct mmc_host *host, struct mmc_card *card)
263 DECLARE_WAITQUEUE(wait, current);
267 add_wait_queue(&host->wq, &wait);
268 spin_lock_irqsave(&host->lock, flags);
270 set_current_state(TASK_UNINTERRUPTIBLE);
271 if (host->card_busy == NULL)
273 spin_unlock_irqrestore(&host->lock, flags);
275 spin_lock_irqsave(&host->lock, flags);
277 set_current_state(TASK_RUNNING);
278 host->card_busy = card;
279 spin_unlock_irqrestore(&host->lock, flags);
280 remove_wait_queue(&host->wq, &wait);
282 if (card != (void *)-1) {
283 err = mmc_select_card(host, card);
284 if (err != MMC_ERR_NONE)
291 EXPORT_SYMBOL(__mmc_claim_host);
294 * mmc_release_host - release a host
295 * @host: mmc host to release
297 * Release a MMC host, allowing others to claim the host
298 * for their operations.
300 void mmc_release_host(struct mmc_host *host)
304 BUG_ON(host->card_busy == NULL);
306 spin_lock_irqsave(&host->lock, flags);
307 host->card_busy = NULL;
308 spin_unlock_irqrestore(&host->lock, flags);
313 EXPORT_SYMBOL(mmc_release_host);
315 static int mmc_select_card(struct mmc_host *host, struct mmc_card *card)
318 struct mmc_command cmd;
320 BUG_ON(host->card_busy == NULL);
322 if (host->card_selected == card)
325 host->card_selected = card;
327 cmd.opcode = MMC_SELECT_CARD;
328 cmd.arg = card->rca << 16;
329 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
331 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
332 if (err != MMC_ERR_NONE)
336 * Default bus width is 1 bit.
338 host->ios.bus_width = MMC_BUS_WIDTH_1;
341 * We can only change the bus width of the selected
342 * card so therefore we have to put the handling
345 if (host->caps & MMC_CAP_4_BIT_DATA) {
347 * The card is in 1 bit mode by default so
348 * we only need to change if it supports the
351 if (mmc_card_sd(card) &&
352 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
353 struct mmc_command cmd;
354 cmd.opcode = SD_APP_SET_BUS_WIDTH;
355 cmd.arg = SD_BUS_WIDTH_4;
356 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
358 err = mmc_wait_for_app_cmd(host, card->rca, &cmd,
360 if (err != MMC_ERR_NONE)
363 host->ios.bus_width = MMC_BUS_WIDTH_4;
367 host->ops->set_ios(host, &host->ios);
373 * Ensure that no card is selected.
375 static void mmc_deselect_cards(struct mmc_host *host)
377 struct mmc_command cmd;
379 if (host->card_selected) {
380 host->card_selected = NULL;
382 cmd.opcode = MMC_SELECT_CARD;
384 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
386 mmc_wait_for_cmd(host, &cmd, 0);
391 static inline void mmc_delay(unsigned int ms)
393 if (ms < HZ / 1000) {
397 msleep_interruptible (ms);
402 * Mask off any voltages we don't support and select
405 static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
409 ocr &= host->ocr_avail;
418 host->ops->set_ios(host, &host->ios);
426 #define UNSTUFF_BITS(resp,start,size) \
428 const int __size = size; \
429 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
430 const int __off = 3 - ((start) / 32); \
431 const int __shft = (start) & 31; \
434 __res = resp[__off] >> __shft; \
435 if (__size + __shft > 32) \
436 __res |= resp[__off-1] << ((32 - __shft) % 32); \
441 * Given the decoded CSD structure, decode the raw CID to our CID structure.
443 static void mmc_decode_cid(struct mmc_card *card)
445 u32 *resp = card->raw_cid;
447 memset(&card->cid, 0, sizeof(struct mmc_cid));
449 if (mmc_card_sd(card)) {
451 * SD doesn't currently have a version field so we will
452 * have to assume we can parse this.
454 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
455 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
456 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
457 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
458 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
459 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
460 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
461 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
462 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
463 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
464 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
465 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
467 card->cid.year += 2000; /* SD cards year offset */
470 * The selection of the format here is based upon published
471 * specs from sandisk and from what people have reported.
473 switch (card->csd.mmca_vsn) {
474 case 0: /* MMC v1.0 - v1.2 */
475 case 1: /* MMC v1.4 */
476 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
477 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
478 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
479 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
480 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
481 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
482 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
483 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
484 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
485 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
486 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
487 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
488 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
491 case 2: /* MMC v2.0 - v2.2 */
492 case 3: /* MMC v3.1 - v3.3 */
494 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
495 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
496 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
497 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
498 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
499 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
500 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
501 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
502 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
503 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
504 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
508 printk("%s: card has unknown MMCA version %d\n",
509 mmc_hostname(card->host), card->csd.mmca_vsn);
510 mmc_card_set_bad(card);
517 * Given a 128-bit response, decode to our card CSD structure.
519 static void mmc_decode_csd(struct mmc_card *card)
521 struct mmc_csd *csd = &card->csd;
522 unsigned int e, m, csd_struct;
523 u32 *resp = card->raw_csd;
525 if (mmc_card_sd(card)) {
526 csd_struct = UNSTUFF_BITS(resp, 126, 2);
527 if (csd_struct != 0) {
528 printk("%s: unrecognised CSD structure version %d\n",
529 mmc_hostname(card->host), csd_struct);
530 mmc_card_set_bad(card);
534 m = UNSTUFF_BITS(resp, 115, 4);
535 e = UNSTUFF_BITS(resp, 112, 3);
536 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
537 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
539 m = UNSTUFF_BITS(resp, 99, 4);
540 e = UNSTUFF_BITS(resp, 96, 3);
541 csd->max_dtr = tran_exp[e] * tran_mant[m];
542 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
544 e = UNSTUFF_BITS(resp, 47, 3);
545 m = UNSTUFF_BITS(resp, 62, 12);
546 csd->capacity = (1 + m) << (e + 2);
548 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
549 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
550 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
551 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
552 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
553 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
554 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
557 * We only understand CSD structure v1.1 and v1.2.
558 * v1.2 has extra information in bits 15, 11 and 10.
560 csd_struct = UNSTUFF_BITS(resp, 126, 2);
561 if (csd_struct != 1 && csd_struct != 2) {
562 printk("%s: unrecognised CSD structure version %d\n",
563 mmc_hostname(card->host), csd_struct);
564 mmc_card_set_bad(card);
568 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
569 m = UNSTUFF_BITS(resp, 115, 4);
570 e = UNSTUFF_BITS(resp, 112, 3);
571 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
572 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
574 m = UNSTUFF_BITS(resp, 99, 4);
575 e = UNSTUFF_BITS(resp, 96, 3);
576 csd->max_dtr = tran_exp[e] * tran_mant[m];
577 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
579 e = UNSTUFF_BITS(resp, 47, 3);
580 m = UNSTUFF_BITS(resp, 62, 12);
581 csd->capacity = (1 + m) << (e + 2);
583 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
584 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
585 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
586 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
587 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
588 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
589 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
594 * Given a 64-bit response, decode to our card SCR structure.
596 static void mmc_decode_scr(struct mmc_card *card)
598 struct sd_scr *scr = &card->scr;
599 unsigned int scr_struct;
602 BUG_ON(!mmc_card_sd(card));
604 resp[3] = card->raw_scr[1];
605 resp[2] = card->raw_scr[0];
607 scr_struct = UNSTUFF_BITS(resp, 60, 4);
608 if (scr_struct != 0) {
609 printk("%s: unrecognised SCR structure version %d\n",
610 mmc_hostname(card->host), scr_struct);
611 mmc_card_set_bad(card);
615 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
616 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
620 * Locate a MMC card on this MMC host given a raw CID.
622 static struct mmc_card *mmc_find_card(struct mmc_host *host, u32 *raw_cid)
624 struct mmc_card *card;
626 list_for_each_entry(card, &host->cards, node) {
627 if (memcmp(card->raw_cid, raw_cid, sizeof(card->raw_cid)) == 0)
634 * Allocate a new MMC card, and assign a unique RCA.
636 static struct mmc_card *
637 mmc_alloc_card(struct mmc_host *host, u32 *raw_cid, unsigned int *frca)
639 struct mmc_card *card, *c;
640 unsigned int rca = *frca;
642 card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL);
644 return ERR_PTR(-ENOMEM);
646 mmc_init_card(card, host);
647 memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid));
650 list_for_each_entry(c, &host->cards, node)
664 * Tell attached cards to go to IDLE state
666 static void mmc_idle_cards(struct mmc_host *host)
668 struct mmc_command cmd;
670 host->ios.chip_select = MMC_CS_HIGH;
671 host->ops->set_ios(host, &host->ios);
675 cmd.opcode = MMC_GO_IDLE_STATE;
677 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
679 mmc_wait_for_cmd(host, &cmd, 0);
683 host->ios.chip_select = MMC_CS_DONTCARE;
684 host->ops->set_ios(host, &host->ios);
690 * Apply power to the MMC stack. This is a two-stage process.
691 * First, we enable power to the card without the clock running.
692 * We then wait a bit for the power to stabilise. Finally,
693 * enable the bus drivers and clock to the card.
695 * We must _NOT_ enable the clock prior to power stablising.
697 * If a host does all the power sequencing itself, ignore the
698 * initial MMC_POWER_UP stage.
700 static void mmc_power_up(struct mmc_host *host)
702 int bit = fls(host->ocr_avail) - 1;
705 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
706 host->ios.chip_select = MMC_CS_DONTCARE;
707 host->ios.power_mode = MMC_POWER_UP;
708 host->ios.bus_width = MMC_BUS_WIDTH_1;
709 host->ops->set_ios(host, &host->ios);
713 host->ios.clock = host->f_min;
714 host->ios.power_mode = MMC_POWER_ON;
715 host->ops->set_ios(host, &host->ios);
720 static void mmc_power_off(struct mmc_host *host)
724 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
725 host->ios.chip_select = MMC_CS_DONTCARE;
726 host->ios.power_mode = MMC_POWER_OFF;
727 host->ios.bus_width = MMC_BUS_WIDTH_1;
728 host->ops->set_ios(host, &host->ios);
731 static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
733 struct mmc_command cmd;
736 cmd.opcode = MMC_SEND_OP_COND;
738 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
740 for (i = 100; i; i--) {
741 err = mmc_wait_for_cmd(host, &cmd, 0);
742 if (err != MMC_ERR_NONE)
745 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
748 err = MMC_ERR_TIMEOUT;
759 static int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
761 struct mmc_command cmd;
764 cmd.opcode = SD_APP_OP_COND;
766 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
768 for (i = 100; i; i--) {
769 err = mmc_wait_for_app_cmd(host, 0, &cmd, CMD_RETRIES);
770 if (err != MMC_ERR_NONE)
773 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
776 err = MMC_ERR_TIMEOUT;
788 * Discover cards by requesting their CID. If this command
789 * times out, it is not an error; there are no further cards
790 * to be discovered. Add new cards to the list.
792 * Create a mmc_card entry for each discovered card, assigning
793 * it an RCA, and save the raw CID for decoding later.
795 static void mmc_discover_cards(struct mmc_host *host)
797 struct mmc_card *card;
798 unsigned int first_rca = 1, err;
801 struct mmc_command cmd;
803 cmd.opcode = MMC_ALL_SEND_CID;
805 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
807 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
808 if (err == MMC_ERR_TIMEOUT) {
812 if (err != MMC_ERR_NONE) {
813 printk(KERN_ERR "%s: error requesting CID: %d\n",
814 mmc_hostname(host), err);
818 card = mmc_find_card(host, cmd.resp);
820 card = mmc_alloc_card(host, cmd.resp, &first_rca);
825 list_add(&card->node, &host->cards);
828 card->state &= ~MMC_STATE_DEAD;
830 if (host->mode == MMC_MODE_SD) {
831 mmc_card_set_sd(card);
833 cmd.opcode = SD_SEND_RELATIVE_ADDR;
835 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
837 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
838 if (err != MMC_ERR_NONE)
839 mmc_card_set_dead(card);
841 card->rca = cmd.resp[0] >> 16;
843 if (!host->ops->get_ro) {
844 printk(KERN_WARNING "%s: host does not "
845 "support reading read-only "
846 "switch. assuming write-enable.\n",
849 if (host->ops->get_ro(host))
850 mmc_card_set_readonly(card);
854 cmd.opcode = MMC_SET_RELATIVE_ADDR;
855 cmd.arg = card->rca << 16;
856 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
858 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
859 if (err != MMC_ERR_NONE)
860 mmc_card_set_dead(card);
865 static void mmc_read_csds(struct mmc_host *host)
867 struct mmc_card *card;
869 list_for_each_entry(card, &host->cards, node) {
870 struct mmc_command cmd;
873 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
876 cmd.opcode = MMC_SEND_CSD;
877 cmd.arg = card->rca << 16;
878 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
880 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
881 if (err != MMC_ERR_NONE) {
882 mmc_card_set_dead(card);
886 memcpy(card->raw_csd, cmd.resp, sizeof(card->raw_csd));
888 mmc_decode_csd(card);
889 mmc_decode_cid(card);
893 static void mmc_read_scrs(struct mmc_host *host)
896 struct mmc_card *card;
898 struct mmc_request mrq;
899 struct mmc_command cmd;
900 struct mmc_data data;
902 struct scatterlist sg;
904 list_for_each_entry(card, &host->cards, node) {
905 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
907 if (!mmc_card_sd(card))
910 err = mmc_select_card(host, card);
911 if (err != MMC_ERR_NONE) {
912 mmc_card_set_dead(card);
916 memset(&cmd, 0, sizeof(struct mmc_command));
918 cmd.opcode = MMC_APP_CMD;
919 cmd.arg = card->rca << 16;
920 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
922 err = mmc_wait_for_cmd(host, &cmd, 0);
923 if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD)) {
924 mmc_card_set_dead(card);
928 memset(&cmd, 0, sizeof(struct mmc_command));
930 cmd.opcode = SD_APP_SEND_SCR;
932 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
934 memset(&data, 0, sizeof(struct mmc_data));
936 data.timeout_ns = card->csd.tacc_ns * 10;
937 data.timeout_clks = card->csd.tacc_clks * 10;
940 data.flags = MMC_DATA_READ;
944 memset(&mrq, 0, sizeof(struct mmc_request));
949 sg_init_one(&sg, (u8*)card->raw_scr, 8);
951 mmc_wait_for_req(host, &mrq);
953 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
954 mmc_card_set_dead(card);
958 card->raw_scr[0] = ntohl(card->raw_scr[0]);
959 card->raw_scr[1] = ntohl(card->raw_scr[1]);
961 mmc_decode_scr(card);
964 mmc_deselect_cards(host);
967 static unsigned int mmc_calculate_clock(struct mmc_host *host)
969 struct mmc_card *card;
970 unsigned int max_dtr = host->f_max;
972 list_for_each_entry(card, &host->cards, node)
973 if (!mmc_card_dead(card) && max_dtr > card->csd.max_dtr)
974 max_dtr = card->csd.max_dtr;
976 pr_debug("MMC: selected %d.%03dMHz transfer rate\n",
977 max_dtr / 1000000, (max_dtr / 1000) % 1000);
983 * Check whether cards we already know about are still present.
984 * We do this by requesting status, and checking whether a card
987 * A request for status does not cause a state change in data
990 static void mmc_check_cards(struct mmc_host *host)
992 struct list_head *l, *n;
994 mmc_deselect_cards(host);
996 list_for_each_safe(l, n, &host->cards) {
997 struct mmc_card *card = mmc_list_to_card(l);
998 struct mmc_command cmd;
1001 cmd.opcode = MMC_SEND_STATUS;
1002 cmd.arg = card->rca << 16;
1003 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1005 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1006 if (err == MMC_ERR_NONE)
1009 mmc_card_set_dead(card);
1013 static void mmc_setup(struct mmc_host *host)
1015 if (host->ios.power_mode != MMC_POWER_ON) {
1019 host->mode = MMC_MODE_SD;
1022 mmc_idle_cards(host);
1024 err = mmc_send_app_op_cond(host, 0, &ocr);
1027 * If we fail to detect any SD cards then try
1028 * searching for MMC cards.
1030 if (err != MMC_ERR_NONE) {
1031 host->mode = MMC_MODE_MMC;
1033 err = mmc_send_op_cond(host, 0, &ocr);
1034 if (err != MMC_ERR_NONE)
1038 host->ocr = mmc_select_voltage(host, ocr);
1041 * Since we're changing the OCR value, we seem to
1042 * need to tell some cards to go back to the idle
1043 * state. We wait 1ms to give cards time to
1047 mmc_idle_cards(host);
1049 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1050 host->ios.clock = host->f_min;
1051 host->ops->set_ios(host, &host->ios);
1054 * We should remember the OCR mask from the existing
1055 * cards, and detect the new cards OCR mask, combine
1056 * the two and re-select the VDD. However, if we do
1057 * change VDD, we should do an idle, and then do a
1058 * full re-initialisation. We would need to notify
1059 * drivers so that they can re-setup the cards as
1060 * well, while keeping their queues at bay.
1062 * For the moment, we take the easy way out - if the
1063 * new cards don't like our currently selected VDD,
1064 * they drop off the bus.
1072 * Send the selected OCR multiple times... until the cards
1073 * all get the idea that they should be ready for CMD2.
1074 * (My SanDisk card seems to need this.)
1076 if (host->mode == MMC_MODE_SD)
1077 mmc_send_app_op_cond(host, host->ocr, NULL);
1079 mmc_send_op_cond(host, host->ocr, NULL);
1081 mmc_discover_cards(host);
1084 * Ok, now switch to push-pull mode.
1086 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1087 host->ops->set_ios(host, &host->ios);
1089 mmc_read_csds(host);
1091 if (host->mode == MMC_MODE_SD)
1092 mmc_read_scrs(host);
1097 * mmc_detect_change - process change of state on a MMC socket
1098 * @host: host which changed state.
1099 * @delay: optional delay to wait before detection (jiffies)
1101 * All we know is that card(s) have been inserted or removed
1102 * from the socket(s). We don't know which socket or cards.
1104 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1107 schedule_delayed_work(&host->detect, delay);
1109 schedule_work(&host->detect);
1112 EXPORT_SYMBOL(mmc_detect_change);
1115 static void mmc_rescan(void *data)
1117 struct mmc_host *host = data;
1118 struct list_head *l, *n;
1120 mmc_claim_host(host);
1122 if (host->ios.power_mode == MMC_POWER_ON)
1123 mmc_check_cards(host);
1127 if (!list_empty(&host->cards)) {
1129 * (Re-)calculate the fastest clock rate which the
1130 * attached cards and the host support.
1132 host->ios.clock = mmc_calculate_clock(host);
1133 host->ops->set_ios(host, &host->ios);
1136 mmc_release_host(host);
1138 list_for_each_safe(l, n, &host->cards) {
1139 struct mmc_card *card = mmc_list_to_card(l);
1142 * If this is a new and good card, register it.
1144 if (!mmc_card_present(card) && !mmc_card_dead(card)) {
1145 if (mmc_register_card(card))
1146 mmc_card_set_dead(card);
1148 mmc_card_set_present(card);
1152 * If this card is dead, destroy it.
1154 if (mmc_card_dead(card)) {
1155 list_del(&card->node);
1156 mmc_remove_card(card);
1161 * If we discover that there are no cards on the
1162 * bus, turn off the clock and power down.
1164 if (list_empty(&host->cards))
1165 mmc_power_off(host);
1170 * mmc_alloc_host - initialise the per-host structure.
1171 * @extra: sizeof private data structure
1172 * @dev: pointer to host device model structure
1174 * Initialise the per-host structure.
1176 struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
1178 struct mmc_host *host;
1180 host = mmc_alloc_host_sysfs(extra, dev);
1182 spin_lock_init(&host->lock);
1183 init_waitqueue_head(&host->wq);
1184 INIT_LIST_HEAD(&host->cards);
1185 INIT_WORK(&host->detect, mmc_rescan, host);
1188 * By default, hosts do not support SGIO or large requests.
1189 * They have to set these according to their abilities.
1191 host->max_hw_segs = 1;
1192 host->max_phys_segs = 1;
1193 host->max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
1194 host->max_seg_size = PAGE_CACHE_SIZE;
1200 EXPORT_SYMBOL(mmc_alloc_host);
1203 * mmc_add_host - initialise host hardware
1206 int mmc_add_host(struct mmc_host *host)
1210 ret = mmc_add_host_sysfs(host);
1212 mmc_power_off(host);
1213 mmc_detect_change(host, 0);
1219 EXPORT_SYMBOL(mmc_add_host);
1222 * mmc_remove_host - remove host hardware
1225 * Unregister and remove all cards associated with this host,
1226 * and power down the MMC bus.
1228 void mmc_remove_host(struct mmc_host *host)
1230 struct list_head *l, *n;
1232 list_for_each_safe(l, n, &host->cards) {
1233 struct mmc_card *card = mmc_list_to_card(l);
1235 mmc_remove_card(card);
1238 mmc_power_off(host);
1239 mmc_remove_host_sysfs(host);
1242 EXPORT_SYMBOL(mmc_remove_host);
1245 * mmc_free_host - free the host structure
1248 * Free the host once all references to it have been dropped.
1250 void mmc_free_host(struct mmc_host *host)
1252 flush_scheduled_work();
1253 mmc_free_host_sysfs(host);
1256 EXPORT_SYMBOL(mmc_free_host);
1261 * mmc_suspend_host - suspend a host
1263 * @state: suspend mode (PM_SUSPEND_xxx)
1265 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1267 mmc_claim_host(host);
1268 mmc_deselect_cards(host);
1269 mmc_power_off(host);
1270 mmc_release_host(host);
1275 EXPORT_SYMBOL(mmc_suspend_host);
1278 * mmc_resume_host - resume a previously suspended host
1281 int mmc_resume_host(struct mmc_host *host)
1288 EXPORT_SYMBOL(mmc_resume_host);
1292 MODULE_LICENSE("GPL");