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);
178 * __mmc_claim_host - exclusively claim a host
179 * @host: mmc host to claim
180 * @card: mmc card to claim host for
182 * Claim a host for a set of operations. If a valid card
183 * is passed and this wasn't the last card selected, select
184 * the card before returning.
186 * Note: you should use mmc_card_claim_host or mmc_claim_host.
188 int __mmc_claim_host(struct mmc_host *host, struct mmc_card *card)
190 DECLARE_WAITQUEUE(wait, current);
194 add_wait_queue(&host->wq, &wait);
195 spin_lock_irqsave(&host->lock, flags);
197 set_current_state(TASK_UNINTERRUPTIBLE);
198 if (host->card_busy == NULL)
200 spin_unlock_irqrestore(&host->lock, flags);
202 spin_lock_irqsave(&host->lock, flags);
204 set_current_state(TASK_RUNNING);
205 host->card_busy = card;
206 spin_unlock_irqrestore(&host->lock, flags);
207 remove_wait_queue(&host->wq, &wait);
209 if (card != (void *)-1 && host->card_selected != card) {
210 struct mmc_command cmd;
212 host->card_selected = card;
214 cmd.opcode = MMC_SELECT_CARD;
215 cmd.arg = card->rca << 16;
216 cmd.flags = MMC_RSP_R1;
218 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
224 EXPORT_SYMBOL(__mmc_claim_host);
227 * mmc_release_host - release a host
228 * @host: mmc host to release
230 * Release a MMC host, allowing others to claim the host
231 * for their operations.
233 void mmc_release_host(struct mmc_host *host)
237 BUG_ON(host->card_busy == NULL);
239 spin_lock_irqsave(&host->lock, flags);
240 host->card_busy = NULL;
241 spin_unlock_irqrestore(&host->lock, flags);
246 EXPORT_SYMBOL(mmc_release_host);
249 * Ensure that no card is selected.
251 static void mmc_deselect_cards(struct mmc_host *host)
253 struct mmc_command cmd;
255 if (host->card_selected) {
256 host->card_selected = NULL;
258 cmd.opcode = MMC_SELECT_CARD;
260 cmd.flags = MMC_RSP_NONE;
262 mmc_wait_for_cmd(host, &cmd, 0);
267 static inline void mmc_delay(unsigned int ms)
269 if (ms < HZ / 1000) {
273 msleep_interruptible (ms);
278 * Mask off any voltages we don't support and select
281 static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
285 ocr &= host->ocr_avail;
294 host->ops->set_ios(host, &host->ios);
302 #define UNSTUFF_BITS(resp,start,size) \
304 const u32 __mask = (1 << (size)) - 1; \
305 const int __off = 3 - ((start) / 32); \
306 const int __shft = (start) & 31; \
309 __res = resp[__off] >> __shft; \
310 if ((size) + __shft >= 32) \
311 __res |= resp[__off-1] << (32 - __shft); \
316 * Given the decoded CSD structure, decode the raw CID to our CID structure.
318 static void mmc_decode_cid(struct mmc_card *card)
320 u32 *resp = card->raw_cid;
322 memset(&card->cid, 0, sizeof(struct mmc_cid));
325 * The selection of the format here is guesswork based upon
326 * information people have sent to date.
328 switch (card->csd.mmca_vsn) {
329 case 0: /* MMC v1.? */
330 case 1: /* MMC v1.4 */
331 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
332 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
333 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
334 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
335 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
336 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
337 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
338 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
339 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
340 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
341 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
342 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
343 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
346 case 2: /* MMC v2.x ? */
347 case 3: /* MMC v3.x ? */
348 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
349 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
350 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
351 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
352 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
353 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
354 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
355 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
356 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
357 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
358 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
362 printk("%s: card has unknown MMCA version %d\n",
363 card->host->host_name, card->csd.mmca_vsn);
364 mmc_card_set_bad(card);
370 * Given a 128-bit response, decode to our card CSD structure.
372 static void mmc_decode_csd(struct mmc_card *card)
374 struct mmc_csd *csd = &card->csd;
375 unsigned int e, m, csd_struct;
376 u32 *resp = card->raw_csd;
379 * We only understand CSD structure v1.1 and v2.
380 * v2 has extra information in bits 15, 11 and 10.
382 csd_struct = UNSTUFF_BITS(resp, 126, 2);
383 if (csd_struct != 1 && csd_struct != 2) {
384 printk("%s: unrecognised CSD structure version %d\n",
385 card->host->host_name, csd_struct);
386 mmc_card_set_bad(card);
390 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
391 m = UNSTUFF_BITS(resp, 115, 4);
392 e = UNSTUFF_BITS(resp, 112, 3);
393 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
394 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
396 m = UNSTUFF_BITS(resp, 99, 4);
397 e = UNSTUFF_BITS(resp, 96, 3);
398 csd->max_dtr = tran_exp[e] * tran_mant[m];
399 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
401 e = UNSTUFF_BITS(resp, 47, 3);
402 m = UNSTUFF_BITS(resp, 62, 12);
403 csd->capacity = (1 + m) << (e + 2);
405 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
409 * Locate a MMC card on this MMC host given a raw CID.
411 static struct mmc_card *mmc_find_card(struct mmc_host *host, u32 *raw_cid)
413 struct mmc_card *card;
415 list_for_each_entry(card, &host->cards, node) {
416 if (memcmp(card->raw_cid, raw_cid, sizeof(card->raw_cid)) == 0)
423 * Allocate a new MMC card, and assign a unique RCA.
425 static struct mmc_card *
426 mmc_alloc_card(struct mmc_host *host, u32 *raw_cid, unsigned int *frca)
428 struct mmc_card *card, *c;
429 unsigned int rca = *frca;
431 card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL);
433 return ERR_PTR(-ENOMEM);
435 mmc_init_card(card, host);
436 memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid));
439 list_for_each_entry(c, &host->cards, node)
453 * Tell attached cards to go to IDLE state
455 static void mmc_idle_cards(struct mmc_host *host)
457 struct mmc_command cmd;
459 cmd.opcode = MMC_GO_IDLE_STATE;
461 cmd.flags = MMC_RSP_NONE;
463 mmc_wait_for_cmd(host, &cmd, 0);
469 * Apply power to the MMC stack.
471 static void mmc_power_up(struct mmc_host *host)
473 int bit = fls(host->ocr_avail) - 1;
476 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
477 host->ios.power_mode = MMC_POWER_UP;
478 host->ops->set_ios(host, &host->ios);
482 host->ios.clock = host->f_min;
483 host->ios.power_mode = MMC_POWER_ON;
484 host->ops->set_ios(host, &host->ios);
489 static void mmc_power_off(struct mmc_host *host)
493 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
494 host->ios.power_mode = MMC_POWER_OFF;
495 host->ops->set_ios(host, &host->ios);
498 static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
500 struct mmc_command cmd;
503 cmd.opcode = MMC_SEND_OP_COND;
505 cmd.flags = MMC_RSP_R3;
507 for (i = 100; i; i--) {
508 err = mmc_wait_for_cmd(host, &cmd, 0);
509 if (err != MMC_ERR_NONE)
512 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
515 err = MMC_ERR_TIMEOUT;
527 * Discover cards by requesting their CID. If this command
528 * times out, it is not an error; there are no further cards
529 * to be discovered. Add new cards to the list.
531 * Create a mmc_card entry for each discovered card, assigning
532 * it an RCA, and save the raw CID for decoding later.
534 static void mmc_discover_cards(struct mmc_host *host)
536 struct mmc_card *card;
537 unsigned int first_rca = 1, err;
540 struct mmc_command cmd;
542 cmd.opcode = MMC_ALL_SEND_CID;
544 cmd.flags = MMC_RSP_R2;
546 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
547 if (err == MMC_ERR_TIMEOUT) {
551 if (err != MMC_ERR_NONE) {
552 printk(KERN_ERR "%s: error requesting CID: %d\n",
553 host->host_name, err);
557 card = mmc_find_card(host, cmd.resp);
559 card = mmc_alloc_card(host, cmd.resp, &first_rca);
564 list_add(&card->node, &host->cards);
567 card->state &= ~MMC_STATE_DEAD;
569 cmd.opcode = MMC_SET_RELATIVE_ADDR;
570 cmd.arg = card->rca << 16;
571 cmd.flags = MMC_RSP_R1;
573 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
574 if (err != MMC_ERR_NONE)
575 mmc_card_set_dead(card);
579 static void mmc_read_csds(struct mmc_host *host)
581 struct mmc_card *card;
583 list_for_each_entry(card, &host->cards, node) {
584 struct mmc_command cmd;
587 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
590 cmd.opcode = MMC_SEND_CSD;
591 cmd.arg = card->rca << 16;
592 cmd.flags = MMC_RSP_R2;
594 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
595 if (err != MMC_ERR_NONE) {
596 mmc_card_set_dead(card);
600 memcpy(card->raw_csd, cmd.resp, sizeof(card->raw_csd));
602 mmc_decode_csd(card);
603 mmc_decode_cid(card);
607 static unsigned int mmc_calculate_clock(struct mmc_host *host)
609 struct mmc_card *card;
610 unsigned int max_dtr = host->f_max;
612 list_for_each_entry(card, &host->cards, node)
613 if (!mmc_card_dead(card) && max_dtr > card->csd.max_dtr)
614 max_dtr = card->csd.max_dtr;
616 DBG("MMC: selected %d.%03dMHz transfer rate\n",
617 max_dtr / 1000000, (max_dtr / 1000) % 1000);
623 * Check whether cards we already know about are still present.
624 * We do this by requesting status, and checking whether a card
627 * A request for status does not cause a state change in data
630 static void mmc_check_cards(struct mmc_host *host)
632 struct list_head *l, *n;
634 mmc_deselect_cards(host);
636 list_for_each_safe(l, n, &host->cards) {
637 struct mmc_card *card = mmc_list_to_card(l);
638 struct mmc_command cmd;
641 cmd.opcode = MMC_SEND_STATUS;
642 cmd.arg = card->rca << 16;
643 cmd.flags = MMC_RSP_R1;
645 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
646 if (err == MMC_ERR_NONE)
649 mmc_card_set_dead(card);
653 static void mmc_setup(struct mmc_host *host)
655 if (host->ios.power_mode != MMC_POWER_ON) {
660 mmc_idle_cards(host);
662 err = mmc_send_op_cond(host, 0, &ocr);
663 if (err != MMC_ERR_NONE)
666 host->ocr = mmc_select_voltage(host, ocr);
669 * Since we're changing the OCR value, we seem to
670 * need to tell some cards to go back to the idle
671 * state. We wait 1ms to give cards time to
675 mmc_idle_cards(host);
677 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
678 host->ios.clock = host->f_min;
679 host->ops->set_ios(host, &host->ios);
682 * We should remember the OCR mask from the existing
683 * cards, and detect the new cards OCR mask, combine
684 * the two and re-select the VDD. However, if we do
685 * change VDD, we should do an idle, and then do a
686 * full re-initialisation. We would need to notify
687 * drivers so that they can re-setup the cards as
688 * well, while keeping their queues at bay.
690 * For the moment, we take the easy way out - if the
691 * new cards don't like our currently selected VDD,
692 * they drop off the bus.
700 * Send the selected OCR multiple times... until the cards
701 * all get the idea that they should be ready for CMD2.
702 * (My SanDisk card seems to need this.)
704 mmc_send_op_cond(host, host->ocr, NULL);
706 mmc_discover_cards(host);
709 * Ok, now switch to push-pull mode.
711 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
712 host->ops->set_ios(host, &host->ios);
719 * mmc_detect_change - process change of state on a MMC socket
720 * @host: host which changed state.
722 * All we know is that card(s) have been inserted or removed
723 * from the socket(s). We don't know which socket or cards.
725 void mmc_detect_change(struct mmc_host *host)
727 schedule_work(&host->detect);
730 EXPORT_SYMBOL(mmc_detect_change);
733 static void mmc_rescan(void *data)
735 struct mmc_host *host = data;
736 struct list_head *l, *n;
738 mmc_claim_host(host);
740 if (host->ios.power_mode == MMC_POWER_ON)
741 mmc_check_cards(host);
745 if (!list_empty(&host->cards)) {
747 * (Re-)calculate the fastest clock rate which the
748 * attached cards and the host support.
750 host->ios.clock = mmc_calculate_clock(host);
751 host->ops->set_ios(host, &host->ios);
754 mmc_release_host(host);
756 list_for_each_safe(l, n, &host->cards) {
757 struct mmc_card *card = mmc_list_to_card(l);
760 * If this is a new and good card, register it.
762 if (!mmc_card_present(card) && !mmc_card_dead(card)) {
763 if (mmc_register_card(card))
764 mmc_card_set_dead(card);
766 mmc_card_set_present(card);
770 * If this card is dead, destroy it.
772 if (mmc_card_dead(card)) {
773 list_del(&card->node);
774 mmc_remove_card(card);
779 * If we discover that there are no cards on the
780 * bus, turn off the clock and power down.
782 if (list_empty(&host->cards))
788 * mmc_alloc_host - initialise the per-host structure.
789 * @extra: sizeof private data structure
790 * @dev: pointer to host device model structure
792 * Initialise the per-host structure.
794 struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
796 struct mmc_host *host;
798 host = kmalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
800 memset(host, 0, sizeof(struct mmc_host) + extra);
802 spin_lock_init(&host->lock);
803 init_waitqueue_head(&host->wq);
804 INIT_LIST_HEAD(&host->cards);
805 INIT_WORK(&host->detect, mmc_rescan, host);
810 * By default, hosts do not support SGIO or large requests.
811 * They have to set these according to their abilities.
813 host->max_hw_segs = 1;
814 host->max_phys_segs = 1;
815 host->max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
816 host->max_seg_size = PAGE_CACHE_SIZE;
822 EXPORT_SYMBOL(mmc_alloc_host);
825 * mmc_add_host - initialise host hardware
828 int mmc_add_host(struct mmc_host *host)
830 static unsigned int host_num;
832 snprintf(host->host_name, sizeof(host->host_name),
833 "mmc%d", host_num++);
836 mmc_detect_change(host);
841 EXPORT_SYMBOL(mmc_add_host);
844 * mmc_remove_host - remove host hardware
847 * Unregister and remove all cards associated with this host,
848 * and power down the MMC bus.
850 void mmc_remove_host(struct mmc_host *host)
852 struct list_head *l, *n;
854 list_for_each_safe(l, n, &host->cards) {
855 struct mmc_card *card = mmc_list_to_card(l);
857 mmc_remove_card(card);
863 EXPORT_SYMBOL(mmc_remove_host);
866 * mmc_free_host - free the host structure
869 * Free the host once all references to it have been dropped.
871 void mmc_free_host(struct mmc_host *host)
873 flush_scheduled_work();
877 EXPORT_SYMBOL(mmc_free_host);
882 * mmc_suspend_host - suspend a host
884 * @state: suspend mode (PM_SUSPEND_xxx)
886 int mmc_suspend_host(struct mmc_host *host, u32 state)
888 mmc_claim_host(host);
889 mmc_deselect_cards(host);
891 mmc_release_host(host);
896 EXPORT_SYMBOL(mmc_suspend_host);
899 * mmc_resume_host - resume a previously suspended host
902 int mmc_resume_host(struct mmc_host *host)
904 mmc_detect_change(host);
909 EXPORT_SYMBOL(mmc_resume_host);
913 MODULE_LICENSE("GPL");