2 * linux/drivers/mmc/core/core.c
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
6 * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
7 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
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/mmc.h>
27 #include <linux/mmc/sd.h>
38 * OCR Bit positions to 10s of Vdd mV.
40 static const unsigned short mmc_ocr_bit_to_vdd[] = {
41 150, 155, 160, 165, 170, 180, 190, 200,
42 210, 220, 230, 240, 250, 260, 270, 280,
43 290, 300, 310, 320, 330, 340, 350, 360
46 static const unsigned int tran_exp[] = {
47 10000, 100000, 1000000, 10000000,
51 static const unsigned char tran_mant[] = {
52 0, 10, 12, 13, 15, 20, 25, 30,
53 35, 40, 45, 50, 55, 60, 70, 80,
56 static const unsigned int tacc_exp[] = {
57 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
60 static const unsigned int tacc_mant[] = {
61 0, 10, 12, 13, 15, 20, 25, 30,
62 35, 40, 45, 50, 55, 60, 70, 80,
67 * mmc_request_done - finish processing an MMC request
68 * @host: MMC host which completed request
69 * @mrq: MMC request which request
71 * MMC drivers should call this function when they have completed
72 * their processing of a request.
74 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
76 struct mmc_command *cmd = mrq->cmd;
79 pr_debug("%s: req done (CMD%u): %d/%d/%d: %08x %08x %08x %08x\n",
80 mmc_hostname(host), cmd->opcode, err,
81 mrq->data ? mrq->data->error : 0,
82 mrq->stop ? mrq->stop->error : 0,
83 cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
85 if (err && cmd->retries) {
88 host->ops->request(host, mrq);
89 } else if (mrq->done) {
94 EXPORT_SYMBOL(mmc_request_done);
97 * mmc_start_request - start a command on a host
98 * @host: MMC host to start command on
99 * @mrq: MMC request to start
101 * Queue a command on the specified host. We expect the
102 * caller to be holding the host lock with interrupts disabled.
105 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
107 #ifdef CONFIG_MMC_DEBUG
111 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
112 mmc_hostname(host), mrq->cmd->opcode,
113 mrq->cmd->arg, mrq->cmd->flags);
115 WARN_ON(!host->claimed);
120 BUG_ON(mrq->data->blksz > host->max_blk_size);
121 BUG_ON(mrq->data->blocks > host->max_blk_count);
122 BUG_ON(mrq->data->blocks * mrq->data->blksz >
125 #ifdef CONFIG_MMC_DEBUG
127 for (i = 0;i < mrq->data->sg_len;i++)
128 sz += mrq->data->sg[i].length;
129 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
132 mrq->cmd->data = mrq->data;
133 mrq->data->error = 0;
134 mrq->data->mrq = mrq;
136 mrq->data->stop = mrq->stop;
137 mrq->stop->error = 0;
138 mrq->stop->mrq = mrq;
141 host->ops->request(host, mrq);
144 EXPORT_SYMBOL(mmc_start_request);
146 static void mmc_wait_done(struct mmc_request *mrq)
148 complete(mrq->done_data);
151 int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
153 DECLARE_COMPLETION_ONSTACK(complete);
155 mrq->done_data = &complete;
156 mrq->done = mmc_wait_done;
158 mmc_start_request(host, mrq);
160 wait_for_completion(&complete);
165 EXPORT_SYMBOL(mmc_wait_for_req);
168 * mmc_wait_for_cmd - start a command and wait for completion
169 * @host: MMC host to start command
170 * @cmd: MMC command to start
171 * @retries: maximum number of retries
173 * Start a new MMC command for a host, and wait for the command
174 * to complete. Return any error that occurred while the command
175 * was executing. Do not attempt to parse the response.
177 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
179 struct mmc_request mrq;
181 BUG_ON(!host->claimed);
183 memset(&mrq, 0, sizeof(struct mmc_request));
185 memset(cmd->resp, 0, sizeof(cmd->resp));
186 cmd->retries = retries;
191 mmc_wait_for_req(host, &mrq);
196 EXPORT_SYMBOL(mmc_wait_for_cmd);
199 * mmc_set_data_timeout - set the timeout for a data command
200 * @data: data phase for command
201 * @card: the MMC card associated with the data transfer
202 * @write: flag to differentiate reads from writes
204 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
210 * SD cards use a 100 multiplier rather than 10
212 mult = mmc_card_sd(card) ? 100 : 10;
215 * Scale up the multiplier (and therefore the timeout) by
216 * the r2w factor for writes.
219 mult <<= card->csd.r2w_factor;
221 data->timeout_ns = card->csd.tacc_ns * mult;
222 data->timeout_clks = card->csd.tacc_clks * mult;
225 * SD cards also have an upper limit on the timeout.
227 if (mmc_card_sd(card)) {
228 unsigned int timeout_us, limit_us;
230 timeout_us = data->timeout_ns / 1000;
231 timeout_us += data->timeout_clks * 1000 /
232 (card->host->ios.clock / 1000);
240 * SDHC cards always use these fixed values.
242 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
243 data->timeout_ns = limit_us * 1000;
244 data->timeout_clks = 0;
248 EXPORT_SYMBOL(mmc_set_data_timeout);
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 void mmc_claim_host(struct mmc_host *host)
263 DECLARE_WAITQUEUE(wait, current);
266 add_wait_queue(&host->wq, &wait);
267 spin_lock_irqsave(&host->lock, flags);
269 set_current_state(TASK_UNINTERRUPTIBLE);
272 spin_unlock_irqrestore(&host->lock, flags);
274 spin_lock_irqsave(&host->lock, flags);
276 set_current_state(TASK_RUNNING);
278 spin_unlock_irqrestore(&host->lock, flags);
279 remove_wait_queue(&host->wq, &wait);
282 EXPORT_SYMBOL(mmc_claim_host);
285 * mmc_release_host - release a host
286 * @host: mmc host to release
288 * Release a MMC host, allowing others to claim the host
289 * for their operations.
291 void mmc_release_host(struct mmc_host *host)
295 BUG_ON(!host->claimed);
297 spin_lock_irqsave(&host->lock, flags);
299 spin_unlock_irqrestore(&host->lock, flags);
304 EXPORT_SYMBOL(mmc_release_host);
306 static inline void mmc_set_ios(struct mmc_host *host)
308 struct mmc_ios *ios = &host->ios;
310 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
311 "width %u timing %u\n",
312 mmc_hostname(host), ios->clock, ios->bus_mode,
313 ios->power_mode, ios->chip_select, ios->vdd,
314 ios->bus_width, ios->timing);
316 host->ops->set_ios(host, ios);
319 void mmc_set_chip_select(struct mmc_host *host, int mode)
321 host->ios.chip_select = mode;
326 * Mask off any voltages we don't support and select
329 static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
333 ocr &= host->ocr_avail;
350 #define UNSTUFF_BITS(resp,start,size) \
352 const int __size = size; \
353 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
354 const int __off = 3 - ((start) / 32); \
355 const int __shft = (start) & 31; \
358 __res = resp[__off] >> __shft; \
359 if (__size + __shft > 32) \
360 __res |= resp[__off-1] << ((32 - __shft) % 32); \
365 * Given the decoded CSD structure, decode the raw CID to our CID structure.
367 static void mmc_decode_cid(struct mmc_card *card)
369 u32 *resp = card->raw_cid;
371 memset(&card->cid, 0, sizeof(struct mmc_cid));
373 if (mmc_card_sd(card)) {
375 * SD doesn't currently have a version field so we will
376 * have to assume we can parse this.
378 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
379 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
380 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
381 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
382 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
383 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
384 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
385 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
386 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
387 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
388 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
389 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
391 card->cid.year += 2000; /* SD cards year offset */
394 * The selection of the format here is based upon published
395 * specs from sandisk and from what people have reported.
397 switch (card->csd.mmca_vsn) {
398 case 0: /* MMC v1.0 - v1.2 */
399 case 1: /* MMC v1.4 */
400 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
401 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
402 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
403 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
404 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
405 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
406 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
407 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
408 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
409 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
410 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
411 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
412 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
415 case 2: /* MMC v2.0 - v2.2 */
416 case 3: /* MMC v3.1 - v3.3 */
418 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
419 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
420 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
421 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
422 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
423 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
424 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
425 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
426 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
427 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
428 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
432 printk("%s: card has unknown MMCA version %d\n",
433 mmc_hostname(card->host), card->csd.mmca_vsn);
434 mmc_card_set_bad(card);
441 * Given a 128-bit response, decode to our card CSD structure.
443 static void mmc_decode_csd(struct mmc_card *card)
445 struct mmc_csd *csd = &card->csd;
446 unsigned int e, m, csd_struct;
447 u32 *resp = card->raw_csd;
449 if (mmc_card_sd(card)) {
450 csd_struct = UNSTUFF_BITS(resp, 126, 2);
452 switch (csd_struct) {
454 m = UNSTUFF_BITS(resp, 115, 4);
455 e = UNSTUFF_BITS(resp, 112, 3);
456 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
457 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
459 m = UNSTUFF_BITS(resp, 99, 4);
460 e = UNSTUFF_BITS(resp, 96, 3);
461 csd->max_dtr = tran_exp[e] * tran_mant[m];
462 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
464 e = UNSTUFF_BITS(resp, 47, 3);
465 m = UNSTUFF_BITS(resp, 62, 12);
466 csd->capacity = (1 + m) << (e + 2);
468 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
469 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
470 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
471 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
472 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
473 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
474 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
478 * This is a block-addressed SDHC card. Most
479 * interesting fields are unused and have fixed
480 * values. To avoid getting tripped by buggy cards,
481 * we assume those fixed values ourselves.
483 mmc_card_set_blockaddr(card);
485 csd->tacc_ns = 0; /* Unused */
486 csd->tacc_clks = 0; /* Unused */
488 m = UNSTUFF_BITS(resp, 99, 4);
489 e = UNSTUFF_BITS(resp, 96, 3);
490 csd->max_dtr = tran_exp[e] * tran_mant[m];
491 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
493 m = UNSTUFF_BITS(resp, 48, 22);
494 csd->capacity = (1 + m) << 10;
496 csd->read_blkbits = 9;
497 csd->read_partial = 0;
498 csd->write_misalign = 0;
499 csd->read_misalign = 0;
500 csd->r2w_factor = 4; /* Unused */
501 csd->write_blkbits = 9;
502 csd->write_partial = 0;
505 printk("%s: unrecognised CSD structure version %d\n",
506 mmc_hostname(card->host), csd_struct);
507 mmc_card_set_bad(card);
512 * We only understand CSD structure v1.1 and v1.2.
513 * v1.2 has extra information in bits 15, 11 and 10.
515 csd_struct = UNSTUFF_BITS(resp, 126, 2);
516 if (csd_struct != 1 && csd_struct != 2) {
517 printk("%s: unrecognised CSD structure version %d\n",
518 mmc_hostname(card->host), csd_struct);
519 mmc_card_set_bad(card);
523 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
524 m = UNSTUFF_BITS(resp, 115, 4);
525 e = UNSTUFF_BITS(resp, 112, 3);
526 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
527 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
529 m = UNSTUFF_BITS(resp, 99, 4);
530 e = UNSTUFF_BITS(resp, 96, 3);
531 csd->max_dtr = tran_exp[e] * tran_mant[m];
532 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
534 e = UNSTUFF_BITS(resp, 47, 3);
535 m = UNSTUFF_BITS(resp, 62, 12);
536 csd->capacity = (1 + m) << (e + 2);
538 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
539 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
540 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
541 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
542 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
543 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
544 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
549 * Given a 64-bit response, decode to our card SCR structure.
551 static void mmc_decode_scr(struct mmc_card *card)
553 struct sd_scr *scr = &card->scr;
554 unsigned int scr_struct;
557 BUG_ON(!mmc_card_sd(card));
559 resp[3] = card->raw_scr[1];
560 resp[2] = card->raw_scr[0];
562 scr_struct = UNSTUFF_BITS(resp, 60, 4);
563 if (scr_struct != 0) {
564 printk("%s: unrecognised SCR structure version %d\n",
565 mmc_hostname(card->host), scr_struct);
566 mmc_card_set_bad(card);
570 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
571 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
575 * Allocate a new MMC card
577 static struct mmc_card *
578 mmc_alloc_card(struct mmc_host *host, u32 *raw_cid)
580 struct mmc_card *card;
582 card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL);
584 return ERR_PTR(-ENOMEM);
586 mmc_init_card(card, host);
587 memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid));
593 * Apply power to the MMC stack. This is a two-stage process.
594 * First, we enable power to the card without the clock running.
595 * We then wait a bit for the power to stabilise. Finally,
596 * enable the bus drivers and clock to the card.
598 * We must _NOT_ enable the clock prior to power stablising.
600 * If a host does all the power sequencing itself, ignore the
601 * initial MMC_POWER_UP stage.
603 static void mmc_power_up(struct mmc_host *host)
605 int bit = fls(host->ocr_avail) - 1;
608 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
609 host->ios.chip_select = MMC_CS_DONTCARE;
610 host->ios.power_mode = MMC_POWER_UP;
611 host->ios.bus_width = MMC_BUS_WIDTH_1;
612 host->ios.timing = MMC_TIMING_LEGACY;
617 host->ios.clock = host->f_min;
618 host->ios.power_mode = MMC_POWER_ON;
624 static void mmc_power_off(struct mmc_host *host)
628 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
629 host->ios.chip_select = MMC_CS_DONTCARE;
630 host->ios.power_mode = MMC_POWER_OFF;
631 host->ios.bus_width = MMC_BUS_WIDTH_1;
632 host->ios.timing = MMC_TIMING_LEGACY;
637 * Discover the card by requesting its CID.
639 * Create a mmc_card entry for the discovered card, assigning
640 * it an RCA, and save the raw CID for decoding later.
642 static void mmc_discover_card(struct mmc_host *host)
649 err = mmc_all_send_cid(host, cid);
650 if (err != MMC_ERR_NONE) {
651 printk(KERN_ERR "%s: error requesting CID: %d\n",
652 mmc_hostname(host), err);
656 host->card = mmc_alloc_card(host, cid);
657 if (IS_ERR(host->card)) {
658 err = PTR_ERR(host->card);
663 if (host->mode == MMC_MODE_SD) {
664 host->card->type = MMC_TYPE_SD;
666 err = mmc_send_relative_addr(host, &host->card->rca);
667 if (err != MMC_ERR_NONE)
668 mmc_card_set_dead(host->card);
670 if (!host->ops->get_ro) {
671 printk(KERN_WARNING "%s: host does not "
672 "support reading read-only "
673 "switch. assuming write-enable.\n",
676 if (host->ops->get_ro(host))
677 mmc_card_set_readonly(host->card);
681 host->card->type = MMC_TYPE_MMC;
684 err = mmc_set_relative_addr(host->card);
685 if (err != MMC_ERR_NONE)
686 mmc_card_set_dead(host->card);
690 static void mmc_read_csd(struct mmc_host *host)
696 if (mmc_card_dead(host->card))
699 err = mmc_send_csd(host->card, host->card->raw_csd);
700 if (err != MMC_ERR_NONE) {
701 mmc_card_set_dead(host->card);
705 mmc_decode_csd(host->card);
706 mmc_decode_cid(host->card);
709 static void mmc_process_ext_csd(struct mmc_host *host)
716 if (mmc_card_dead(host->card))
718 if (mmc_card_sd(host->card))
720 if (host->card->csd.mmca_vsn < CSD_SPEC_VER_4)
724 * As the ext_csd is so large and mostly unused, we don't store the
725 * raw block in mmc_card.
727 ext_csd = kmalloc(512, GFP_KERNEL);
729 printk("%s: could not allocate a buffer to receive the ext_csd."
730 "mmc v4 cards will be treated as v3.\n",
735 err = mmc_send_ext_csd(host->card, ext_csd);
736 if (err != MMC_ERR_NONE) {
737 if (host->card->csd.capacity == (4096 * 512)) {
738 printk(KERN_ERR "%s: unable to read EXT_CSD "
739 "on a possible high capacity card. "
740 "Card will be ignored.\n",
742 mmc_card_set_dead(host->card);
744 printk(KERN_WARNING "%s: unable to read "
745 "EXT_CSD, performance might "
752 host->card->ext_csd.sectors =
753 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
754 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
755 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
756 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
757 if (host->card->ext_csd.sectors)
758 mmc_card_set_blockaddr(host->card);
760 switch (ext_csd[EXT_CSD_CARD_TYPE]) {
761 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
762 host->card->ext_csd.hs_max_dtr = 52000000;
764 case EXT_CSD_CARD_TYPE_26:
765 host->card->ext_csd.hs_max_dtr = 26000000;
768 /* MMC v4 spec says this cannot happen */
769 printk("%s: card is mmc v4 but doesn't support "
770 "any high-speed modes.\n",
775 if (host->caps & MMC_CAP_MMC_HIGHSPEED) {
776 /* Activate highspeed support. */
777 err = mmc_switch(host->card, MMC_SWITCH_MODE_WRITE_BYTE,
778 EXT_CSD_HS_TIMING, 1);
779 if (err != MMC_ERR_NONE) {
780 printk("%s: failed to switch card to mmc v4 "
781 "high-speed mode.\n",
786 mmc_card_set_highspeed(host->card);
788 host->ios.timing = MMC_TIMING_MMC_HS;
792 /* Check for host support for wide-bus modes. */
793 if (host->caps & MMC_CAP_4_BIT_DATA) {
794 /* Activate 4-bit support. */
795 err = mmc_switch(host->card, MMC_SWITCH_MODE_WRITE_BYTE,
796 EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_4 |
797 EXT_CSD_CMD_SET_NORMAL);
798 if (err != MMC_ERR_NONE) {
799 printk("%s: failed to switch card to "
800 "mmc v4 4-bit bus mode.\n",
805 host->ios.bus_width = MMC_BUS_WIDTH_4;
813 static void mmc_read_scr(struct mmc_host *host)
819 if (mmc_card_dead(host->card))
821 if (!mmc_card_sd(host->card))
824 err = mmc_app_send_scr(host->card, host->card->raw_scr);
825 if (err != MMC_ERR_NONE) {
826 mmc_card_set_dead(host->card);
830 mmc_decode_scr(host->card);
833 static void mmc_read_switch_caps(struct mmc_host *host)
836 unsigned char *status;
838 if (!(host->caps & MMC_CAP_SD_HIGHSPEED))
843 if (mmc_card_dead(host->card))
845 if (!mmc_card_sd(host->card))
847 if (host->card->scr.sda_vsn < SCR_SPEC_VER_1)
850 status = kmalloc(64, GFP_KERNEL);
852 printk(KERN_WARNING "%s: Unable to allocate buffer for "
853 "reading switch capabilities.\n",
858 err = mmc_sd_switch(host->card, SD_SWITCH_CHECK,
859 SD_SWITCH_GRP_ACCESS, SD_SWITCH_ACCESS_HS, status);
860 if (err != MMC_ERR_NONE) {
861 printk("%s: unable to read switch capabilities, "
862 "performance might suffer.\n",
867 if (status[13] & 0x02)
868 host->card->sw_caps.hs_max_dtr = 50000000;
870 err = mmc_sd_switch(host->card, SD_SWITCH_SET,
871 SD_SWITCH_GRP_ACCESS, SD_SWITCH_ACCESS_HS, status);
872 if (err != MMC_ERR_NONE || (status[16] & 0xF) != 1) {
873 printk(KERN_WARNING "%s: Problem switching card "
874 "into high-speed mode!\n",
879 mmc_card_set_highspeed(host->card);
881 host->ios.timing = MMC_TIMING_SD_HS;
888 static unsigned int mmc_calculate_clock(struct mmc_host *host)
890 unsigned int max_dtr = host->f_max;
892 if (host->card && !mmc_card_dead(host->card)) {
893 if (mmc_card_highspeed(host->card) && mmc_card_sd(host->card)) {
894 if (max_dtr > host->card->sw_caps.hs_max_dtr)
895 max_dtr = host->card->sw_caps.hs_max_dtr;
896 } else if (mmc_card_highspeed(host->card) && !mmc_card_sd(host->card)) {
897 if (max_dtr > host->card->ext_csd.hs_max_dtr)
898 max_dtr = host->card->ext_csd.hs_max_dtr;
899 } else if (max_dtr > host->card->csd.max_dtr) {
900 max_dtr = host->card->csd.max_dtr;
904 pr_debug("%s: selected %d.%03dMHz transfer rate\n",
906 max_dtr / 1000000, (max_dtr / 1000) % 1000);
912 * Check whether cards we already know about are still present.
913 * We do this by requesting status, and checking whether a card
916 * A request for status does not cause a state change in data
919 static void mmc_check_card(struct mmc_card *card)
925 err = mmc_send_status(card, NULL);
926 if (err == MMC_ERR_NONE)
929 mmc_card_set_dead(card);
932 static void mmc_setup(struct mmc_host *host)
937 host->mode = MMC_MODE_SD;
942 err = mmc_send_if_cond(host, host->ocr_avail);
943 if (err != MMC_ERR_NONE) {
946 err = mmc_send_app_op_cond(host, 0, &ocr);
949 * If we fail to detect any SD cards then try
950 * searching for MMC cards.
952 if (err != MMC_ERR_NONE) {
953 host->mode = MMC_MODE_MMC;
955 err = mmc_send_op_cond(host, 0, &ocr);
956 if (err != MMC_ERR_NONE)
960 host->ocr = mmc_select_voltage(host, ocr);
966 * Since we're changing the OCR value, we seem to
967 * need to tell some cards to go back to the idle
968 * state. We wait 1ms to give cards time to
974 * Send the selected OCR multiple times... until the cards
975 * all get the idea that they should be ready for CMD2.
976 * (My SanDisk card seems to need this.)
978 if (host->mode == MMC_MODE_SD) {
980 * If SD_SEND_IF_COND indicates an SD 2.0
981 * compliant card and we should set bit 30
982 * of the ocr to indicate that we can handle
983 * block-addressed SDHC cards.
985 err = mmc_send_if_cond(host, host->ocr);
986 if (err == MMC_ERR_NONE)
987 ocr = host->ocr | (1 << 30);
989 mmc_send_app_op_cond(host, ocr, NULL);
991 /* The extra bit indicates that we support high capacity */
992 mmc_send_op_cond(host, host->ocr | (1 << 30), NULL);
995 mmc_discover_card(host);
998 * Ok, now switch to push-pull mode.
1000 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1005 if (host->card && !mmc_card_dead(host->card)) {
1006 err = mmc_select_card(host->card);
1007 if (err != MMC_ERR_NONE)
1008 mmc_card_set_dead(host->card);
1012 * The card is in 1 bit mode by default so
1013 * we only need to change if it supports the
1016 if (host->card && !mmc_card_dead(host->card) &&
1017 mmc_card_sd(host->card) &&
1018 (host->card->scr.bus_widths & SD_SCR_BUS_WIDTH_4) &&
1019 (host->card->host->caps & MMC_CAP_4_BIT_DATA)) {
1020 err = mmc_app_set_bus_width(host->card, SD_BUS_WIDTH_4);
1021 if (err != MMC_ERR_NONE)
1022 mmc_card_set_dead(host->card);
1024 host->ios.bus_width = MMC_BUS_WIDTH_4;
1029 if (host->mode == MMC_MODE_SD) {
1031 mmc_read_switch_caps(host);
1033 mmc_process_ext_csd(host);
1038 * mmc_detect_change - process change of state on a MMC socket
1039 * @host: host which changed state.
1040 * @delay: optional delay to wait before detection (jiffies)
1042 * All we know is that card(s) have been inserted or removed
1043 * from the socket(s). We don't know which socket or cards.
1045 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1047 #ifdef CONFIG_MMC_DEBUG
1048 mmc_claim_host(host);
1049 BUG_ON(host->removed);
1050 mmc_release_host(host);
1053 mmc_schedule_delayed_work(&host->detect, delay);
1056 EXPORT_SYMBOL(mmc_detect_change);
1059 static void mmc_rescan(struct work_struct *work)
1061 struct mmc_host *host =
1062 container_of(work, struct mmc_host, detect.work);
1064 mmc_claim_host(host);
1067 * Check for removed card and newly inserted ones. We check for
1068 * removed cards first so we can intelligently re-select the VDD.
1071 mmc_check_card(host->card);
1073 mmc_release_host(host);
1075 if (mmc_card_dead(host->card)) {
1076 mmc_remove_card(host->card);
1085 if (host->card && !mmc_card_dead(host->card)) {
1087 * (Re-)calculate the fastest clock rate which the
1088 * attached cards and the host support.
1090 host->ios.clock = mmc_calculate_clock(host);
1094 mmc_release_host(host);
1097 * If this is a new and good card, register it.
1099 if (host->card && !mmc_card_dead(host->card)) {
1100 if (mmc_register_card(host->card))
1101 mmc_card_set_dead(host->card);
1105 * If this card is dead, destroy it.
1107 if (host->card && mmc_card_dead(host->card)) {
1108 mmc_remove_card(host->card);
1114 * If we discover that there are no cards on the
1115 * bus, turn off the clock and power down.
1118 mmc_power_off(host);
1123 * mmc_alloc_host - initialise the per-host structure.
1124 * @extra: sizeof private data structure
1125 * @dev: pointer to host device model structure
1127 * Initialise the per-host structure.
1129 struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
1131 struct mmc_host *host;
1133 host = mmc_alloc_host_sysfs(extra, dev);
1135 spin_lock_init(&host->lock);
1136 init_waitqueue_head(&host->wq);
1137 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
1140 * By default, hosts do not support SGIO or large requests.
1141 * They have to set these according to their abilities.
1143 host->max_hw_segs = 1;
1144 host->max_phys_segs = 1;
1145 host->max_seg_size = PAGE_CACHE_SIZE;
1147 host->max_req_size = PAGE_CACHE_SIZE;
1148 host->max_blk_size = 512;
1149 host->max_blk_count = PAGE_CACHE_SIZE / 512;
1155 EXPORT_SYMBOL(mmc_alloc_host);
1158 * mmc_add_host - initialise host hardware
1161 int mmc_add_host(struct mmc_host *host)
1165 ret = mmc_add_host_sysfs(host);
1167 mmc_power_off(host);
1168 mmc_detect_change(host, 0);
1174 EXPORT_SYMBOL(mmc_add_host);
1177 * mmc_remove_host - remove host hardware
1180 * Unregister and remove all cards associated with this host,
1181 * and power down the MMC bus.
1183 void mmc_remove_host(struct mmc_host *host)
1185 #ifdef CONFIG_MMC_DEBUG
1186 mmc_claim_host(host);
1188 mmc_release_host(host);
1191 mmc_flush_scheduled_work();
1194 mmc_remove_card(host->card);
1198 mmc_power_off(host);
1199 mmc_remove_host_sysfs(host);
1202 EXPORT_SYMBOL(mmc_remove_host);
1205 * mmc_free_host - free the host structure
1208 * Free the host once all references to it have been dropped.
1210 void mmc_free_host(struct mmc_host *host)
1212 mmc_free_host_sysfs(host);
1215 EXPORT_SYMBOL(mmc_free_host);
1220 * mmc_suspend_host - suspend a host
1222 * @state: suspend mode (PM_SUSPEND_xxx)
1224 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1226 mmc_flush_scheduled_work();
1229 mmc_remove_card(host->card);
1233 mmc_power_off(host);
1238 EXPORT_SYMBOL(mmc_suspend_host);
1241 * mmc_resume_host - resume a previously suspended host
1244 int mmc_resume_host(struct mmc_host *host)
1246 mmc_rescan(&host->detect.work);
1251 EXPORT_SYMBOL(mmc_resume_host);
1255 MODULE_LICENSE("GPL");