mmc: Allow host drivers to specify a max block size
[linux-2.6/mini2440.git] / drivers / mmc / mmc.c
blob9bda3fddad17380771903a39e30da3d371520ce8
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.
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/protocol.h>
28 #include "mmc.h"
30 #define CMD_RETRIES 3
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,
43 0, 0, 0, 0
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,
61 /**
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 = cmd->error;
74 pr_debug("%s: req done (CMD%u): %d/%d/%d: %08x %08x %08x %08x\n",
75 mmc_hostname(host), cmd->opcode, err,
76 mrq->data ? mrq->data->error : 0,
77 mrq->stop ? mrq->stop->error : 0,
78 cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
80 if (err && cmd->retries) {
81 cmd->retries--;
82 cmd->error = 0;
83 host->ops->request(host, mrq);
84 } else if (mrq->done) {
85 mrq->done(mrq);
89 EXPORT_SYMBOL(mmc_request_done);
91 /**
92 * mmc_start_request - start a command on a host
93 * @host: MMC host to start command on
94 * @mrq: MMC request to start
96 * Queue a command on the specified host. We expect the
97 * caller to be holding the host lock with interrupts disabled.
99 void
100 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
102 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
103 mmc_hostname(host), mrq->cmd->opcode,
104 mrq->cmd->arg, mrq->cmd->flags);
106 WARN_ON(!host->claimed);
108 mrq->cmd->error = 0;
109 mrq->cmd->mrq = mrq;
110 if (mrq->data) {
111 BUG_ON(mrq->data->blksz > host->max_blk_size);
113 mrq->cmd->data = mrq->data;
114 mrq->data->error = 0;
115 mrq->data->mrq = mrq;
116 if (mrq->stop) {
117 mrq->data->stop = mrq->stop;
118 mrq->stop->error = 0;
119 mrq->stop->mrq = mrq;
122 host->ops->request(host, mrq);
125 EXPORT_SYMBOL(mmc_start_request);
127 static void mmc_wait_done(struct mmc_request *mrq)
129 complete(mrq->done_data);
132 int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
134 DECLARE_COMPLETION_ONSTACK(complete);
136 mrq->done_data = &complete;
137 mrq->done = mmc_wait_done;
139 mmc_start_request(host, mrq);
141 wait_for_completion(&complete);
143 return 0;
146 EXPORT_SYMBOL(mmc_wait_for_req);
149 * mmc_wait_for_cmd - start a command and wait for completion
150 * @host: MMC host to start command
151 * @cmd: MMC command to start
152 * @retries: maximum number of retries
154 * Start a new MMC command for a host, and wait for the command
155 * to complete. Return any error that occurred while the command
156 * was executing. Do not attempt to parse the response.
158 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
160 struct mmc_request mrq;
162 BUG_ON(!host->claimed);
164 memset(&mrq, 0, sizeof(struct mmc_request));
166 memset(cmd->resp, 0, sizeof(cmd->resp));
167 cmd->retries = retries;
169 mrq.cmd = cmd;
170 cmd->data = NULL;
172 mmc_wait_for_req(host, &mrq);
174 return cmd->error;
177 EXPORT_SYMBOL(mmc_wait_for_cmd);
180 * mmc_wait_for_app_cmd - start an application command and wait for
181 completion
182 * @host: MMC host to start command
183 * @rca: RCA to send MMC_APP_CMD to
184 * @cmd: MMC command to start
185 * @retries: maximum number of retries
187 * Sends a MMC_APP_CMD, checks the card response, sends the command
188 * in the parameter and waits for it to complete. Return any error
189 * that occurred while the command was executing. Do not attempt to
190 * parse the response.
192 int mmc_wait_for_app_cmd(struct mmc_host *host, unsigned int rca,
193 struct mmc_command *cmd, int retries)
195 struct mmc_request mrq;
196 struct mmc_command appcmd;
198 int i, err;
200 BUG_ON(!host->claimed);
201 BUG_ON(retries < 0);
203 err = MMC_ERR_INVALID;
206 * We have to resend MMC_APP_CMD for each attempt so
207 * we cannot use the retries field in mmc_command.
209 for (i = 0;i <= retries;i++) {
210 memset(&mrq, 0, sizeof(struct mmc_request));
212 appcmd.opcode = MMC_APP_CMD;
213 appcmd.arg = rca << 16;
214 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
215 appcmd.retries = 0;
216 memset(appcmd.resp, 0, sizeof(appcmd.resp));
217 appcmd.data = NULL;
219 mrq.cmd = &appcmd;
220 appcmd.data = NULL;
222 mmc_wait_for_req(host, &mrq);
224 if (appcmd.error) {
225 err = appcmd.error;
226 continue;
229 /* Check that card supported application commands */
230 if (!(appcmd.resp[0] & R1_APP_CMD))
231 return MMC_ERR_FAILED;
233 memset(&mrq, 0, sizeof(struct mmc_request));
235 memset(cmd->resp, 0, sizeof(cmd->resp));
236 cmd->retries = 0;
238 mrq.cmd = cmd;
239 cmd->data = NULL;
241 mmc_wait_for_req(host, &mrq);
243 err = cmd->error;
244 if (cmd->error == MMC_ERR_NONE)
245 break;
248 return err;
251 EXPORT_SYMBOL(mmc_wait_for_app_cmd);
254 * mmc_set_data_timeout - set the timeout for a data command
255 * @data: data phase for command
256 * @card: the MMC card associated with the data transfer
257 * @write: flag to differentiate reads from writes
259 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
260 int write)
262 unsigned int mult;
265 * SD cards use a 100 multiplier rather than 10
267 mult = mmc_card_sd(card) ? 100 : 10;
270 * Scale up the multiplier (and therefore the timeout) by
271 * the r2w factor for writes.
273 if (write)
274 mult <<= card->csd.r2w_factor;
276 data->timeout_ns = card->csd.tacc_ns * mult;
277 data->timeout_clks = card->csd.tacc_clks * mult;
280 * SD cards also have an upper limit on the timeout.
282 if (mmc_card_sd(card)) {
283 unsigned int timeout_us, limit_us;
285 timeout_us = data->timeout_ns / 1000;
286 timeout_us += data->timeout_clks * 1000 /
287 (card->host->ios.clock / 1000);
289 if (write)
290 limit_us = 250000;
291 else
292 limit_us = 100000;
295 * SDHC cards always use these fixed values.
297 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
298 data->timeout_ns = limit_us * 1000;
299 data->timeout_clks = 0;
303 EXPORT_SYMBOL(mmc_set_data_timeout);
305 static int mmc_select_card(struct mmc_host *host, struct mmc_card *card);
308 * __mmc_claim_host - exclusively claim a host
309 * @host: mmc host to claim
310 * @card: mmc card to claim host for
312 * Claim a host for a set of operations. If a valid card
313 * is passed and this wasn't the last card selected, select
314 * the card before returning.
316 * Note: you should use mmc_card_claim_host or mmc_claim_host.
318 int __mmc_claim_host(struct mmc_host *host, struct mmc_card *card)
320 DECLARE_WAITQUEUE(wait, current);
321 unsigned long flags;
322 int err = 0;
324 add_wait_queue(&host->wq, &wait);
325 spin_lock_irqsave(&host->lock, flags);
326 while (1) {
327 set_current_state(TASK_UNINTERRUPTIBLE);
328 if (!host->claimed)
329 break;
330 spin_unlock_irqrestore(&host->lock, flags);
331 schedule();
332 spin_lock_irqsave(&host->lock, flags);
334 set_current_state(TASK_RUNNING);
335 host->claimed = 1;
336 spin_unlock_irqrestore(&host->lock, flags);
337 remove_wait_queue(&host->wq, &wait);
339 if (card != (void *)-1) {
340 err = mmc_select_card(host, card);
341 if (err != MMC_ERR_NONE)
342 return err;
345 return err;
348 EXPORT_SYMBOL(__mmc_claim_host);
351 * mmc_release_host - release a host
352 * @host: mmc host to release
354 * Release a MMC host, allowing others to claim the host
355 * for their operations.
357 void mmc_release_host(struct mmc_host *host)
359 unsigned long flags;
361 BUG_ON(!host->claimed);
363 spin_lock_irqsave(&host->lock, flags);
364 host->claimed = 0;
365 spin_unlock_irqrestore(&host->lock, flags);
367 wake_up(&host->wq);
370 EXPORT_SYMBOL(mmc_release_host);
372 static inline void mmc_set_ios(struct mmc_host *host)
374 struct mmc_ios *ios = &host->ios;
376 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u width %u\n",
377 mmc_hostname(host), ios->clock, ios->bus_mode,
378 ios->power_mode, ios->chip_select, ios->vdd,
379 ios->bus_width);
381 host->ops->set_ios(host, ios);
384 static int mmc_select_card(struct mmc_host *host, struct mmc_card *card)
386 int err;
387 struct mmc_command cmd;
389 BUG_ON(!host->claimed);
391 if (host->card_selected == card)
392 return MMC_ERR_NONE;
394 host->card_selected = card;
396 cmd.opcode = MMC_SELECT_CARD;
397 cmd.arg = card->rca << 16;
398 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
400 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
401 if (err != MMC_ERR_NONE)
402 return err;
405 * We can only change the bus width of SD cards when
406 * they are selected so we have to put the handling
407 * here.
409 * The card is in 1 bit mode by default so
410 * we only need to change if it supports the
411 * wider version.
413 if (mmc_card_sd(card) &&
414 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
417 * Default bus width is 1 bit.
419 host->ios.bus_width = MMC_BUS_WIDTH_1;
421 if (host->caps & MMC_CAP_4_BIT_DATA) {
422 struct mmc_command cmd;
423 cmd.opcode = SD_APP_SET_BUS_WIDTH;
424 cmd.arg = SD_BUS_WIDTH_4;
425 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
427 err = mmc_wait_for_app_cmd(host, card->rca, &cmd,
428 CMD_RETRIES);
429 if (err != MMC_ERR_NONE)
430 return err;
432 host->ios.bus_width = MMC_BUS_WIDTH_4;
436 mmc_set_ios(host);
438 return MMC_ERR_NONE;
442 * Ensure that no card is selected.
444 static void mmc_deselect_cards(struct mmc_host *host)
446 struct mmc_command cmd;
448 if (host->card_selected) {
449 host->card_selected = NULL;
451 cmd.opcode = MMC_SELECT_CARD;
452 cmd.arg = 0;
453 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
455 mmc_wait_for_cmd(host, &cmd, 0);
460 static inline void mmc_delay(unsigned int ms)
462 if (ms < 1000 / HZ) {
463 cond_resched();
464 mdelay(ms);
465 } else {
466 msleep(ms);
471 * Mask off any voltages we don't support and select
472 * the lowest voltage
474 static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
476 int bit;
478 ocr &= host->ocr_avail;
480 bit = ffs(ocr);
481 if (bit) {
482 bit -= 1;
484 ocr &= 3 << bit;
486 host->ios.vdd = bit;
487 mmc_set_ios(host);
488 } else {
489 ocr = 0;
492 return ocr;
495 #define UNSTUFF_BITS(resp,start,size) \
496 ({ \
497 const int __size = size; \
498 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
499 const int __off = 3 - ((start) / 32); \
500 const int __shft = (start) & 31; \
501 u32 __res; \
503 __res = resp[__off] >> __shft; \
504 if (__size + __shft > 32) \
505 __res |= resp[__off-1] << ((32 - __shft) % 32); \
506 __res & __mask; \
510 * Given the decoded CSD structure, decode the raw CID to our CID structure.
512 static void mmc_decode_cid(struct mmc_card *card)
514 u32 *resp = card->raw_cid;
516 memset(&card->cid, 0, sizeof(struct mmc_cid));
518 if (mmc_card_sd(card)) {
520 * SD doesn't currently have a version field so we will
521 * have to assume we can parse this.
523 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
524 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
525 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
526 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
527 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
528 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
529 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
530 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
531 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
532 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
533 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
534 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
536 card->cid.year += 2000; /* SD cards year offset */
537 } else {
539 * The selection of the format here is based upon published
540 * specs from sandisk and from what people have reported.
542 switch (card->csd.mmca_vsn) {
543 case 0: /* MMC v1.0 - v1.2 */
544 case 1: /* MMC v1.4 */
545 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
546 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
547 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
548 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
549 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
550 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
551 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
552 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
553 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
554 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
555 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
556 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
557 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
558 break;
560 case 2: /* MMC v2.0 - v2.2 */
561 case 3: /* MMC v3.1 - v3.3 */
562 case 4: /* MMC v4 */
563 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
564 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
565 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
566 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
567 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
568 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
569 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
570 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
571 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
572 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
573 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
574 break;
576 default:
577 printk("%s: card has unknown MMCA version %d\n",
578 mmc_hostname(card->host), card->csd.mmca_vsn);
579 mmc_card_set_bad(card);
580 break;
586 * Given a 128-bit response, decode to our card CSD structure.
588 static void mmc_decode_csd(struct mmc_card *card)
590 struct mmc_csd *csd = &card->csd;
591 unsigned int e, m, csd_struct;
592 u32 *resp = card->raw_csd;
594 if (mmc_card_sd(card)) {
595 csd_struct = UNSTUFF_BITS(resp, 126, 2);
597 switch (csd_struct) {
598 case 0:
599 m = UNSTUFF_BITS(resp, 115, 4);
600 e = UNSTUFF_BITS(resp, 112, 3);
601 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
602 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
604 m = UNSTUFF_BITS(resp, 99, 4);
605 e = UNSTUFF_BITS(resp, 96, 3);
606 csd->max_dtr = tran_exp[e] * tran_mant[m];
607 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
609 e = UNSTUFF_BITS(resp, 47, 3);
610 m = UNSTUFF_BITS(resp, 62, 12);
611 csd->capacity = (1 + m) << (e + 2);
613 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
614 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
615 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
616 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
617 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
618 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
619 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
620 break;
621 case 1:
623 * This is a block-addressed SDHC card. Most
624 * interesting fields are unused and have fixed
625 * values. To avoid getting tripped by buggy cards,
626 * we assume those fixed values ourselves.
628 mmc_card_set_blockaddr(card);
630 csd->tacc_ns = 0; /* Unused */
631 csd->tacc_clks = 0; /* Unused */
633 m = UNSTUFF_BITS(resp, 99, 4);
634 e = UNSTUFF_BITS(resp, 96, 3);
635 csd->max_dtr = tran_exp[e] * tran_mant[m];
636 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
638 m = UNSTUFF_BITS(resp, 48, 22);
639 csd->capacity = (1 + m) << 10;
641 csd->read_blkbits = 9;
642 csd->read_partial = 0;
643 csd->write_misalign = 0;
644 csd->read_misalign = 0;
645 csd->r2w_factor = 4; /* Unused */
646 csd->write_blkbits = 9;
647 csd->write_partial = 0;
648 break;
649 default:
650 printk("%s: unrecognised CSD structure version %d\n",
651 mmc_hostname(card->host), csd_struct);
652 mmc_card_set_bad(card);
653 return;
655 } else {
657 * We only understand CSD structure v1.1 and v1.2.
658 * v1.2 has extra information in bits 15, 11 and 10.
660 csd_struct = UNSTUFF_BITS(resp, 126, 2);
661 if (csd_struct != 1 && csd_struct != 2) {
662 printk("%s: unrecognised CSD structure version %d\n",
663 mmc_hostname(card->host), csd_struct);
664 mmc_card_set_bad(card);
665 return;
668 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
669 m = UNSTUFF_BITS(resp, 115, 4);
670 e = UNSTUFF_BITS(resp, 112, 3);
671 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
672 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
674 m = UNSTUFF_BITS(resp, 99, 4);
675 e = UNSTUFF_BITS(resp, 96, 3);
676 csd->max_dtr = tran_exp[e] * tran_mant[m];
677 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
679 e = UNSTUFF_BITS(resp, 47, 3);
680 m = UNSTUFF_BITS(resp, 62, 12);
681 csd->capacity = (1 + m) << (e + 2);
683 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
684 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
685 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
686 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
687 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
688 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
689 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
694 * Given a 64-bit response, decode to our card SCR structure.
696 static void mmc_decode_scr(struct mmc_card *card)
698 struct sd_scr *scr = &card->scr;
699 unsigned int scr_struct;
700 u32 resp[4];
702 BUG_ON(!mmc_card_sd(card));
704 resp[3] = card->raw_scr[1];
705 resp[2] = card->raw_scr[0];
707 scr_struct = UNSTUFF_BITS(resp, 60, 4);
708 if (scr_struct != 0) {
709 printk("%s: unrecognised SCR structure version %d\n",
710 mmc_hostname(card->host), scr_struct);
711 mmc_card_set_bad(card);
712 return;
715 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
716 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
720 * Locate a MMC card on this MMC host given a raw CID.
722 static struct mmc_card *mmc_find_card(struct mmc_host *host, u32 *raw_cid)
724 struct mmc_card *card;
726 list_for_each_entry(card, &host->cards, node) {
727 if (memcmp(card->raw_cid, raw_cid, sizeof(card->raw_cid)) == 0)
728 return card;
730 return NULL;
734 * Allocate a new MMC card, and assign a unique RCA.
736 static struct mmc_card *
737 mmc_alloc_card(struct mmc_host *host, u32 *raw_cid, unsigned int *frca)
739 struct mmc_card *card, *c;
740 unsigned int rca = *frca;
742 card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL);
743 if (!card)
744 return ERR_PTR(-ENOMEM);
746 mmc_init_card(card, host);
747 memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid));
749 again:
750 list_for_each_entry(c, &host->cards, node)
751 if (c->rca == rca) {
752 rca++;
753 goto again;
756 card->rca = rca;
758 *frca = rca;
760 return card;
764 * Tell attached cards to go to IDLE state
766 static void mmc_idle_cards(struct mmc_host *host)
768 struct mmc_command cmd;
770 host->ios.chip_select = MMC_CS_HIGH;
771 mmc_set_ios(host);
773 mmc_delay(1);
775 cmd.opcode = MMC_GO_IDLE_STATE;
776 cmd.arg = 0;
777 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
779 mmc_wait_for_cmd(host, &cmd, 0);
781 mmc_delay(1);
783 host->ios.chip_select = MMC_CS_DONTCARE;
784 mmc_set_ios(host);
786 mmc_delay(1);
790 * Apply power to the MMC stack. This is a two-stage process.
791 * First, we enable power to the card without the clock running.
792 * We then wait a bit for the power to stabilise. Finally,
793 * enable the bus drivers and clock to the card.
795 * We must _NOT_ enable the clock prior to power stablising.
797 * If a host does all the power sequencing itself, ignore the
798 * initial MMC_POWER_UP stage.
800 static void mmc_power_up(struct mmc_host *host)
802 int bit = fls(host->ocr_avail) - 1;
804 host->ios.vdd = bit;
805 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
806 host->ios.chip_select = MMC_CS_DONTCARE;
807 host->ios.power_mode = MMC_POWER_UP;
808 host->ios.bus_width = MMC_BUS_WIDTH_1;
809 mmc_set_ios(host);
811 mmc_delay(1);
813 host->ios.clock = host->f_min;
814 host->ios.power_mode = MMC_POWER_ON;
815 mmc_set_ios(host);
817 mmc_delay(2);
820 static void mmc_power_off(struct mmc_host *host)
822 host->ios.clock = 0;
823 host->ios.vdd = 0;
824 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
825 host->ios.chip_select = MMC_CS_DONTCARE;
826 host->ios.power_mode = MMC_POWER_OFF;
827 host->ios.bus_width = MMC_BUS_WIDTH_1;
828 mmc_set_ios(host);
831 static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
833 struct mmc_command cmd;
834 int i, err = 0;
836 cmd.opcode = MMC_SEND_OP_COND;
837 cmd.arg = ocr;
838 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
840 for (i = 100; i; i--) {
841 err = mmc_wait_for_cmd(host, &cmd, 0);
842 if (err != MMC_ERR_NONE)
843 break;
845 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
846 break;
848 err = MMC_ERR_TIMEOUT;
850 mmc_delay(10);
853 if (rocr)
854 *rocr = cmd.resp[0];
856 return err;
859 static int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
861 struct mmc_command cmd;
862 int i, err = 0;
864 cmd.opcode = SD_APP_OP_COND;
865 cmd.arg = ocr;
866 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
868 for (i = 100; i; i--) {
869 err = mmc_wait_for_app_cmd(host, 0, &cmd, CMD_RETRIES);
870 if (err != MMC_ERR_NONE)
871 break;
873 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
874 break;
876 err = MMC_ERR_TIMEOUT;
878 mmc_delay(10);
881 if (rocr)
882 *rocr = cmd.resp[0];
884 return err;
887 static int mmc_send_if_cond(struct mmc_host *host, u32 ocr, int *rsd2)
889 struct mmc_command cmd;
890 int err, sd2;
891 static const u8 test_pattern = 0xAA;
894 * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
895 * before SD_APP_OP_COND. This command will harmlessly fail for
896 * SD 1.0 cards.
898 cmd.opcode = SD_SEND_IF_COND;
899 cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern;
900 cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
902 err = mmc_wait_for_cmd(host, &cmd, 0);
903 if (err == MMC_ERR_NONE) {
904 if ((cmd.resp[0] & 0xFF) == test_pattern) {
905 sd2 = 1;
906 } else {
907 sd2 = 0;
908 err = MMC_ERR_FAILED;
910 } else {
912 * Treat errors as SD 1.0 card.
914 sd2 = 0;
915 err = MMC_ERR_NONE;
917 if (rsd2)
918 *rsd2 = sd2;
919 return err;
923 * Discover cards by requesting their CID. If this command
924 * times out, it is not an error; there are no further cards
925 * to be discovered. Add new cards to the list.
927 * Create a mmc_card entry for each discovered card, assigning
928 * it an RCA, and save the raw CID for decoding later.
930 static void mmc_discover_cards(struct mmc_host *host)
932 struct mmc_card *card;
933 unsigned int first_rca = 1, err;
935 while (1) {
936 struct mmc_command cmd;
938 cmd.opcode = MMC_ALL_SEND_CID;
939 cmd.arg = 0;
940 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
942 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
943 if (err == MMC_ERR_TIMEOUT) {
944 err = MMC_ERR_NONE;
945 break;
947 if (err != MMC_ERR_NONE) {
948 printk(KERN_ERR "%s: error requesting CID: %d\n",
949 mmc_hostname(host), err);
950 break;
953 card = mmc_find_card(host, cmd.resp);
954 if (!card) {
955 card = mmc_alloc_card(host, cmd.resp, &first_rca);
956 if (IS_ERR(card)) {
957 err = PTR_ERR(card);
958 break;
960 list_add(&card->node, &host->cards);
963 card->state &= ~MMC_STATE_DEAD;
965 if (host->mode == MMC_MODE_SD) {
966 mmc_card_set_sd(card);
968 cmd.opcode = SD_SEND_RELATIVE_ADDR;
969 cmd.arg = 0;
970 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
972 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
973 if (err != MMC_ERR_NONE)
974 mmc_card_set_dead(card);
975 else {
976 card->rca = cmd.resp[0] >> 16;
978 if (!host->ops->get_ro) {
979 printk(KERN_WARNING "%s: host does not "
980 "support reading read-only "
981 "switch. assuming write-enable.\n",
982 mmc_hostname(host));
983 } else {
984 if (host->ops->get_ro(host))
985 mmc_card_set_readonly(card);
988 } else {
989 cmd.opcode = MMC_SET_RELATIVE_ADDR;
990 cmd.arg = card->rca << 16;
991 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
993 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
994 if (err != MMC_ERR_NONE)
995 mmc_card_set_dead(card);
1000 static void mmc_read_csds(struct mmc_host *host)
1002 struct mmc_card *card;
1004 list_for_each_entry(card, &host->cards, node) {
1005 struct mmc_command cmd;
1006 int err;
1008 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
1009 continue;
1011 cmd.opcode = MMC_SEND_CSD;
1012 cmd.arg = card->rca << 16;
1013 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
1015 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1016 if (err != MMC_ERR_NONE) {
1017 mmc_card_set_dead(card);
1018 continue;
1021 memcpy(card->raw_csd, cmd.resp, sizeof(card->raw_csd));
1023 mmc_decode_csd(card);
1024 mmc_decode_cid(card);
1028 static void mmc_process_ext_csds(struct mmc_host *host)
1030 int err;
1031 struct mmc_card *card;
1033 struct mmc_request mrq;
1034 struct mmc_command cmd;
1035 struct mmc_data data;
1037 struct scatterlist sg;
1040 * As the ext_csd is so large and mostly unused, we don't store the
1041 * raw block in mmc_card.
1043 u8 *ext_csd;
1044 ext_csd = kmalloc(512, GFP_KERNEL);
1045 if (!ext_csd) {
1046 printk("%s: could not allocate a buffer to receive the ext_csd."
1047 "mmc v4 cards will be treated as v3.\n",
1048 mmc_hostname(host));
1049 return;
1052 list_for_each_entry(card, &host->cards, node) {
1053 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
1054 continue;
1055 if (mmc_card_sd(card))
1056 continue;
1057 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
1058 continue;
1060 err = mmc_select_card(host, card);
1061 if (err != MMC_ERR_NONE) {
1062 mmc_card_set_dead(card);
1063 continue;
1066 memset(&cmd, 0, sizeof(struct mmc_command));
1068 cmd.opcode = MMC_SEND_EXT_CSD;
1069 cmd.arg = 0;
1070 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1072 memset(&data, 0, sizeof(struct mmc_data));
1074 mmc_set_data_timeout(&data, card, 0);
1076 data.blksz = 512;
1077 data.blocks = 1;
1078 data.flags = MMC_DATA_READ;
1079 data.sg = &sg;
1080 data.sg_len = 1;
1082 memset(&mrq, 0, sizeof(struct mmc_request));
1084 mrq.cmd = &cmd;
1085 mrq.data = &data;
1087 sg_init_one(&sg, ext_csd, 512);
1089 mmc_wait_for_req(host, &mrq);
1091 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
1092 mmc_card_set_dead(card);
1093 continue;
1096 switch (ext_csd[EXT_CSD_CARD_TYPE]) {
1097 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
1098 card->ext_csd.hs_max_dtr = 52000000;
1099 break;
1100 case EXT_CSD_CARD_TYPE_26:
1101 card->ext_csd.hs_max_dtr = 26000000;
1102 break;
1103 default:
1104 /* MMC v4 spec says this cannot happen */
1105 printk("%s: card is mmc v4 but doesn't support "
1106 "any high-speed modes.\n",
1107 mmc_hostname(card->host));
1108 mmc_card_set_bad(card);
1109 continue;
1112 /* Activate highspeed support. */
1113 cmd.opcode = MMC_SWITCH;
1114 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1115 (EXT_CSD_HS_TIMING << 16) |
1116 (1 << 8) |
1117 EXT_CSD_CMD_SET_NORMAL;
1118 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1120 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1121 if (err != MMC_ERR_NONE) {
1122 printk("%s: failed to switch card to mmc v4 "
1123 "high-speed mode.\n",
1124 mmc_hostname(card->host));
1125 continue;
1128 mmc_card_set_highspeed(card);
1130 /* Check for host support for wide-bus modes. */
1131 if (!(host->caps & MMC_CAP_4_BIT_DATA)) {
1132 continue;
1135 /* Activate 4-bit support. */
1136 cmd.opcode = MMC_SWITCH;
1137 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1138 (EXT_CSD_BUS_WIDTH << 16) |
1139 (EXT_CSD_BUS_WIDTH_4 << 8) |
1140 EXT_CSD_CMD_SET_NORMAL;
1141 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1143 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1144 if (err != MMC_ERR_NONE) {
1145 printk("%s: failed to switch card to "
1146 "mmc v4 4-bit bus mode.\n",
1147 mmc_hostname(card->host));
1148 continue;
1151 host->ios.bus_width = MMC_BUS_WIDTH_4;
1154 kfree(ext_csd);
1156 mmc_deselect_cards(host);
1159 static void mmc_read_scrs(struct mmc_host *host)
1161 int err;
1162 struct mmc_card *card;
1163 struct mmc_request mrq;
1164 struct mmc_command cmd;
1165 struct mmc_data data;
1166 struct scatterlist sg;
1168 list_for_each_entry(card, &host->cards, node) {
1169 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
1170 continue;
1171 if (!mmc_card_sd(card))
1172 continue;
1174 err = mmc_select_card(host, card);
1175 if (err != MMC_ERR_NONE) {
1176 mmc_card_set_dead(card);
1177 continue;
1180 memset(&cmd, 0, sizeof(struct mmc_command));
1182 cmd.opcode = MMC_APP_CMD;
1183 cmd.arg = card->rca << 16;
1184 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1186 err = mmc_wait_for_cmd(host, &cmd, 0);
1187 if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD)) {
1188 mmc_card_set_dead(card);
1189 continue;
1192 memset(&cmd, 0, sizeof(struct mmc_command));
1194 cmd.opcode = SD_APP_SEND_SCR;
1195 cmd.arg = 0;
1196 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1198 memset(&data, 0, sizeof(struct mmc_data));
1200 mmc_set_data_timeout(&data, card, 0);
1202 data.blksz = 1 << 3;
1203 data.blocks = 1;
1204 data.flags = MMC_DATA_READ;
1205 data.sg = &sg;
1206 data.sg_len = 1;
1208 memset(&mrq, 0, sizeof(struct mmc_request));
1210 mrq.cmd = &cmd;
1211 mrq.data = &data;
1213 sg_init_one(&sg, (u8*)card->raw_scr, 8);
1215 mmc_wait_for_req(host, &mrq);
1217 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
1218 mmc_card_set_dead(card);
1219 continue;
1222 card->raw_scr[0] = ntohl(card->raw_scr[0]);
1223 card->raw_scr[1] = ntohl(card->raw_scr[1]);
1225 mmc_decode_scr(card);
1228 mmc_deselect_cards(host);
1231 static void mmc_read_switch_caps(struct mmc_host *host)
1233 int err;
1234 struct mmc_card *card;
1235 struct mmc_request mrq;
1236 struct mmc_command cmd;
1237 struct mmc_data data;
1238 unsigned char *status;
1239 struct scatterlist sg;
1241 status = kmalloc(64, GFP_KERNEL);
1242 if (!status) {
1243 printk(KERN_WARNING "%s: Unable to allocate buffer for "
1244 "reading switch capabilities.\n",
1245 mmc_hostname(host));
1246 return;
1249 list_for_each_entry(card, &host->cards, node) {
1250 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
1251 continue;
1252 if (!mmc_card_sd(card))
1253 continue;
1254 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
1255 continue;
1257 err = mmc_select_card(host, card);
1258 if (err != MMC_ERR_NONE) {
1259 mmc_card_set_dead(card);
1260 continue;
1263 memset(&cmd, 0, sizeof(struct mmc_command));
1265 cmd.opcode = SD_SWITCH;
1266 cmd.arg = 0x00FFFFF1;
1267 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1269 memset(&data, 0, sizeof(struct mmc_data));
1271 mmc_set_data_timeout(&data, card, 0);
1273 data.blksz = 64;
1274 data.blocks = 1;
1275 data.flags = MMC_DATA_READ;
1276 data.sg = &sg;
1277 data.sg_len = 1;
1279 memset(&mrq, 0, sizeof(struct mmc_request));
1281 mrq.cmd = &cmd;
1282 mrq.data = &data;
1284 sg_init_one(&sg, status, 64);
1286 mmc_wait_for_req(host, &mrq);
1288 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
1289 mmc_card_set_dead(card);
1290 continue;
1293 if (status[13] & 0x02)
1294 card->sw_caps.hs_max_dtr = 50000000;
1296 memset(&cmd, 0, sizeof(struct mmc_command));
1298 cmd.opcode = SD_SWITCH;
1299 cmd.arg = 0x80FFFFF1;
1300 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1302 memset(&data, 0, sizeof(struct mmc_data));
1304 mmc_set_data_timeout(&data, card, 0);
1306 data.blksz = 64;
1307 data.blocks = 1;
1308 data.flags = MMC_DATA_READ;
1309 data.sg = &sg;
1310 data.sg_len = 1;
1312 memset(&mrq, 0, sizeof(struct mmc_request));
1314 mrq.cmd = &cmd;
1315 mrq.data = &data;
1317 sg_init_one(&sg, status, 64);
1319 mmc_wait_for_req(host, &mrq);
1321 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
1322 mmc_card_set_dead(card);
1323 continue;
1326 if ((status[16] & 0xF) != 1) {
1327 printk(KERN_WARNING "%s: Problem switching card "
1328 "into high-speed mode!\n",
1329 mmc_hostname(host));
1330 continue;
1333 mmc_card_set_highspeed(card);
1336 kfree(status);
1338 mmc_deselect_cards(host);
1341 static unsigned int mmc_calculate_clock(struct mmc_host *host)
1343 struct mmc_card *card;
1344 unsigned int max_dtr = host->f_max;
1346 list_for_each_entry(card, &host->cards, node)
1347 if (!mmc_card_dead(card)) {
1348 if (mmc_card_highspeed(card) && mmc_card_sd(card)) {
1349 if (max_dtr > card->sw_caps.hs_max_dtr)
1350 max_dtr = card->sw_caps.hs_max_dtr;
1351 } else if (mmc_card_highspeed(card) && !mmc_card_sd(card)) {
1352 if (max_dtr > card->ext_csd.hs_max_dtr)
1353 max_dtr = card->ext_csd.hs_max_dtr;
1354 } else if (max_dtr > card->csd.max_dtr) {
1355 max_dtr = card->csd.max_dtr;
1359 pr_debug("%s: selected %d.%03dMHz transfer rate\n",
1360 mmc_hostname(host),
1361 max_dtr / 1000000, (max_dtr / 1000) % 1000);
1363 return max_dtr;
1367 * Check whether cards we already know about are still present.
1368 * We do this by requesting status, and checking whether a card
1369 * responds.
1371 * A request for status does not cause a state change in data
1372 * transfer mode.
1374 static void mmc_check_cards(struct mmc_host *host)
1376 struct list_head *l, *n;
1378 mmc_deselect_cards(host);
1380 list_for_each_safe(l, n, &host->cards) {
1381 struct mmc_card *card = mmc_list_to_card(l);
1382 struct mmc_command cmd;
1383 int err;
1385 cmd.opcode = MMC_SEND_STATUS;
1386 cmd.arg = card->rca << 16;
1387 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1389 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1390 if (err == MMC_ERR_NONE)
1391 continue;
1393 mmc_card_set_dead(card);
1397 static void mmc_setup(struct mmc_host *host)
1399 if (host->ios.power_mode != MMC_POWER_ON) {
1400 int err;
1401 u32 ocr;
1403 host->mode = MMC_MODE_SD;
1405 mmc_power_up(host);
1406 mmc_idle_cards(host);
1408 err = mmc_send_if_cond(host, host->ocr_avail, NULL);
1409 if (err != MMC_ERR_NONE) {
1410 return;
1412 err = mmc_send_app_op_cond(host, 0, &ocr);
1415 * If we fail to detect any SD cards then try
1416 * searching for MMC cards.
1418 if (err != MMC_ERR_NONE) {
1419 host->mode = MMC_MODE_MMC;
1421 err = mmc_send_op_cond(host, 0, &ocr);
1422 if (err != MMC_ERR_NONE)
1423 return;
1426 host->ocr = mmc_select_voltage(host, ocr);
1429 * Since we're changing the OCR value, we seem to
1430 * need to tell some cards to go back to the idle
1431 * state. We wait 1ms to give cards time to
1432 * respond.
1434 if (host->ocr)
1435 mmc_idle_cards(host);
1436 } else {
1437 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1438 host->ios.clock = host->f_min;
1439 mmc_set_ios(host);
1442 * We should remember the OCR mask from the existing
1443 * cards, and detect the new cards OCR mask, combine
1444 * the two and re-select the VDD. However, if we do
1445 * change VDD, we should do an idle, and then do a
1446 * full re-initialisation. We would need to notify
1447 * drivers so that they can re-setup the cards as
1448 * well, while keeping their queues at bay.
1450 * For the moment, we take the easy way out - if the
1451 * new cards don't like our currently selected VDD,
1452 * they drop off the bus.
1456 if (host->ocr == 0)
1457 return;
1460 * Send the selected OCR multiple times... until the cards
1461 * all get the idea that they should be ready for CMD2.
1462 * (My SanDisk card seems to need this.)
1464 if (host->mode == MMC_MODE_SD) {
1465 int err, sd2;
1466 err = mmc_send_if_cond(host, host->ocr, &sd2);
1467 if (err == MMC_ERR_NONE) {
1469 * If SD_SEND_IF_COND indicates an SD 2.0
1470 * compliant card and we should set bit 30
1471 * of the ocr to indicate that we can handle
1472 * block-addressed SDHC cards.
1474 mmc_send_app_op_cond(host, host->ocr | (sd2 << 30), NULL);
1476 } else {
1477 mmc_send_op_cond(host, host->ocr, NULL);
1480 mmc_discover_cards(host);
1483 * Ok, now switch to push-pull mode.
1485 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1486 mmc_set_ios(host);
1488 mmc_read_csds(host);
1490 if (host->mode == MMC_MODE_SD) {
1491 mmc_read_scrs(host);
1492 mmc_read_switch_caps(host);
1493 } else
1494 mmc_process_ext_csds(host);
1499 * mmc_detect_change - process change of state on a MMC socket
1500 * @host: host which changed state.
1501 * @delay: optional delay to wait before detection (jiffies)
1503 * All we know is that card(s) have been inserted or removed
1504 * from the socket(s). We don't know which socket or cards.
1506 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1508 mmc_schedule_delayed_work(&host->detect, delay);
1511 EXPORT_SYMBOL(mmc_detect_change);
1514 static void mmc_rescan(struct work_struct *work)
1516 struct mmc_host *host =
1517 container_of(work, struct mmc_host, detect.work);
1518 struct list_head *l, *n;
1519 unsigned char power_mode;
1521 mmc_claim_host(host);
1524 * Check for removed cards and newly inserted ones. We check for
1525 * removed cards first so we can intelligently re-select the VDD.
1527 power_mode = host->ios.power_mode;
1528 if (power_mode == MMC_POWER_ON)
1529 mmc_check_cards(host);
1531 mmc_setup(host);
1534 * Some broken cards process CMD1 even in stand-by state. There is
1535 * no reply, but an ILLEGAL_COMMAND error is cached and returned
1536 * after next command. We poll for card status here to clear any
1537 * possibly pending error.
1539 if (power_mode == MMC_POWER_ON)
1540 mmc_check_cards(host);
1542 if (!list_empty(&host->cards)) {
1544 * (Re-)calculate the fastest clock rate which the
1545 * attached cards and the host support.
1547 host->ios.clock = mmc_calculate_clock(host);
1548 mmc_set_ios(host);
1551 mmc_release_host(host);
1553 list_for_each_safe(l, n, &host->cards) {
1554 struct mmc_card *card = mmc_list_to_card(l);
1557 * If this is a new and good card, register it.
1559 if (!mmc_card_present(card) && !mmc_card_dead(card)) {
1560 if (mmc_register_card(card))
1561 mmc_card_set_dead(card);
1562 else
1563 mmc_card_set_present(card);
1567 * If this card is dead, destroy it.
1569 if (mmc_card_dead(card)) {
1570 list_del(&card->node);
1571 mmc_remove_card(card);
1576 * If we discover that there are no cards on the
1577 * bus, turn off the clock and power down.
1579 if (list_empty(&host->cards))
1580 mmc_power_off(host);
1585 * mmc_alloc_host - initialise the per-host structure.
1586 * @extra: sizeof private data structure
1587 * @dev: pointer to host device model structure
1589 * Initialise the per-host structure.
1591 struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
1593 struct mmc_host *host;
1595 host = mmc_alloc_host_sysfs(extra, dev);
1596 if (host) {
1597 spin_lock_init(&host->lock);
1598 init_waitqueue_head(&host->wq);
1599 INIT_LIST_HEAD(&host->cards);
1600 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
1603 * By default, hosts do not support SGIO or large requests.
1604 * They have to set these according to their abilities.
1606 host->max_hw_segs = 1;
1607 host->max_phys_segs = 1;
1608 host->max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
1609 host->max_seg_size = PAGE_CACHE_SIZE;
1611 host->max_blk_size = 512;
1614 return host;
1617 EXPORT_SYMBOL(mmc_alloc_host);
1620 * mmc_add_host - initialise host hardware
1621 * @host: mmc host
1623 int mmc_add_host(struct mmc_host *host)
1625 int ret;
1627 ret = mmc_add_host_sysfs(host);
1628 if (ret == 0) {
1629 mmc_power_off(host);
1630 mmc_detect_change(host, 0);
1633 return ret;
1636 EXPORT_SYMBOL(mmc_add_host);
1639 * mmc_remove_host - remove host hardware
1640 * @host: mmc host
1642 * Unregister and remove all cards associated with this host,
1643 * and power down the MMC bus.
1645 void mmc_remove_host(struct mmc_host *host)
1647 struct list_head *l, *n;
1649 list_for_each_safe(l, n, &host->cards) {
1650 struct mmc_card *card = mmc_list_to_card(l);
1652 mmc_remove_card(card);
1655 mmc_power_off(host);
1656 mmc_remove_host_sysfs(host);
1659 EXPORT_SYMBOL(mmc_remove_host);
1662 * mmc_free_host - free the host structure
1663 * @host: mmc host
1665 * Free the host once all references to it have been dropped.
1667 void mmc_free_host(struct mmc_host *host)
1669 mmc_flush_scheduled_work();
1670 mmc_free_host_sysfs(host);
1673 EXPORT_SYMBOL(mmc_free_host);
1675 #ifdef CONFIG_PM
1678 * mmc_suspend_host - suspend a host
1679 * @host: mmc host
1680 * @state: suspend mode (PM_SUSPEND_xxx)
1682 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1684 mmc_claim_host(host);
1685 mmc_deselect_cards(host);
1686 mmc_power_off(host);
1687 mmc_release_host(host);
1689 return 0;
1692 EXPORT_SYMBOL(mmc_suspend_host);
1695 * mmc_resume_host - resume a previously suspended host
1696 * @host: mmc host
1698 int mmc_resume_host(struct mmc_host *host)
1700 mmc_rescan(&host->detect.work);
1702 return 0;
1705 EXPORT_SYMBOL(mmc_resume_host);
1707 #endif
1709 MODULE_LICENSE("GPL");