MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / drivers / mmc / mmc.c
blob31e827b65e6fc8b57f118de370687693e02b309d
1 /*
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>
27 #include "mmc.h"
29 #define CMD_RETRIES 3
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,
42 0, 0, 0, 0
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,
60 /**
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;
71 int err = cmd->error;
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) {
80 cmd->retries--;
81 cmd->error = 0;
82 host->ops->request(host, mrq);
83 } else if (mrq->done) {
84 mrq->done(mrq);
88 EXPORT_SYMBOL(mmc_request_done);
90 /**
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.
98 void
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);
107 mrq->cmd->error = 0;
108 mrq->cmd->mrq = mrq;
109 if (mrq->data) {
110 mrq->cmd->data = mrq->data;
111 mrq->data->error = 0;
112 mrq->data->mrq = mrq;
113 if (mrq->stop) {
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);
140 return 0;
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;
166 mrq.cmd = cmd;
167 cmd->data = NULL;
169 mmc_wait_for_req(host, &mrq);
171 return cmd->error;
174 EXPORT_SYMBOL(mmc_wait_for_cmd);
177 * mmc_wait_for_app_cmd - start an application command and wait for
178 completion
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;
195 int i, err;
197 BUG_ON(host->card_busy == NULL);
198 BUG_ON(retries < 0);
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;
212 appcmd.retries = 0;
213 memset(appcmd.resp, 0, sizeof(appcmd.resp));
214 appcmd.data = NULL;
216 mrq.cmd = &appcmd;
217 appcmd.data = NULL;
219 mmc_wait_for_req(host, &mrq);
221 if (appcmd.error) {
222 err = appcmd.error;
223 continue;
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));
233 cmd->retries = 0;
235 mrq.cmd = cmd;
236 cmd->data = NULL;
238 mmc_wait_for_req(host, &mrq);
240 err = cmd->error;
241 if (cmd->error == MMC_ERR_NONE)
242 break;
245 return err;
248 EXPORT_SYMBOL(mmc_wait_for_app_cmd);
251 * mmc_set_data_timeout - set the timeout for a data command
252 * @data: data phase for command
253 * @card: the MMC card associated with the data transfer
254 * @write: flag to differentiate reads from writes
256 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
257 int write)
259 unsigned int mult;
262 * SD cards use a 100 multiplier rather than 10
264 mult = mmc_card_sd(card) ? 100 : 10;
267 * Scale up the multiplier (and therefore the timeout) by
268 * the r2w factor for writes.
270 if (write)
271 mult <<= card->csd.r2w_factor;
273 data->timeout_ns = card->csd.tacc_ns * mult;
274 data->timeout_clks = card->csd.tacc_clks * mult;
277 * SD cards also have an upper limit on the timeout.
279 if (mmc_card_sd(card)) {
280 unsigned int timeout_us, limit_us;
282 timeout_us = data->timeout_ns / 1000;
283 timeout_us += data->timeout_clks * 1000 /
284 (card->host->ios.clock / 1000);
286 if (write)
287 limit_us = 250000;
288 else
289 limit_us = 100000;
291 if (timeout_us > limit_us) {
292 data->timeout_ns = limit_us * 1000;
293 data->timeout_clks = 0;
297 EXPORT_SYMBOL(mmc_set_data_timeout);
299 static int mmc_select_card(struct mmc_host *host, struct mmc_card *card);
302 * __mmc_claim_host - exclusively claim a host
303 * @host: mmc host to claim
304 * @card: mmc card to claim host for
306 * Claim a host for a set of operations. If a valid card
307 * is passed and this wasn't the last card selected, select
308 * the card before returning.
310 * Note: you should use mmc_card_claim_host or mmc_claim_host.
312 int __mmc_claim_host(struct mmc_host *host, struct mmc_card *card)
314 DECLARE_WAITQUEUE(wait, current);
315 unsigned long flags;
316 int err = 0;
318 add_wait_queue(&host->wq, &wait);
319 spin_lock_irqsave(&host->lock, flags);
320 while (1) {
321 set_current_state(TASK_UNINTERRUPTIBLE);
322 if (host->card_busy == NULL)
323 break;
324 spin_unlock_irqrestore(&host->lock, flags);
325 schedule();
326 spin_lock_irqsave(&host->lock, flags);
328 set_current_state(TASK_RUNNING);
329 host->card_busy = card;
330 spin_unlock_irqrestore(&host->lock, flags);
331 remove_wait_queue(&host->wq, &wait);
333 if (card != (void *)-1) {
334 err = mmc_select_card(host, card);
335 if (err != MMC_ERR_NONE)
336 return err;
339 return err;
342 EXPORT_SYMBOL(__mmc_claim_host);
345 * mmc_release_host - release a host
346 * @host: mmc host to release
348 * Release a MMC host, allowing others to claim the host
349 * for their operations.
351 void mmc_release_host(struct mmc_host *host)
353 unsigned long flags;
355 BUG_ON(host->card_busy == NULL);
357 spin_lock_irqsave(&host->lock, flags);
358 host->card_busy = NULL;
359 spin_unlock_irqrestore(&host->lock, flags);
361 wake_up(&host->wq);
364 EXPORT_SYMBOL(mmc_release_host);
366 static inline void mmc_set_ios(struct mmc_host *host)
368 struct mmc_ios *ios = &host->ios;
370 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u width %u\n",
371 mmc_hostname(host), ios->clock, ios->bus_mode,
372 ios->power_mode, ios->chip_select, ios->vdd,
373 ios->bus_width);
375 host->ops->set_ios(host, ios);
378 static int mmc_select_card(struct mmc_host *host, struct mmc_card *card)
380 int err;
381 struct mmc_command cmd;
383 BUG_ON(host->card_busy == NULL);
385 if (host->card_selected == card)
386 return MMC_ERR_NONE;
388 host->card_selected = card;
390 cmd.opcode = MMC_SELECT_CARD;
391 cmd.arg = card->rca << 16;
392 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
394 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
395 if (err != MMC_ERR_NONE)
396 return err;
399 * Default bus width is 1 bit.
401 host->ios.bus_width = MMC_BUS_WIDTH_1;
404 * We can only change the bus width of the selected
405 * card so therefore we have to put the handling
406 * here.
408 if (host->caps & MMC_CAP_4_BIT_DATA) {
410 * The card is in 1 bit mode by default so
411 * we only need to change if it supports the
412 * wider version.
414 if (mmc_card_sd(card) &&
415 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
416 struct mmc_command cmd;
417 cmd.opcode = SD_APP_SET_BUS_WIDTH;
418 cmd.arg = SD_BUS_WIDTH_4;
419 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
421 err = mmc_wait_for_app_cmd(host, card->rca, &cmd,
422 CMD_RETRIES);
423 if (err != MMC_ERR_NONE)
424 return err;
426 host->ios.bus_width = MMC_BUS_WIDTH_4;
430 mmc_set_ios(host);
432 return MMC_ERR_NONE;
436 * Ensure that no card is selected.
438 static void mmc_deselect_cards(struct mmc_host *host)
440 struct mmc_command cmd;
442 if (host->card_selected) {
443 host->card_selected = NULL;
445 cmd.opcode = MMC_SELECT_CARD;
446 cmd.arg = 0;
447 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
449 mmc_wait_for_cmd(host, &cmd, 0);
454 static inline void mmc_delay(unsigned int ms)
456 if (ms < HZ / 1000) {
457 yield();
458 mdelay(ms);
459 } else {
460 msleep_interruptible (ms);
465 * Mask off any voltages we don't support and select
466 * the lowest voltage
468 static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
470 int bit;
472 ocr &= host->ocr_avail;
474 bit = ffs(ocr);
475 if (bit) {
476 bit -= 1;
478 ocr &= 3 << bit;
480 host->ios.vdd = bit;
481 mmc_set_ios(host);
482 } else {
483 ocr = 0;
486 return ocr;
489 #if 0 // mask by Victor Yu. 03-07-2007
490 #define UNSTUFF_BITS(resp,start,size) \
491 ({ \
492 const int __size = size; \
493 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
494 const int __off = 3 - ((start) / 32); \
495 const int __shft = (start) & 31; \
496 u32 __res; \
498 __res = resp[__off] >> __shft; \
499 if (__size + __shft > 32) \
500 __res |= resp[__off-1] << ((32 - __shft) % 32); \
501 __res & __mask; \
503 #else
504 #define UNSTUFF_BITS(resp,start,size) \
505 ({ \
506 const int __size = size; \
507 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
508 const int __off = ((start) / 32); \
509 const int __shft = (start) & 31; \
510 u32 __res; \
512 __res = resp[__off] >> __shft; \
513 if (__size + __shft > 32) \
514 __res |= resp[__off+1] << ((32 - __shft) % 32); \
515 __res & __mask; \
517 #endif
520 * Given the decoded CSD structure, decode the raw CID to our CID structure.
522 static void mmc_decode_cid(struct mmc_card *card)
524 u32 *resp = card->raw_cid;
526 memset(&card->cid, 0, sizeof(struct mmc_cid));
528 if (mmc_card_sd(card)) {
530 * SD doesn't currently have a version field so we will
531 * have to assume we can parse this.
533 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
534 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
535 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
536 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
537 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
538 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
539 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
540 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
541 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
542 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
543 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
544 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
546 card->cid.year += 2000; /* SD cards year offset */
547 } else {
549 * The selection of the format here is based upon published
550 * specs from sandisk and from what people have reported.
552 switch (card->csd.mmca_vsn) {
553 case 0: /* MMC v1.0 - v1.2 */
554 case 1: /* MMC v1.4 */
555 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
556 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
557 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
558 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
559 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
560 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
561 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
562 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
563 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
564 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
565 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
566 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
567 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
568 break;
570 case 2: /* MMC v2.0 - v2.2 */
571 case 3: /* MMC v3.1 - v3.3 */
572 case 4: /* MMC v4 */
573 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
574 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
575 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
576 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
577 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
578 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
579 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
580 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
581 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
582 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
583 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
584 break;
586 default:
587 printk("%s: card has unknown MMCA version %d\n",
588 mmc_hostname(card->host), card->csd.mmca_vsn);
589 mmc_card_set_bad(card);
590 break;
596 * Given a 128-bit response, decode to our card CSD structure.
598 static void mmc_decode_csd(struct mmc_card *card)
600 struct mmc_csd *csd = &card->csd;
601 unsigned int e, m, csd_struct;
602 u32 *resp = card->raw_csd;
604 if (mmc_card_sd(card)) {
605 csd_struct = UNSTUFF_BITS(resp, 126, 2);
606 if (csd_struct != 0) {
607 printk("%s: unrecognised CSD structure version %d\n",
608 mmc_hostname(card->host), csd_struct);
609 mmc_card_set_bad(card);
610 return;
613 m = UNSTUFF_BITS(resp, 115, 4);
614 e = UNSTUFF_BITS(resp, 112, 3);
615 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
616 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
618 m = UNSTUFF_BITS(resp, 99, 4);
619 e = UNSTUFF_BITS(resp, 96, 3);
620 csd->max_dtr = tran_exp[e] * tran_mant[m];
621 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
623 e = UNSTUFF_BITS(resp, 47, 3);
624 m = UNSTUFF_BITS(resp, 62, 12);
625 csd->capacity = (1 + m) << (e + 2);
627 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
628 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
629 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
630 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
631 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
632 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
633 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
634 } else {
636 * We only understand CSD structure v1.1 and v1.2.
637 * v1.2 has extra information in bits 15, 11 and 10.
639 csd_struct = UNSTUFF_BITS(resp, 126, 2);
640 if (csd_struct != 1 && csd_struct != 2) {
641 printk("%s: unrecognised CSD structure version %d\n",
642 mmc_hostname(card->host), csd_struct);
643 mmc_card_set_bad(card);
644 return;
647 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
648 m = UNSTUFF_BITS(resp, 115, 4);
649 e = UNSTUFF_BITS(resp, 112, 3);
650 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
651 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
653 m = UNSTUFF_BITS(resp, 99, 4);
654 e = UNSTUFF_BITS(resp, 96, 3);
655 csd->max_dtr = tran_exp[e] * tran_mant[m];
656 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
658 e = UNSTUFF_BITS(resp, 47, 3);
659 m = UNSTUFF_BITS(resp, 62, 12);
660 csd->capacity = (1 + m) << (e + 2);
662 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
663 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
664 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
665 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
666 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
667 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
668 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
673 * Given a 64-bit response, decode to our card SCR structure.
675 static void mmc_decode_scr(struct mmc_card *card)
677 struct sd_scr *scr = &card->scr;
678 unsigned int scr_struct;
679 u32 resp[4];
681 BUG_ON(!mmc_card_sd(card));
683 resp[3] = card->raw_scr[1];
684 resp[2] = card->raw_scr[0];
686 scr_struct = UNSTUFF_BITS(resp, 60, 4);
687 if (scr_struct != 0) {
688 printk("%s: unrecognised SCR structure version %d\n",
689 mmc_hostname(card->host), scr_struct);
690 mmc_card_set_bad(card);
691 return;
694 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
695 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
699 * Locate a MMC card on this MMC host given a raw CID.
701 static struct mmc_card *mmc_find_card(struct mmc_host *host, u32 *raw_cid)
703 struct mmc_card *card;
705 list_for_each_entry(card, &host->cards, node) {
706 if (memcmp(card->raw_cid, raw_cid, sizeof(card->raw_cid)) == 0)
707 return card;
709 return NULL;
713 * Allocate a new MMC card, and assign a unique RCA.
715 static struct mmc_card *
716 mmc_alloc_card(struct mmc_host *host, u32 *raw_cid, unsigned int *frca)
718 struct mmc_card *card, *c;
719 unsigned int rca = *frca;
721 card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL);
722 if (!card)
723 return ERR_PTR(-ENOMEM);
725 mmc_init_card(card, host);
726 memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid));
728 again:
729 list_for_each_entry(c, &host->cards, node)
730 if (c->rca == rca) {
731 rca++;
732 goto again;
735 card->rca = rca;
737 *frca = rca;
739 return card;
743 * Tell attached cards to go to IDLE state
745 static void mmc_idle_cards(struct mmc_host *host)
747 struct mmc_command cmd;
749 host->ios.chip_select = MMC_CS_HIGH;
750 mmc_set_ios(host);
752 mmc_delay(1);
754 cmd.opcode = MMC_GO_IDLE_STATE;
755 cmd.arg = 0;
756 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
758 mmc_wait_for_cmd(host, &cmd, 0);
760 mmc_delay(1);
762 host->ios.chip_select = MMC_CS_DONTCARE;
763 mmc_set_ios(host);
765 mmc_delay(1);
769 * Apply power to the MMC stack. This is a two-stage process.
770 * First, we enable power to the card without the clock running.
771 * We then wait a bit for the power to stabilise. Finally,
772 * enable the bus drivers and clock to the card.
774 * We must _NOT_ enable the clock prior to power stablising.
776 * If a host does all the power sequencing itself, ignore the
777 * initial MMC_POWER_UP stage.
779 static void mmc_power_up(struct mmc_host *host)
781 int bit = fls(host->ocr_avail) - 1;
783 host->ios.vdd = bit;
784 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
785 host->ios.chip_select = MMC_CS_DONTCARE;
786 host->ios.power_mode = MMC_POWER_UP;
787 host->ios.bus_width = MMC_BUS_WIDTH_1;
788 mmc_set_ios(host);
790 mmc_delay(1);
792 host->ios.clock = host->f_min;
793 host->ios.power_mode = MMC_POWER_ON;
794 mmc_set_ios(host);
796 mmc_delay(2);
799 static void mmc_power_off(struct mmc_host *host)
801 host->ios.clock = 0;
802 host->ios.vdd = 0;
803 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
804 host->ios.chip_select = MMC_CS_DONTCARE;
805 host->ios.power_mode = MMC_POWER_OFF;
806 host->ios.bus_width = MMC_BUS_WIDTH_1;
807 mmc_set_ios(host);
810 static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
812 struct mmc_command cmd;
813 int i, err = 0;
815 cmd.opcode = MMC_SEND_OP_COND;
816 cmd.arg = ocr;
817 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
819 for (i = 100; i; i--) {
820 err = mmc_wait_for_cmd(host, &cmd, 0);
821 if (err != MMC_ERR_NONE)
822 break;
824 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
825 break;
827 err = MMC_ERR_TIMEOUT;
829 mmc_delay(10);
832 if (rocr)
833 *rocr = cmd.resp[0];
835 return err;
838 static int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
840 struct mmc_command cmd;
841 int i, err = 0;
843 cmd.opcode = SD_APP_OP_COND;
844 cmd.arg = ocr;
845 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
847 for (i = 100; i; i--) {
848 err = mmc_wait_for_app_cmd(host, 0, &cmd, CMD_RETRIES);
849 if (err != MMC_ERR_NONE)
850 break;
852 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
853 break;
855 err = MMC_ERR_TIMEOUT;
857 mmc_delay(10);
860 if (rocr)
861 *rocr = cmd.resp[0];
863 return err;
867 * Discover cards by requesting their CID. If this command
868 * times out, it is not an error; there are no further cards
869 * to be discovered. Add new cards to the list.
871 * Create a mmc_card entry for each discovered card, assigning
872 * it an RCA, and save the raw CID for decoding later.
874 static void mmc_discover_cards(struct mmc_host *host)
876 struct mmc_card *card;
877 unsigned int first_rca = 1, err;
879 while (1) {
880 struct mmc_command cmd;
882 cmd.opcode = MMC_ALL_SEND_CID;
883 cmd.arg = 0;
884 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
886 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
887 if (err == MMC_ERR_TIMEOUT) {
888 err = MMC_ERR_NONE;
889 break;
891 if (err != MMC_ERR_NONE) {
892 printk(KERN_ERR "%s: error requesting CID: %d\n",
893 mmc_hostname(host), err);
894 break;
897 card = mmc_find_card(host, cmd.resp);
898 if (!card) {
899 card = mmc_alloc_card(host, cmd.resp, &first_rca);
900 if (IS_ERR(card)) {
901 err = PTR_ERR(card);
902 break;
904 list_add(&card->node, &host->cards);
907 card->state &= ~MMC_STATE_DEAD;
909 if (host->mode == MMC_MODE_SD) {
910 mmc_card_set_sd(card);
912 cmd.opcode = SD_SEND_RELATIVE_ADDR;
913 cmd.arg = 0;
914 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
916 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
917 if (err != MMC_ERR_NONE)
918 mmc_card_set_dead(card);
919 else {
920 card->rca = cmd.resp[0] >> 16;
922 if (!host->ops->get_ro) {
923 printk(KERN_WARNING "%s: host does not "
924 "support reading read-only "
925 "switch. assuming write-enable.\n",
926 mmc_hostname(host));
927 } else {
928 if (host->ops->get_ro(host))
929 mmc_card_set_readonly(card);
932 } else {
933 cmd.opcode = MMC_SET_RELATIVE_ADDR;
934 cmd.arg = card->rca << 16;
935 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
937 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
938 if (err != MMC_ERR_NONE)
939 mmc_card_set_dead(card);
944 static void mmc_read_csds(struct mmc_host *host)
946 struct mmc_card *card;
948 list_for_each_entry(card, &host->cards, node) {
949 struct mmc_command cmd;
950 int err;
952 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
953 continue;
955 cmd.opcode = MMC_SEND_CSD;
956 cmd.arg = card->rca << 16;
957 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
959 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
960 if (err != MMC_ERR_NONE) {
961 mmc_card_set_dead(card);
962 continue;
965 memcpy(card->raw_csd, cmd.resp, sizeof(card->raw_csd));
967 mmc_decode_csd(card);
968 mmc_decode_cid(card);
972 static void mmc_read_scrs(struct mmc_host *host)
974 int err;
975 struct mmc_card *card;
976 struct mmc_request mrq;
977 struct mmc_command cmd;
978 struct mmc_data data;
979 struct scatterlist sg;
981 list_for_each_entry(card, &host->cards, node) {
982 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
983 continue;
984 if (!mmc_card_sd(card))
985 continue;
987 err = mmc_select_card(host, card);
988 if (err != MMC_ERR_NONE) {
989 mmc_card_set_dead(card);
990 continue;
993 memset(&cmd, 0, sizeof(struct mmc_command));
995 cmd.opcode = MMC_APP_CMD;
996 cmd.arg = card->rca << 16;
997 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
999 err = mmc_wait_for_cmd(host, &cmd, 0);
1000 if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD)) {
1001 mmc_card_set_dead(card);
1002 continue;
1005 memset(&cmd, 0, sizeof(struct mmc_command));
1007 cmd.opcode = SD_APP_SEND_SCR;
1008 cmd.arg = 0;
1009 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1011 memset(&data, 0, sizeof(struct mmc_data));
1013 mmc_set_data_timeout(&data, card, 0);
1015 data.blksz = 1 << 3;
1016 data.blocks = 1;
1017 data.flags = MMC_DATA_READ;
1018 data.sg = &sg;
1019 data.sg_len = 1;
1021 memset(&mrq, 0, sizeof(struct mmc_request));
1023 mrq.cmd = &cmd;
1024 mrq.data = &data;
1026 sg_init_one(&sg, (u8*)card->raw_scr, 8);
1028 mmc_wait_for_req(host, &mrq);
1030 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
1031 mmc_card_set_dead(card);
1032 continue;
1035 card->raw_scr[0] = ntohl(card->raw_scr[0]);
1036 card->raw_scr[1] = ntohl(card->raw_scr[1]);
1038 mmc_decode_scr(card);
1041 mmc_deselect_cards(host);
1044 static unsigned int mmc_calculate_clock(struct mmc_host *host)
1046 struct mmc_card *card;
1047 unsigned int max_dtr = host->f_max;
1049 list_for_each_entry(card, &host->cards, node)
1050 if (!mmc_card_dead(card) && max_dtr > card->csd.max_dtr)
1051 max_dtr = card->csd.max_dtr;
1053 pr_debug("%s: selected %d.%03dMHz transfer rate\n",
1054 mmc_hostname(host),
1055 max_dtr / 1000000, (max_dtr / 1000) % 1000);
1057 return max_dtr;
1061 * Check whether cards we already know about are still present.
1062 * We do this by requesting status, and checking whether a card
1063 * responds.
1065 * A request for status does not cause a state change in data
1066 * transfer mode.
1068 static void mmc_check_cards(struct mmc_host *host)
1070 struct list_head *l, *n;
1072 mmc_deselect_cards(host);
1074 list_for_each_safe(l, n, &host->cards) {
1075 struct mmc_card *card = mmc_list_to_card(l);
1076 struct mmc_command cmd;
1077 int err;
1079 cmd.opcode = MMC_SEND_STATUS;
1080 cmd.arg = card->rca << 16;
1081 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1083 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1084 if (err == MMC_ERR_NONE)
1085 continue;
1087 mmc_card_set_dead(card);
1091 static void mmc_setup(struct mmc_host *host)
1093 if (host->ios.power_mode != MMC_POWER_ON) {
1094 int err;
1095 u32 ocr;
1097 host->mode = MMC_MODE_SD;
1099 mmc_power_up(host);
1100 mmc_idle_cards(host);
1102 err = mmc_send_app_op_cond(host, 0, &ocr);
1105 * If we fail to detect any SD cards then try
1106 * searching for MMC cards.
1108 if (err != MMC_ERR_NONE) {
1109 host->mode = MMC_MODE_MMC;
1111 err = mmc_send_op_cond(host, 0, &ocr);
1112 if (err != MMC_ERR_NONE)
1113 return;
1116 host->ocr = mmc_select_voltage(host, ocr);
1119 * Since we're changing the OCR value, we seem to
1120 * need to tell some cards to go back to the idle
1121 * state. We wait 1ms to give cards time to
1122 * respond.
1124 if (host->ocr)
1125 mmc_idle_cards(host);
1126 } else {
1127 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1128 host->ios.clock = host->f_min;
1129 mmc_set_ios(host);
1132 * We should remember the OCR mask from the existing
1133 * cards, and detect the new cards OCR mask, combine
1134 * the two and re-select the VDD. However, if we do
1135 * change VDD, we should do an idle, and then do a
1136 * full re-initialisation. We would need to notify
1137 * drivers so that they can re-setup the cards as
1138 * well, while keeping their queues at bay.
1140 * For the moment, we take the easy way out - if the
1141 * new cards don't like our currently selected VDD,
1142 * they drop off the bus.
1146 if (host->ocr == 0)
1147 return;
1150 * Send the selected OCR multiple times... until the cards
1151 * all get the idea that they should be ready for CMD2.
1152 * (My SanDisk card seems to need this.)
1154 if (host->mode == MMC_MODE_SD)
1155 mmc_send_app_op_cond(host, host->ocr, NULL);
1156 else
1157 mmc_send_op_cond(host, host->ocr, NULL);
1159 mmc_discover_cards(host);
1162 * Ok, now switch to push-pull mode.
1164 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1165 mmc_set_ios(host);
1167 mmc_read_csds(host);
1169 if (host->mode == MMC_MODE_SD)
1170 mmc_read_scrs(host);
1175 * mmc_detect_change - process change of state on a MMC socket
1176 * @host: host which changed state.
1177 * @delay: optional delay to wait before detection (jiffies)
1179 * All we know is that card(s) have been inserted or removed
1180 * from the socket(s). We don't know which socket or cards.
1182 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1184 if (delay)
1185 mmc_schedule_delayed_work(&host->detect, delay);
1186 else
1187 mmc_schedule_work(&host->detect);
1190 EXPORT_SYMBOL(mmc_detect_change);
1193 static void mmc_rescan(void *data)
1195 struct mmc_host *host = data;
1196 struct list_head *l, *n;
1197 unsigned char power_mode;
1199 mmc_claim_host(host);
1202 * Check for removed cards and newly inserted ones. We check for
1203 * removed cards first so we can intelligently re-select the VDD.
1205 power_mode = host->ios.power_mode;
1206 if (power_mode == MMC_POWER_ON)
1207 mmc_check_cards(host);
1209 mmc_setup(host);
1212 * Some broken cards process CMD1 even in stand-by state. There is
1213 * no reply, but an ILLEGAL_COMMAND error is cached and returned
1214 * after next command. We poll for card status here to clear any
1215 * possibly pending error.
1217 if (power_mode == MMC_POWER_ON)
1218 mmc_check_cards(host);
1220 if (!list_empty(&host->cards)) {
1222 * (Re-)calculate the fastest clock rate which the
1223 * attached cards and the host support.
1225 host->ios.clock = mmc_calculate_clock(host);
1226 mmc_set_ios(host);
1229 mmc_release_host(host);
1231 list_for_each_safe(l, n, &host->cards) {
1232 struct mmc_card *card = mmc_list_to_card(l);
1235 * If this is a new and good card, register it.
1237 if (!mmc_card_present(card) && !mmc_card_dead(card)) {
1238 if (mmc_register_card(card))
1239 mmc_card_set_dead(card);
1240 else
1241 mmc_card_set_present(card);
1245 * If this card is dead, destroy it.
1247 if (mmc_card_dead(card)) {
1248 list_del(&card->node);
1249 mmc_remove_card(card);
1254 * If we discover that there are no cards on the
1255 * bus, turn off the clock and power down.
1257 if (list_empty(&host->cards))
1258 mmc_power_off(host);
1263 * mmc_alloc_host - initialise the per-host structure.
1264 * @extra: sizeof private data structure
1265 * @dev: pointer to host device model structure
1267 * Initialise the per-host structure.
1269 struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
1271 struct mmc_host *host;
1273 host = mmc_alloc_host_sysfs(extra, dev);
1274 if (host) {
1275 spin_lock_init(&host->lock);
1276 init_waitqueue_head(&host->wq);
1277 INIT_LIST_HEAD(&host->cards);
1278 INIT_WORK(&host->detect, mmc_rescan, host);
1281 * By default, hosts do not support SGIO or large requests.
1282 * They have to set these according to their abilities.
1284 host->max_hw_segs = 1;
1285 host->max_phys_segs = 1;
1286 host->max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
1287 host->max_seg_size = PAGE_CACHE_SIZE;
1290 return host;
1293 EXPORT_SYMBOL(mmc_alloc_host);
1296 * mmc_add_host - initialise host hardware
1297 * @host: mmc host
1299 int mmc_add_host(struct mmc_host *host)
1301 int ret;
1303 ret = mmc_add_host_sysfs(host);
1304 if (ret == 0) {
1305 mmc_power_off(host);
1306 mmc_detect_change(host, 0);
1309 return ret;
1312 EXPORT_SYMBOL(mmc_add_host);
1315 * mmc_remove_host - remove host hardware
1316 * @host: mmc host
1318 * Unregister and remove all cards associated with this host,
1319 * and power down the MMC bus.
1321 void mmc_remove_host(struct mmc_host *host)
1323 struct list_head *l, *n;
1325 list_for_each_safe(l, n, &host->cards) {
1326 struct mmc_card *card = mmc_list_to_card(l);
1328 mmc_remove_card(card);
1331 mmc_power_off(host);
1332 mmc_remove_host_sysfs(host);
1335 EXPORT_SYMBOL(mmc_remove_host);
1338 * mmc_free_host - free the host structure
1339 * @host: mmc host
1341 * Free the host once all references to it have been dropped.
1343 void mmc_free_host(struct mmc_host *host)
1345 mmc_flush_scheduled_work();
1346 mmc_free_host_sysfs(host);
1349 EXPORT_SYMBOL(mmc_free_host);
1351 #ifdef CONFIG_PM
1354 * mmc_suspend_host - suspend a host
1355 * @host: mmc host
1356 * @state: suspend mode (PM_SUSPEND_xxx)
1358 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1360 mmc_claim_host(host);
1361 mmc_deselect_cards(host);
1362 mmc_power_off(host);
1363 mmc_release_host(host);
1365 return 0;
1368 EXPORT_SYMBOL(mmc_suspend_host);
1371 * mmc_resume_host - resume a previously suspended host
1372 * @host: mmc host
1374 int mmc_resume_host(struct mmc_host *host)
1376 mmc_rescan(host);
1378 return 0;
1381 EXPORT_SYMBOL(mmc_resume_host);
1383 #endif
1385 MODULE_LICENSE("GPL");