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->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
553 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
556 * We only understand CSD structure v1.1 and v1.2.
557 * v1.2 has extra information in bits 15, 11 and 10.
559 csd_struct = UNSTUFF_BITS(resp, 126, 2);
560 if (csd_struct != 1 && csd_struct != 2) {
561 printk("%s: unrecognised CSD structure version %d\n",
562 mmc_hostname(card->host), csd_struct);
563 mmc_card_set_bad(card);
567 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
568 m = UNSTUFF_BITS(resp, 115, 4);
569 e = UNSTUFF_BITS(resp, 112, 3);
570 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
571 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
573 m = UNSTUFF_BITS(resp, 99, 4);
574 e = UNSTUFF_BITS(resp, 96, 3);
575 csd->max_dtr = tran_exp[e] * tran_mant[m];
576 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
578 e = UNSTUFF_BITS(resp, 47, 3);
579 m = UNSTUFF_BITS(resp, 62, 12);
580 csd->capacity = (1 + m) << (e + 2);
582 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
583 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
584 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
585 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
586 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
587 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
592 * Given a 64-bit response, decode to our card SCR structure.
594 static void mmc_decode_scr(struct mmc_card *card)
596 struct sd_scr *scr = &card->scr;
597 unsigned int scr_struct;
600 BUG_ON(!mmc_card_sd(card));
602 resp[3] = card->raw_scr[1];
603 resp[2] = card->raw_scr[0];
605 scr_struct = UNSTUFF_BITS(resp, 60, 4);
606 if (scr_struct != 0) {
607 printk("%s: unrecognised SCR structure version %d\n",
608 mmc_hostname(card->host), scr_struct);
609 mmc_card_set_bad(card);
613 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
614 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
618 * Locate a MMC card on this MMC host given a raw CID.
620 static struct mmc_card *mmc_find_card(struct mmc_host *host, u32 *raw_cid)
622 struct mmc_card *card;
624 list_for_each_entry(card, &host->cards, node) {
625 if (memcmp(card->raw_cid, raw_cid, sizeof(card->raw_cid)) == 0)
632 * Allocate a new MMC card, and assign a unique RCA.
634 static struct mmc_card *
635 mmc_alloc_card(struct mmc_host *host, u32 *raw_cid, unsigned int *frca)
637 struct mmc_card *card, *c;
638 unsigned int rca = *frca;
640 card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL);
642 return ERR_PTR(-ENOMEM);
644 mmc_init_card(card, host);
645 memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid));
648 list_for_each_entry(c, &host->cards, node)
662 * Tell attached cards to go to IDLE state
664 static void mmc_idle_cards(struct mmc_host *host)
666 struct mmc_command cmd;
668 host->ios.chip_select = MMC_CS_HIGH;
669 host->ops->set_ios(host, &host->ios);
673 cmd.opcode = MMC_GO_IDLE_STATE;
675 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
677 mmc_wait_for_cmd(host, &cmd, 0);
681 host->ios.chip_select = MMC_CS_DONTCARE;
682 host->ops->set_ios(host, &host->ios);
688 * Apply power to the MMC stack. This is a two-stage process.
689 * First, we enable power to the card without the clock running.
690 * We then wait a bit for the power to stabilise. Finally,
691 * enable the bus drivers and clock to the card.
693 * We must _NOT_ enable the clock prior to power stablising.
695 * If a host does all the power sequencing itself, ignore the
696 * initial MMC_POWER_UP stage.
698 static void mmc_power_up(struct mmc_host *host)
700 int bit = fls(host->ocr_avail) - 1;
703 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
704 host->ios.chip_select = MMC_CS_DONTCARE;
705 host->ios.power_mode = MMC_POWER_UP;
706 host->ios.bus_width = MMC_BUS_WIDTH_1;
707 host->ops->set_ios(host, &host->ios);
711 host->ios.clock = host->f_min;
712 host->ios.power_mode = MMC_POWER_ON;
713 host->ops->set_ios(host, &host->ios);
718 static void mmc_power_off(struct mmc_host *host)
722 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
723 host->ios.chip_select = MMC_CS_DONTCARE;
724 host->ios.power_mode = MMC_POWER_OFF;
725 host->ios.bus_width = MMC_BUS_WIDTH_1;
726 host->ops->set_ios(host, &host->ios);
729 static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
731 struct mmc_command cmd;
734 cmd.opcode = MMC_SEND_OP_COND;
736 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
738 for (i = 100; i; i--) {
739 err = mmc_wait_for_cmd(host, &cmd, 0);
740 if (err != MMC_ERR_NONE)
743 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
746 err = MMC_ERR_TIMEOUT;
757 static int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
759 struct mmc_command cmd;
762 cmd.opcode = SD_APP_OP_COND;
764 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
766 for (i = 100; i; i--) {
767 err = mmc_wait_for_app_cmd(host, 0, &cmd, CMD_RETRIES);
768 if (err != MMC_ERR_NONE)
771 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
774 err = MMC_ERR_TIMEOUT;
786 * Discover cards by requesting their CID. If this command
787 * times out, it is not an error; there are no further cards
788 * to be discovered. Add new cards to the list.
790 * Create a mmc_card entry for each discovered card, assigning
791 * it an RCA, and save the raw CID for decoding later.
793 static void mmc_discover_cards(struct mmc_host *host)
795 struct mmc_card *card;
796 unsigned int first_rca = 1, err;
799 struct mmc_command cmd;
801 cmd.opcode = MMC_ALL_SEND_CID;
803 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
805 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
806 if (err == MMC_ERR_TIMEOUT) {
810 if (err != MMC_ERR_NONE) {
811 printk(KERN_ERR "%s: error requesting CID: %d\n",
812 mmc_hostname(host), err);
816 card = mmc_find_card(host, cmd.resp);
818 card = mmc_alloc_card(host, cmd.resp, &first_rca);
823 list_add(&card->node, &host->cards);
826 card->state &= ~MMC_STATE_DEAD;
828 if (host->mode == MMC_MODE_SD) {
829 mmc_card_set_sd(card);
831 cmd.opcode = SD_SEND_RELATIVE_ADDR;
833 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
835 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
836 if (err != MMC_ERR_NONE)
837 mmc_card_set_dead(card);
839 card->rca = cmd.resp[0] >> 16;
841 if (!host->ops->get_ro) {
842 printk(KERN_WARNING "%s: host does not "
843 "support reading read-only "
844 "switch. assuming write-enable.\n",
847 if (host->ops->get_ro(host))
848 mmc_card_set_readonly(card);
852 cmd.opcode = MMC_SET_RELATIVE_ADDR;
853 cmd.arg = card->rca << 16;
854 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
856 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
857 if (err != MMC_ERR_NONE)
858 mmc_card_set_dead(card);
863 static void mmc_read_csds(struct mmc_host *host)
865 struct mmc_card *card;
867 list_for_each_entry(card, &host->cards, node) {
868 struct mmc_command cmd;
871 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
874 cmd.opcode = MMC_SEND_CSD;
875 cmd.arg = card->rca << 16;
876 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
878 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
879 if (err != MMC_ERR_NONE) {
880 mmc_card_set_dead(card);
884 memcpy(card->raw_csd, cmd.resp, sizeof(card->raw_csd));
886 mmc_decode_csd(card);
887 mmc_decode_cid(card);
891 static void mmc_read_scrs(struct mmc_host *host)
894 struct mmc_card *card;
896 struct mmc_request mrq;
897 struct mmc_command cmd;
898 struct mmc_data data;
900 struct scatterlist sg;
902 list_for_each_entry(card, &host->cards, node) {
903 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
905 if (!mmc_card_sd(card))
908 err = mmc_select_card(host, card);
909 if (err != MMC_ERR_NONE) {
910 mmc_card_set_dead(card);
914 memset(&cmd, 0, sizeof(struct mmc_command));
916 cmd.opcode = MMC_APP_CMD;
917 cmd.arg = card->rca << 16;
918 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
920 err = mmc_wait_for_cmd(host, &cmd, 0);
921 if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD)) {
922 mmc_card_set_dead(card);
926 memset(&cmd, 0, sizeof(struct mmc_command));
928 cmd.opcode = SD_APP_SEND_SCR;
930 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
932 memset(&data, 0, sizeof(struct mmc_data));
934 data.timeout_ns = card->csd.tacc_ns * 10;
935 data.timeout_clks = card->csd.tacc_clks * 10;
938 data.flags = MMC_DATA_READ;
942 memset(&mrq, 0, sizeof(struct mmc_request));
947 sg_init_one(&sg, (u8*)card->raw_scr, 8);
949 mmc_wait_for_req(host, &mrq);
951 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
952 mmc_card_set_dead(card);
956 card->raw_scr[0] = ntohl(card->raw_scr[0]);
957 card->raw_scr[1] = ntohl(card->raw_scr[1]);
959 mmc_decode_scr(card);
962 mmc_deselect_cards(host);
965 static unsigned int mmc_calculate_clock(struct mmc_host *host)
967 struct mmc_card *card;
968 unsigned int max_dtr = host->f_max;
970 list_for_each_entry(card, &host->cards, node)
971 if (!mmc_card_dead(card) && max_dtr > card->csd.max_dtr)
972 max_dtr = card->csd.max_dtr;
974 pr_debug("MMC: selected %d.%03dMHz transfer rate\n",
975 max_dtr / 1000000, (max_dtr / 1000) % 1000);
981 * Check whether cards we already know about are still present.
982 * We do this by requesting status, and checking whether a card
985 * A request for status does not cause a state change in data
988 static void mmc_check_cards(struct mmc_host *host)
990 struct list_head *l, *n;
992 mmc_deselect_cards(host);
994 list_for_each_safe(l, n, &host->cards) {
995 struct mmc_card *card = mmc_list_to_card(l);
996 struct mmc_command cmd;
999 cmd.opcode = MMC_SEND_STATUS;
1000 cmd.arg = card->rca << 16;
1001 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1003 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1004 if (err == MMC_ERR_NONE)
1007 mmc_card_set_dead(card);
1011 static void mmc_setup(struct mmc_host *host)
1013 if (host->ios.power_mode != MMC_POWER_ON) {
1017 host->mode = MMC_MODE_SD;
1020 mmc_idle_cards(host);
1022 err = mmc_send_app_op_cond(host, 0, &ocr);
1025 * If we fail to detect any SD cards then try
1026 * searching for MMC cards.
1028 if (err != MMC_ERR_NONE) {
1029 host->mode = MMC_MODE_MMC;
1031 err = mmc_send_op_cond(host, 0, &ocr);
1032 if (err != MMC_ERR_NONE)
1036 host->ocr = mmc_select_voltage(host, ocr);
1039 * Since we're changing the OCR value, we seem to
1040 * need to tell some cards to go back to the idle
1041 * state. We wait 1ms to give cards time to
1045 mmc_idle_cards(host);
1047 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1048 host->ios.clock = host->f_min;
1049 host->ops->set_ios(host, &host->ios);
1052 * We should remember the OCR mask from the existing
1053 * cards, and detect the new cards OCR mask, combine
1054 * the two and re-select the VDD. However, if we do
1055 * change VDD, we should do an idle, and then do a
1056 * full re-initialisation. We would need to notify
1057 * drivers so that they can re-setup the cards as
1058 * well, while keeping their queues at bay.
1060 * For the moment, we take the easy way out - if the
1061 * new cards don't like our currently selected VDD,
1062 * they drop off the bus.
1070 * Send the selected OCR multiple times... until the cards
1071 * all get the idea that they should be ready for CMD2.
1072 * (My SanDisk card seems to need this.)
1074 if (host->mode == MMC_MODE_SD)
1075 mmc_send_app_op_cond(host, host->ocr, NULL);
1077 mmc_send_op_cond(host, host->ocr, NULL);
1079 mmc_discover_cards(host);
1082 * Ok, now switch to push-pull mode.
1084 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1085 host->ops->set_ios(host, &host->ios);
1087 mmc_read_csds(host);
1089 if (host->mode == MMC_MODE_SD)
1090 mmc_read_scrs(host);
1095 * mmc_detect_change - process change of state on a MMC socket
1096 * @host: host which changed state.
1097 * @delay: optional delay to wait before detection (jiffies)
1099 * All we know is that card(s) have been inserted or removed
1100 * from the socket(s). We don't know which socket or cards.
1102 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1105 schedule_delayed_work(&host->detect, delay);
1107 schedule_work(&host->detect);
1110 EXPORT_SYMBOL(mmc_detect_change);
1113 static void mmc_rescan(void *data)
1115 struct mmc_host *host = data;
1116 struct list_head *l, *n;
1118 mmc_claim_host(host);
1120 if (host->ios.power_mode == MMC_POWER_ON)
1121 mmc_check_cards(host);
1125 if (!list_empty(&host->cards)) {
1127 * (Re-)calculate the fastest clock rate which the
1128 * attached cards and the host support.
1130 host->ios.clock = mmc_calculate_clock(host);
1131 host->ops->set_ios(host, &host->ios);
1134 mmc_release_host(host);
1136 list_for_each_safe(l, n, &host->cards) {
1137 struct mmc_card *card = mmc_list_to_card(l);
1140 * If this is a new and good card, register it.
1142 if (!mmc_card_present(card) && !mmc_card_dead(card)) {
1143 if (mmc_register_card(card))
1144 mmc_card_set_dead(card);
1146 mmc_card_set_present(card);
1150 * If this card is dead, destroy it.
1152 if (mmc_card_dead(card)) {
1153 list_del(&card->node);
1154 mmc_remove_card(card);
1159 * If we discover that there are no cards on the
1160 * bus, turn off the clock and power down.
1162 if (list_empty(&host->cards))
1163 mmc_power_off(host);
1168 * mmc_alloc_host - initialise the per-host structure.
1169 * @extra: sizeof private data structure
1170 * @dev: pointer to host device model structure
1172 * Initialise the per-host structure.
1174 struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
1176 struct mmc_host *host;
1178 host = mmc_alloc_host_sysfs(extra, dev);
1180 spin_lock_init(&host->lock);
1181 init_waitqueue_head(&host->wq);
1182 INIT_LIST_HEAD(&host->cards);
1183 INIT_WORK(&host->detect, mmc_rescan, host);
1186 * By default, hosts do not support SGIO or large requests.
1187 * They have to set these according to their abilities.
1189 host->max_hw_segs = 1;
1190 host->max_phys_segs = 1;
1191 host->max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
1192 host->max_seg_size = PAGE_CACHE_SIZE;
1198 EXPORT_SYMBOL(mmc_alloc_host);
1201 * mmc_add_host - initialise host hardware
1204 int mmc_add_host(struct mmc_host *host)
1208 ret = mmc_add_host_sysfs(host);
1210 mmc_power_off(host);
1211 mmc_detect_change(host, 0);
1217 EXPORT_SYMBOL(mmc_add_host);
1220 * mmc_remove_host - remove host hardware
1223 * Unregister and remove all cards associated with this host,
1224 * and power down the MMC bus.
1226 void mmc_remove_host(struct mmc_host *host)
1228 struct list_head *l, *n;
1230 list_for_each_safe(l, n, &host->cards) {
1231 struct mmc_card *card = mmc_list_to_card(l);
1233 mmc_remove_card(card);
1236 mmc_power_off(host);
1237 mmc_remove_host_sysfs(host);
1240 EXPORT_SYMBOL(mmc_remove_host);
1243 * mmc_free_host - free the host structure
1246 * Free the host once all references to it have been dropped.
1248 void mmc_free_host(struct mmc_host *host)
1250 flush_scheduled_work();
1251 mmc_free_host_sysfs(host);
1254 EXPORT_SYMBOL(mmc_free_host);
1259 * mmc_suspend_host - suspend a host
1261 * @state: suspend mode (PM_SUSPEND_xxx)
1263 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1265 mmc_claim_host(host);
1266 mmc_deselect_cards(host);
1267 mmc_power_off(host);
1268 mmc_release_host(host);
1273 EXPORT_SYMBOL(mmc_suspend_host);
1276 * mmc_resume_host - resume a previously suspended host
1279 int mmc_resume_host(struct mmc_host *host)
1286 EXPORT_SYMBOL(mmc_resume_host);
1290 MODULE_LICENSE("GPL");