2 * linux/drivers/mmc/mmc.c
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 #include <linux/config.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/completion.h>
15 #include <linux/device.h>
16 #include <linux/delay.h>
17 #include <linux/pagemap.h>
18 #include <linux/err.h>
20 #include <linux/mmc/card.h>
21 #include <linux/mmc/host.h>
22 #include <linux/mmc/protocol.h>
26 #ifdef CONFIG_MMC_DEBUG
27 #define DBG(x...) printk(KERN_DEBUG x)
29 #define DBG(x...) do { } while (0)
35 * OCR Bit positions to 10s of Vdd mV.
37 static const unsigned short mmc_ocr_bit_to_vdd
[] = {
38 150, 155, 160, 165, 170, 180, 190, 200,
39 210, 220, 230, 240, 250, 260, 270, 280,
40 290, 300, 310, 320, 330, 340, 350, 360
43 static const unsigned int tran_exp
[] = {
44 10000, 100000, 1000000, 10000000,
48 static const unsigned char tran_mant
[] = {
49 0, 10, 12, 13, 15, 20, 25, 30,
50 35, 40, 45, 50, 55, 60, 70, 80,
53 static const unsigned int tacc_exp
[] = {
54 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
57 static const unsigned int tacc_mant
[] = {
58 0, 10, 12, 13, 15, 20, 25, 30,
59 35, 40, 45, 50, 55, 60, 70, 80,
64 * mmc_request_done - finish processing an MMC command
65 * @host: MMC host which completed command
66 * @mrq: MMC request which completed
68 * MMC drivers should call this function when they have completed
69 * their processing of a command. This should be called before the
70 * data part of the command has completed.
72 void mmc_request_done(struct mmc_host
*host
, struct mmc_request
*mrq
)
74 struct mmc_command
*cmd
= mrq
->cmd
;
75 int err
= mrq
->cmd
->error
;
76 DBG("MMC: req done (%02x): %d: %08x %08x %08x %08x\n", cmd
->opcode
,
77 err
, cmd
->resp
[0], cmd
->resp
[1], cmd
->resp
[2], cmd
->resp
[3]);
79 if (err
&& cmd
->retries
) {
82 host
->ops
->request(host
, mrq
);
83 } else if (mrq
->done
) {
88 EXPORT_SYMBOL(mmc_request_done
);
91 * mmc_start_request - start a command on a host
92 * @host: MMC host to start command on
93 * @mrq: MMC request to start
95 * Queue a command on the specified host. We expect the
96 * caller to be holding the host lock with interrupts disabled.
99 mmc_start_request(struct mmc_host
*host
, struct mmc_request
*mrq
)
101 DBG("MMC: starting cmd %02x arg %08x flags %08x\n",
102 mrq
->cmd
->opcode
, mrq
->cmd
->arg
, mrq
->cmd
->flags
);
104 WARN_ON(host
->card_busy
== NULL
);
109 mrq
->cmd
->data
= mrq
->data
;
110 mrq
->data
->error
= 0;
111 mrq
->data
->mrq
= mrq
;
113 mrq
->data
->stop
= mrq
->stop
;
114 mrq
->stop
->error
= 0;
115 mrq
->stop
->mrq
= mrq
;
118 host
->ops
->request(host
, mrq
);
121 EXPORT_SYMBOL(mmc_start_request
);
123 static void mmc_wait_done(struct mmc_request
*mrq
)
125 complete(mrq
->done_data
);
128 int mmc_wait_for_req(struct mmc_host
*host
, struct mmc_request
*mrq
)
130 DECLARE_COMPLETION(complete
);
132 mrq
->done_data
= &complete
;
133 mrq
->done
= mmc_wait_done
;
135 mmc_start_request(host
, mrq
);
137 wait_for_completion(&complete
);
142 EXPORT_SYMBOL(mmc_wait_for_req
);
145 * mmc_wait_for_cmd - start a command and wait for completion
146 * @host: MMC host to start command
147 * @cmd: MMC command to start
148 * @retries: maximum number of retries
150 * Start a new MMC command for a host, and wait for the command
151 * to complete. Return any error that occurred while the command
152 * was executing. Do not attempt to parse the response.
154 int mmc_wait_for_cmd(struct mmc_host
*host
, struct mmc_command
*cmd
, int retries
)
156 struct mmc_request mrq
;
158 BUG_ON(host
->card_busy
== NULL
);
160 memset(&mrq
, 0, sizeof(struct mmc_request
));
162 memset(cmd
->resp
, 0, sizeof(cmd
->resp
));
163 cmd
->retries
= retries
;
168 mmc_wait_for_req(host
, &mrq
);
173 EXPORT_SYMBOL(mmc_wait_for_cmd
);
176 * mmc_wait_for_app_cmd - start an application command and wait for
178 * @host: MMC host to start command
179 * @rca: RCA to send MMC_APP_CMD to
180 * @cmd: MMC command to start
181 * @retries: maximum number of retries
183 * Sends a MMC_APP_CMD, checks the card response, sends the command
184 * in the parameter and waits for it to complete. Return any error
185 * that occurred while the command was executing. Do not attempt to
186 * parse the response.
188 int mmc_wait_for_app_cmd(struct mmc_host
*host
, unsigned int rca
,
189 struct mmc_command
*cmd
, int retries
)
191 struct mmc_request mrq
;
192 struct mmc_command appcmd
;
196 BUG_ON(host
->card_busy
== NULL
);
199 err
= MMC_ERR_INVALID
;
202 * We have to resend MMC_APP_CMD for each attempt so
203 * we cannot use the retries field in mmc_command.
205 for (i
= 0;i
<= retries
;i
++) {
206 memset(&mrq
, 0, sizeof(struct mmc_request
));
208 appcmd
.opcode
= MMC_APP_CMD
;
209 appcmd
.arg
= rca
<< 16;
210 appcmd
.flags
= MMC_RSP_R1
;
212 memset(appcmd
.resp
, 0, sizeof(appcmd
.resp
));
218 mmc_wait_for_req(host
, &mrq
);
225 /* Check that card supported application commands */
226 if (!(appcmd
.resp
[0] & R1_APP_CMD
))
227 return MMC_ERR_FAILED
;
229 memset(&mrq
, 0, sizeof(struct mmc_request
));
231 memset(cmd
->resp
, 0, sizeof(cmd
->resp
));
237 mmc_wait_for_req(host
, &mrq
);
240 if (cmd
->error
== MMC_ERR_NONE
)
247 EXPORT_SYMBOL(mmc_wait_for_app_cmd
);
250 * __mmc_claim_host - exclusively claim a host
251 * @host: mmc host to claim
252 * @card: mmc card to claim host for
254 * Claim a host for a set of operations. If a valid card
255 * is passed and this wasn't the last card selected, select
256 * the card before returning.
258 * Note: you should use mmc_card_claim_host or mmc_claim_host.
260 int __mmc_claim_host(struct mmc_host
*host
, struct mmc_card
*card
)
262 DECLARE_WAITQUEUE(wait
, current
);
266 add_wait_queue(&host
->wq
, &wait
);
267 spin_lock_irqsave(&host
->lock
, flags
);
269 set_current_state(TASK_UNINTERRUPTIBLE
);
270 if (host
->card_busy
== NULL
)
272 spin_unlock_irqrestore(&host
->lock
, flags
);
274 spin_lock_irqsave(&host
->lock
, flags
);
276 set_current_state(TASK_RUNNING
);
277 host
->card_busy
= card
;
278 spin_unlock_irqrestore(&host
->lock
, flags
);
279 remove_wait_queue(&host
->wq
, &wait
);
281 if (card
!= (void *)-1 && host
->card_selected
!= card
) {
282 struct mmc_command cmd
;
284 host
->card_selected
= card
;
286 cmd
.opcode
= MMC_SELECT_CARD
;
287 cmd
.arg
= card
->rca
<< 16;
288 cmd
.flags
= MMC_RSP_R1
;
290 err
= mmc_wait_for_cmd(host
, &cmd
, CMD_RETRIES
);
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
)
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
);
318 EXPORT_SYMBOL(mmc_release_host
);
321 * Ensure that no card is selected.
323 static void mmc_deselect_cards(struct mmc_host
*host
)
325 struct mmc_command cmd
;
327 if (host
->card_selected
) {
328 host
->card_selected
= NULL
;
330 cmd
.opcode
= MMC_SELECT_CARD
;
332 cmd
.flags
= MMC_RSP_NONE
;
334 mmc_wait_for_cmd(host
, &cmd
, 0);
339 static inline void mmc_delay(unsigned int ms
)
341 if (ms
< HZ
/ 1000) {
345 msleep_interruptible (ms
);
350 * Mask off any voltages we don't support and select
353 static u32
mmc_select_voltage(struct mmc_host
*host
, u32 ocr
)
357 ocr
&= host
->ocr_avail
;
366 host
->ops
->set_ios(host
, &host
->ios
);
374 #define UNSTUFF_BITS(resp,start,size) \
376 const int __size = size; \
377 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
378 const int __off = 3 - ((start) / 32); \
379 const int __shft = (start) & 31; \
382 __res = resp[__off] >> __shft; \
383 if (__size + __shft > 32) \
384 __res |= resp[__off-1] << ((32 - __shft) % 32); \
389 * Given the decoded CSD structure, decode the raw CID to our CID structure.
391 static void mmc_decode_cid(struct mmc_card
*card
)
393 u32
*resp
= card
->raw_cid
;
395 memset(&card
->cid
, 0, sizeof(struct mmc_cid
));
397 if (mmc_card_sd(card
)) {
399 * SD doesn't currently have a version field so we will
400 * have to assume we can parse this.
402 card
->cid
.manfid
= UNSTUFF_BITS(resp
, 120, 8);
403 card
->cid
.oemid
= UNSTUFF_BITS(resp
, 104, 16);
404 card
->cid
.prod_name
[0] = UNSTUFF_BITS(resp
, 96, 8);
405 card
->cid
.prod_name
[1] = UNSTUFF_BITS(resp
, 88, 8);
406 card
->cid
.prod_name
[2] = UNSTUFF_BITS(resp
, 80, 8);
407 card
->cid
.prod_name
[3] = UNSTUFF_BITS(resp
, 72, 8);
408 card
->cid
.prod_name
[4] = UNSTUFF_BITS(resp
, 64, 8);
409 card
->cid
.hwrev
= UNSTUFF_BITS(resp
, 60, 4);
410 card
->cid
.fwrev
= UNSTUFF_BITS(resp
, 56, 4);
411 card
->cid
.serial
= UNSTUFF_BITS(resp
, 24, 32);
412 card
->cid
.year
= UNSTUFF_BITS(resp
, 12, 8);
413 card
->cid
.month
= UNSTUFF_BITS(resp
, 8, 4);
415 card
->cid
.year
+= 2000; /* SD cards year offset */
418 * The selection of the format here is based upon published
419 * specs from sandisk and from what people have reported.
421 switch (card
->csd
.mmca_vsn
) {
422 case 0: /* MMC v1.0 - v1.2 */
423 case 1: /* MMC v1.4 */
424 card
->cid
.manfid
= UNSTUFF_BITS(resp
, 104, 24);
425 card
->cid
.prod_name
[0] = UNSTUFF_BITS(resp
, 96, 8);
426 card
->cid
.prod_name
[1] = UNSTUFF_BITS(resp
, 88, 8);
427 card
->cid
.prod_name
[2] = UNSTUFF_BITS(resp
, 80, 8);
428 card
->cid
.prod_name
[3] = UNSTUFF_BITS(resp
, 72, 8);
429 card
->cid
.prod_name
[4] = UNSTUFF_BITS(resp
, 64, 8);
430 card
->cid
.prod_name
[5] = UNSTUFF_BITS(resp
, 56, 8);
431 card
->cid
.prod_name
[6] = UNSTUFF_BITS(resp
, 48, 8);
432 card
->cid
.hwrev
= UNSTUFF_BITS(resp
, 44, 4);
433 card
->cid
.fwrev
= UNSTUFF_BITS(resp
, 40, 4);
434 card
->cid
.serial
= UNSTUFF_BITS(resp
, 16, 24);
435 card
->cid
.month
= UNSTUFF_BITS(resp
, 12, 4);
436 card
->cid
.year
= UNSTUFF_BITS(resp
, 8, 4) + 1997;
439 case 2: /* MMC v2.0 - v2.2 */
440 case 3: /* MMC v3.1 - v3.3 */
441 card
->cid
.manfid
= UNSTUFF_BITS(resp
, 120, 8);
442 card
->cid
.oemid
= UNSTUFF_BITS(resp
, 104, 16);
443 card
->cid
.prod_name
[0] = UNSTUFF_BITS(resp
, 96, 8);
444 card
->cid
.prod_name
[1] = UNSTUFF_BITS(resp
, 88, 8);
445 card
->cid
.prod_name
[2] = UNSTUFF_BITS(resp
, 80, 8);
446 card
->cid
.prod_name
[3] = UNSTUFF_BITS(resp
, 72, 8);
447 card
->cid
.prod_name
[4] = UNSTUFF_BITS(resp
, 64, 8);
448 card
->cid
.prod_name
[5] = UNSTUFF_BITS(resp
, 56, 8);
449 card
->cid
.serial
= UNSTUFF_BITS(resp
, 16, 32);
450 card
->cid
.month
= UNSTUFF_BITS(resp
, 12, 4);
451 card
->cid
.year
= UNSTUFF_BITS(resp
, 8, 4) + 1997;
455 printk("%s: card has unknown MMCA version %d\n",
456 mmc_hostname(card
->host
), card
->csd
.mmca_vsn
);
457 mmc_card_set_bad(card
);
464 * Given a 128-bit response, decode to our card CSD structure.
466 static void mmc_decode_csd(struct mmc_card
*card
)
468 struct mmc_csd
*csd
= &card
->csd
;
469 unsigned int e
, m
, csd_struct
;
470 u32
*resp
= card
->raw_csd
;
472 if (mmc_card_sd(card
)) {
473 csd_struct
= UNSTUFF_BITS(resp
, 126, 2);
474 if (csd_struct
!= 0) {
475 printk("%s: unrecognised CSD structure version %d\n",
476 mmc_hostname(card
->host
), csd_struct
);
477 mmc_card_set_bad(card
);
481 m
= UNSTUFF_BITS(resp
, 115, 4);
482 e
= UNSTUFF_BITS(resp
, 112, 3);
483 csd
->tacc_ns
= (tacc_exp
[e
] * tacc_mant
[m
] + 9) / 10;
484 csd
->tacc_clks
= UNSTUFF_BITS(resp
, 104, 8) * 100;
486 m
= UNSTUFF_BITS(resp
, 99, 4);
487 e
= UNSTUFF_BITS(resp
, 96, 3);
488 csd
->max_dtr
= tran_exp
[e
] * tran_mant
[m
];
489 csd
->cmdclass
= UNSTUFF_BITS(resp
, 84, 12);
491 e
= UNSTUFF_BITS(resp
, 47, 3);
492 m
= UNSTUFF_BITS(resp
, 62, 12);
493 csd
->capacity
= (1 + m
) << (e
+ 2);
495 csd
->read_blkbits
= UNSTUFF_BITS(resp
, 80, 4);
498 * We only understand CSD structure v1.1 and v1.2.
499 * v1.2 has extra information in bits 15, 11 and 10.
501 csd_struct
= UNSTUFF_BITS(resp
, 126, 2);
502 if (csd_struct
!= 1 && csd_struct
!= 2) {
503 printk("%s: unrecognised CSD structure version %d\n",
504 mmc_hostname(card
->host
), csd_struct
);
505 mmc_card_set_bad(card
);
509 csd
->mmca_vsn
= UNSTUFF_BITS(resp
, 122, 4);
510 m
= UNSTUFF_BITS(resp
, 115, 4);
511 e
= UNSTUFF_BITS(resp
, 112, 3);
512 csd
->tacc_ns
= (tacc_exp
[e
] * tacc_mant
[m
] + 9) / 10;
513 csd
->tacc_clks
= UNSTUFF_BITS(resp
, 104, 8) * 100;
515 m
= UNSTUFF_BITS(resp
, 99, 4);
516 e
= UNSTUFF_BITS(resp
, 96, 3);
517 csd
->max_dtr
= tran_exp
[e
] * tran_mant
[m
];
518 csd
->cmdclass
= UNSTUFF_BITS(resp
, 84, 12);
520 e
= UNSTUFF_BITS(resp
, 47, 3);
521 m
= UNSTUFF_BITS(resp
, 62, 12);
522 csd
->capacity
= (1 + m
) << (e
+ 2);
524 csd
->read_blkbits
= UNSTUFF_BITS(resp
, 80, 4);
529 * Locate a MMC card on this MMC host given a raw CID.
531 static struct mmc_card
*mmc_find_card(struct mmc_host
*host
, u32
*raw_cid
)
533 struct mmc_card
*card
;
535 list_for_each_entry(card
, &host
->cards
, node
) {
536 if (memcmp(card
->raw_cid
, raw_cid
, sizeof(card
->raw_cid
)) == 0)
543 * Allocate a new MMC card, and assign a unique RCA.
545 static struct mmc_card
*
546 mmc_alloc_card(struct mmc_host
*host
, u32
*raw_cid
, unsigned int *frca
)
548 struct mmc_card
*card
, *c
;
549 unsigned int rca
= *frca
;
551 card
= kmalloc(sizeof(struct mmc_card
), GFP_KERNEL
);
553 return ERR_PTR(-ENOMEM
);
555 mmc_init_card(card
, host
);
556 memcpy(card
->raw_cid
, raw_cid
, sizeof(card
->raw_cid
));
559 list_for_each_entry(c
, &host
->cards
, node
)
573 * Tell attached cards to go to IDLE state
575 static void mmc_idle_cards(struct mmc_host
*host
)
577 struct mmc_command cmd
;
579 host
->ios
.chip_select
= MMC_CS_HIGH
;
580 host
->ops
->set_ios(host
, &host
->ios
);
584 cmd
.opcode
= MMC_GO_IDLE_STATE
;
586 cmd
.flags
= MMC_RSP_NONE
;
588 mmc_wait_for_cmd(host
, &cmd
, 0);
592 host
->ios
.chip_select
= MMC_CS_DONTCARE
;
593 host
->ops
->set_ios(host
, &host
->ios
);
599 * Apply power to the MMC stack.
601 static void mmc_power_up(struct mmc_host
*host
)
603 int bit
= fls(host
->ocr_avail
) - 1;
606 host
->ios
.bus_mode
= MMC_BUSMODE_OPENDRAIN
;
607 host
->ios
.chip_select
= MMC_CS_DONTCARE
;
608 host
->ios
.power_mode
= MMC_POWER_UP
;
609 host
->ops
->set_ios(host
, &host
->ios
);
613 host
->ios
.clock
= host
->f_min
;
614 host
->ios
.power_mode
= MMC_POWER_ON
;
615 host
->ops
->set_ios(host
, &host
->ios
);
620 static void mmc_power_off(struct mmc_host
*host
)
624 host
->ios
.bus_mode
= MMC_BUSMODE_OPENDRAIN
;
625 host
->ios
.chip_select
= MMC_CS_DONTCARE
;
626 host
->ios
.power_mode
= MMC_POWER_OFF
;
627 host
->ops
->set_ios(host
, &host
->ios
);
630 static int mmc_send_op_cond(struct mmc_host
*host
, u32 ocr
, u32
*rocr
)
632 struct mmc_command cmd
;
635 cmd
.opcode
= MMC_SEND_OP_COND
;
637 cmd
.flags
= MMC_RSP_R3
;
639 for (i
= 100; i
; i
--) {
640 err
= mmc_wait_for_cmd(host
, &cmd
, 0);
641 if (err
!= MMC_ERR_NONE
)
644 if (cmd
.resp
[0] & MMC_CARD_BUSY
|| ocr
== 0)
647 err
= MMC_ERR_TIMEOUT
;
658 static int mmc_send_app_op_cond(struct mmc_host
*host
, u32 ocr
, u32
*rocr
)
660 struct mmc_command cmd
;
663 cmd
.opcode
= SD_APP_OP_COND
;
665 cmd
.flags
= MMC_RSP_R3
;
667 for (i
= 100; i
; i
--) {
668 err
= mmc_wait_for_app_cmd(host
, 0, &cmd
, CMD_RETRIES
);
669 if (err
!= MMC_ERR_NONE
)
672 if (cmd
.resp
[0] & MMC_CARD_BUSY
|| ocr
== 0)
675 err
= MMC_ERR_TIMEOUT
;
687 * Discover cards by requesting their CID. If this command
688 * times out, it is not an error; there are no further cards
689 * to be discovered. Add new cards to the list.
691 * Create a mmc_card entry for each discovered card, assigning
692 * it an RCA, and save the raw CID for decoding later.
694 static void mmc_discover_cards(struct mmc_host
*host
)
696 struct mmc_card
*card
;
697 unsigned int first_rca
= 1, err
;
700 struct mmc_command cmd
;
702 cmd
.opcode
= MMC_ALL_SEND_CID
;
704 cmd
.flags
= MMC_RSP_R2
;
706 err
= mmc_wait_for_cmd(host
, &cmd
, CMD_RETRIES
);
707 if (err
== MMC_ERR_TIMEOUT
) {
711 if (err
!= MMC_ERR_NONE
) {
712 printk(KERN_ERR
"%s: error requesting CID: %d\n",
713 mmc_hostname(host
), err
);
717 card
= mmc_find_card(host
, cmd
.resp
);
719 card
= mmc_alloc_card(host
, cmd
.resp
, &first_rca
);
724 list_add(&card
->node
, &host
->cards
);
727 card
->state
&= ~MMC_STATE_DEAD
;
729 if (host
->mode
== MMC_MODE_SD
) {
730 mmc_card_set_sd(card
);
732 cmd
.opcode
= SD_SEND_RELATIVE_ADDR
;
734 cmd
.flags
= MMC_RSP_R1
;
736 err
= mmc_wait_for_cmd(host
, &cmd
, CMD_RETRIES
);
737 if (err
!= MMC_ERR_NONE
)
738 mmc_card_set_dead(card
);
740 card
->rca
= cmd
.resp
[0] >> 16;
742 if (!host
->ops
->get_ro
) {
743 printk(KERN_WARNING
"%s: host does not "
744 "support reading read-only "
745 "switch. assuming write-enable.\n",
748 if (host
->ops
->get_ro(host
))
749 mmc_card_set_readonly(card
);
753 cmd
.opcode
= MMC_SET_RELATIVE_ADDR
;
754 cmd
.arg
= card
->rca
<< 16;
755 cmd
.flags
= MMC_RSP_R1
;
757 err
= mmc_wait_for_cmd(host
, &cmd
, CMD_RETRIES
);
758 if (err
!= MMC_ERR_NONE
)
759 mmc_card_set_dead(card
);
764 static void mmc_read_csds(struct mmc_host
*host
)
766 struct mmc_card
*card
;
768 list_for_each_entry(card
, &host
->cards
, node
) {
769 struct mmc_command cmd
;
772 if (card
->state
& (MMC_STATE_DEAD
|MMC_STATE_PRESENT
))
775 cmd
.opcode
= MMC_SEND_CSD
;
776 cmd
.arg
= card
->rca
<< 16;
777 cmd
.flags
= MMC_RSP_R2
;
779 err
= mmc_wait_for_cmd(host
, &cmd
, CMD_RETRIES
);
780 if (err
!= MMC_ERR_NONE
) {
781 mmc_card_set_dead(card
);
785 memcpy(card
->raw_csd
, cmd
.resp
, sizeof(card
->raw_csd
));
787 mmc_decode_csd(card
);
788 mmc_decode_cid(card
);
792 static unsigned int mmc_calculate_clock(struct mmc_host
*host
)
794 struct mmc_card
*card
;
795 unsigned int max_dtr
= host
->f_max
;
797 list_for_each_entry(card
, &host
->cards
, node
)
798 if (!mmc_card_dead(card
) && max_dtr
> card
->csd
.max_dtr
)
799 max_dtr
= card
->csd
.max_dtr
;
801 DBG("MMC: selected %d.%03dMHz transfer rate\n",
802 max_dtr
/ 1000000, (max_dtr
/ 1000) % 1000);
808 * Check whether cards we already know about are still present.
809 * We do this by requesting status, and checking whether a card
812 * A request for status does not cause a state change in data
815 static void mmc_check_cards(struct mmc_host
*host
)
817 struct list_head
*l
, *n
;
819 mmc_deselect_cards(host
);
821 list_for_each_safe(l
, n
, &host
->cards
) {
822 struct mmc_card
*card
= mmc_list_to_card(l
);
823 struct mmc_command cmd
;
826 cmd
.opcode
= MMC_SEND_STATUS
;
827 cmd
.arg
= card
->rca
<< 16;
828 cmd
.flags
= MMC_RSP_R1
;
830 err
= mmc_wait_for_cmd(host
, &cmd
, CMD_RETRIES
);
831 if (err
== MMC_ERR_NONE
)
834 mmc_card_set_dead(card
);
838 static void mmc_setup(struct mmc_host
*host
)
840 if (host
->ios
.power_mode
!= MMC_POWER_ON
) {
844 host
->mode
= MMC_MODE_SD
;
847 mmc_idle_cards(host
);
849 err
= mmc_send_app_op_cond(host
, 0, &ocr
);
852 * If we fail to detect any SD cards then try
853 * searching for MMC cards.
855 if (err
!= MMC_ERR_NONE
) {
856 host
->mode
= MMC_MODE_MMC
;
858 err
= mmc_send_op_cond(host
, 0, &ocr
);
859 if (err
!= MMC_ERR_NONE
)
863 host
->ocr
= mmc_select_voltage(host
, ocr
);
866 * Since we're changing the OCR value, we seem to
867 * need to tell some cards to go back to the idle
868 * state. We wait 1ms to give cards time to
872 mmc_idle_cards(host
);
874 host
->ios
.bus_mode
= MMC_BUSMODE_OPENDRAIN
;
875 host
->ios
.clock
= host
->f_min
;
876 host
->ops
->set_ios(host
, &host
->ios
);
879 * We should remember the OCR mask from the existing
880 * cards, and detect the new cards OCR mask, combine
881 * the two and re-select the VDD. However, if we do
882 * change VDD, we should do an idle, and then do a
883 * full re-initialisation. We would need to notify
884 * drivers so that they can re-setup the cards as
885 * well, while keeping their queues at bay.
887 * For the moment, we take the easy way out - if the
888 * new cards don't like our currently selected VDD,
889 * they drop off the bus.
897 * Send the selected OCR multiple times... until the cards
898 * all get the idea that they should be ready for CMD2.
899 * (My SanDisk card seems to need this.)
901 if (host
->mode
== MMC_MODE_SD
)
902 mmc_send_app_op_cond(host
, host
->ocr
, NULL
);
904 mmc_send_op_cond(host
, host
->ocr
, NULL
);
906 mmc_discover_cards(host
);
909 * Ok, now switch to push-pull mode.
911 host
->ios
.bus_mode
= MMC_BUSMODE_PUSHPULL
;
912 host
->ops
->set_ios(host
, &host
->ios
);
919 * mmc_detect_change - process change of state on a MMC socket
920 * @host: host which changed state.
922 * All we know is that card(s) have been inserted or removed
923 * from the socket(s). We don't know which socket or cards.
925 void mmc_detect_change(struct mmc_host
*host
)
927 schedule_work(&host
->detect
);
930 EXPORT_SYMBOL(mmc_detect_change
);
933 static void mmc_rescan(void *data
)
935 struct mmc_host
*host
= data
;
936 struct list_head
*l
, *n
;
938 mmc_claim_host(host
);
940 if (host
->ios
.power_mode
== MMC_POWER_ON
)
941 mmc_check_cards(host
);
945 if (!list_empty(&host
->cards
)) {
947 * (Re-)calculate the fastest clock rate which the
948 * attached cards and the host support.
950 host
->ios
.clock
= mmc_calculate_clock(host
);
951 host
->ops
->set_ios(host
, &host
->ios
);
954 mmc_release_host(host
);
956 list_for_each_safe(l
, n
, &host
->cards
) {
957 struct mmc_card
*card
= mmc_list_to_card(l
);
960 * If this is a new and good card, register it.
962 if (!mmc_card_present(card
) && !mmc_card_dead(card
)) {
963 if (mmc_register_card(card
))
964 mmc_card_set_dead(card
);
966 mmc_card_set_present(card
);
970 * If this card is dead, destroy it.
972 if (mmc_card_dead(card
)) {
973 list_del(&card
->node
);
974 mmc_remove_card(card
);
979 * If we discover that there are no cards on the
980 * bus, turn off the clock and power down.
982 if (list_empty(&host
->cards
))
988 * mmc_alloc_host - initialise the per-host structure.
989 * @extra: sizeof private data structure
990 * @dev: pointer to host device model structure
992 * Initialise the per-host structure.
994 struct mmc_host
*mmc_alloc_host(int extra
, struct device
*dev
)
996 struct mmc_host
*host
;
998 host
= mmc_alloc_host_sysfs(extra
, dev
);
1000 spin_lock_init(&host
->lock
);
1001 init_waitqueue_head(&host
->wq
);
1002 INIT_LIST_HEAD(&host
->cards
);
1003 INIT_WORK(&host
->detect
, mmc_rescan
, host
);
1006 * By default, hosts do not support SGIO or large requests.
1007 * They have to set these according to their abilities.
1009 host
->max_hw_segs
= 1;
1010 host
->max_phys_segs
= 1;
1011 host
->max_sectors
= 1 << (PAGE_CACHE_SHIFT
- 9);
1012 host
->max_seg_size
= PAGE_CACHE_SIZE
;
1018 EXPORT_SYMBOL(mmc_alloc_host
);
1021 * mmc_add_host - initialise host hardware
1024 int mmc_add_host(struct mmc_host
*host
)
1028 ret
= mmc_add_host_sysfs(host
);
1030 mmc_power_off(host
);
1031 mmc_detect_change(host
);
1037 EXPORT_SYMBOL(mmc_add_host
);
1040 * mmc_remove_host - remove host hardware
1043 * Unregister and remove all cards associated with this host,
1044 * and power down the MMC bus.
1046 void mmc_remove_host(struct mmc_host
*host
)
1048 struct list_head
*l
, *n
;
1050 list_for_each_safe(l
, n
, &host
->cards
) {
1051 struct mmc_card
*card
= mmc_list_to_card(l
);
1053 mmc_remove_card(card
);
1056 mmc_power_off(host
);
1057 mmc_remove_host_sysfs(host
);
1060 EXPORT_SYMBOL(mmc_remove_host
);
1063 * mmc_free_host - free the host structure
1066 * Free the host once all references to it have been dropped.
1068 void mmc_free_host(struct mmc_host
*host
)
1070 flush_scheduled_work();
1071 mmc_free_host_sysfs(host
);
1074 EXPORT_SYMBOL(mmc_free_host
);
1079 * mmc_suspend_host - suspend a host
1081 * @state: suspend mode (PM_SUSPEND_xxx)
1083 int mmc_suspend_host(struct mmc_host
*host
, pm_message_t state
)
1085 mmc_claim_host(host
);
1086 mmc_deselect_cards(host
);
1087 mmc_power_off(host
);
1088 mmc_release_host(host
);
1093 EXPORT_SYMBOL(mmc_suspend_host
);
1096 * mmc_resume_host - resume a previously suspended host
1099 int mmc_resume_host(struct mmc_host
*host
)
1101 mmc_detect_change(host
);
1106 EXPORT_SYMBOL(mmc_resume_host
);
1110 MODULE_LICENSE("GPL");