aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mmc/mmc.c
diff options
context:
space:
mode:
authorPierre Ossman <drzeus-list@drzeus.cx>2005-09-06 18:18:50 -0400
committerLinus Torvalds <torvalds@g5.osdl.org>2005-09-07 19:57:50 -0400
commit335eadf2ef6a1122a720aea98e758e5d431da87d (patch)
treeb20a0c21736cfca40a9df7ab2834b65079469992 /drivers/mmc/mmc.c
parent328b9227865026268261a24a97a578907b280415 (diff)
[PATCH] sd: initialize SD cards
Support for the Secure Digital protocol in the MMC layer. A summary of the legal issues surrounding SD cards, as understood by yours truly: Members of the Secure Digital Association, hereafter SDA, are required to sign a NDA[1] before given access to any specifications. It has been speculated that including an SD implementation would forbid these members to redistribute Linux. This is the basic problem with SD support so it is unclear if it even is a problem since it has no effect on those of us that aren't members. The SDA doesn't seem to enforce these rules though since the patches included here are based on documentation made public by some of the members. The most complete specs[2] are actually released by Sandisk, one of the founding companies of the SDA. Because of this the NDA is considered a non-issue by most involved in the discussions concerning these patches. It might be that the SDA is only interested in protecting the so called "secure" bits of SD, which so far hasn't been found in any public spec. (The card is split into two sections, one "normal" and one "secure" which has an access scheme similar to TPM:s). (As a side note, Microsoft is working to make things easier for us since they want to be able to include the source code for a SD driver in one of their development kits. HP is making sure that the new NDA will allow a Linux implementation. So far only the SDIO specs have been opened up[3]. More will hopefully follow.) [1] http://www.sdcard.org/membership/images/ippolicy.pdf [2] http://www.sandisk.com/pdf/oem/ProdManualSDCardv1.9.pdf [3] http://www.sdcard.org/sdio/Simplified%20SDIO%20Card%20Specification.pdf This patch contains the central parts of the SD support. If no MMC cards are found on a bus then the MMC layer proceeds looking for SD cards. Helper functions are extended to handle the special needs of SD cards. Signed-off-by: Pierre Ossman <drzeus@drzeus.cx> Cc: Russell King <rmk@arm.linux.org.uk> Cc: David Brownell <david-b@pacbell.net> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'drivers/mmc/mmc.c')
-rw-r--r--drivers/mmc/mmc.c326
1 files changed, 253 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