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 request
63 * @host: MMC host which completed request
64 * @mrq: MMC request which request
66 * MMC drivers should call this function when they have completed
67 * their processing of a request.
69 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
71 struct mmc_command *cmd = mrq->cmd;
72 int err = mrq->cmd->error;
73 pr_debug("MMC: req done (%02x): %d: %08x %08x %08x %08x\n",
74 cmd->opcode, err, cmd->resp[0], cmd->resp[1],
75 cmd->resp[2], cmd->resp[3]);
77 if (err && cmd->retries) {
80 host->ops->request(host, mrq);
81 } else if (mrq->done) {
86 EXPORT_SYMBOL(mmc_request_done);
89 * mmc_start_request - start a command on a host
90 * @host: MMC host to start command on
91 * @mrq: MMC request to start
93 * Queue a command on the specified host. We expect the
94 * caller to be holding the host lock with interrupts disabled.
97 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
99 pr_debug("MMC: starting cmd %02x arg %08x flags %08x\n",
100 mrq->cmd->opcode, mrq->cmd->arg, mrq->cmd->flags);
102 WARN_ON(host->card_busy == NULL);
107 mrq->cmd->data = mrq->data;
108 mrq->data->error = 0;
109 mrq->data->mrq = mrq;
111 mrq->data->stop = mrq->stop;
112 mrq->stop->error = 0;
113 mrq->stop->mrq = mrq;
116 host->ops->request(host, mrq);
119 EXPORT_SYMBOL(mmc_start_request);
121 static void mmc_wait_done(struct mmc_request *mrq)
123 complete(mrq->done_data);
126 int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
128 DECLARE_COMPLETION(complete);
130 mrq->done_data = &complete;
131 mrq->done = mmc_wait_done;
133 mmc_start_request(host, mrq);
135 wait_for_completion(&complete);
140 EXPORT_SYMBOL(mmc_wait_for_req);
143 * mmc_wait_for_cmd - start a command and wait for completion
144 * @host: MMC host to start command
145 * @cmd: MMC command to start
146 * @retries: maximum number of retries
148 * Start a new MMC command for a host, and wait for the command
149 * to complete. Return any error that occurred while the command
150 * was executing. Do not attempt to parse the response.
152 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
154 struct mmc_request mrq;
156 BUG_ON(host->card_busy == NULL);
158 memset(&mrq, 0, sizeof(struct mmc_request));
160 memset(cmd->resp, 0, sizeof(cmd->resp));
161 cmd->retries = retries;
166 mmc_wait_for_req(host, &mrq);
171 EXPORT_SYMBOL(mmc_wait_for_cmd);
174 * mmc_wait_for_app_cmd - start an application command and wait for
176 * @host: MMC host to start command
177 * @rca: RCA to send MMC_APP_CMD to
178 * @cmd: MMC command to start
179 * @retries: maximum number of retries
181 * Sends a MMC_APP_CMD, checks the card response, sends the command
182 * in the parameter and waits for it to complete. Return any error
183 * that occurred while the command was executing. Do not attempt to
184 * parse the response.
186 int mmc_wait_for_app_cmd(struct mmc_host *host, unsigned int rca,
187 struct mmc_command *cmd, int retries)
189 struct mmc_request mrq;
190 struct mmc_command appcmd;
194 BUG_ON(host->card_busy == NULL);
197 err = MMC_ERR_INVALID;
200 * We have to resend MMC_APP_CMD for each attempt so
201 * we cannot use the retries field in mmc_command.
203 for (i = 0;i <= retries;i++) {
204 memset(&mrq, 0, sizeof(struct mmc_request));
206 appcmd.opcode = MMC_APP_CMD;
207 appcmd.arg = rca << 16;
208 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
210 memset(appcmd.resp, 0, sizeof(appcmd.resp));
216 mmc_wait_for_req(host, &mrq);
223 /* Check that card supported application commands */
224 if (!(appcmd.resp[0] & R1_APP_CMD))
225 return MMC_ERR_FAILED;
227 memset(&mrq, 0, sizeof(struct mmc_request));
229 memset(cmd->resp, 0, sizeof(cmd->resp));
235 mmc_wait_for_req(host, &mrq);
238 if (cmd->error == MMC_ERR_NONE)
245 EXPORT_SYMBOL(mmc_wait_for_app_cmd);
247 static int mmc_select_card(struct mmc_host *host, struct mmc_card *card);
250 * __mmc_claim_host - exclusively claim a host
251 * @host: mmc host to claim
252 * @card: mmc card to claim host for
254 * Claim a host for a set of operations. If a valid card
255 * is passed and this wasn't the last card selected, select
256 * the card before returning.
258 * Note: you should use mmc_card_claim_host or mmc_claim_host.
260 int __mmc_claim_host(struct mmc_host *host, struct mmc_card *card)
262 DECLARE_WAITQUEUE(wait, current);
266 add_wait_queue(&host->wq, &wait);
267 spin_lock_irqsave(&host->lock, flags);
269 set_current_state(TASK_UNINTERRUPTIBLE);
270 if (host->card_busy == NULL)
272 spin_unlock_irqrestore(&host->lock, flags);
274 spin_lock_irqsave(&host->lock, flags);
276 set_current_state(TASK_RUNNING);
277 host->card_busy = card;
278 spin_unlock_irqrestore(&host->lock, flags);
279 remove_wait_queue(&host->wq, &wait);
281 if (card != (void *)-1) {
282 err = mmc_select_card(host, card);
283 if (err != MMC_ERR_NONE)
290 EXPORT_SYMBOL(__mmc_claim_host);
293 * mmc_release_host - release a host
294 * @host: mmc host to release
296 * Release a MMC host, allowing others to claim the host
297 * for their operations.
299 void mmc_release_host(struct mmc_host *host)
303 BUG_ON(host->card_busy == NULL);
305 spin_lock_irqsave(&host->lock, flags);
306 host->card_busy = NULL;
307 spin_unlock_irqrestore(&host->lock, flags);
312 EXPORT_SYMBOL(mmc_release_host);
314 static int mmc_select_card(struct mmc_host *host, struct mmc_card *card)
317 struct mmc_command cmd;
319 BUG_ON(host->card_busy == NULL);
321 if (host->card_selected == card)
324 host->card_selected = card;
326 cmd.opcode = MMC_SELECT_CARD;
327 cmd.arg = card->rca << 16;
328 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
330 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
331 if (err != MMC_ERR_NONE)
335 * Default bus width is 1 bit.
337 host->ios.bus_width = MMC_BUS_WIDTH_1;
340 * We can only change the bus width of the selected
341 * card so therefore we have to put the handling
344 if (host->caps & MMC_CAP_4_BIT_DATA) {
346 * The card is in 1 bit mode by default so
347 * we only need to change if it supports the
350 if (mmc_card_sd(card) &&
351 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
352 struct mmc_command cmd;
353 cmd.opcode = SD_APP_SET_BUS_WIDTH;
354 cmd.arg = SD_BUS_WIDTH_4;
355 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
357 err = mmc_wait_for_app_cmd(host, card->rca, &cmd,
359 if (err != MMC_ERR_NONE)
362 host->ios.bus_width = MMC_BUS_WIDTH_4;
366 host->ops->set_ios(host, &host->ios);
372 * Ensure that no card is selected.
374 static void mmc_deselect_cards(struct mmc_host *host)
376 struct mmc_command cmd;
378 if (host->card_selected) {
379 host->card_selected = NULL;
381 cmd.opcode = MMC_SELECT_CARD;
383 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
385 mmc_wait_for_cmd(host, &cmd, 0);
390 static inline void mmc_delay(unsigned int ms)
392 if (ms < HZ / 1000) {
396 msleep_interruptible (ms);
401 * Mask off any voltages we don't support and select
404 static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
408 ocr &= host->ocr_avail;
417 host->ops->set_ios(host, &host->ios);
425 #define UNSTUFF_BITS(resp,start,size) \
427 const int __size = size; \
428 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
429 const int __off = 3 - ((start) / 32); \
430 const int __shft = (start) & 31; \
433 __res = resp[__off] >> __shft; \
434 if (__size + __shft > 32) \
435 __res |= resp[__off-1] << ((32 - __shft) % 32); \
440 * Given the decoded CSD structure, decode the raw CID to our CID structure.
442 static void mmc_decode_cid(struct mmc_card *card)
444 u32 *resp = card->raw_cid;
446 memset(&card->cid, 0, sizeof(struct mmc_cid));
448 if (mmc_card_sd(card)) {
450 * SD doesn't currently have a version field so we will
451 * have to assume we can parse this.
453 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
454 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
455 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
456 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
457 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
458 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
459 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
460 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
461 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
462 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
463 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
464 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
466 card->cid.year += 2000; /* SD cards year offset */
469 * The selection of the format here is based upon published
470 * specs from sandisk and from what people have reported.
472 switch (card->csd.mmca_vsn) {
473 case 0: /* MMC v1.0 - v1.2 */
474 case 1: /* MMC v1.4 */
475 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
476 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
477 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
478 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
479 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
480 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
481 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
482 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
483 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
484 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
485 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
486 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
487 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
490 case 2: /* MMC v2.0 - v2.2 */
491 case 3: /* MMC v3.1 - v3.3 */
493 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
494 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
495 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
496 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
497 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
498 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
499 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
500 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
501 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
502 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
503 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
507 printk("%s: card has unknown MMCA version %d\n",
508 mmc_hostname(card->host), card->csd.mmca_vsn);
509 mmc_card_set_bad(card);
516 * Given a 128-bit response, decode to our card CSD structure.
518 static void mmc_decode_csd(struct mmc_card *card)
520 struct mmc_csd *csd = &card->csd;
521 unsigned int e, m, csd_struct;
522 u32 *resp = card->raw_csd;
524 if (mmc_card_sd(card)) {
525 csd_struct = UNSTUFF_BITS(resp, 126, 2);
526 if (csd_struct != 0) {
527 printk("%s: unrecognised CSD structure version %d\n",
528 mmc_hostname(card->host), csd_struct);
529 mmc_card_set_bad(card);
533 m = UNSTUFF_BITS(resp, 115, 4);
534 e = UNSTUFF_BITS(resp, 112, 3);
535 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
536 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
538 m = UNSTUFF_BITS(resp, 99, 4);
539 e = UNSTUFF_BITS(resp, 96, 3);
540 csd->max_dtr = tran_exp[e] * tran_mant[m];
541 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
543 e = UNSTUFF_BITS(resp, 47, 3);
544 m = UNSTUFF_BITS(resp, 62, 12);
545 csd->capacity = (1 + m) << (e + 2);
547 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
548 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
549 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
550 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
551 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
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->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
587 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
588 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
593 * Given a 64-bit response, decode to our card SCR structure.
595 static void mmc_decode_scr(struct mmc_card *card)
597 struct sd_scr *scr = &card->scr;
598 unsigned int scr_struct;
601 BUG_ON(!mmc_card_sd(card));
603 resp[3] = card->raw_scr[1];
604 resp[2] = card->raw_scr[0];
606 scr_struct = UNSTUFF_BITS(resp, 60, 4);
607 if (scr_struct != 0) {
608 printk("%s: unrecognised SCR structure version %d\n",
609 mmc_hostname(card->host), scr_struct);
610 mmc_card_set_bad(card);
614 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
615 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
619 * Locate a MMC card on this MMC host given a raw CID.
621 static struct mmc_card *mmc_find_card(struct mmc_host *host, u32 *raw_cid)
623 struct mmc_card *card;
625 list_for_each_entry(card, &host->cards, node) {
626 if (memcmp(card->raw_cid, raw_cid, sizeof(card->raw_cid)) == 0)
633 * Allocate a new MMC card, and assign a unique RCA.
635 static struct mmc_card *
636 mmc_alloc_card(struct mmc_host *host, u32 *raw_cid, unsigned int *frca)
638 struct mmc_card *card, *c;
639 unsigned int rca = *frca;
641 card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL);
643 return ERR_PTR(-ENOMEM);
645 mmc_init_card(card, host);
646 memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid));
649 list_for_each_entry(c, &host->cards, node)
663 * Tell attached cards to go to IDLE state
665 static void mmc_idle_cards(struct mmc_host *host)
667 struct mmc_command cmd;
669 host->ios.chip_select = MMC_CS_HIGH;
670 host->ops->set_ios(host, &host->ios);
674 cmd.opcode = MMC_GO_IDLE_STATE;
676 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
678 mmc_wait_for_cmd(host, &cmd, 0);
682 host->ios.chip_select = MMC_CS_DONTCARE;
683 host->ops->set_ios(host, &host->ios);
689 * Apply power to the MMC stack. This is a two-stage process.
690 * First, we enable power to the card without the clock running.
691 * We then wait a bit for the power to stabilise. Finally,
692 * enable the bus drivers and clock to the card.
694 * We must _NOT_ enable the clock prior to power stablising.
696 * If a host does all the power sequencing itself, ignore the
697 * initial MMC_POWER_UP stage.
699 static void mmc_power_up(struct mmc_host *host)
701 int bit = fls(host->ocr_avail) - 1;
704 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
705 host->ios.chip_select = MMC_CS_DONTCARE;
706 host->ios.power_mode = MMC_POWER_UP;
707 host->ios.bus_width = MMC_BUS_WIDTH_1;
708 host->ops->set_ios(host, &host->ios);
712 host->ios.clock = host->f_min;
713 host->ios.power_mode = MMC_POWER_ON;
714 host->ops->set_ios(host, &host->ios);
719 static void mmc_power_off(struct mmc_host *host)
723 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
724 host->ios.chip_select = MMC_CS_DONTCARE;
725 host->ios.power_mode = MMC_POWER_OFF;
726 host->ios.bus_width = MMC_BUS_WIDTH_1;
727 host->ops->set_ios(host, &host->ios);
730 static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
732 struct mmc_command cmd;
735 cmd.opcode = MMC_SEND_OP_COND;
737 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
739 for (i = 100; i; i--) {
740 err = mmc_wait_for_cmd(host, &cmd, 0);
741 if (err != MMC_ERR_NONE)
744 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
747 err = MMC_ERR_TIMEOUT;
758 static int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
760 struct mmc_command cmd;
763 cmd.opcode = SD_APP_OP_COND;
765 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
767 for (i = 100; i; i--) {
768 err = mmc_wait_for_app_cmd(host, 0, &cmd, CMD_RETRIES);
769 if (err != MMC_ERR_NONE)
772 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
775 err = MMC_ERR_TIMEOUT;
787 * Discover cards by requesting their CID. If this command
788 * times out, it is not an error; there are no further cards
789 * to be discovered. Add new cards to the list.
791 * Create a mmc_card entry for each discovered card, assigning
792 * it an RCA, and save the raw CID for decoding later.
794 static void mmc_discover_cards(struct mmc_host *host)
796 struct mmc_card *card;
797 unsigned int first_rca = 1, err;
800 struct mmc_command cmd;
802 cmd.opcode = MMC_ALL_SEND_CID;
804 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
806 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
807 if (err == MMC_ERR_TIMEOUT) {
811 if (err != MMC_ERR_NONE) {
812 printk(KERN_ERR "%s: error requesting CID: %d\n",
813 mmc_hostname(host), err);
817 card = mmc_find_card(host, cmd.resp);
819 card = mmc_alloc_card(host, cmd.resp, &first_rca);
824 list_add(&card->node, &host->cards);
827 card->state &= ~MMC_STATE_DEAD;
829 if (host->mode == MMC_MODE_SD) {
830 mmc_card_set_sd(card);
832 cmd.opcode = SD_SEND_RELATIVE_ADDR;
834 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
836 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
837 if (err != MMC_ERR_NONE)
838 mmc_card_set_dead(card);
840 card->rca = cmd.resp[0] >> 16;
842 if (!host->ops->get_ro) {
843 printk(KERN_WARNING "%s: host does not "
844 "support reading read-only "
845 "switch. assuming write-enable.\n",
848 if (host->ops->get_ro(host))
849 mmc_card_set_readonly(card);
853 cmd.opcode = MMC_SET_RELATIVE_ADDR;
854 cmd.arg = card->rca << 16;
855 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
857 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
858 if (err != MMC_ERR_NONE)
859 mmc_card_set_dead(card);
864 static void mmc_read_csds(struct mmc_host *host)
866 struct mmc_card *card;
868 list_for_each_entry(card, &host->cards, node) {
869 struct mmc_command cmd;
872 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
875 cmd.opcode = MMC_SEND_CSD;
876 cmd.arg = card->rca << 16;
877 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
879 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
880 if (err != MMC_ERR_NONE) {
881 mmc_card_set_dead(card);
885 memcpy(card->raw_csd, cmd.resp, sizeof(card->raw_csd));
887 mmc_decode_csd(card);
888 mmc_decode_cid(card);
892 static void mmc_read_scrs(struct mmc_host *host)
895 struct mmc_card *card;
897 struct mmc_request mrq;
898 struct mmc_command cmd;
899 struct mmc_data data;
901 struct scatterlist sg;
903 list_for_each_entry(card, &host->cards, node) {
904 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
906 if (!mmc_card_sd(card))
909 err = mmc_select_card(host, card);
910 if (err != MMC_ERR_NONE) {
911 mmc_card_set_dead(card);
915 memset(&cmd, 0, sizeof(struct mmc_command));
917 cmd.opcode = MMC_APP_CMD;
918 cmd.arg = card->rca << 16;
919 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
921 err = mmc_wait_for_cmd(host, &cmd, 0);
922 if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD)) {
923 mmc_card_set_dead(card);
927 memset(&cmd, 0, sizeof(struct mmc_command));
929 cmd.opcode = SD_APP_SEND_SCR;
931 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
933 memset(&data, 0, sizeof(struct mmc_data));
935 data.timeout_ns = card->csd.tacc_ns * 10;
936 data.timeout_clks = card->csd.tacc_clks * 10;
939 data.flags = MMC_DATA_READ;
943 memset(&mrq, 0, sizeof(struct mmc_request));
948 sg_init_one(&sg, (u8*)card->raw_scr, 8);
950 mmc_wait_for_req(host, &mrq);
952 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
953 mmc_card_set_dead(card);
957 card->raw_scr[0] = ntohl(card->raw_scr[0]);
958 card->raw_scr[1] = ntohl(card->raw_scr[1]);
960 mmc_decode_scr(card);
963 mmc_deselect_cards(host);
966 static unsigned int mmc_calculate_clock(struct mmc_host *host)
968 struct mmc_card *card;
969 unsigned int max_dtr = host->f_max;
971 list_for_each_entry(card, &host->cards, node)
972 if (!mmc_card_dead(card) && max_dtr > card->csd.max_dtr)
973 max_dtr = card->csd.max_dtr;
975 pr_debug("MMC: selected %d.%03dMHz transfer rate\n",
976 max_dtr / 1000000, (max_dtr / 1000) % 1000);
982 * Check whether cards we already know about are still present.
983 * We do this by requesting status, and checking whether a card
986 * A request for status does not cause a state change in data
989 static void mmc_check_cards(struct mmc_host *host)
991 struct list_head *l, *n;
993 mmc_deselect_cards(host);
995 list_for_each_safe(l, n, &host->cards) {
996 struct mmc_card *card = mmc_list_to_card(l);
997 struct mmc_command cmd;
1000 cmd.opcode = MMC_SEND_STATUS;
1001 cmd.arg = card->rca << 16;
1002 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1004 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1005 if (err == MMC_ERR_NONE)
1008 mmc_card_set_dead(card);
1012 static void mmc_setup(struct mmc_host *host)
1014 if (host->ios.power_mode != MMC_POWER_ON) {
1018 host->mode = MMC_MODE_SD;
1021 mmc_idle_cards(host);
1023 err = mmc_send_app_op_cond(host, 0, &ocr);
1026 * If we fail to detect any SD cards then try
1027 * searching for MMC cards.
1029 if (err != MMC_ERR_NONE) {
1030 host->mode = MMC_MODE_MMC;
1032 err = mmc_send_op_cond(host, 0, &ocr);
1033 if (err != MMC_ERR_NONE)
1037 host->ocr = mmc_select_voltage(host, ocr);
1040 * Since we're changing the OCR value, we seem to
1041 * need to tell some cards to go back to the idle
1042 * state. We wait 1ms to give cards time to
1046 mmc_idle_cards(host);
1048 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1049 host->ios.clock = host->f_min;
1050 host->ops->set_ios(host, &host->ios);
1053 * We should remember the OCR mask from the existing
1054 * cards, and detect the new cards OCR mask, combine
1055 * the two and re-select the VDD. However, if we do
1056 * change VDD, we should do an idle, and then do a
1057 * full re-initialisation. We would need to notify
1058 * drivers so that they can re-setup the cards as
1059 * well, while keeping their queues at bay.
1061 * For the moment, we take the easy way out - if the
1062 * new cards don't like our currently selected VDD,
1063 * they drop off the bus.
1071 * Send the selected OCR multiple times... until the cards
1072 * all get the idea that they should be ready for CMD2.
1073 * (My SanDisk card seems to need this.)
1075 if (host->mode == MMC_MODE_SD)
1076 mmc_send_app_op_cond(host, host->ocr, NULL);
1078 mmc_send_op_cond(host, host->ocr, NULL);
1080 mmc_discover_cards(host);
1083 * Ok, now switch to push-pull mode.
1085 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1086 host->ops->set_ios(host, &host->ios);
1088 mmc_read_csds(host);
1090 if (host->mode == MMC_MODE_SD)
1091 mmc_read_scrs(host);
1096 * mmc_detect_change - process change of state on a MMC socket
1097 * @host: host which changed state.
1098 * @delay: optional delay to wait before detection (jiffies)
1100 * All we know is that card(s) have been inserted or removed
1101 * from the socket(s). We don't know which socket or cards.
1103 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1106 schedule_delayed_work(&host->detect, delay);
1108 schedule_work(&host->detect);
1111 EXPORT_SYMBOL(mmc_detect_change);
1114 static void mmc_rescan(void *data)
1116 struct mmc_host *host = data;
1117 struct list_head *l, *n;
1119 mmc_claim_host(host);
1121 if (host->ios.power_mode == MMC_POWER_ON)
1122 mmc_check_cards(host);
1126 if (!list_empty(&host->cards)) {
1128 * (Re-)calculate the fastest clock rate which the
1129 * attached cards and the host support.
1131 host->ios.clock = mmc_calculate_clock(host);
1132 host->ops->set_ios(host, &host->ios);
1135 mmc_release_host(host);
1137 list_for_each_safe(l, n, &host->cards) {
1138 struct mmc_card *card = mmc_list_to_card(l);
1141 * If this is a new and good card, register it.
1143 if (!mmc_card_present(card) && !mmc_card_dead(card)) {
1144 if (mmc_register_card(card))
1145 mmc_card_set_dead(card);
1147 mmc_card_set_present(card);
1151 * If this card is dead, destroy it.
1153 if (mmc_card_dead(card)) {
1154 list_del(&card->node);
1155 mmc_remove_card(card);
1160 * If we discover that there are no cards on the
1161 * bus, turn off the clock and power down.
1163 if (list_empty(&host->cards))
1164 mmc_power_off(host);
1169 * mmc_alloc_host - initialise the per-host structure.
1170 * @extra: sizeof private data structure
1171 * @dev: pointer to host device model structure
1173 * Initialise the per-host structure.
1175 struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
1177 struct mmc_host *host;
1179 host = mmc_alloc_host_sysfs(extra, dev);
1181 spin_lock_init(&host->lock);
1182 init_waitqueue_head(&host->wq);
1183 INIT_LIST_HEAD(&host->cards);
1184 INIT_WORK(&host->detect, mmc_rescan, host);
1187 * By default, hosts do not support SGIO or large requests.
1188 * They have to set these according to their abilities.
1190 host->max_hw_segs = 1;
1191 host->max_phys_segs = 1;
1192 host->max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
1193 host->max_seg_size = PAGE_CACHE_SIZE;
1199 EXPORT_SYMBOL(mmc_alloc_host);
1202 * mmc_add_host - initialise host hardware
1205 int mmc_add_host(struct mmc_host *host)
1209 ret = mmc_add_host_sysfs(host);
1211 mmc_power_off(host);
1212 mmc_detect_change(host, 0);
1218 EXPORT_SYMBOL(mmc_add_host);
1221 * mmc_remove_host - remove host hardware
1224 * Unregister and remove all cards associated with this host,
1225 * and power down the MMC bus.
1227 void mmc_remove_host(struct mmc_host *host)
1229 struct list_head *l, *n;
1231 list_for_each_safe(l, n, &host->cards) {
1232 struct mmc_card *card = mmc_list_to_card(l);
1234 mmc_remove_card(card);
1237 mmc_power_off(host);
1238 mmc_remove_host_sysfs(host);
1241 EXPORT_SYMBOL(mmc_remove_host);
1244 * mmc_free_host - free the host structure
1247 * Free the host once all references to it have been dropped.
1249 void mmc_free_host(struct mmc_host *host)
1251 flush_scheduled_work();
1252 mmc_free_host_sysfs(host);
1255 EXPORT_SYMBOL(mmc_free_host);
1260 * mmc_suspend_host - suspend a host
1262 * @state: suspend mode (PM_SUSPEND_xxx)
1264 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1266 mmc_claim_host(host);
1267 mmc_deselect_cards(host);
1268 mmc_power_off(host);
1269 mmc_release_host(host);
1274 EXPORT_SYMBOL(mmc_suspend_host);
1277 * mmc_resume_host - resume a previously suspended host
1280 int mmc_resume_host(struct mmc_host *host)
1287 EXPORT_SYMBOL(mmc_resume_host);
1291 MODULE_LICENSE("GPL");