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 <asm/mach-types.h>
26 #include <linux/mmc/card.h>
27 #include <linux/mmc/host.h>
28 #include <linux/mmc/protocol.h>
32 #ifdef CONFIG_MMC_DEBUG
33 #define DBG(x...) printk(KERN_DEBUG x)
35 #define DBG(x...) do { } while (0)
41 * OCR Bit positions to 10s of Vdd mV.
43 static const unsigned short mmc_ocr_bit_to_vdd[] = {
44 150, 155, 160, 165, 170, 180, 190, 200,
45 210, 220, 230, 240, 250, 260, 270, 280,
46 290, 300, 310, 320, 330, 340, 350, 360
49 static const unsigned int tran_exp[] = {
50 10000, 100000, 1000000, 10000000,
54 static const unsigned char tran_mant[] = {
55 0, 10, 12, 13, 15, 20, 25, 30,
56 35, 40, 45, 50, 55, 60, 70, 80,
59 static const unsigned int tacc_exp[] = {
60 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
63 static const unsigned int tacc_mant[] = {
64 0, 10, 12, 13, 15, 20, 25, 30,
65 35, 40, 45, 50, 55, 60, 70, 80,
70 * mmc_request_done - finish processing an MMC command
71 * @host: MMC host which completed command
72 * @mrq: MMC request which completed
74 * MMC drivers should call this function when they have completed
75 * their processing of a command. This should be called before the
76 * data part of the command has completed.
78 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
80 struct mmc_command *cmd = mrq->cmd;
81 int err = mrq->cmd->error;
82 DBG("MMC: req done (%02x): %d: %08x %08x %08x %08x\n", cmd->opcode,
83 err, cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
85 if (err && cmd->retries) {
88 host->ops->request(host, mrq);
89 } else if (mrq->done) {
94 EXPORT_SYMBOL(mmc_request_done);
97 * mmc_start_request - start a command on a host
98 * @host: MMC host to start command on
99 * @mrq: MMC request to start
101 * Queue a command on the specified host. We expect the
102 * caller to be holding the host lock with interrupts disabled.
105 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
107 DBG("MMC: starting cmd %02x arg %08x flags %08x\n",
108 mrq->cmd->opcode, mrq->cmd->arg, mrq->cmd->flags);
110 WARN_ON(host->card_busy == NULL);
115 mrq->cmd->data = mrq->data;
116 mrq->data->error = 0;
117 mrq->data->mrq = mrq;
119 mrq->data->stop = mrq->stop;
120 mrq->stop->error = 0;
121 mrq->stop->mrq = mrq;
124 host->ops->request(host, mrq);
127 EXPORT_SYMBOL(mmc_start_request);
129 static void mmc_wait_done(struct mmc_request *mrq)
131 complete(mrq->done_data);
134 int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
136 DECLARE_COMPLETION(complete);
138 mrq->done_data = &complete;
139 mrq->done = mmc_wait_done;
141 mmc_start_request(host, mrq);
143 wait_for_completion(&complete);
148 EXPORT_SYMBOL(mmc_wait_for_req);
151 * mmc_wait_for_cmd - start a command and wait for completion
152 * @host: MMC host to start command
153 * @cmd: MMC command to start
154 * @retries: maximum number of retries
156 * Start a new MMC command for a host, and wait for the command
157 * to complete. Return any error that occurred while the command
158 * was executing. Do not attempt to parse the response.
160 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
162 struct mmc_request mrq;
164 BUG_ON(host->card_busy == NULL);
166 memset(&mrq, 0, sizeof(struct mmc_request));
168 memset(cmd->resp, 0, sizeof(cmd->resp));
169 cmd->retries = retries;
174 mmc_wait_for_req(host, &mrq);
179 EXPORT_SYMBOL(mmc_wait_for_cmd);
182 * mmc_wait_for_app_cmd - start an application command and wait for
184 * @host: MMC host to start command
185 * @rca: RCA to send MMC_APP_CMD to
186 * @cmd: MMC command to start
187 * @retries: maximum number of retries
189 * Sends a MMC_APP_CMD, checks the card response, sends the command
190 * in the parameter and waits for it to complete. Return any error
191 * that occurred while the command was executing. Do not attempt to
192 * parse the response.
194 int mmc_wait_for_app_cmd(struct mmc_host *host, unsigned int rca,
195 struct mmc_command *cmd, int retries)
197 struct mmc_request mrq;
198 struct mmc_command appcmd;
202 BUG_ON(host->card_busy == NULL);
205 err = MMC_ERR_INVALID;
208 * We have to resend MMC_APP_CMD for each attempt so
209 * we cannot use the retries field in mmc_command.
211 for (i = 0;i <= retries;i++) {
212 memset(&mrq, 0, sizeof(struct mmc_request));
214 appcmd.opcode = MMC_APP_CMD;
215 appcmd.arg = rca << 16;
216 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
218 memset(appcmd.resp, 0, sizeof(appcmd.resp));
224 mmc_wait_for_req(host, &mrq);
231 /* Check that card supported application commands */
232 if (!(appcmd.resp[0] & R1_APP_CMD))
233 return MMC_ERR_FAILED;
235 memset(&mrq, 0, sizeof(struct mmc_request));
237 memset(cmd->resp, 0, sizeof(cmd->resp));
243 mmc_wait_for_req(host, &mrq);
246 if (cmd->error == MMC_ERR_NONE)
253 EXPORT_SYMBOL(mmc_wait_for_app_cmd);
255 static int mmc_select_card(struct mmc_host *host, struct mmc_card *card);
258 * __mmc_claim_host - exclusively claim a host
259 * @host: mmc host to claim
260 * @card: mmc card to claim host for
262 * Claim a host for a set of operations. If a valid card
263 * is passed and this wasn't the last card selected, select
264 * the card before returning.
266 * Note: you should use mmc_card_claim_host or mmc_claim_host.
268 int __mmc_claim_host(struct mmc_host *host, struct mmc_card *card)
270 DECLARE_WAITQUEUE(wait, current);
274 add_wait_queue(&host->wq, &wait);
275 spin_lock_irqsave(&host->lock, flags);
277 set_current_state(TASK_UNINTERRUPTIBLE);
278 if (host->card_busy == NULL)
280 spin_unlock_irqrestore(&host->lock, flags);
282 spin_lock_irqsave(&host->lock, flags);
284 set_current_state(TASK_RUNNING);
285 host->card_busy = card;
286 spin_unlock_irqrestore(&host->lock, flags);
287 remove_wait_queue(&host->wq, &wait);
289 if (card != (void *)-1) {
290 err = mmc_select_card(host, card);
291 if (err != MMC_ERR_NONE)
298 EXPORT_SYMBOL(__mmc_claim_host);
301 * mmc_release_host - release a host
302 * @host: mmc host to release
304 * Release a MMC host, allowing others to claim the host
305 * for their operations.
307 void mmc_release_host(struct mmc_host *host)
311 BUG_ON(host->card_busy == NULL);
313 spin_lock_irqsave(&host->lock, flags);
314 host->card_busy = NULL;
315 spin_unlock_irqrestore(&host->lock, flags);
320 EXPORT_SYMBOL(mmc_release_host);
322 static int mmc_select_card(struct mmc_host *host, struct mmc_card *card)
325 struct mmc_command cmd;
327 BUG_ON(host->card_busy == NULL);
329 if (host->card_selected == card)
332 host->card_selected = card;
334 cmd.opcode = MMC_SELECT_CARD;
335 cmd.arg = card->rca << 16;
336 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
338 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
339 if (err != MMC_ERR_NONE)
343 * Default bus width is 1 bit.
345 host->ios.bus_width = MMC_BUS_WIDTH_1;
348 * We can only change the bus width of the selected
349 * card so therefore we have to put the handling
352 if (host->caps & MMC_CAP_4_BIT_DATA) {
354 * The card is in 1 bit mode by default so
355 * we only need to change if it supports the
358 if (mmc_card_sd(card) &&
359 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
360 struct mmc_command cmd;
361 cmd.opcode = SD_APP_SET_BUS_WIDTH;
362 cmd.arg = SD_BUS_WIDTH_4;
363 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
365 err = mmc_wait_for_app_cmd(host, card->rca, &cmd,
367 if (err != MMC_ERR_NONE)
370 host->ios.bus_width = MMC_BUS_WIDTH_4;
374 host->ops->set_ios(host, &host->ios);
380 * Ensure that no card is selected.
382 static void mmc_deselect_cards(struct mmc_host *host)
384 struct mmc_command cmd;
386 if (host->card_selected) {
387 host->card_selected = NULL;
389 cmd.opcode = MMC_SELECT_CARD;
391 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
393 mmc_wait_for_cmd(host, &cmd, 0);
398 static inline void mmc_delay(unsigned int ms)
400 if (ms < HZ / 1000) {
404 msleep_interruptible (ms);
409 * Mask off any voltages we don't support and select
412 static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
416 ocr &= host->ocr_avail;
425 host->ops->set_ios(host, &host->ios);
433 #define UNSTUFF_BITS(resp,start,size) \
435 const int __size = size; \
436 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
437 const int __off = 3 - ((start) / 32); \
438 const int __shft = (start) & 31; \
441 __res = resp[__off] >> __shft; \
442 if (__size + __shft > 32) \
443 __res |= resp[__off-1] << ((32 - __shft) % 32); \
448 * Given the decoded CSD structure, decode the raw CID to our CID structure.
450 static void mmc_decode_cid(struct mmc_card *card)
452 u32 *resp = card->raw_cid;
454 memset(&card->cid, 0, sizeof(struct mmc_cid));
456 if (mmc_card_sd(card)) {
458 * SD doesn't currently have a version field so we will
459 * have to assume we can parse this.
461 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
462 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
463 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
464 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
465 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
466 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
467 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
468 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
469 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
470 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
471 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
472 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
474 card->cid.year += 2000; /* SD cards year offset */
477 * The selection of the format here is based upon published
478 * specs from sandisk and from what people have reported.
480 switch (card->csd.mmca_vsn) {
481 case 0: /* MMC v1.0 - v1.2 */
482 case 1: /* MMC v1.4 */
483 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
484 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
485 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
486 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
487 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
488 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
489 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
490 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
491 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
492 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
493 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
494 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
495 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
498 case 2: /* MMC v2.0 - v2.2 */
499 case 3: /* MMC v3.1 - v3.3 */
501 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
502 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
503 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
504 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
505 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
506 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
507 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
508 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
509 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
510 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
511 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
515 printk("%s: card has unknown MMCA version %d\n",
516 mmc_hostname(card->host), card->csd.mmca_vsn);
517 mmc_card_set_bad(card);
524 * Given a 128-bit response, decode to our card CSD structure.
526 static void mmc_decode_csd(struct mmc_card *card)
528 struct mmc_csd *csd = &card->csd;
529 unsigned int e, m, csd_struct;
530 u32 *resp = card->raw_csd;
532 if (mmc_card_sd(card)) {
533 csd_struct = UNSTUFF_BITS(resp, 126, 2);
534 if (csd_struct != 0) {
535 printk("%s: unrecognised CSD structure version %d\n",
536 mmc_hostname(card->host), csd_struct);
537 mmc_card_set_bad(card);
541 m = UNSTUFF_BITS(resp, 115, 4);
542 e = UNSTUFF_BITS(resp, 112, 3);
543 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
544 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
546 m = UNSTUFF_BITS(resp, 99, 4);
547 e = UNSTUFF_BITS(resp, 96, 3);
548 csd->max_dtr = tran_exp[e] * tran_mant[m];
549 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
551 e = UNSTUFF_BITS(resp, 47, 3);
552 m = UNSTUFF_BITS(resp, 62, 12);
553 csd->capacity = (1 + m) << (e + 2);
555 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
556 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
557 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
558 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
559 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
560 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
563 * We only understand CSD structure v1.1 and v1.2.
564 * v1.2 has extra information in bits 15, 11 and 10.
566 csd_struct = UNSTUFF_BITS(resp, 126, 2);
567 if (csd_struct != 1 && csd_struct != 2) {
568 printk("%s: unrecognised CSD structure version %d\n",
569 mmc_hostname(card->host), csd_struct);
570 mmc_card_set_bad(card);
574 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
575 m = UNSTUFF_BITS(resp, 115, 4);
576 e = UNSTUFF_BITS(resp, 112, 3);
577 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
578 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
580 m = UNSTUFF_BITS(resp, 99, 4);
581 e = UNSTUFF_BITS(resp, 96, 3);
582 csd->max_dtr = tran_exp[e] * tran_mant[m];
583 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
585 e = UNSTUFF_BITS(resp, 47, 3);
586 m = UNSTUFF_BITS(resp, 62, 12);
587 csd->capacity = (1 + m) << (e + 2);
589 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
590 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
591 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
592 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
593 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
594 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
599 * Given a 64-bit response, decode to our card SCR structure.
601 static void mmc_decode_scr(struct mmc_card *card)
603 struct sd_scr *scr = &card->scr;
604 unsigned int scr_struct;
607 BUG_ON(!mmc_card_sd(card));
609 resp[3] = card->raw_scr[1];
610 resp[2] = card->raw_scr[0];
612 scr_struct = UNSTUFF_BITS(resp, 60, 4);
613 if (scr_struct != 0) {
614 printk("%s: unrecognised SCR structure version %d\n",
615 mmc_hostname(card->host), scr_struct);
616 mmc_card_set_bad(card);
620 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
621 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
625 * Locate a MMC card on this MMC host given a raw CID.
627 static struct mmc_card *mmc_find_card(struct mmc_host *host, u32 *raw_cid)
629 struct mmc_card *card;
631 list_for_each_entry(card, &host->cards, node) {
632 if (memcmp(card->raw_cid, raw_cid, sizeof(card->raw_cid)) == 0)
639 * Allocate a new MMC card, and assign a unique RCA.
641 static struct mmc_card *
642 mmc_alloc_card(struct mmc_host *host, u32 *raw_cid, unsigned int *frca)
644 struct mmc_card *card, *c;
645 unsigned int rca = *frca;
647 card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL);
649 return ERR_PTR(-ENOMEM);
651 mmc_init_card(card, host);
652 memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid));
655 list_for_each_entry(c, &host->cards, node)
669 * Tell attached cards to go to IDLE state
671 static void mmc_idle_cards(struct mmc_host *host)
673 struct mmc_command cmd;
675 host->ios.chip_select = MMC_CS_HIGH;
676 host->ops->set_ios(host, &host->ios);
680 cmd.opcode = MMC_GO_IDLE_STATE;
682 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
684 mmc_wait_for_cmd(host, &cmd, 0);
688 host->ios.chip_select = MMC_CS_DONTCARE;
689 host->ops->set_ios(host, &host->ios);
695 * Apply power to the MMC stack. This is a two-stage process.
696 * First, we enable power to the card without the clock running.
697 * We then wait a bit for the power to stabilise. Finally,
698 * enable the bus drivers and clock to the card.
700 * We must _NOT_ enable the clock prior to power stablising.
702 * If a host does all the power sequencing itself, ignore the
703 * initial MMC_POWER_UP stage.
705 static void mmc_power_up(struct mmc_host *host)
707 int bit = fls(host->ocr_avail) - 1;
710 host->ios.clock = host->f_min;
711 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
712 host->ios.chip_select = MMC_CS_DONTCARE;
713 host->ios.power_mode = MMC_POWER_UP;
714 host->ios.bus_width = MMC_BUS_WIDTH_1;
715 host->ops->set_ios(host, &host->ios);
719 host->ios.power_mode = MMC_POWER_ON;
720 host->ops->set_ios(host, &host->ios);
725 static void mmc_power_off(struct mmc_host *host)
729 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
730 host->ios.chip_select = MMC_CS_DONTCARE;
731 host->ios.power_mode = MMC_POWER_OFF;
732 host->ios.bus_width = MMC_BUS_WIDTH_1;
733 host->ops->set_ios(host, &host->ios);
736 static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
738 struct mmc_command cmd;
741 cmd.opcode = MMC_SEND_OP_COND;
743 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
745 for (i = 100; i; i--) {
746 err = mmc_wait_for_cmd(host, &cmd, 0);
747 if (err != MMC_ERR_NONE)
750 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
753 err = MMC_ERR_TIMEOUT;
764 static int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
766 struct mmc_command cmd;
769 cmd.opcode = SD_APP_OP_COND;
771 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
773 for (i = 100; i; i--) {
774 err = mmc_wait_for_app_cmd(host, 0, &cmd, CMD_RETRIES);
775 if (err != MMC_ERR_NONE)
778 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
781 err = MMC_ERR_TIMEOUT;
793 * Discover cards by requesting their CID. If this command
794 * times out, it is not an error; there are no further cards
795 * to be discovered. Add new cards to the list.
797 * Create a mmc_card entry for each discovered card, assigning
798 * it an RCA, and save the raw CID for decoding later.
800 static void mmc_discover_cards(struct mmc_host *host)
802 struct mmc_card *card;
803 unsigned int first_rca = 1, err;
806 struct mmc_command cmd;
808 cmd.opcode = MMC_ALL_SEND_CID;
810 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
812 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
813 if (err == MMC_ERR_TIMEOUT) {
817 if (err != MMC_ERR_NONE) {
818 printk(KERN_ERR "%s: error requesting CID: %d\n",
819 mmc_hostname(host), err);
823 card = mmc_find_card(host, cmd.resp);
825 card = mmc_alloc_card(host, cmd.resp, &first_rca);
830 list_add(&card->node, &host->cards);
833 card->state &= ~MMC_STATE_DEAD;
835 if (host->mode == MMC_MODE_SD) {
836 mmc_card_set_sd(card);
838 cmd.opcode = SD_SEND_RELATIVE_ADDR;
840 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
842 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
843 if (err != MMC_ERR_NONE)
844 mmc_card_set_dead(card);
846 card->rca = cmd.resp[0] >> 16;
848 if (!host->ops->get_ro) {
849 printk(KERN_WARNING "%s: host does not "
850 "support reading read-only "
851 "switch. assuming write-enable.\n",
854 if (host->ops->get_ro(host))
855 mmc_card_set_readonly(card);
859 cmd.opcode = MMC_SET_RELATIVE_ADDR;
860 cmd.arg = card->rca << 16;
861 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
863 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
864 if (err != MMC_ERR_NONE)
865 mmc_card_set_dead(card);
870 static void mmc_read_csds(struct mmc_host *host)
872 struct mmc_card *card;
874 list_for_each_entry(card, &host->cards, node) {
875 struct mmc_command cmd;
878 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
881 cmd.opcode = MMC_SEND_CSD;
882 cmd.arg = card->rca << 16;
883 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
885 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
886 if (err != MMC_ERR_NONE) {
887 mmc_card_set_dead(card);
891 memcpy(card->raw_csd, cmd.resp, sizeof(card->raw_csd));
893 mmc_decode_csd(card);
894 mmc_decode_cid(card);
898 static void mmc_read_scrs(struct mmc_host *host)
901 struct mmc_card *card;
903 struct mmc_request mrq;
904 struct mmc_command cmd;
905 struct mmc_data data;
907 struct scatterlist sg;
909 list_for_each_entry(card, &host->cards, node) {
910 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
912 if (!mmc_card_sd(card))
915 err = mmc_select_card(host, card);
916 if (err != MMC_ERR_NONE) {
917 mmc_card_set_dead(card);
921 memset(&cmd, 0, sizeof(struct mmc_command));
923 cmd.opcode = MMC_APP_CMD;
924 cmd.arg = card->rca << 16;
925 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
927 err = mmc_wait_for_cmd(host, &cmd, 0);
928 if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD)) {
929 mmc_card_set_dead(card);
933 memset(&cmd, 0, sizeof(struct mmc_command));
935 cmd.opcode = SD_APP_SEND_SCR;
937 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
939 memset(&data, 0, sizeof(struct mmc_data));
941 data.timeout_ns = card->csd.tacc_ns * 10;
942 data.timeout_clks = card->csd.tacc_clks * 10;
945 data.flags = MMC_DATA_READ;
949 memset(&mrq, 0, sizeof(struct mmc_request));
954 sg_init_one(&sg, (u8*)card->raw_scr, 8);
956 mmc_wait_for_req(host, &mrq);
958 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
959 mmc_card_set_dead(card);
963 card->raw_scr[0] = ntohl(card->raw_scr[0]);
964 card->raw_scr[1] = ntohl(card->raw_scr[1]);
966 mmc_decode_scr(card);
968 if (!machine_is_omap_h6300()) {
969 mmc_deselect_cards(host);
973 static unsigned int mmc_calculate_clock(struct mmc_host *host)
975 struct mmc_card *card;
976 unsigned int max_dtr = host->f_max;
978 list_for_each_entry(card, &host->cards, node)
979 if (!mmc_card_dead(card) && max_dtr > card->csd.max_dtr)
980 max_dtr = card->csd.max_dtr;
982 DBG("MMC: selected %d.%03dMHz transfer rate\n",
983 max_dtr / 1000000, (max_dtr / 1000) % 1000);
989 * Check whether cards we already know about are still present.
990 * We do this by requesting status, and checking whether a card
993 * A request for status does not cause a state change in data
996 static void mmc_check_cards(struct mmc_host *host)
998 struct list_head *l, *n;
1000 mmc_deselect_cards(host);
1002 list_for_each_safe(l, n, &host->cards) {
1003 struct mmc_card *card = mmc_list_to_card(l);
1004 struct mmc_command cmd;
1007 cmd.opcode = MMC_SEND_STATUS;
1008 cmd.arg = card->rca << 16;
1009 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1011 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1012 if (err == MMC_ERR_NONE)
1015 mmc_card_set_dead(card);
1019 static void mmc_setup(struct mmc_host *host)
1021 if (host->ios.power_mode != MMC_POWER_ON) {
1025 host->mode = MMC_MODE_SD;
1028 mmc_idle_cards(host);
1030 err = mmc_send_app_op_cond(host, 0, &ocr);
1033 * If we fail to detect any SD cards then try
1034 * searching for MMC cards.
1036 if (err != MMC_ERR_NONE) {
1037 host->mode = MMC_MODE_MMC;
1039 err = mmc_send_op_cond(host, 0, &ocr);
1040 if (err != MMC_ERR_NONE)
1044 host->ocr = mmc_select_voltage(host, ocr);
1047 * Since we're changing the OCR value, we seem to
1048 * need to tell some cards to go back to the idle
1049 * state. We wait 1ms to give cards time to
1053 mmc_idle_cards(host);
1055 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1056 host->ios.clock = host->f_min;
1057 host->ops->set_ios(host, &host->ios);
1060 * We should remember the OCR mask from the existing
1061 * cards, and detect the new cards OCR mask, combine
1062 * the two and re-select the VDD. However, if we do
1063 * change VDD, we should do an idle, and then do a
1064 * full re-initialisation. We would need to notify
1065 * drivers so that they can re-setup the cards as
1066 * well, while keeping their queues at bay.
1068 * For the moment, we take the easy way out - if the
1069 * new cards don't like our currently selected VDD,
1070 * they drop off the bus.
1078 * Send the selected OCR multiple times... until the cards
1079 * all get the idea that they should be ready for CMD2.
1080 * (My SanDisk card seems to need this.)
1082 if (host->mode == MMC_MODE_SD)
1083 mmc_send_app_op_cond(host, host->ocr, NULL);
1085 mmc_send_op_cond(host, host->ocr, NULL);
1087 mmc_discover_cards(host);
1090 * Ok, now switch to push-pull mode.
1092 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1093 host->ops->set_ios(host, &host->ios);
1096 * Some already detectd cards get confused in the card identification
1097 * mode and futher commands can fail. Doing an extra status inquiry
1098 * after the identification mode seems to get cards back to their
1101 mmc_check_cards(host);
1103 mmc_read_csds(host);
1105 if (host->mode == MMC_MODE_SD)
1106 mmc_read_scrs(host);
1111 * mmc_detect_change - process change of state on a MMC socket
1112 * @host: host which changed state.
1113 * @delay: optional delay to wait before detection (jiffies)
1115 * All we know is that card(s) have been inserted or removed
1116 * from the socket(s). We don't know which socket or cards.
1118 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1121 schedule_delayed_work(&host->detect, delay);
1123 schedule_work(&host->detect);
1126 EXPORT_SYMBOL(mmc_detect_change);
1129 static void mmc_rescan(void *data)
1131 struct mmc_host *host = data;
1132 struct list_head *l, *n;
1134 mmc_claim_host(host);
1136 if (host->ios.power_mode == MMC_POWER_ON)
1137 mmc_check_cards(host);
1141 if (!list_empty(&host->cards)) {
1143 * (Re-)calculate the fastest clock rate which the
1144 * attached cards and the host support.
1146 host->ios.clock = mmc_calculate_clock(host);
1147 host->ops->set_ios(host, &host->ios);
1150 mmc_release_host(host);
1152 list_for_each_safe(l, n, &host->cards) {
1153 struct mmc_card *card = mmc_list_to_card(l);
1156 * If this is a new and good card, register it.
1158 if (!mmc_card_present(card) && !mmc_card_dead(card)) {
1159 if (mmc_register_card(card))
1160 mmc_card_set_dead(card);
1162 mmc_card_set_present(card);
1166 * If this card is dead, destroy it.
1168 if (mmc_card_dead(card)) {
1169 list_del(&card->node);
1170 mmc_remove_card(card);
1175 * If we discover that there are no cards on the
1176 * bus, turn off the clock and power down.
1178 if (list_empty(&host->cards))
1179 mmc_power_off(host);
1184 * mmc_alloc_host - initialise the per-host structure.
1185 * @extra: sizeof private data structure
1186 * @dev: pointer to host device model structure
1188 * Initialise the per-host structure.
1190 struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
1192 struct mmc_host *host;
1194 host = mmc_alloc_host_sysfs(extra, dev);
1196 spin_lock_init(&host->lock);
1197 init_waitqueue_head(&host->wq);
1198 INIT_LIST_HEAD(&host->cards);
1199 INIT_WORK(&host->detect, mmc_rescan, host);
1202 * By default, hosts do not support SGIO or large requests.
1203 * They have to set these according to their abilities.
1205 host->max_hw_segs = 1;
1206 host->max_phys_segs = 1;
1207 host->max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
1208 host->max_seg_size = PAGE_CACHE_SIZE;
1214 EXPORT_SYMBOL(mmc_alloc_host);
1217 * mmc_add_host - initialise host hardware
1220 int mmc_add_host(struct mmc_host *host)
1224 ret = mmc_add_host_sysfs(host);
1226 mmc_power_off(host);
1227 mmc_detect_change(host, 0);
1233 EXPORT_SYMBOL(mmc_add_host);
1236 * mmc_remove_host - remove host hardware
1239 * Unregister and remove all cards associated with this host,
1240 * and power down the MMC bus.
1242 void mmc_remove_host(struct mmc_host *host)
1244 struct list_head *l, *n;
1246 list_for_each_safe(l, n, &host->cards) {
1247 struct mmc_card *card = mmc_list_to_card(l);
1249 mmc_remove_card(card);
1252 mmc_power_off(host);
1253 mmc_remove_host_sysfs(host);
1256 EXPORT_SYMBOL(mmc_remove_host);
1259 * mmc_free_host - free the host structure
1262 * Free the host once all references to it have been dropped.
1264 void mmc_free_host(struct mmc_host *host)
1266 flush_scheduled_work();
1267 mmc_free_host_sysfs(host);
1270 EXPORT_SYMBOL(mmc_free_host);
1275 * mmc_suspend_host - suspend a host
1277 * @state: suspend mode (PM_SUSPEND_xxx)
1279 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1281 mmc_claim_host(host);
1282 mmc_deselect_cards(host);
1283 mmc_power_off(host);
1284 mmc_release_host(host);
1289 EXPORT_SYMBOL(mmc_suspend_host);
1292 * mmc_resume_host - resume a previously suspended host
1295 int mmc_resume_host(struct mmc_host *host)
1302 EXPORT_SYMBOL(mmc_resume_host);
1306 MODULE_LICENSE("GPL");