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/module.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/completion.h>
16 #include <linux/device.h>
17 #include <linux/delay.h>
18 #include <linux/pagemap.h>
19 #include <linux/err.h>
20 #include <asm/scatterlist.h>
21 #include <linux/scatterlist.h>
23 #include <linux/mmc/card.h>
24 #include <linux/mmc/host.h>
25 #include <linux/mmc/protocol.h>
32 * OCR Bit positions to 10s of Vdd mV.
34 static const unsigned short mmc_ocr_bit_to_vdd[] = {
35 150, 155, 160, 165, 170, 180, 190, 200,
36 210, 220, 230, 240, 250, 260, 270, 280,
37 290, 300, 310, 320, 330, 340, 350, 360
40 static const unsigned int tran_exp[] = {
41 10000, 100000, 1000000, 10000000,
45 static const unsigned char tran_mant[] = {
46 0, 10, 12, 13, 15, 20, 25, 30,
47 35, 40, 45, 50, 55, 60, 70, 80,
50 static const unsigned int tacc_exp[] = {
51 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
54 static const unsigned int tacc_mant[] = {
55 0, 10, 12, 13, 15, 20, 25, 30,
56 35, 40, 45, 50, 55, 60, 70, 80,
61 * mmc_request_done - finish processing an MMC request
62 * @host: MMC host which completed request
63 * @mrq: MMC request which request
65 * MMC drivers should call this function when they have completed
66 * their processing of a request.
68 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
70 struct mmc_command *cmd = mrq->cmd;
73 pr_debug("%s: req done (CMD%u): %d/%d/%d: %08x %08x %08x %08x\n",
74 mmc_hostname(host), cmd->opcode, err,
75 mrq->data ? mrq->data->error : 0,
76 mrq->stop ? mrq->stop->error : 0,
77 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 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
102 mmc_hostname(host), mrq->cmd->opcode,
103 mrq->cmd->arg, mrq->cmd->flags);
105 WARN_ON(host->card_busy == NULL);
110 mrq->cmd->data = mrq->data;
111 mrq->data->error = 0;
112 mrq->data->mrq = mrq;
114 mrq->data->stop = mrq->stop;
115 mrq->stop->error = 0;
116 mrq->stop->mrq = mrq;
119 host->ops->request(host, mrq);
122 EXPORT_SYMBOL(mmc_start_request);
124 static void mmc_wait_done(struct mmc_request *mrq)
126 complete(mrq->done_data);
129 int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
131 DECLARE_COMPLETION_ONSTACK(complete);
133 mrq->done_data = &complete;
134 mrq->done = mmc_wait_done;
136 mmc_start_request(host, mrq);
138 wait_for_completion(&complete);
143 EXPORT_SYMBOL(mmc_wait_for_req);
146 * mmc_wait_for_cmd - start a command and wait for completion
147 * @host: MMC host to start command
148 * @cmd: MMC command to start
149 * @retries: maximum number of retries
151 * Start a new MMC command for a host, and wait for the command
152 * to complete. Return any error that occurred while the command
153 * was executing. Do not attempt to parse the response.
155 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
157 struct mmc_request mrq;
159 BUG_ON(host->card_busy == NULL);
161 memset(&mrq, 0, sizeof(struct mmc_request));
163 memset(cmd->resp, 0, sizeof(cmd->resp));
164 cmd->retries = retries;
169 mmc_wait_for_req(host, &mrq);
174 EXPORT_SYMBOL(mmc_wait_for_cmd);
177 * mmc_wait_for_app_cmd - start an application command and wait for
179 * @host: MMC host to start command
180 * @rca: RCA to send MMC_APP_CMD to
181 * @cmd: MMC command to start
182 * @retries: maximum number of retries
184 * Sends a MMC_APP_CMD, checks the card response, sends the command
185 * in the parameter and waits for it to complete. Return any error
186 * that occurred while the command was executing. Do not attempt to
187 * parse the response.
189 int mmc_wait_for_app_cmd(struct mmc_host *host, unsigned int rca,
190 struct mmc_command *cmd, int retries)
192 struct mmc_request mrq;
193 struct mmc_command appcmd;
197 BUG_ON(host->card_busy == NULL);
200 err = MMC_ERR_INVALID;
203 * We have to resend MMC_APP_CMD for each attempt so
204 * we cannot use the retries field in mmc_command.
206 for (i = 0;i <= retries;i++) {
207 memset(&mrq, 0, sizeof(struct mmc_request));
209 appcmd.opcode = MMC_APP_CMD;
210 appcmd.arg = rca << 16;
211 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
213 memset(appcmd.resp, 0, sizeof(appcmd.resp));
219 mmc_wait_for_req(host, &mrq);
226 /* Check that card supported application commands */
227 if (!(appcmd.resp[0] & R1_APP_CMD))
228 return MMC_ERR_FAILED;
230 memset(&mrq, 0, sizeof(struct mmc_request));
232 memset(cmd->resp, 0, sizeof(cmd->resp));
238 mmc_wait_for_req(host, &mrq);
241 if (cmd->error == MMC_ERR_NONE)
248 EXPORT_SYMBOL(mmc_wait_for_app_cmd);
250 static int mmc_select_card(struct mmc_host *host, struct mmc_card *card);
253 * __mmc_claim_host - exclusively claim a host
254 * @host: mmc host to claim
255 * @card: mmc card to claim host for
257 * Claim a host for a set of operations. If a valid card
258 * is passed and this wasn't the last card selected, select
259 * the card before returning.
261 * Note: you should use mmc_card_claim_host or mmc_claim_host.
263 int __mmc_claim_host(struct mmc_host *host, struct mmc_card *card)
265 DECLARE_WAITQUEUE(wait, current);
269 add_wait_queue(&host->wq, &wait);
270 spin_lock_irqsave(&host->lock, flags);
272 set_current_state(TASK_UNINTERRUPTIBLE);
273 if (host->card_busy == NULL)
275 spin_unlock_irqrestore(&host->lock, flags);
277 spin_lock_irqsave(&host->lock, flags);
279 set_current_state(TASK_RUNNING);
280 host->card_busy = card;
281 spin_unlock_irqrestore(&host->lock, flags);
282 remove_wait_queue(&host->wq, &wait);
284 if (card != (void *)-1) {
285 err = mmc_select_card(host, card);
286 if (err != MMC_ERR_NONE)
293 EXPORT_SYMBOL(__mmc_claim_host);
296 * mmc_release_host - release a host
297 * @host: mmc host to release
299 * Release a MMC host, allowing others to claim the host
300 * for their operations.
302 void mmc_release_host(struct mmc_host *host)
306 BUG_ON(host->card_busy == NULL);
308 spin_lock_irqsave(&host->lock, flags);
309 host->card_busy = NULL;
310 spin_unlock_irqrestore(&host->lock, flags);
315 EXPORT_SYMBOL(mmc_release_host);
317 static inline void mmc_set_ios(struct mmc_host *host)
319 struct mmc_ios *ios = &host->ios;
321 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u width %u\n",
322 mmc_hostname(host), ios->clock, ios->bus_mode,
323 ios->power_mode, ios->chip_select, ios->vdd,
326 host->ops->set_ios(host, ios);
329 static int mmc_select_card(struct mmc_host *host, struct mmc_card *card)
332 struct mmc_command cmd;
334 BUG_ON(host->card_busy == NULL);
336 if (host->card_selected == card)
339 host->card_selected = card;
341 cmd.opcode = MMC_SELECT_CARD;
342 cmd.arg = card->rca << 16;
343 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
345 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
346 if (err != MMC_ERR_NONE)
350 * Default bus width is 1 bit.
352 host->ios.bus_width = MMC_BUS_WIDTH_1;
355 * We can only change the bus width of the selected
356 * card so therefore we have to put the handling
359 if (host->caps & MMC_CAP_4_BIT_DATA) {
361 * The card is in 1 bit mode by default so
362 * we only need to change if it supports the
365 if (mmc_card_sd(card) &&
366 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
367 struct mmc_command cmd;
368 cmd.opcode = SD_APP_SET_BUS_WIDTH;
369 cmd.arg = SD_BUS_WIDTH_4;
370 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
372 err = mmc_wait_for_app_cmd(host, card->rca, &cmd,
374 if (err != MMC_ERR_NONE)
377 host->ios.bus_width = MMC_BUS_WIDTH_4;
387 * Ensure that no card is selected.
389 static void mmc_deselect_cards(struct mmc_host *host)
391 struct mmc_command cmd;
393 if (host->card_selected) {
394 host->card_selected = NULL;
396 cmd.opcode = MMC_SELECT_CARD;
398 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
400 mmc_wait_for_cmd(host, &cmd, 0);
405 static inline void mmc_delay(unsigned int ms)
407 if (ms < HZ / 1000) {
411 msleep_interruptible (ms);
416 * Mask off any voltages we don't support and select
419 static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
423 ocr &= host->ocr_avail;
440 #define UNSTUFF_BITS(resp,start,size) \
442 const int __size = size; \
443 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
444 const int __off = 3 - ((start) / 32); \
445 const int __shft = (start) & 31; \
448 __res = resp[__off] >> __shft; \
449 if (__size + __shft > 32) \
450 __res |= resp[__off-1] << ((32 - __shft) % 32); \
455 * Given the decoded CSD structure, decode the raw CID to our CID structure.
457 static void mmc_decode_cid(struct mmc_card *card)
459 u32 *resp = card->raw_cid;
461 memset(&card->cid, 0, sizeof(struct mmc_cid));
463 if (mmc_card_sd(card)) {
465 * SD doesn't currently have a version field so we will
466 * have to assume we can parse this.
468 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
469 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
470 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
471 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
472 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
473 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
474 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
475 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
476 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
477 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
478 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
479 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
481 card->cid.year += 2000; /* SD cards year offset */
484 * The selection of the format here is based upon published
485 * specs from sandisk and from what people have reported.
487 switch (card->csd.mmca_vsn) {
488 case 0: /* MMC v1.0 - v1.2 */
489 case 1: /* MMC v1.4 */
490 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
491 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
492 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
493 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
494 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
495 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
496 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
497 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
498 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
499 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
500 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
501 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
502 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
505 case 2: /* MMC v2.0 - v2.2 */
506 case 3: /* MMC v3.1 - v3.3 */
508 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
509 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
510 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
511 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
512 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
513 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
514 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
515 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
516 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
517 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
518 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
522 printk("%s: card has unknown MMCA version %d\n",
523 mmc_hostname(card->host), card->csd.mmca_vsn);
524 mmc_card_set_bad(card);
531 * Given a 128-bit response, decode to our card CSD structure.
533 static void mmc_decode_csd(struct mmc_card *card)
535 struct mmc_csd *csd = &card->csd;
536 unsigned int e, m, csd_struct;
537 u32 *resp = card->raw_csd;
539 if (mmc_card_sd(card)) {
540 csd_struct = UNSTUFF_BITS(resp, 126, 2);
541 if (csd_struct != 0) {
542 printk("%s: unrecognised CSD structure version %d\n",
543 mmc_hostname(card->host), csd_struct);
544 mmc_card_set_bad(card);
548 m = UNSTUFF_BITS(resp, 115, 4);
549 e = UNSTUFF_BITS(resp, 112, 3);
550 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
551 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
553 m = UNSTUFF_BITS(resp, 99, 4);
554 e = UNSTUFF_BITS(resp, 96, 3);
555 csd->max_dtr = tran_exp[e] * tran_mant[m];
556 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
558 e = UNSTUFF_BITS(resp, 47, 3);
559 m = UNSTUFF_BITS(resp, 62, 12);
560 csd->capacity = (1 + m) << (e + 2);
562 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
563 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
564 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
565 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
566 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
567 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
568 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
571 * We only understand CSD structure v1.1 and v1.2.
572 * v1.2 has extra information in bits 15, 11 and 10.
574 csd_struct = UNSTUFF_BITS(resp, 126, 2);
575 if (csd_struct != 1 && csd_struct != 2) {
576 printk("%s: unrecognised CSD structure version %d\n",
577 mmc_hostname(card->host), csd_struct);
578 mmc_card_set_bad(card);
582 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
583 m = UNSTUFF_BITS(resp, 115, 4);
584 e = UNSTUFF_BITS(resp, 112, 3);
585 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
586 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
588 m = UNSTUFF_BITS(resp, 99, 4);
589 e = UNSTUFF_BITS(resp, 96, 3);
590 csd->max_dtr = tran_exp[e] * tran_mant[m];
591 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
593 e = UNSTUFF_BITS(resp, 47, 3);
594 m = UNSTUFF_BITS(resp, 62, 12);
595 csd->capacity = (1 + m) << (e + 2);
597 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
598 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
599 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
600 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
601 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
602 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
603 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
608 * Given a 64-bit response, decode to our card SCR structure.
610 static void mmc_decode_scr(struct mmc_card *card)
612 struct sd_scr *scr = &card->scr;
613 unsigned int scr_struct;
616 BUG_ON(!mmc_card_sd(card));
618 resp[3] = card->raw_scr[1];
619 resp[2] = card->raw_scr[0];
621 scr_struct = UNSTUFF_BITS(resp, 60, 4);
622 if (scr_struct != 0) {
623 printk("%s: unrecognised SCR structure version %d\n",
624 mmc_hostname(card->host), scr_struct);
625 mmc_card_set_bad(card);
629 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
630 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
634 * Locate a MMC card on this MMC host given a raw CID.
636 static struct mmc_card *mmc_find_card(struct mmc_host *host, u32 *raw_cid)
638 struct mmc_card *card;
640 list_for_each_entry(card, &host->cards, node) {
641 if (memcmp(card->raw_cid, raw_cid, sizeof(card->raw_cid)) == 0)
648 * Allocate a new MMC card, and assign a unique RCA.
650 static struct mmc_card *
651 mmc_alloc_card(struct mmc_host *host, u32 *raw_cid, unsigned int *frca)
653 struct mmc_card *card, *c;
654 unsigned int rca = *frca;
656 card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL);
658 return ERR_PTR(-ENOMEM);
660 mmc_init_card(card, host);
661 memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid));
664 list_for_each_entry(c, &host->cards, node)
678 * Tell attached cards to go to IDLE state
680 static void mmc_idle_cards(struct mmc_host *host)
682 struct mmc_command cmd;
684 host->ios.chip_select = MMC_CS_HIGH;
689 cmd.opcode = MMC_GO_IDLE_STATE;
691 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
693 mmc_wait_for_cmd(host, &cmd, 0);
697 host->ios.chip_select = MMC_CS_DONTCARE;
704 * Apply power to the MMC stack. This is a two-stage process.
705 * First, we enable power to the card without the clock running.
706 * We then wait a bit for the power to stabilise. Finally,
707 * enable the bus drivers and clock to the card.
709 * We must _NOT_ enable the clock prior to power stablising.
711 * If a host does all the power sequencing itself, ignore the
712 * initial MMC_POWER_UP stage.
714 static void mmc_power_up(struct mmc_host *host)
716 int bit = fls(host->ocr_avail) - 1;
719 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
720 host->ios.chip_select = MMC_CS_DONTCARE;
721 host->ios.power_mode = MMC_POWER_UP;
722 host->ios.bus_width = MMC_BUS_WIDTH_1;
727 host->ios.clock = host->f_min;
728 host->ios.power_mode = MMC_POWER_ON;
734 static void mmc_power_off(struct mmc_host *host)
738 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
739 host->ios.chip_select = MMC_CS_DONTCARE;
740 host->ios.power_mode = MMC_POWER_OFF;
741 host->ios.bus_width = MMC_BUS_WIDTH_1;
745 static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
747 struct mmc_command cmd;
750 cmd.opcode = MMC_SEND_OP_COND;
752 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
754 for (i = 100; i; i--) {
755 err = mmc_wait_for_cmd(host, &cmd, 0);
756 if (err != MMC_ERR_NONE)
759 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
762 err = MMC_ERR_TIMEOUT;
773 static int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
775 struct mmc_command cmd;
778 cmd.opcode = SD_APP_OP_COND;
780 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
782 for (i = 100; i; i--) {
783 err = mmc_wait_for_app_cmd(host, 0, &cmd, CMD_RETRIES);
784 if (err != MMC_ERR_NONE)
787 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
790 err = MMC_ERR_TIMEOUT;
802 * Discover cards by requesting their CID. If this command
803 * times out, it is not an error; there are no further cards
804 * to be discovered. Add new cards to the list.
806 * Create a mmc_card entry for each discovered card, assigning
807 * it an RCA, and save the raw CID for decoding later.
809 static void mmc_discover_cards(struct mmc_host *host)
811 struct mmc_card *card;
812 unsigned int first_rca = 1, err;
815 struct mmc_command cmd;
817 cmd.opcode = MMC_ALL_SEND_CID;
819 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
821 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
822 if (err == MMC_ERR_TIMEOUT) {
826 if (err != MMC_ERR_NONE) {
827 printk(KERN_ERR "%s: error requesting CID: %d\n",
828 mmc_hostname(host), err);
832 card = mmc_find_card(host, cmd.resp);
834 card = mmc_alloc_card(host, cmd.resp, &first_rca);
839 list_add(&card->node, &host->cards);
842 card->state &= ~MMC_STATE_DEAD;
844 if (host->mode == MMC_MODE_SD) {
845 mmc_card_set_sd(card);
847 cmd.opcode = SD_SEND_RELATIVE_ADDR;
849 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
851 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
852 if (err != MMC_ERR_NONE)
853 mmc_card_set_dead(card);
855 card->rca = cmd.resp[0] >> 16;
857 if (!host->ops->get_ro) {
858 printk(KERN_WARNING "%s: host does not "
859 "support reading read-only "
860 "switch. assuming write-enable.\n",
863 if (host->ops->get_ro(host))
864 mmc_card_set_readonly(card);
868 cmd.opcode = MMC_SET_RELATIVE_ADDR;
869 cmd.arg = card->rca << 16;
870 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
872 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
873 if (err != MMC_ERR_NONE)
874 mmc_card_set_dead(card);
879 static void mmc_read_csds(struct mmc_host *host)
881 struct mmc_card *card;
883 list_for_each_entry(card, &host->cards, node) {
884 struct mmc_command cmd;
887 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
890 cmd.opcode = MMC_SEND_CSD;
891 cmd.arg = card->rca << 16;
892 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
894 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
895 if (err != MMC_ERR_NONE) {
896 mmc_card_set_dead(card);
900 memcpy(card->raw_csd, cmd.resp, sizeof(card->raw_csd));
902 mmc_decode_csd(card);
903 mmc_decode_cid(card);
907 static void mmc_read_scrs(struct mmc_host *host)
910 struct mmc_card *card;
912 struct mmc_request mrq;
913 struct mmc_command cmd;
914 struct mmc_data data;
916 struct scatterlist sg;
918 list_for_each_entry(card, &host->cards, node) {
919 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
921 if (!mmc_card_sd(card))
924 err = mmc_select_card(host, card);
925 if (err != MMC_ERR_NONE) {
926 mmc_card_set_dead(card);
930 memset(&cmd, 0, sizeof(struct mmc_command));
932 cmd.opcode = MMC_APP_CMD;
933 cmd.arg = card->rca << 16;
934 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
936 err = mmc_wait_for_cmd(host, &cmd, 0);
937 if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD)) {
938 mmc_card_set_dead(card);
942 memset(&cmd, 0, sizeof(struct mmc_command));
944 cmd.opcode = SD_APP_SEND_SCR;
946 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
948 memset(&data, 0, sizeof(struct mmc_data));
950 data.timeout_ns = card->csd.tacc_ns * 10;
951 data.timeout_clks = card->csd.tacc_clks * 10;
955 data.flags = MMC_DATA_READ;
959 memset(&mrq, 0, sizeof(struct mmc_request));
964 sg_init_one(&sg, (u8*)card->raw_scr, 8);
966 mmc_wait_for_req(host, &mrq);
968 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
969 mmc_card_set_dead(card);
973 card->raw_scr[0] = ntohl(card->raw_scr[0]);
974 card->raw_scr[1] = ntohl(card->raw_scr[1]);
976 mmc_decode_scr(card);
979 mmc_deselect_cards(host);
982 static unsigned int mmc_calculate_clock(struct mmc_host *host)
984 struct mmc_card *card;
985 unsigned int max_dtr = host->f_max;
987 list_for_each_entry(card, &host->cards, node)
988 if (!mmc_card_dead(card) && max_dtr > card->csd.max_dtr)
989 max_dtr = card->csd.max_dtr;
991 pr_debug("%s: selected %d.%03dMHz transfer rate\n",
993 max_dtr / 1000000, (max_dtr / 1000) % 1000);
999 * Check whether cards we already know about are still present.
1000 * We do this by requesting status, and checking whether a card
1003 * A request for status does not cause a state change in data
1006 static void mmc_check_cards(struct mmc_host *host)
1008 struct list_head *l, *n;
1010 mmc_deselect_cards(host);
1012 list_for_each_safe(l, n, &host->cards) {
1013 struct mmc_card *card = mmc_list_to_card(l);
1014 struct mmc_command cmd;
1017 cmd.opcode = MMC_SEND_STATUS;
1018 cmd.arg = card->rca << 16;
1019 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1021 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1022 if (err == MMC_ERR_NONE)
1025 mmc_card_set_dead(card);
1029 static void mmc_setup(struct mmc_host *host)
1031 if (host->ios.power_mode != MMC_POWER_ON) {
1035 host->mode = MMC_MODE_SD;
1038 mmc_idle_cards(host);
1040 err = mmc_send_app_op_cond(host, 0, &ocr);
1043 * If we fail to detect any SD cards then try
1044 * searching for MMC cards.
1046 if (err != MMC_ERR_NONE) {
1047 host->mode = MMC_MODE_MMC;
1049 err = mmc_send_op_cond(host, 0, &ocr);
1050 if (err != MMC_ERR_NONE)
1054 host->ocr = mmc_select_voltage(host, ocr);
1057 * Since we're changing the OCR value, we seem to
1058 * need to tell some cards to go back to the idle
1059 * state. We wait 1ms to give cards time to
1063 mmc_idle_cards(host);
1065 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1066 host->ios.clock = host->f_min;
1070 * We should remember the OCR mask from the existing
1071 * cards, and detect the new cards OCR mask, combine
1072 * the two and re-select the VDD. However, if we do
1073 * change VDD, we should do an idle, and then do a
1074 * full re-initialisation. We would need to notify
1075 * drivers so that they can re-setup the cards as
1076 * well, while keeping their queues at bay.
1078 * For the moment, we take the easy way out - if the
1079 * new cards don't like our currently selected VDD,
1080 * they drop off the bus.
1088 * Send the selected OCR multiple times... until the cards
1089 * all get the idea that they should be ready for CMD2.
1090 * (My SanDisk card seems to need this.)
1092 if (host->mode == MMC_MODE_SD)
1093 mmc_send_app_op_cond(host, host->ocr, NULL);
1095 mmc_send_op_cond(host, host->ocr, NULL);
1097 mmc_discover_cards(host);
1100 * Ok, now switch to push-pull mode.
1102 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1105 mmc_read_csds(host);
1107 if (host->mode == MMC_MODE_SD)
1108 mmc_read_scrs(host);
1113 * mmc_detect_change - process change of state on a MMC socket
1114 * @host: host which changed state.
1115 * @delay: optional delay to wait before detection (jiffies)
1117 * All we know is that card(s) have been inserted or removed
1118 * from the socket(s). We don't know which socket or cards.
1120 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1123 schedule_delayed_work(&host->detect, delay);
1125 schedule_work(&host->detect);
1128 EXPORT_SYMBOL(mmc_detect_change);
1131 static void mmc_rescan(void *data)
1133 struct mmc_host *host = data;
1134 struct list_head *l, *n;
1136 mmc_claim_host(host);
1138 if (host->ios.power_mode == MMC_POWER_ON)
1139 mmc_check_cards(host);
1143 if (!list_empty(&host->cards)) {
1145 * (Re-)calculate the fastest clock rate which the
1146 * attached cards and the host support.
1148 host->ios.clock = mmc_calculate_clock(host);
1152 mmc_release_host(host);
1154 list_for_each_safe(l, n, &host->cards) {
1155 struct mmc_card *card = mmc_list_to_card(l);
1158 * If this is a new and good card, register it.
1160 if (!mmc_card_present(card) && !mmc_card_dead(card)) {
1161 if (mmc_register_card(card))
1162 mmc_card_set_dead(card);
1164 mmc_card_set_present(card);
1168 * If this card is dead, destroy it.
1170 if (mmc_card_dead(card)) {
1171 list_del(&card->node);
1172 mmc_remove_card(card);
1177 * If we discover that there are no cards on the
1178 * bus, turn off the clock and power down.
1180 if (list_empty(&host->cards))
1181 mmc_power_off(host);
1186 * mmc_alloc_host - initialise the per-host structure.
1187 * @extra: sizeof private data structure
1188 * @dev: pointer to host device model structure
1190 * Initialise the per-host structure.
1192 struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
1194 struct mmc_host *host;
1196 host = mmc_alloc_host_sysfs(extra, dev);
1198 spin_lock_init(&host->lock);
1199 init_waitqueue_head(&host->wq);
1200 INIT_LIST_HEAD(&host->cards);
1201 INIT_WORK(&host->detect, mmc_rescan, host);
1204 * By default, hosts do not support SGIO or large requests.
1205 * They have to set these according to their abilities.
1207 host->max_hw_segs = 1;
1208 host->max_phys_segs = 1;
1209 host->max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
1210 host->max_seg_size = PAGE_CACHE_SIZE;
1216 EXPORT_SYMBOL(mmc_alloc_host);
1219 * mmc_add_host - initialise host hardware
1222 int mmc_add_host(struct mmc_host *host)
1226 ret = mmc_add_host_sysfs(host);
1228 mmc_power_off(host);
1229 mmc_detect_change(host, 0);
1235 EXPORT_SYMBOL(mmc_add_host);
1238 * mmc_remove_host - remove host hardware
1241 * Unregister and remove all cards associated with this host,
1242 * and power down the MMC bus.
1244 void mmc_remove_host(struct mmc_host *host)
1246 struct list_head *l, *n;
1248 list_for_each_safe(l, n, &host->cards) {
1249 struct mmc_card *card = mmc_list_to_card(l);
1251 mmc_remove_card(card);
1254 mmc_power_off(host);
1255 mmc_remove_host_sysfs(host);
1258 EXPORT_SYMBOL(mmc_remove_host);
1261 * mmc_free_host - free the host structure
1264 * Free the host once all references to it have been dropped.
1266 void mmc_free_host(struct mmc_host *host)
1268 flush_scheduled_work();
1269 mmc_free_host_sysfs(host);
1272 EXPORT_SYMBOL(mmc_free_host);
1277 * mmc_suspend_host - suspend a host
1279 * @state: suspend mode (PM_SUSPEND_xxx)
1281 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1283 mmc_claim_host(host);
1284 mmc_deselect_cards(host);
1285 mmc_power_off(host);
1286 mmc_release_host(host);
1291 EXPORT_SYMBOL(mmc_suspend_host);
1294 * mmc_resume_host - resume a previously suspended host
1297 int mmc_resume_host(struct mmc_host *host)
1304 EXPORT_SYMBOL(mmc_resume_host);
1308 MODULE_LICENSE("GPL");