MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / mmc.old / mmc.c
blob01d09f7b9a89a529d7b85c1714ad80e37699e95a
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/config.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/completion.h>
17 #include <linux/device.h>
18 #include <linux/delay.h>
19 #include <linux/pagemap.h>
20 #include <linux/err.h>
21 #include <asm-arm/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 #ifdef CONFIG_MMC_DEBUG
31 #define DBG(x...) printk(KERN_DEBUG x)
32 #else
33 #define DBG(x...) do { } while (0)
34 #endif
36 #define CMD_RETRIES 3
39 * OCR Bit positions to 10s of Vdd mV.
41 static const unsigned short mmc_ocr_bit_to_vdd[] = {
42 150, 155, 160, 165, 170, 180, 190, 200,
43 210, 220, 230, 240, 250, 260, 270, 280,
44 290, 300, 310, 320, 330, 340, 350, 360
47 static const unsigned int tran_exp[] = {
48 10000, 100000, 1000000, 10000000,
49 0, 0, 0, 0
52 static const unsigned char tran_mant[] = {
53 0, 10, 12, 13, 15, 20, 25, 30,
54 35, 40, 45, 50, 55, 60, 70, 80,
57 static const unsigned int tacc_exp[] = {
58 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
61 static const unsigned int tacc_mant[] = {
62 0, 10, 12, 13, 15, 20, 25, 30,
63 35, 40, 45, 50, 55, 60, 70, 80,
67 /**
68 * mmc_request_done - finish processing an MMC command
69 * @host: MMC host which completed command
70 * @mrq: MMC request which completed
72 * MMC drivers should call this function when they have completed
73 * their processing of a command. This should be called before the
74 * data part of the command has completed.
76 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
78 struct mmc_command *cmd = mrq->cmd;
79 int err = mrq->cmd->error;
80 DBG("MMC: req done (%02x): %d: %08x %08x %08x %08x\n", cmd->opcode,
81 err, cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
83 if (err && cmd->retries) {
84 cmd->retries--;
85 cmd->error = 0;
86 host->ops->request(host, mrq);
87 } else if (mrq->done) {
88 mrq->done(mrq);
92 EXPORT_SYMBOL(mmc_request_done);
94 /**
95 * mmc_start_request - start a command on a host
96 * @host: MMC host to start command on
97 * @mrq: MMC request to start
99 * Queue a command on the specified host. We expect the
100 * caller to be holding the host lock with interrupts disabled.
102 void
103 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
105 DBG("MMC: starting cmd %02x arg %08x flags %08x\n",
106 mrq->cmd->opcode, mrq->cmd->arg, mrq->cmd->flags);
108 WARN_ON(host->card_busy == NULL);
110 mrq->cmd->error = 0;
111 mrq->cmd->mrq = mrq;
112 if (mrq->data) {
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(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->card_busy == NULL);
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->card_busy == NULL);
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;
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);
253 static int mmc_select_card(struct mmc_host *host, struct mmc_card *card);
256 * __mmc_claim_host - exclusively claim a host
257 * @host: mmc host to claim
258 * @card: mmc card to claim host for
260 * Claim a host for a set of operations. If a valid card
261 * is passed and this wasn't the last card selected, select
262 * the card before returning.
264 * Note: you should use mmc_card_claim_host or mmc_claim_host.
266 int __mmc_claim_host(struct mmc_host *host, struct mmc_card *card)
268 DECLARE_WAITQUEUE(wait, current);
269 unsigned long flags;
270 int err = 0;
272 add_wait_queue(&host->wq, &wait);
273 spin_lock_irqsave(&host->lock, flags);
274 while (1) {
275 set_current_state(TASK_UNINTERRUPTIBLE);
276 if (host->card_busy == NULL)
277 break;
278 spin_unlock_irqrestore(&host->lock, flags);
279 schedule();
280 spin_lock_irqsave(&host->lock, flags);
282 set_current_state(TASK_RUNNING);
283 host->card_busy = card;
284 spin_unlock_irqrestore(&host->lock, flags);
285 remove_wait_queue(&host->wq, &wait);
287 if (card != (void *)-1) {
288 err = mmc_select_card(host, card);
289 if (err != MMC_ERR_NONE)
290 return err;
293 return err;
296 EXPORT_SYMBOL(__mmc_claim_host);
299 * mmc_release_host - release a host
300 * @host: mmc host to release
302 * Release a MMC host, allowing others to claim the host
303 * for their operations.
305 void mmc_release_host(struct mmc_host *host)
307 unsigned long flags;
309 BUG_ON(host->card_busy == NULL);
311 spin_lock_irqsave(&host->lock, flags);
312 host->card_busy = NULL;
313 spin_unlock_irqrestore(&host->lock, flags);
315 wake_up(&host->wq);
318 EXPORT_SYMBOL(mmc_release_host);
320 static int mmc_select_card(struct mmc_host *host, struct mmc_card *card)
322 int err;
323 struct mmc_command cmd;
325 BUG_ON(host->card_busy == NULL);
327 if (host->card_selected == card)
328 return MMC_ERR_NONE;
330 host->card_selected = card;
332 cmd.opcode = MMC_SELECT_CARD;
333 cmd.arg = card->rca << 16;
334 cmd.flags = MMC_RSP_R1;
336 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
337 if (err != MMC_ERR_NONE)
338 return err;
341 * Default bus width is 1 bit.
343 host->ios.bus_width = MMC_BUS_WIDTH_1;
346 * We can only change the bus width of the selected
347 * card so therefore we have to put the handling
348 * here.
350 if (host->caps & MMC_CAP_4_BIT_DATA) {
352 * The card is in 1 bit mode by default so
353 * we only need to change if it supports the
354 * wider version.
356 if (mmc_card_sd(card) &&
357 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
358 struct mmc_command cmd;
359 cmd.opcode = SD_APP_SET_BUS_WIDTH;
360 cmd.arg = SD_BUS_WIDTH_4;
361 cmd.flags = MMC_RSP_R1;
363 err = mmc_wait_for_app_cmd(host, card->rca, &cmd,
364 CMD_RETRIES);
365 if (err != MMC_ERR_NONE)
366 return err;
368 host->ios.bus_width = MMC_BUS_WIDTH_4;
372 host->ops->set_ios(host, &host->ios);
374 return MMC_ERR_NONE;
378 * Ensure that no card is selected.
380 static void mmc_deselect_cards(struct mmc_host *host)
382 struct mmc_command cmd;
384 if (host->card_selected) {
385 host->card_selected = NULL;
387 cmd.opcode = MMC_SELECT_CARD;
388 cmd.arg = 0;
389 cmd.flags = MMC_RSP_NONE;
391 mmc_wait_for_cmd(host, &cmd, 0);
396 static inline void mmc_delay(unsigned int ms)
398 if (ms < HZ / 1000) {
399 yield();
400 mdelay(ms);
401 } else {
402 msleep_interruptible (ms);
407 * Mask off any voltages we don't support and select
408 * the lowest voltage
410 static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
412 int bit;
414 ocr &= host->ocr_avail;
416 bit = ffs(ocr);
417 if (bit) {
418 bit -= 1;
420 ocr = 3 << bit;
422 host->ios.vdd = bit;
423 host->ops->set_ios(host, &host->ios);
424 } else {
425 ocr = 0;
428 return ocr;
430 #if 0 // mask by Victor Yu. 11-30-2005
431 #define UNSTUFF_BITS(resp,start,size) \
432 ({ \
433 const int __size = size; \
434 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
435 const int __off = 3 - ((start) / 32); \
436 const int __shft = (start) & 31; \
437 u32 __res; \
439 __res = resp[__off] >> __shft; \
440 if (__size + __shft > 32) \
441 __res |= resp[__off-1] << ((32 - __shft) % 32); \
442 __res & __mask; \
444 #else // add by Victor Yu. 11-30-2005
445 #define UNSTUFF_BITS(resp,start,size) \
446 ({ \
447 const int __size = size; \
448 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
449 const int __off = ((start) / 32); \
450 const int __shft = (start) & 31; \
451 u32 __res; \
453 __res = resp[__off] >> __shft; \
454 if (__size + __shft > 32) \
455 __res |= resp[__off+1] << ((32 - __shft) % 32); \
456 __res & __mask; \
458 #endif // 11-30-2005
461 * Given the decoded CSD structure, decode the raw CID to our CID structure.
463 static void mmc_decode_cid(struct mmc_card *card)
465 u32 *resp = card->raw_cid;
467 memset(&card->cid, 0, sizeof(struct mmc_cid));
469 if (mmc_card_sd(card)) {
471 * SD doesn't currently have a version field so we will
472 * have to assume we can parse this.
474 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
475 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
476 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
477 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
478 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
479 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
480 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
481 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
482 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
483 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
484 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
485 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
487 card->cid.year += 2000; /* SD cards year offset */
488 } else {
490 * The selection of the format here is based upon published
491 * specs from sandisk and from what people have reported.
493 switch (card->csd.mmca_vsn) {
494 case 0: /* MMC v1.0 - v1.2 */
495 case 1: /* MMC v1.4 */
496 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
497 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
498 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
499 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
500 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
501 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
502 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
503 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
504 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
505 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
506 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
507 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
508 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
509 break;
511 case 2: /* MMC v2.0 - v2.2 */
512 case 3: /* MMC v3.1 - v3.3 */
513 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
514 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
515 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
516 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
517 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
518 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
519 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
520 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
521 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
522 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
523 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
524 break;
526 default:
527 printk("%s: card has unknown MMCA version %d\n",
528 mmc_hostname(card->host), card->csd.mmca_vsn);
529 mmc_card_set_bad(card);
530 break;
536 * Given a 128-bit response, decode to our card CSD structure.
538 static void mmc_decode_csd(struct mmc_card *card)
540 struct mmc_csd *csd = &card->csd;
541 unsigned int e, m, csd_struct;
542 u32 *resp = card->raw_csd;
544 if (mmc_card_sd(card)) {
545 csd_struct = UNSTUFF_BITS(resp, 126, 2);
546 if (csd_struct != 0) {
547 printk("%s: unrecognised CSD structure version %d\n",
548 mmc_hostname(card->host), csd_struct);
549 mmc_card_set_bad(card);
550 return;
553 m = UNSTUFF_BITS(resp, 115, 4);
554 e = UNSTUFF_BITS(resp, 112, 3);
555 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
556 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
558 m = UNSTUFF_BITS(resp, 99, 4);
559 e = UNSTUFF_BITS(resp, 96, 3);
560 csd->max_dtr = tran_exp[e] * tran_mant[m];
561 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
563 e = UNSTUFF_BITS(resp, 47, 3);
564 m = UNSTUFF_BITS(resp, 62, 12);
565 csd->capacity = (1 + m) << (e + 2);
567 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
568 } else {
570 * We only understand CSD structure v1.1 and v1.2.
571 * v1.2 has extra information in bits 15, 11 and 10.
573 csd_struct = UNSTUFF_BITS(resp, 126, 2);
574 if (csd_struct != 1 && csd_struct != 2) {
575 printk("%s: unrecognised CSD structure version %d\n",
576 mmc_hostname(card->host), csd_struct);
577 mmc_card_set_bad(card);
578 return;
581 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
582 m = UNSTUFF_BITS(resp, 115, 4);
583 e = UNSTUFF_BITS(resp, 112, 3);
584 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
585 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
587 m = UNSTUFF_BITS(resp, 99, 4);
588 e = UNSTUFF_BITS(resp, 96, 3);
589 csd->max_dtr = tran_exp[e] * tran_mant[m];
590 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
592 e = UNSTUFF_BITS(resp, 47, 3);
593 m = UNSTUFF_BITS(resp, 62, 12);
594 csd->capacity = (1 + m) << (e + 2);
596 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
601 * Given a 64-bit response, decode to our card SCR structure.
603 static void mmc_decode_scr(struct mmc_card *card)
605 struct sd_scr *scr = &card->scr;
606 unsigned int scr_struct;
607 u32 resp[4];
609 BUG_ON(!mmc_card_sd(card));
611 resp[3] = card->raw_scr[1];
612 resp[2] = card->raw_scr[0];
614 scr_struct = UNSTUFF_BITS(resp, 60, 4);
615 if (scr_struct != 0) {
616 printk("%s: unrecognised SCR structure version %d\n",
617 mmc_hostname(card->host), scr_struct);
618 mmc_card_set_bad(card);
619 return;
622 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
623 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
627 * Locate a MMC card on this MMC host given a raw CID.
629 static struct mmc_card *mmc_find_card(struct mmc_host *host, u32 *raw_cid)
631 struct mmc_card *card;
633 list_for_each_entry(card, &host->cards, node) {
634 if (memcmp(card->raw_cid, raw_cid, sizeof(card->raw_cid)) == 0)
635 return card;
637 return NULL;
641 * Allocate a new MMC card, and assign a unique RCA.
643 static struct mmc_card *
644 mmc_alloc_card(struct mmc_host *host, u32 *raw_cid, unsigned int *frca)
646 struct mmc_card *card, *c;
647 unsigned int rca = *frca;
649 card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL);
650 if (!card)
651 return ERR_PTR(-ENOMEM);
653 mmc_init_card(card, host);
654 memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid));
656 again:
657 list_for_each_entry(c, &host->cards, node)
658 if (c->rca == rca) {
659 rca++;
660 goto again;
663 card->rca = rca;
665 *frca = rca;
667 return card;
671 * Tell attached cards to go to IDLE state
673 static void mmc_idle_cards(struct mmc_host *host)
675 struct mmc_command cmd;
677 host->ios.chip_select = MMC_CS_HIGH;
678 host->ops->set_ios(host, &host->ios);
680 mmc_delay(1);
682 cmd.opcode = MMC_GO_IDLE_STATE;
683 cmd.arg = 0;
684 cmd.flags = MMC_RSP_NONE;
686 mmc_wait_for_cmd(host, &cmd, 0);
688 mmc_delay(1);
690 host->ios.chip_select = MMC_CS_DONTCARE;
691 host->ops->set_ios(host, &host->ios);
693 mmc_delay(1);
697 * Apply power to the MMC stack.
699 static void mmc_power_up(struct mmc_host *host)
701 int bit = fls(host->ocr_avail) - 1;
703 host->ios.vdd = bit;
704 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
705 host->ios.chip_select = MMC_CS_DONTCARE;
706 host->ios.power_mode = MMC_POWER_UP;
707 host->ios.bus_width = MMC_BUS_WIDTH_1;
708 host->ops->set_ios(host, &host->ios);
710 mmc_delay(1);
712 host->ios.clock = host->f_min;
713 host->ios.power_mode = MMC_POWER_ON;
714 host->ops->set_ios(host, &host->ios);
716 mmc_delay(2);
719 static void mmc_power_off(struct mmc_host *host)
721 host->ios.clock = 0;
722 host->ios.vdd = 0;
723 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
724 host->ios.chip_select = MMC_CS_DONTCARE;
725 host->ios.power_mode = MMC_POWER_OFF;
726 host->ios.bus_width = MMC_BUS_WIDTH_1;
727 host->ops->set_ios(host, &host->ios);
730 static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
732 struct mmc_command cmd;
733 int i, err = 0;
735 cmd.opcode = MMC_SEND_OP_COND;
736 cmd.arg = ocr;
737 cmd.flags = MMC_RSP_R3;
739 for (i = 100; i; i--) {
740 err = mmc_wait_for_cmd(host, &cmd, 0);
741 if (err != MMC_ERR_NONE)
742 break;
744 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
745 break;
747 err = MMC_ERR_TIMEOUT;
749 mmc_delay(10);
752 if (rocr)
753 *rocr = cmd.resp[0];
755 return err;
758 static int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
760 struct mmc_command cmd;
761 int i, err = 0;
763 cmd.opcode = SD_APP_OP_COND;
764 cmd.arg = ocr;
765 cmd.flags = MMC_RSP_R3;
767 for (i = 100; i; i--) {
768 err = mmc_wait_for_app_cmd(host, 0, &cmd, CMD_RETRIES);
769 if (err != MMC_ERR_NONE)
770 break;
772 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
773 break;
775 err = MMC_ERR_TIMEOUT;
777 mmc_delay(10);
780 if (rocr)
781 *rocr = cmd.resp[0];
783 return err;
787 * Discover cards by requesting their CID. If this command
788 * times out, it is not an error; there are no further cards
789 * to be discovered. Add new cards to the list.
791 * Create a mmc_card entry for each discovered card, assigning
792 * it an RCA, and save the raw CID for decoding later.
794 static void mmc_discover_cards(struct mmc_host *host)
796 struct mmc_card *card;
797 unsigned int first_rca = 1, err;
799 while (1) {
800 struct mmc_command cmd;
802 cmd.opcode = MMC_ALL_SEND_CID;
803 cmd.arg = 0;
804 cmd.flags = MMC_RSP_R2;
806 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
807 if (err == MMC_ERR_TIMEOUT) {
808 err = MMC_ERR_NONE;
809 break;
811 if (err != MMC_ERR_NONE) {
812 printk(KERN_ERR "%s: error requesting CID: %d\n",
813 mmc_hostname(host), err);
814 break;
817 card = mmc_find_card(host, cmd.resp);
818 if (!card) {
819 card = mmc_alloc_card(host, cmd.resp, &first_rca);
820 if (IS_ERR(card)) {
821 err = PTR_ERR(card);
822 break;
824 list_add(&card->node, &host->cards);
827 card->state &= ~MMC_STATE_DEAD;
829 if (host->mode == MMC_MODE_SD) {
830 mmc_card_set_sd(card);
832 cmd.opcode = SD_SEND_RELATIVE_ADDR;
833 cmd.arg = 0;
834 cmd.flags = MMC_RSP_R1;
836 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
837 if (err != MMC_ERR_NONE) {
838 mmc_card_set_dead(card);
839 } else {
840 card->rca = cmd.resp[0] >> 16;
842 if (!host->ops->get_ro) {
843 printk(KERN_WARNING "%s: host does not "
844 "support reading read-only "
845 "switch. assuming write-enable.\n",
846 mmc_hostname(host));
847 } else {
848 if (host->ops->get_ro(host))
849 mmc_card_set_readonly(card);
852 } else {
853 cmd.opcode = MMC_SET_RELATIVE_ADDR;
854 cmd.arg = card->rca << 16;
855 cmd.flags = MMC_RSP_R1;
857 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
858 if (err != MMC_ERR_NONE)
859 mmc_card_set_dead(card);
864 static void mmc_read_csds(struct mmc_host *host)
866 struct mmc_card *card;
868 list_for_each_entry(card, &host->cards, node) {
869 struct mmc_command cmd;
870 int err;
872 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
873 continue;
875 cmd.opcode = MMC_SEND_CSD;
876 cmd.arg = card->rca << 16;
877 cmd.flags = MMC_RSP_R2;
879 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
880 if (err != MMC_ERR_NONE) {
881 mmc_card_set_dead(card);
882 continue;
885 memcpy(card->raw_csd, cmd.resp, sizeof(card->raw_csd));
887 mmc_decode_csd(card);
888 mmc_decode_cid(card);
892 static void mmc_read_scrs(struct mmc_host *host)
894 int err;
895 struct mmc_card *card;
897 struct mmc_request mrq;
898 struct mmc_command cmd;
899 struct mmc_data data;
901 struct scatterlist sg;
903 list_for_each_entry(card, &host->cards, node) {
904 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
905 continue;
906 if (!mmc_card_sd(card))
907 continue;
909 err = mmc_select_card(host, card);
910 if (err != MMC_ERR_NONE) {
911 mmc_card_set_dead(card);
912 continue;
915 memset(&cmd, 0, sizeof(struct mmc_command));
917 cmd.opcode = MMC_APP_CMD;
918 cmd.arg = card->rca << 16;
919 cmd.flags = MMC_RSP_R1;
921 err = mmc_wait_for_cmd(host, &cmd, 0);
922 if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD)) {
923 mmc_card_set_dead(card);
924 continue;
927 memset(&cmd, 0, sizeof(struct mmc_command));
929 cmd.opcode = SD_APP_SEND_SCR;
930 cmd.arg = 0;
931 cmd.flags = MMC_RSP_R1;
933 memset(&data, 0, sizeof(struct mmc_data));
935 data.timeout_ns = card->csd.tacc_ns * 10;
936 data.timeout_clks = card->csd.tacc_clks * 10;
937 data.blksz_bits = 3;
938 data.blocks = 1;
939 data.flags = MMC_DATA_READ;
940 data.sg = &sg;
941 data.sg_len = 1;
943 memset(&mrq, 0, sizeof(struct mmc_request));
945 mrq.cmd = &cmd;
946 mrq.data = &data;
948 sg_init_one(&sg, (u8*)card->raw_scr, 8);
950 err = mmc_wait_for_req(host, &mrq);
951 if (err != MMC_ERR_NONE) {
952 mmc_card_set_dead(card);
953 continue;
956 card->raw_scr[0] = ntohl(card->raw_scr[0]);
957 card->raw_scr[1] = ntohl(card->raw_scr[1]);
959 mmc_decode_scr(card);
962 mmc_deselect_cards(host);
965 static unsigned int mmc_calculate_clock(struct mmc_host *host)
967 struct mmc_card *card;
968 unsigned int max_dtr = host->f_max;
970 list_for_each_entry(card, &host->cards, node)
971 if (!mmc_card_dead(card) && max_dtr > card->csd.max_dtr)
972 max_dtr = card->csd.max_dtr;
974 DBG("MMC: selected %d.%03dMHz transfer rate\n",
975 max_dtr / 1000000, (max_dtr / 1000) % 1000);
977 return max_dtr;
981 * Check whether cards we already know about are still present.
982 * We do this by requesting status, and checking whether a card
983 * responds.
985 * A request for status does not cause a state change in data
986 * transfer mode.
988 static void mmc_check_cards(struct mmc_host *host)
990 struct list_head *l, *n;
992 mmc_deselect_cards(host);
994 list_for_each_safe(l, n, &host->cards) {
995 struct mmc_card *card = mmc_list_to_card(l);
996 struct mmc_command cmd;
997 int err;
999 cmd.opcode = MMC_SEND_STATUS;
1000 cmd.arg = card->rca << 16;
1001 cmd.flags = MMC_RSP_R1;
1003 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1004 if (err == MMC_ERR_NONE)
1005 continue;
1007 mmc_card_set_dead(card);
1011 static void mmc_setup(struct mmc_host *host)
1013 if (host->ios.power_mode != MMC_POWER_ON) {
1014 int err;
1015 u32 ocr;
1017 host->mode = MMC_MODE_SD;
1019 mmc_power_up(host);
1020 mmc_idle_cards(host);
1022 err = mmc_send_app_op_cond(host, 0, &ocr);
1025 * If we fail to detect any SD cards then try
1026 * searching for MMC cards.
1028 if (err != MMC_ERR_NONE) {
1029 host->mode = MMC_MODE_MMC;
1031 err = mmc_send_op_cond(host, 0, &ocr);
1032 if (err != MMC_ERR_NONE)
1033 return;
1036 host->ocr = mmc_select_voltage(host, ocr);
1039 * Since we're changing the OCR value, we seem to
1040 * need to tell some cards to go back to the idle
1041 * state. We wait 1ms to give cards time to
1042 * respond.
1044 if (host->ocr)
1045 mmc_idle_cards(host);
1046 } else {
1047 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1048 host->ios.clock = host->f_min;
1049 host->ops->set_ios(host, &host->ios);
1052 * We should remember the OCR mask from the existing
1053 * cards, and detect the new cards OCR mask, combine
1054 * the two and re-select the VDD. However, if we do
1055 * change VDD, we should do an idle, and then do a
1056 * full re-initialisation. We would need to notify
1057 * drivers so that they can re-setup the cards as
1058 * well, while keeping their queues at bay.
1060 * For the moment, we take the easy way out - if the
1061 * new cards don't like our currently selected VDD,
1062 * they drop off the bus.
1066 if (host->ocr == 0)
1067 return;
1070 * Send the selected OCR multiple times... until the cards
1071 * all get the idea that they should be ready for CMD2.
1072 * (My SanDisk card seems to need this.)
1074 if (host->mode == MMC_MODE_SD) {
1075 mmc_send_app_op_cond(host, host->ocr, NULL);
1076 } else {
1077 mmc_send_op_cond(host, host->ocr, NULL);
1080 mmc_discover_cards(host);
1083 * Ok, now switch to push-pull mode.
1085 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1086 host->ops->set_ios(host, &host->ios);
1088 mmc_read_csds(host);
1090 if (host->mode == MMC_MODE_SD) {
1091 mmc_read_scrs(host);
1097 * mmc_detect_change - process change of state on a MMC socket
1098 * @host: host which changed state.
1099 * @delay: optional delay to wait before detection (jiffies)
1101 * All we know is that card(s) have been inserted or removed
1102 * from the socket(s). We don't know which socket or cards.
1104 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1106 if (delay) {
1107 schedule_delayed_work(&host->detect, delay);
1108 } else {
1109 schedule_work(&host->detect);
1113 EXPORT_SYMBOL(mmc_detect_change);
1116 static void mmc_rescan(void *data)
1118 struct mmc_host *host = data;
1119 struct list_head *l, *n;
1121 mmc_claim_host(host);
1123 if (host->ios.power_mode == MMC_POWER_ON) {
1124 mmc_check_cards(host);
1127 mmc_setup(host);
1129 if (!list_empty(&host->cards)) {
1131 * (Re-)calculate the fastest clock rate which the
1132 * attached cards and the host support.
1134 host->ios.clock = mmc_calculate_clock(host);
1135 host->ops->set_ios(host, &host->ios);
1138 mmc_release_host(host);
1140 list_for_each_safe(l, n, &host->cards) {
1141 struct mmc_card *card = mmc_list_to_card(l);
1144 * If this is a new and good card, register it.
1146 if (!mmc_card_present(card) && !mmc_card_dead(card)) {
1147 if (mmc_register_card(card)) {
1148 mmc_card_set_dead(card);
1149 } else {
1150 mmc_card_set_present(card);
1155 * If this card is dead, destroy it.
1157 if (mmc_card_dead(card)) {
1158 list_del(&card->node);
1159 mmc_remove_card(card);
1164 * If we discover that there are no cards on the
1165 * bus, turn off the clock and power down.
1167 if (list_empty(&host->cards)) {
1168 mmc_power_off(host);
1174 * mmc_alloc_host - initialise the per-host structure.
1175 * @extra: sizeof private data structure
1176 * @dev: pointer to host device model structure
1178 * Initialise the per-host structure.
1180 struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
1182 struct mmc_host *host;
1184 host = mmc_alloc_host_sysfs(extra, dev);
1185 if (host) {
1186 spin_lock_init(&host->lock);
1187 init_waitqueue_head(&host->wq);
1188 INIT_LIST_HEAD(&host->cards);
1189 INIT_WORK(&host->detect, mmc_rescan, host);
1192 * By default, hosts do not support SGIO or large requests.
1193 * They have to set these according to their abilities.
1195 host->max_hw_segs = 1;
1196 host->max_phys_segs = 1;
1197 host->max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
1198 host->max_seg_size = PAGE_CACHE_SIZE;
1201 return host;
1204 EXPORT_SYMBOL(mmc_alloc_host);
1207 * mmc_add_host - initialise host hardware
1208 * @host: mmc host
1210 int mmc_add_host(struct mmc_host *host)
1212 int ret;
1214 ret = mmc_add_host_sysfs(host);
1215 if (ret == 0) {
1216 mmc_power_off(host);
1217 mmc_detect_change(host, 0);
1220 return ret;
1223 EXPORT_SYMBOL(mmc_add_host);
1226 * mmc_remove_host - remove host hardware
1227 * @host: mmc host
1229 * Unregister and remove all cards associated with this host,
1230 * and power down the MMC bus.
1232 void mmc_remove_host(struct mmc_host *host)
1234 struct list_head *l, *n;
1236 list_for_each_safe(l, n, &host->cards) {
1237 struct mmc_card *card = mmc_list_to_card(l);
1239 mmc_remove_card(card);
1242 mmc_power_off(host);
1243 mmc_remove_host_sysfs(host);
1246 EXPORT_SYMBOL(mmc_remove_host);
1249 * mmc_free_host - free the host structure
1250 * @host: mmc host
1252 * Free the host once all references to it have been dropped.
1254 void mmc_free_host(struct mmc_host *host)
1256 flush_scheduled_work();
1257 mmc_free_host_sysfs(host);
1260 EXPORT_SYMBOL(mmc_free_host);
1262 #ifdef CONFIG_PM
1265 * mmc_suspend_host - suspend a host
1266 * @host: mmc host
1267 * @state: suspend mode (PM_SUSPEND_xxx)
1269 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1271 mmc_claim_host(host);
1272 mmc_deselect_cards(host);
1273 mmc_power_off(host);
1274 mmc_release_host(host);
1276 return 0;
1279 EXPORT_SYMBOL(mmc_suspend_host);
1282 * mmc_resume_host - resume a previously suspended host
1283 * @host: mmc host
1285 int mmc_resume_host(struct mmc_host *host)
1287 mmc_detect_change(host, 0);
1289 return 0;
1292 EXPORT_SYMBOL(mmc_resume_host);
1294 #endif
1296 MODULE_LICENSE("GPL");