aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/mmc/mmc.c326
-rw-r--r--include/linux/mmc/card.h3
-rw-r--r--include/linux/mmc/host.h4
-rw-r--r--include/linux/mmc/mmc.h2
4 files changed, 262 insertions, 73 deletions
diff --git a/drivers/mmc/mmc.c b/drivers/mmc/mmc.c
index 0a8165974ba7..294961a102ca 100644
--- a/drivers/mmc/mmc.c
+++ b/drivers/mmc/mmc.c
@@ -172,7 +172,79 @@ int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries
172 172
173EXPORT_SYMBOL(mmc_wait_for_cmd); 173EXPORT_SYMBOL(mmc_wait_for_cmd);
174 174
175/**
176 * mmc_wait_for_app_cmd - start an application command and wait for
177 completion
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
182 *
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.
187 */
188int mmc_wait_for_app_cmd(struct mmc_host *host, unsigned int rca,
189 struct mmc_command *cmd, int retries)
190{
191 struct mmc_request mrq;
192 struct mmc_command appcmd;
193
194 int i, err;
195
196 BUG_ON(host->card_busy == NULL);
197 BUG_ON(retries < 0);
198
199 err = MMC_ERR_INVALID;
200
201 /*
202 * We have to resend MMC_APP_CMD for each attempt so
203 * we cannot use the retries field in mmc_command.
204 */
205 for (i = 0;i <= retries;i++) {
206 memset(&mrq, 0, sizeof(struct mmc_request));
207
208 appcmd.opcode = MMC_APP_CMD;
209 appcmd.arg = rca << 16;
210 appcmd.flags = MMC_RSP_R1;
211 appcmd.retries = 0;
212 memset(appcmd.resp, 0, sizeof(appcmd.resp));
213 appcmd.data = NULL;
214
215 mrq.cmd = &appcmd;
216 appcmd.data = NULL;
217
218 mmc_wait_for_req(host, &mrq);
219
220 if (appcmd.error) {
221 err = appcmd.error;
222 continue;
223 }
224
225 /* Check that card supported application commands */
226 if (!(appcmd.resp[0] & R1_APP_CMD))
227 return MMC_ERR_FAILED;
228
229 memset(&mrq, 0, sizeof(struct mmc_request));
230
231 memset(cmd->resp, 0, sizeof(cmd->resp));
232 cmd->retries = 0;
233
234 mrq.cmd = cmd;
235 cmd->data = NULL;
236
237 mmc_wait_for_req(host, &mrq);
175 238
239 err = cmd->error;
240 if (cmd->error == MMC_ERR_NONE)
241 break;
242 }
243
244 return err;
245}
246
247EXPORT_SYMBOL(mmc_wait_for_app_cmd);
176 248
177/** 249/**
178 * __mmc_claim_host - exclusively claim a host 250 * __mmc_claim_host - exclusively claim a host
@@ -322,48 +394,70 @@ static void mmc_decode_cid(struct mmc_card *card)
322 394
323 memset(&card->cid, 0, sizeof(struct mmc_cid)); 395 memset(&card->cid, 0, sizeof(struct mmc_cid));
324 396
325 /* 397 if (mmc_card_sd(card)) {
326 * The selection of the format here is guesswork based upon 398 /*
327 * information people have sent to date. 399 * SD doesn't currently have a version field so we will
328 */ 400 * have to assume we can parse this.
329 switch (card->csd.mmca_vsn) { 401 */
330 case 0: /* MMC v1.? */ 402 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
331 case 1: /* MMC v1.4 */ 403 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
332 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24); 404 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
333 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); 405 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
334 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); 406 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
335 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); 407 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
336 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); 408 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
337 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); 409 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
338 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); 410 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
339 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8); 411 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
340 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4); 412 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
341 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4); 413 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
342 card->cid.serial = UNSTUFF_BITS(resp, 16, 24); 414
343 card->cid.month = UNSTUFF_BITS(resp, 12, 4); 415 card->cid.year += 2000; /* SD cards year offset */
344 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; 416 }
345 break; 417 else {
346 418 /*
347 case 2: /* MMC v2.x ? */ 419 * The selection of the format here is based upon published
348 case 3: /* MMC v3.x ? */ 420 * specs from sandisk and from what people have reported.
349 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); 421 */
350 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); 422 switch (card->csd.mmca_vsn) {
351 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); 423 case 0: /* MMC v1.0 - v1.2 */
352 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); 424 case 1: /* MMC v1.4 */
353 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); 425 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
354 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); 426 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
355 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); 427 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
356 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); 428 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
357 card->cid.serial = UNSTUFF_BITS(resp, 16, 32); 429 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
358 card->cid.month = UNSTUFF_BITS(resp, 12, 4); 430 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
359 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; 431 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
360 break; 432 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
361 433 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
362 default: 434 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
363 printk("%s: card has unknown MMCA version %d\n", 435 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
364 mmc_hostname(card->host), card->csd.mmca_vsn); 436 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
365 mmc_card_set_bad(card); 437 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
366 break; 438 break;
439
440 case 2: /* MMC v2.0 - v2.2 */
441 case 3: /* MMC v3.1 - v3.3 */
442 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
443 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
444 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
445 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
446 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
447 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
448 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
449 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
450 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
451 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
452 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
453 break;
454
455 default:
456 printk("%s: card has unknown MMCA version %d\n",
457 mmc_hostname(card->host), card->csd.mmca_vsn);
458 mmc_card_set_bad(card);
459 break;
460 }
367 } 461 }
368} 462}
369 463
@@ -376,34 +470,61 @@ static void mmc_decode_csd(struct mmc_card *card)
376 unsigned int e, m, csd_struct; 470 unsigned int e, m, csd_struct;
377 u32 *resp = card->raw_csd; 471 u32 *resp = card->raw_csd;
378 472
379 /* 473 if (mmc_card_sd(card)) {
380 * We only understand CSD structure v1.1 and v2. 474 csd_struct = UNSTUFF_BITS(resp, 126, 2);
381 * v2 has extra information in bits 15, 11 and 10. 475 if (csd_struct != 0) {
382 */ 476 printk("%s: unrecognised CSD structure version %d\n",
383 csd_struct = UNSTUFF_BITS(resp, 126, 2); 477 mmc_hostname(card->host), csd_struct);
384 if (csd_struct != 1 && csd_struct != 2) { 478 mmc_card_set_bad(card);
385 printk("%s: unrecognised CSD structure version %d\n", 479 return;
386 mmc_hostname(card->host), csd_struct); 480 }
387 mmc_card_set_bad(card); 481
388 return; 482 m = UNSTUFF_BITS(resp, 115, 4);
483 e = UNSTUFF_BITS(resp, 112, 3);
484 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
485 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
486
487 m = UNSTUFF_BITS(resp, 99, 4);
488 e = UNSTUFF_BITS(resp, 96, 3);
489 csd->max_dtr = tran_exp[e] * tran_mant[m];
490 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
491
492 e = UNSTUFF_BITS(resp, 47, 3);
493 m = UNSTUFF_BITS(resp, 62, 12);
494 csd->capacity = (1 + m) << (e + 2);
495
496 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
389 } 497 }
498 else {
499 /*
500 * We only understand CSD structure v1.1 and v1.2.
501 * v1.2 has extra information in bits 15, 11 and 10.
502 */
503 csd_struct = UNSTUFF_BITS(resp, 126, 2);
504 if (csd_struct != 1 && csd_struct != 2) {
505 printk("%s: unrecognised CSD structure version %d\n",
506 mmc_hostname(card->host), csd_struct);
507 mmc_card_set_bad(card);
508 return;
509 }
390 510
391 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4); 511 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
392 m = UNSTUFF_BITS(resp, 115, 4); 512 m = UNSTUFF_BITS(resp, 115, 4);
393 e = UNSTUFF_BITS(resp, 112, 3); 513 e = UNSTUFF_BITS(resp, 112, 3);
394 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; 514 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
395 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; 515 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
396 516
397 m = UNSTUFF_BITS(resp, 99, 4); 517 m = UNSTUFF_BITS(resp, 99, 4);
398 e = UNSTUFF_BITS(resp, 96, 3); 518 e = UNSTUFF_BITS(resp, 96, 3);
399 csd->max_dtr = tran_exp[e] * tran_mant[m]; 519 csd->max_dtr = tran_exp[e] * tran_mant[m];
400 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); 520 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
401 521
402 e = UNSTUFF_BITS(resp, 47, 3); 522 e = UNSTUFF_BITS(resp, 47, 3);
403 m = UNSTUFF_BITS(resp, 62, 12); 523 m = UNSTUFF_BITS(resp, 62, 12);
404 csd->capacity = (1 + m) << (e + 2); 524 csd->capacity = (1 + m) << (e + 2);
405 525
406 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); 526 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
527 }
407} 528}
408 529
409/* 530/*
@@ -536,6 +657,34 @@ static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
536 return err; 657 return err;
537} 658}
538 659
660static int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
661{
662 struct mmc_command cmd;
663 int i, err = 0;
664
665 cmd.opcode = SD_APP_OP_COND;
666 cmd.arg = ocr;
667 cmd.flags = MMC_RSP_R3;
668
669 for (i = 100; i; i--) {
670 err = mmc_wait_for_app_cmd(host, 0, &cmd, CMD_RETRIES);
671 if (err != MMC_ERR_NONE)
672 break;
673
674 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
675 break;
676
677 err = MMC_ERR_TIMEOUT;
678
679 mmc_delay(10);
680 }
681
682 if (rocr)
683 *rocr = cmd.resp[0];
684
685 return err;
686}
687
539/* 688/*
540 * Discover cards by requesting their CID. If this command 689 * Discover cards by requesting their CID. If this command
541 * times out, it is not an error; there are no further cards 690 * times out, it is not an error; there are no further cards
@@ -579,13 +728,28 @@ static void mmc_discover_cards(struct mmc_host *host)
579 728
580 card->state &= ~MMC_STATE_DEAD; 729 card->state &= ~MMC_STATE_DEAD;
581 730
582 cmd.opcode = MMC_SET_RELATIVE_ADDR; 731 if (host->mode == MMC_MODE_SD) {
583 cmd.arg = card->rca << 16; 732 mmc_card_set_sd(card);
584 cmd.flags = MMC_RSP_R1;
585 733
586 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); 734 cmd.opcode = SD_SEND_RELATIVE_ADDR;
587 if (err != MMC_ERR_NONE) 735 cmd.arg = 0;
588 mmc_card_set_dead(card); 736 cmd.flags = MMC_RSP_R1;
737
738 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
739 if (err != MMC_ERR_NONE)
740 mmc_card_set_dead(card);
741 else
742 card->rca = cmd.resp[0] >> 16;
743 }
744 else {
745 cmd.opcode = MMC_SET_RELATIVE_ADDR;
746 cmd.arg = card->rca << 16;
747 cmd.flags = MMC_RSP_R1;
748
749 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
750 if (err != MMC_ERR_NONE)
751 mmc_card_set_dead(card);
752 }
589 } 753 }
590} 754}
591 755
@@ -669,12 +833,25 @@ static void mmc_setup(struct mmc_host *host)
669 int err; 833 int err;
670 u32 ocr; 834 u32 ocr;
671 835
836 host->mode = MMC_MODE_MMC;
837
672 mmc_power_up(host); 838 mmc_power_up(host);
673 mmc_idle_cards(host); 839 mmc_idle_cards(host);
674 840
675 err = mmc_send_op_cond(host, 0, &ocr); 841 err = mmc_send_op_cond(host, 0, &ocr);
842
843 /*
844 * If we fail to detect any cards then try
845 * searching for SD cards.
846 */
676 if (err != MMC_ERR_NONE) 847 if (err != MMC_ERR_NONE)
677 return; 848 {
849 err = mmc_send_app_op_cond(host, 0, &ocr);
850 if (err != MMC_ERR_NONE)
851 return;
852
853 host->mode = MMC_MODE_SD;
854 }
678 855
679 host->ocr = mmc_select_voltage(host, ocr); 856 host->ocr = mmc_select_voltage(host, ocr);
680 857
@@ -714,7 +891,10 @@ static void mmc_setup(struct mmc_host *host)
714 * all get the idea that they should be ready for CMD2. 891 * all get the idea that they should be ready for CMD2.
715 * (My SanDisk card seems to need this.) 892 * (My SanDisk card seems to need this.)
716 */ 893 */
717 mmc_send_op_cond(host, host->ocr, NULL); 894 if (host->mode == MMC_MODE_SD)
895 mmc_send_app_op_cond(host, host->ocr, NULL);
896 else
897 mmc_send_op_cond(host, host->ocr, NULL);
718 898
719 mmc_discover_cards(host); 899 mmc_discover_cards(host);
720 900
diff --git a/include/linux/mmc/card.h b/include/linux/mmc/card.h
index aefedf04b9bb..538e8c86336c 100644
--- a/include/linux/mmc/card.h
+++ b/include/linux/mmc/card.h
@@ -47,6 +47,7 @@ struct mmc_card {
47#define MMC_STATE_PRESENT (1<<0) /* present in sysfs */ 47#define MMC_STATE_PRESENT (1<<0) /* present in sysfs */
48#define MMC_STATE_DEAD (1<<1) /* device no longer in stack */ 48#define MMC_STATE_DEAD (1<<1) /* device no longer in stack */
49#define MMC_STATE_BAD (1<<2) /* unrecognised device */ 49#define MMC_STATE_BAD (1<<2) /* unrecognised device */
50#define MMC_STATE_SDCARD (1<<3) /* is an SD card */
50 u32 raw_cid[4]; /* raw card CID */ 51 u32 raw_cid[4]; /* raw card CID */
51 u32 raw_csd[4]; /* raw card CSD */ 52 u32 raw_csd[4]; /* raw card CSD */
52 struct mmc_cid cid; /* card identification */ 53 struct mmc_cid cid; /* card identification */
@@ -56,10 +57,12 @@ struct mmc_card {
56#define mmc_card_present(c) ((c)->state & MMC_STATE_PRESENT) 57#define mmc_card_present(c) ((c)->state & MMC_STATE_PRESENT)
57#define mmc_card_dead(c) ((c)->state & MMC_STATE_DEAD) 58#define mmc_card_dead(c) ((c)->state & MMC_STATE_DEAD)
58#define mmc_card_bad(c) ((c)->state & MMC_STATE_BAD) 59#define mmc_card_bad(c) ((c)->state & MMC_STATE_BAD)
60#define mmc_card_sd(c) ((c)->state & MMC_STATE_SDCARD)
59 61
60#define mmc_card_set_present(c) ((c)->state |= MMC_STATE_PRESENT) 62#define mmc_card_set_present(c) ((c)->state |= MMC_STATE_PRESENT)
61#define mmc_card_set_dead(c) ((c)->state |= MMC_STATE_DEAD) 63#define mmc_card_set_dead(c) ((c)->state |= MMC_STATE_DEAD)
62#define mmc_card_set_bad(c) ((c)->state |= MMC_STATE_BAD) 64#define mmc_card_set_bad(c) ((c)->state |= MMC_STATE_BAD)
65#define mmc_card_set_sd(c) ((c)->state |= MMC_STATE_SDCARD)
63 66
64#define mmc_card_name(c) ((c)->cid.prod_name) 67#define mmc_card_name(c) ((c)->cid.prod_name)
65#define mmc_card_id(c) ((c)->dev.bus_id) 68#define mmc_card_id(c) ((c)->dev.bus_id)
diff --git a/include/linux/mmc/host.h b/include/linux/mmc/host.h
index 30f68c0c8c6e..845020d90c60 100644
--- a/include/linux/mmc/host.h
+++ b/include/linux/mmc/host.h
@@ -87,6 +87,10 @@ struct mmc_host {
87 struct mmc_ios ios; /* current io bus settings */ 87 struct mmc_ios ios; /* current io bus settings */
88 u32 ocr; /* the current OCR setting */ 88 u32 ocr; /* the current OCR setting */
89 89
90 unsigned int mode; /* current card mode of host */
91#define MMC_MODE_MMC 0
92#define MMC_MODE_SD 1
93
90 struct list_head cards; /* devices attached to this host */ 94 struct list_head cards; /* devices attached to this host */
91 95
92 wait_queue_head_t wq; 96 wait_queue_head_t wq;
diff --git a/include/linux/mmc/mmc.h b/include/linux/mmc/mmc.h
index 0d35d4ffb360..1ab78e8d6c53 100644
--- a/include/linux/mmc/mmc.h
+++ b/include/linux/mmc/mmc.h
@@ -88,6 +88,8 @@ struct mmc_card;
88 88
89extern int mmc_wait_for_req(struct mmc_host *, struct mmc_request *); 89extern int mmc_wait_for_req(struct mmc_host *, struct mmc_request *);
90extern int mmc_wait_for_cmd(struct mmc_host *, struct mmc_command *, int); 90extern int mmc_wait_for_cmd(struct mmc_host *, struct mmc_command *, int);
91extern int mmc_wait_for_app_cmd(struct mmc_host *, unsigned int,
92 struct mmc_command *, int);
91 93
92extern int __mmc_claim_host(struct mmc_host *host, struct mmc_card *card); 94extern int __mmc_claim_host(struct mmc_host *host, struct mmc_card *card);
93 95