Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mmc: add bus handler

Delegate protocol handling to "bus handlers". This allows the core to
just handle the task of arbitrating the bus. Initialisation and
pampering of cards is now done by the different bus handlers.

This design also allows MMC and SD (and later SDIO) to be more cleanly
separated, allowing easier maintenance.

Signed-off-by: Pierre Ossman <drzeus@drzeus.cx>

+1048 -690
+1 -1
drivers/mmc/core/Makefile
··· 7 7 endif 8 8 9 9 obj-$(CONFIG_MMC) += mmc_core.o 10 - mmc_core-y := core.o sysfs.o mmc_ops.o sd_ops.o 10 + mmc_core-y := core.o sysfs.o mmc.o mmc_ops.o sd.o sd_ops.o 11 11
+143 -688
drivers/mmc/core/core.c
··· 32 32 #include "mmc_ops.h" 33 33 #include "sd_ops.h" 34 34 35 - #define CMD_RETRIES 3 36 - 37 - /* 38 - * OCR Bit positions to 10s of Vdd mV. 39 - */ 40 - static const unsigned short mmc_ocr_bit_to_vdd[] = { 41 - 150, 155, 160, 165, 170, 180, 190, 200, 42 - 210, 220, 230, 240, 250, 260, 270, 280, 43 - 290, 300, 310, 320, 330, 340, 350, 360 44 - }; 45 - 46 - static const unsigned int tran_exp[] = { 47 - 10000, 100000, 1000000, 10000000, 48 - 0, 0, 0, 0 49 - }; 50 - 51 - static const unsigned char tran_mant[] = { 52 - 0, 10, 12, 13, 15, 20, 25, 30, 53 - 35, 40, 45, 50, 55, 60, 70, 80, 54 - }; 55 - 56 - static const unsigned int tacc_exp[] = { 57 - 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 58 - }; 59 - 60 - static const unsigned int tacc_mant[] = { 61 - 0, 10, 12, 13, 15, 20, 25, 30, 62 - 35, 40, 45, 50, 55, 60, 70, 80, 63 - }; 64 - 35 + extern int mmc_attach_mmc(struct mmc_host *host, u32 ocr); 36 + extern int mmc_attach_sd(struct mmc_host *host, u32 ocr); 65 37 66 38 /** 67 39 * mmc_request_done - finish processing an MMC request ··· 275 303 276 304 EXPORT_SYMBOL(mmc_release_host); 277 305 306 + /* 307 + * Internal function that does the actual ios call to the host driver, 308 + * optionally printing some debug output. 309 + */ 278 310 static inline void mmc_set_ios(struct mmc_host *host) 279 311 { 280 312 struct mmc_ios *ios = &host->ios; ··· 292 316 host->ops->set_ios(host, ios); 293 317 } 294 318 319 + /* 320 + * Control chip select pin on a host. 321 + */ 295 322 void mmc_set_chip_select(struct mmc_host *host, int mode) 296 323 { 297 324 host->ios.chip_select = mode; ··· 302 323 } 303 324 304 325 /* 326 + * Sets the host clock to the highest possible frequency that 327 + * is below "hz". 328 + */ 329 + void mmc_set_clock(struct mmc_host *host, unsigned int hz) 330 + { 331 + WARN_ON(hz < host->f_min); 332 + 333 + if (hz > host->f_max) 334 + hz = host->f_max; 335 + 336 + host->ios.clock = hz; 337 + mmc_set_ios(host); 338 + } 339 + 340 + /* 341 + * Change the bus mode (open drain/push-pull) of a host. 342 + */ 343 + void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode) 344 + { 345 + host->ios.bus_mode = mode; 346 + mmc_set_ios(host); 347 + } 348 + 349 + /* 350 + * Change data bus width of a host. 351 + */ 352 + void mmc_set_bus_width(struct mmc_host *host, unsigned int width) 353 + { 354 + host->ios.bus_width = width; 355 + mmc_set_ios(host); 356 + } 357 + 358 + /* 305 359 * Mask off any voltages we don't support and select 306 360 * the lowest voltage 307 361 */ 308 - static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr) 362 + u32 mmc_select_voltage(struct mmc_host *host, u32 ocr) 309 363 { 310 364 int bit; 311 365 ··· 359 347 return ocr; 360 348 } 361 349 362 - #define UNSTUFF_BITS(resp,start,size) \ 363 - ({ \ 364 - const int __size = size; \ 365 - const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \ 366 - const int __off = 3 - ((start) / 32); \ 367 - const int __shft = (start) & 31; \ 368 - u32 __res; \ 369 - \ 370 - __res = resp[__off] >> __shft; \ 371 - if (__size + __shft > 32) \ 372 - __res |= resp[__off-1] << ((32 - __shft) % 32); \ 373 - __res & __mask; \ 374 - }) 375 - 376 350 /* 377 - * Given the decoded CSD structure, decode the raw CID to our CID structure. 351 + * Select timing parameters for host. 378 352 */ 379 - static void mmc_decode_cid(struct mmc_card *card) 353 + void mmc_set_timing(struct mmc_host *host, unsigned int timing) 380 354 { 381 - u32 *resp = card->raw_cid; 382 - 383 - memset(&card->cid, 0, sizeof(struct mmc_cid)); 384 - 385 - if (mmc_card_sd(card)) { 386 - /* 387 - * SD doesn't currently have a version field so we will 388 - * have to assume we can parse this. 389 - */ 390 - card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); 391 - card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); 392 - card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); 393 - card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); 394 - card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); 395 - card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); 396 - card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); 397 - card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4); 398 - card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4); 399 - card->cid.serial = UNSTUFF_BITS(resp, 24, 32); 400 - card->cid.year = UNSTUFF_BITS(resp, 12, 8); 401 - card->cid.month = UNSTUFF_BITS(resp, 8, 4); 402 - 403 - card->cid.year += 2000; /* SD cards year offset */ 404 - } else { 405 - /* 406 - * The selection of the format here is based upon published 407 - * specs from sandisk and from what people have reported. 408 - */ 409 - switch (card->csd.mmca_vsn) { 410 - case 0: /* MMC v1.0 - v1.2 */ 411 - case 1: /* MMC v1.4 */ 412 - card->cid.manfid = UNSTUFF_BITS(resp, 104, 24); 413 - card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); 414 - card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); 415 - card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); 416 - card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); 417 - card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); 418 - card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); 419 - card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8); 420 - card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4); 421 - card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4); 422 - card->cid.serial = UNSTUFF_BITS(resp, 16, 24); 423 - card->cid.month = UNSTUFF_BITS(resp, 12, 4); 424 - card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; 425 - break; 426 - 427 - case 2: /* MMC v2.0 - v2.2 */ 428 - case 3: /* MMC v3.1 - v3.3 */ 429 - case 4: /* MMC v4 */ 430 - card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); 431 - card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); 432 - card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); 433 - card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); 434 - card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); 435 - card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); 436 - card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); 437 - card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); 438 - card->cid.serial = UNSTUFF_BITS(resp, 16, 32); 439 - card->cid.month = UNSTUFF_BITS(resp, 12, 4); 440 - card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; 441 - break; 442 - 443 - default: 444 - printk("%s: card has unknown MMCA version %d\n", 445 - mmc_hostname(card->host), card->csd.mmca_vsn); 446 - mmc_card_set_bad(card); 447 - break; 448 - } 449 - } 450 - } 451 - 452 - /* 453 - * Given a 128-bit response, decode to our card CSD structure. 454 - */ 455 - static void mmc_decode_csd(struct mmc_card *card) 456 - { 457 - struct mmc_csd *csd = &card->csd; 458 - unsigned int e, m, csd_struct; 459 - u32 *resp = card->raw_csd; 460 - 461 - if (mmc_card_sd(card)) { 462 - csd_struct = UNSTUFF_BITS(resp, 126, 2); 463 - 464 - switch (csd_struct) { 465 - case 0: 466 - m = UNSTUFF_BITS(resp, 115, 4); 467 - e = UNSTUFF_BITS(resp, 112, 3); 468 - csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; 469 - csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; 470 - 471 - m = UNSTUFF_BITS(resp, 99, 4); 472 - e = UNSTUFF_BITS(resp, 96, 3); 473 - csd->max_dtr = tran_exp[e] * tran_mant[m]; 474 - csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); 475 - 476 - e = UNSTUFF_BITS(resp, 47, 3); 477 - m = UNSTUFF_BITS(resp, 62, 12); 478 - csd->capacity = (1 + m) << (e + 2); 479 - 480 - csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); 481 - csd->read_partial = UNSTUFF_BITS(resp, 79, 1); 482 - csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); 483 - csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); 484 - csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); 485 - csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); 486 - csd->write_partial = UNSTUFF_BITS(resp, 21, 1); 487 - break; 488 - case 1: 489 - /* 490 - * This is a block-addressed SDHC card. Most 491 - * interesting fields are unused and have fixed 492 - * values. To avoid getting tripped by buggy cards, 493 - * we assume those fixed values ourselves. 494 - */ 495 - mmc_card_set_blockaddr(card); 496 - 497 - csd->tacc_ns = 0; /* Unused */ 498 - csd->tacc_clks = 0; /* Unused */ 499 - 500 - m = UNSTUFF_BITS(resp, 99, 4); 501 - e = UNSTUFF_BITS(resp, 96, 3); 502 - csd->max_dtr = tran_exp[e] * tran_mant[m]; 503 - csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); 504 - 505 - m = UNSTUFF_BITS(resp, 48, 22); 506 - csd->capacity = (1 + m) << 10; 507 - 508 - csd->read_blkbits = 9; 509 - csd->read_partial = 0; 510 - csd->write_misalign = 0; 511 - csd->read_misalign = 0; 512 - csd->r2w_factor = 4; /* Unused */ 513 - csd->write_blkbits = 9; 514 - csd->write_partial = 0; 515 - break; 516 - default: 517 - printk("%s: unrecognised CSD structure version %d\n", 518 - mmc_hostname(card->host), csd_struct); 519 - mmc_card_set_bad(card); 520 - return; 521 - } 522 - } else { 523 - /* 524 - * We only understand CSD structure v1.1 and v1.2. 525 - * v1.2 has extra information in bits 15, 11 and 10. 526 - */ 527 - csd_struct = UNSTUFF_BITS(resp, 126, 2); 528 - if (csd_struct != 1 && csd_struct != 2) { 529 - printk("%s: unrecognised CSD structure version %d\n", 530 - mmc_hostname(card->host), csd_struct); 531 - mmc_card_set_bad(card); 532 - return; 533 - } 534 - 535 - csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4); 536 - m = UNSTUFF_BITS(resp, 115, 4); 537 - e = UNSTUFF_BITS(resp, 112, 3); 538 - csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; 539 - csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; 540 - 541 - m = UNSTUFF_BITS(resp, 99, 4); 542 - e = UNSTUFF_BITS(resp, 96, 3); 543 - csd->max_dtr = tran_exp[e] * tran_mant[m]; 544 - csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); 545 - 546 - e = UNSTUFF_BITS(resp, 47, 3); 547 - m = UNSTUFF_BITS(resp, 62, 12); 548 - csd->capacity = (1 + m) << (e + 2); 549 - 550 - csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); 551 - csd->read_partial = UNSTUFF_BITS(resp, 79, 1); 552 - csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); 553 - csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); 554 - csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); 555 - csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); 556 - csd->write_partial = UNSTUFF_BITS(resp, 21, 1); 557 - } 558 - } 559 - 560 - /* 561 - * Given a 64-bit response, decode to our card SCR structure. 562 - */ 563 - static void mmc_decode_scr(struct mmc_card *card) 564 - { 565 - struct sd_scr *scr = &card->scr; 566 - unsigned int scr_struct; 567 - u32 resp[4]; 568 - 569 - BUG_ON(!mmc_card_sd(card)); 570 - 571 - resp[3] = card->raw_scr[1]; 572 - resp[2] = card->raw_scr[0]; 573 - 574 - scr_struct = UNSTUFF_BITS(resp, 60, 4); 575 - if (scr_struct != 0) { 576 - printk("%s: unrecognised SCR structure version %d\n", 577 - mmc_hostname(card->host), scr_struct); 578 - mmc_card_set_bad(card); 579 - return; 580 - } 581 - 582 - scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4); 583 - scr->bus_widths = UNSTUFF_BITS(resp, 48, 4); 355 + host->ios.timing = timing; 356 + mmc_set_ios(host); 584 357 } 585 358 586 359 /* 587 360 * Allocate a new MMC card 588 361 */ 589 - static struct mmc_card * 590 - mmc_alloc_card(struct mmc_host *host, u32 *raw_cid) 362 + struct mmc_card *mmc_alloc_card(struct mmc_host *host) 591 363 { 592 364 struct mmc_card *card; 593 365 ··· 380 584 return ERR_PTR(-ENOMEM); 381 585 382 586 mmc_init_card(card, host); 383 - memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid)); 384 587 385 588 return card; 386 589 } ··· 429 634 } 430 635 431 636 /* 432 - * Discover the card by requesting its CID. 433 - * 434 - * Create a mmc_card entry for the discovered card, assigning 435 - * it an RCA, and save the raw CID for decoding later. 637 + * Assign a mmc bus handler to a host. Only one bus handler may control a 638 + * host at any given time. 436 639 */ 437 - static void mmc_discover_card(struct mmc_host *host) 640 + void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops) 438 641 { 439 - unsigned int err; 440 - u32 cid[4]; 642 + unsigned long flags; 441 643 442 - BUG_ON(host->card); 644 + BUG_ON(!host); 645 + BUG_ON(!ops); 443 646 444 - err = mmc_all_send_cid(host, cid); 445 - if (err != MMC_ERR_NONE) { 446 - printk(KERN_ERR "%s: error requesting CID: %d\n", 447 - mmc_hostname(host), err); 448 - return; 449 - } 647 + BUG_ON(!host->claimed); 450 648 451 - host->card = mmc_alloc_card(host, cid); 452 - if (IS_ERR(host->card)) { 453 - err = PTR_ERR(host->card); 454 - host->card = NULL; 455 - return; 456 - } 649 + spin_lock_irqsave(&host->lock, flags); 457 650 458 - if (host->mode == MMC_MODE_SD) { 459 - host->card->type = MMC_TYPE_SD; 651 + BUG_ON(host->bus_ops); 652 + BUG_ON(host->bus_refs); 460 653 461 - err = mmc_send_relative_addr(host, &host->card->rca); 462 - if (err != MMC_ERR_NONE) 463 - mmc_card_set_dead(host->card); 464 - else { 465 - if (!host->ops->get_ro) { 466 - printk(KERN_WARNING "%s: host does not " 467 - "support reading read-only " 468 - "switch. assuming write-enable.\n", 469 - mmc_hostname(host)); 470 - } else { 471 - if (host->ops->get_ro(host)) 472 - mmc_card_set_readonly(host->card); 473 - } 474 - } 475 - } else { 476 - host->card->type = MMC_TYPE_MMC; 477 - host->card->rca = 1; 654 + host->bus_ops = ops; 655 + host->bus_refs = 1; 656 + host->bus_dead = 0; 478 657 479 - err = mmc_set_relative_addr(host->card); 480 - if (err != MMC_ERR_NONE) 481 - mmc_card_set_dead(host->card); 482 - } 483 - } 484 - 485 - static void mmc_read_csd(struct mmc_host *host) 486 - { 487 - int err; 488 - 489 - if (!host->card) 490 - return; 491 - if (mmc_card_dead(host->card)) 492 - return; 493 - 494 - err = mmc_send_csd(host->card, host->card->raw_csd); 495 - if (err != MMC_ERR_NONE) { 496 - mmc_card_set_dead(host->card); 497 - return; 498 - } 499 - 500 - mmc_decode_csd(host->card); 501 - mmc_decode_cid(host->card); 502 - } 503 - 504 - static void mmc_process_ext_csd(struct mmc_host *host) 505 - { 506 - int err; 507 - u8 *ext_csd; 508 - 509 - if (!host->card) 510 - return; 511 - if (mmc_card_dead(host->card)) 512 - return; 513 - if (mmc_card_sd(host->card)) 514 - return; 515 - if (host->card->csd.mmca_vsn < CSD_SPEC_VER_4) 516 - return; 517 - 518 - /* 519 - * As the ext_csd is so large and mostly unused, we don't store the 520 - * raw block in mmc_card. 521 - */ 522 - ext_csd = kmalloc(512, GFP_KERNEL); 523 - if (!ext_csd) { 524 - printk("%s: could not allocate a buffer to receive the ext_csd." 525 - "mmc v4 cards will be treated as v3.\n", 526 - mmc_hostname(host)); 527 - return; 528 - } 529 - 530 - err = mmc_send_ext_csd(host->card, ext_csd); 531 - if (err != MMC_ERR_NONE) { 532 - if (host->card->csd.capacity == (4096 * 512)) { 533 - printk(KERN_ERR "%s: unable to read EXT_CSD " 534 - "on a possible high capacity card. " 535 - "Card will be ignored.\n", 536 - mmc_hostname(host)); 537 - mmc_card_set_dead(host->card); 538 - } else { 539 - printk(KERN_WARNING "%s: unable to read " 540 - "EXT_CSD, performance might " 541 - "suffer.\n", 542 - mmc_hostname(host)); 543 - } 544 - goto out; 545 - } 546 - 547 - host->card->ext_csd.sectors = 548 - ext_csd[EXT_CSD_SEC_CNT + 0] << 0 | 549 - ext_csd[EXT_CSD_SEC_CNT + 1] << 8 | 550 - ext_csd[EXT_CSD_SEC_CNT + 2] << 16 | 551 - ext_csd[EXT_CSD_SEC_CNT + 3] << 24; 552 - if (host->card->ext_csd.sectors) 553 - mmc_card_set_blockaddr(host->card); 554 - 555 - switch (ext_csd[EXT_CSD_CARD_TYPE]) { 556 - case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26: 557 - host->card->ext_csd.hs_max_dtr = 52000000; 558 - break; 559 - case EXT_CSD_CARD_TYPE_26: 560 - host->card->ext_csd.hs_max_dtr = 26000000; 561 - break; 562 - default: 563 - /* MMC v4 spec says this cannot happen */ 564 - printk("%s: card is mmc v4 but doesn't support " 565 - "any high-speed modes.\n", 566 - mmc_hostname(host)); 567 - goto out; 568 - } 569 - 570 - if (host->caps & MMC_CAP_MMC_HIGHSPEED) { 571 - /* Activate highspeed support. */ 572 - err = mmc_switch(host->card, MMC_SWITCH_MODE_WRITE_BYTE, 573 - EXT_CSD_HS_TIMING, 1); 574 - if (err != MMC_ERR_NONE) { 575 - printk("%s: failed to switch card to mmc v4 " 576 - "high-speed mode.\n", 577 - mmc_hostname(host)); 578 - goto out; 579 - } 580 - 581 - mmc_card_set_highspeed(host->card); 582 - 583 - host->ios.timing = MMC_TIMING_MMC_HS; 584 - mmc_set_ios(host); 585 - } 586 - 587 - /* Check for host support for wide-bus modes. */ 588 - if (host->caps & MMC_CAP_4_BIT_DATA) { 589 - /* Activate 4-bit support. */ 590 - err = mmc_switch(host->card, MMC_SWITCH_MODE_WRITE_BYTE, 591 - EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_4 | 592 - EXT_CSD_CMD_SET_NORMAL); 593 - if (err != MMC_ERR_NONE) { 594 - printk("%s: failed to switch card to " 595 - "mmc v4 4-bit bus mode.\n", 596 - mmc_hostname(host)); 597 - goto out; 598 - } 599 - 600 - host->ios.bus_width = MMC_BUS_WIDTH_4; 601 - mmc_set_ios(host); 602 - } 603 - 604 - out: 605 - kfree(ext_csd); 606 - } 607 - 608 - static void mmc_read_scr(struct mmc_host *host) 609 - { 610 - int err; 611 - 612 - if (!host->card) 613 - return; 614 - if (mmc_card_dead(host->card)) 615 - return; 616 - if (!mmc_card_sd(host->card)) 617 - return; 618 - 619 - err = mmc_app_send_scr(host->card, host->card->raw_scr); 620 - if (err != MMC_ERR_NONE) { 621 - mmc_card_set_dead(host->card); 622 - return; 623 - } 624 - 625 - mmc_decode_scr(host->card); 626 - } 627 - 628 - static void mmc_read_switch_caps(struct mmc_host *host) 629 - { 630 - int err; 631 - unsigned char *status; 632 - 633 - if (!(host->caps & MMC_CAP_SD_HIGHSPEED)) 634 - return; 635 - 636 - if (!host->card) 637 - return; 638 - if (mmc_card_dead(host->card)) 639 - return; 640 - if (!mmc_card_sd(host->card)) 641 - return; 642 - if (host->card->scr.sda_vsn < SCR_SPEC_VER_1) 643 - return; 644 - 645 - status = kmalloc(64, GFP_KERNEL); 646 - if (!status) { 647 - printk(KERN_WARNING "%s: Unable to allocate buffer for " 648 - "reading switch capabilities.\n", 649 - mmc_hostname(host)); 650 - return; 651 - } 652 - 653 - err = mmc_sd_switch(host->card, SD_SWITCH_CHECK, 654 - SD_SWITCH_GRP_ACCESS, SD_SWITCH_ACCESS_HS, status); 655 - if (err != MMC_ERR_NONE) { 656 - printk("%s: unable to read switch capabilities, " 657 - "performance might suffer.\n", 658 - mmc_hostname(host)); 659 - goto out; 660 - } 661 - 662 - if (status[13] & 0x02) 663 - host->card->sw_caps.hs_max_dtr = 50000000; 664 - 665 - err = mmc_sd_switch(host->card, SD_SWITCH_SET, 666 - SD_SWITCH_GRP_ACCESS, SD_SWITCH_ACCESS_HS, status); 667 - if (err != MMC_ERR_NONE || (status[16] & 0xF) != 1) { 668 - printk(KERN_WARNING "%s: Problem switching card " 669 - "into high-speed mode!\n", 670 - mmc_hostname(host)); 671 - goto out; 672 - } 673 - 674 - mmc_card_set_highspeed(host->card); 675 - 676 - host->ios.timing = MMC_TIMING_SD_HS; 677 - mmc_set_ios(host); 678 - 679 - out: 680 - kfree(status); 681 - } 682 - 683 - static unsigned int mmc_calculate_clock(struct mmc_host *host) 684 - { 685 - unsigned int max_dtr = host->f_max; 686 - 687 - if (host->card && !mmc_card_dead(host->card)) { 688 - if (mmc_card_highspeed(host->card) && mmc_card_sd(host->card)) { 689 - if (max_dtr > host->card->sw_caps.hs_max_dtr) 690 - max_dtr = host->card->sw_caps.hs_max_dtr; 691 - } else if (mmc_card_highspeed(host->card) && !mmc_card_sd(host->card)) { 692 - if (max_dtr > host->card->ext_csd.hs_max_dtr) 693 - max_dtr = host->card->ext_csd.hs_max_dtr; 694 - } else if (max_dtr > host->card->csd.max_dtr) { 695 - max_dtr = host->card->csd.max_dtr; 696 - } 697 - } 698 - 699 - pr_debug("%s: selected %d.%03dMHz transfer rate\n", 700 - mmc_hostname(host), 701 - max_dtr / 1000000, (max_dtr / 1000) % 1000); 702 - 703 - return max_dtr; 658 + spin_unlock_irqrestore(&host->lock, flags); 704 659 } 705 660 706 661 /* 707 - * Check whether cards we already know about are still present. 708 - * We do this by requesting status, and checking whether a card 709 - * responds. 710 - * 711 - * A request for status does not cause a state change in data 712 - * transfer mode. 662 + * Remove the current bus handler from a host. Assumes that there are 663 + * no interesting cards left, so the bus is powered down. 713 664 */ 714 - static void mmc_check_card(struct mmc_card *card) 665 + void mmc_detach_bus(struct mmc_host *host) 715 666 { 716 - int err; 667 + unsigned long flags; 717 668 718 - BUG_ON(!card); 669 + BUG_ON(!host); 719 670 720 - err = mmc_send_status(card, NULL); 721 - if (err == MMC_ERR_NONE) 722 - return; 671 + BUG_ON(!host->claimed); 672 + BUG_ON(!host->bus_ops); 723 673 724 - mmc_card_set_dead(card); 674 + spin_lock_irqsave(&host->lock, flags); 675 + 676 + host->bus_dead = 1; 677 + 678 + spin_unlock_irqrestore(&host->lock, flags); 679 + 680 + mmc_power_off(host); 681 + 682 + mmc_bus_put(host); 725 683 } 726 684 727 - static void mmc_setup(struct mmc_host *host) 685 + /* 686 + * Cleanup when the last reference to the bus operator is dropped. 687 + */ 688 + void __mmc_release_bus(struct mmc_host *host) 728 689 { 729 - int err; 730 - u32 ocr; 690 + BUG_ON(!host); 691 + BUG_ON(host->bus_refs); 692 + BUG_ON(!host->bus_dead); 731 693 732 - host->mode = MMC_MODE_SD; 733 - 734 - mmc_power_up(host); 735 - mmc_go_idle(host); 736 - 737 - err = mmc_send_if_cond(host, host->ocr_avail); 738 - if (err != MMC_ERR_NONE) { 739 - return; 740 - } 741 - err = mmc_send_app_op_cond(host, 0, &ocr); 742 - 743 - /* 744 - * If we fail to detect any SD cards then try 745 - * searching for MMC cards. 746 - */ 747 - if (err != MMC_ERR_NONE) { 748 - host->mode = MMC_MODE_MMC; 749 - 750 - err = mmc_send_op_cond(host, 0, &ocr); 751 - if (err != MMC_ERR_NONE) 752 - return; 753 - } 754 - 755 - host->ocr = mmc_select_voltage(host, ocr); 756 - 757 - if (host->ocr == 0) 758 - return; 759 - 760 - /* 761 - * Since we're changing the OCR value, we seem to 762 - * need to tell some cards to go back to the idle 763 - * state. We wait 1ms to give cards time to 764 - * respond. 765 - */ 766 - mmc_go_idle(host); 767 - 768 - /* 769 - * Send the selected OCR multiple times... until the cards 770 - * all get the idea that they should be ready for CMD2. 771 - * (My SanDisk card seems to need this.) 772 - */ 773 - if (host->mode == MMC_MODE_SD) { 774 - /* 775 - * If SD_SEND_IF_COND indicates an SD 2.0 776 - * compliant card and we should set bit 30 777 - * of the ocr to indicate that we can handle 778 - * block-addressed SDHC cards. 779 - */ 780 - err = mmc_send_if_cond(host, host->ocr); 781 - if (err == MMC_ERR_NONE) 782 - ocr = host->ocr | (1 << 30); 783 - 784 - mmc_send_app_op_cond(host, ocr, NULL); 785 - } else { 786 - /* The extra bit indicates that we support high capacity */ 787 - mmc_send_op_cond(host, host->ocr | (1 << 30), NULL); 788 - } 789 - 790 - mmc_discover_card(host); 791 - 792 - /* 793 - * Ok, now switch to push-pull mode. 794 - */ 795 - host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; 796 - mmc_set_ios(host); 797 - 798 - mmc_read_csd(host); 799 - 800 - if (host->card && !mmc_card_dead(host->card)) { 801 - err = mmc_select_card(host->card); 802 - if (err != MMC_ERR_NONE) 803 - mmc_card_set_dead(host->card); 804 - } 805 - 806 - /* 807 - * The card is in 1 bit mode by default so 808 - * we only need to change if it supports the 809 - * wider version. 810 - */ 811 - if (host->card && !mmc_card_dead(host->card) && 812 - mmc_card_sd(host->card) && 813 - (host->card->scr.bus_widths & SD_SCR_BUS_WIDTH_4) && 814 - (host->card->host->caps & MMC_CAP_4_BIT_DATA)) { 815 - err = mmc_app_set_bus_width(host->card, SD_BUS_WIDTH_4); 816 - if (err != MMC_ERR_NONE) 817 - mmc_card_set_dead(host->card); 818 - else { 819 - host->ios.bus_width = MMC_BUS_WIDTH_4; 820 - mmc_set_ios(host); 821 - } 822 - } 823 - 824 - if (host->mode == MMC_MODE_SD) { 825 - mmc_read_scr(host); 826 - mmc_read_switch_caps(host); 827 - } else 828 - mmc_process_ext_csd(host); 694 + host->bus_ops = NULL; 829 695 } 830 - 831 696 832 697 /** 833 698 * mmc_detect_change - process change of state on a MMC socket ··· 515 1060 { 516 1061 struct mmc_host *host = 517 1062 container_of(work, struct mmc_host, detect.work); 1063 + u32 ocr; 1064 + int err; 518 1065 519 - mmc_claim_host(host); 1066 + mmc_bus_get(host); 520 1067 521 - /* 522 - * Check for removed card and newly inserted ones. We check for 523 - * removed cards first so we can intelligently re-select the VDD. 524 - */ 525 - if (host->card) { 526 - mmc_check_card(host->card); 527 - 528 - mmc_release_host(host); 529 - 530 - if (mmc_card_dead(host->card)) { 531 - mmc_remove_card(host->card); 532 - host->card = NULL; 533 - } 534 - 535 - goto out; 536 - } 537 - 538 - mmc_setup(host); 539 - 540 - if (host->card && !mmc_card_dead(host->card)) { 1068 + if (host->bus_ops == NULL) { 541 1069 /* 542 - * (Re-)calculate the fastest clock rate which the 543 - * attached cards and the host support. 1070 + * Only we can add a new handler, so it's safe to 1071 + * release the lock here. 544 1072 */ 545 - host->ios.clock = mmc_calculate_clock(host); 546 - mmc_set_ios(host); 1073 + mmc_bus_put(host); 1074 + 1075 + mmc_claim_host(host); 1076 + 1077 + mmc_power_up(host); 1078 + mmc_go_idle(host); 1079 + 1080 + mmc_send_if_cond(host, host->ocr_avail); 1081 + 1082 + err = mmc_send_app_op_cond(host, 0, &ocr); 1083 + if (err == MMC_ERR_NONE) { 1084 + if (mmc_attach_sd(host, ocr)) 1085 + mmc_power_off(host); 1086 + } else { 1087 + /* 1088 + * If we fail to detect any SD cards then try 1089 + * searching for MMC cards. 1090 + */ 1091 + err = mmc_send_op_cond(host, 0, &ocr); 1092 + if (err == MMC_ERR_NONE) { 1093 + if (mmc_attach_mmc(host, ocr)) 1094 + mmc_power_off(host); 1095 + } else { 1096 + mmc_power_off(host); 1097 + mmc_release_host(host); 1098 + } 1099 + } 1100 + } else { 1101 + if (host->bus_ops->detect && !host->bus_dead) 1102 + host->bus_ops->detect(host); 1103 + 1104 + mmc_bus_put(host); 547 1105 } 548 - 549 - mmc_release_host(host); 550 - 551 - /* 552 - * If this is a new and good card, register it. 553 - */ 554 - if (host->card && !mmc_card_dead(host->card)) { 555 - if (mmc_register_card(host->card)) 556 - mmc_card_set_dead(host->card); 557 - } 558 - 559 - /* 560 - * If this card is dead, destroy it. 561 - */ 562 - if (host->card && mmc_card_dead(host->card)) { 563 - mmc_remove_card(host->card); 564 - host->card = NULL; 565 - } 566 - 567 - out: 568 - /* 569 - * If we discover that there are no cards on the 570 - * bus, turn off the clock and power down. 571 - */ 572 - if (!host->card) 573 - mmc_power_off(host); 574 1106 } 575 1107 576 1108 ··· 632 1190 633 1191 mmc_flush_scheduled_work(); 634 1192 635 - if (host->card) { 636 - mmc_remove_card(host->card); 637 - host->card = NULL; 1193 + mmc_bus_get(host); 1194 + if (host->bus_ops && !host->bus_dead) { 1195 + if (host->bus_ops->remove) 1196 + host->bus_ops->remove(host); 1197 + 1198 + mmc_claim_host(host); 1199 + mmc_detach_bus(host); 1200 + mmc_release_host(host); 638 1201 } 1202 + mmc_bus_put(host); 1203 + 1204 + BUG_ON(host->card); 639 1205 640 1206 mmc_power_off(host); 641 1207 mmc_remove_host_sysfs(host); ··· 675 1225 { 676 1226 mmc_flush_scheduled_work(); 677 1227 678 - if (host->card) { 679 - mmc_remove_card(host->card); 680 - host->card = NULL; 1228 + mmc_bus_get(host); 1229 + if (host->bus_ops && !host->bus_dead) { 1230 + if (host->bus_ops->remove) 1231 + host->bus_ops->remove(host); 1232 + mmc_detach_bus(host); 681 1233 } 1234 + mmc_bus_put(host); 1235 + 1236 + BUG_ON(host->card); 682 1237 683 1238 mmc_power_off(host); 684 1239
+37
drivers/mmc/core/core.h
··· 15 15 16 16 #define MMC_CMD_RETRIES 3 17 17 18 + struct mmc_bus_ops { 19 + void (*remove)(struct mmc_host *); 20 + void (*detect)(struct mmc_host *); 21 + }; 22 + 23 + void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops); 24 + void mmc_detach_bus(struct mmc_host *host); 25 + 26 + void __mmc_release_bus(struct mmc_host *host); 27 + 28 + static inline void mmc_bus_get(struct mmc_host *host) 29 + { 30 + unsigned long flags; 31 + 32 + spin_lock_irqsave(&host->lock, flags); 33 + host->bus_refs++; 34 + spin_unlock_irqrestore(&host->lock, flags); 35 + } 36 + 37 + static inline void mmc_bus_put(struct mmc_host *host) 38 + { 39 + unsigned long flags; 40 + 41 + spin_lock_irqsave(&host->lock, flags); 42 + host->bus_refs--; 43 + if ((host->bus_refs == 0) && host->bus_ops) 44 + __mmc_release_bus(host); 45 + spin_unlock_irqrestore(&host->lock, flags); 46 + } 47 + 18 48 void mmc_set_chip_select(struct mmc_host *host, int mode); 49 + void mmc_set_clock(struct mmc_host *host, unsigned int hz); 50 + void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode); 51 + void mmc_set_bus_width(struct mmc_host *host, unsigned int width); 52 + u32 mmc_select_voltage(struct mmc_host *host, u32 ocr); 53 + void mmc_set_timing(struct mmc_host *host, unsigned int timing); 54 + 55 + struct mmc_card *mmc_alloc_card(struct mmc_host *host); 19 56 20 57 static inline void mmc_delay(unsigned int ms) 21 58 {
+430
drivers/mmc/core/mmc.c
··· 1 + /* 2 + * linux/drivers/mmc/mmc.c 3 + * 4 + * Copyright (C) 2003-2004 Russell King, All Rights Reserved. 5 + * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved. 6 + * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved. 7 + * 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. 11 + */ 12 + 13 + #include <linux/err.h> 14 + 15 + #include <linux/mmc/host.h> 16 + #include <linux/mmc/card.h> 17 + #include <linux/mmc/mmc.h> 18 + 19 + #include "core.h" 20 + #include "sysfs.h" 21 + #include "mmc_ops.h" 22 + 23 + static const unsigned int tran_exp[] = { 24 + 10000, 100000, 1000000, 10000000, 25 + 0, 0, 0, 0 26 + }; 27 + 28 + static const unsigned char tran_mant[] = { 29 + 0, 10, 12, 13, 15, 20, 25, 30, 30 + 35, 40, 45, 50, 55, 60, 70, 80, 31 + }; 32 + 33 + static const unsigned int tacc_exp[] = { 34 + 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 35 + }; 36 + 37 + static const unsigned int tacc_mant[] = { 38 + 0, 10, 12, 13, 15, 20, 25, 30, 39 + 35, 40, 45, 50, 55, 60, 70, 80, 40 + }; 41 + 42 + #define UNSTUFF_BITS(resp,start,size) \ 43 + ({ \ 44 + const int __size = size; \ 45 + const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \ 46 + const int __off = 3 - ((start) / 32); \ 47 + const int __shft = (start) & 31; \ 48 + u32 __res; \ 49 + \ 50 + __res = resp[__off] >> __shft; \ 51 + if (__size + __shft > 32) \ 52 + __res |= resp[__off-1] << ((32 - __shft) % 32); \ 53 + __res & __mask; \ 54 + }) 55 + 56 + /* 57 + * Given the decoded CSD structure, decode the raw CID to our CID structure. 58 + */ 59 + static void mmc_decode_cid(struct mmc_card *card) 60 + { 61 + u32 *resp = card->raw_cid; 62 + 63 + /* 64 + * The selection of the format here is based upon published 65 + * specs from sandisk and from what people have reported. 66 + */ 67 + switch (card->csd.mmca_vsn) { 68 + case 0: /* MMC v1.0 - v1.2 */ 69 + case 1: /* MMC v1.4 */ 70 + card->cid.manfid = UNSTUFF_BITS(resp, 104, 24); 71 + card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); 72 + card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); 73 + card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); 74 + card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); 75 + card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); 76 + card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); 77 + card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8); 78 + card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4); 79 + card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4); 80 + card->cid.serial = UNSTUFF_BITS(resp, 16, 24); 81 + card->cid.month = UNSTUFF_BITS(resp, 12, 4); 82 + card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; 83 + break; 84 + 85 + case 2: /* MMC v2.0 - v2.2 */ 86 + case 3: /* MMC v3.1 - v3.3 */ 87 + case 4: /* MMC v4 */ 88 + card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); 89 + card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); 90 + card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); 91 + card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); 92 + card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); 93 + card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); 94 + card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); 95 + card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); 96 + card->cid.serial = UNSTUFF_BITS(resp, 16, 32); 97 + card->cid.month = UNSTUFF_BITS(resp, 12, 4); 98 + card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; 99 + break; 100 + 101 + default: 102 + printk("%s: card has unknown MMCA version %d\n", 103 + mmc_hostname(card->host), card->csd.mmca_vsn); 104 + mmc_card_set_bad(card); 105 + break; 106 + } 107 + } 108 + 109 + /* 110 + * Given a 128-bit response, decode to our card CSD structure. 111 + */ 112 + static void mmc_decode_csd(struct mmc_card *card) 113 + { 114 + struct mmc_csd *csd = &card->csd; 115 + unsigned int e, m, csd_struct; 116 + u32 *resp = card->raw_csd; 117 + 118 + /* 119 + * We only understand CSD structure v1.1 and v1.2. 120 + * v1.2 has extra information in bits 15, 11 and 10. 121 + */ 122 + csd_struct = UNSTUFF_BITS(resp, 126, 2); 123 + if (csd_struct != 1 && csd_struct != 2) { 124 + printk("%s: unrecognised CSD structure version %d\n", 125 + mmc_hostname(card->host), csd_struct); 126 + mmc_card_set_bad(card); 127 + return; 128 + } 129 + 130 + csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4); 131 + m = UNSTUFF_BITS(resp, 115, 4); 132 + e = UNSTUFF_BITS(resp, 112, 3); 133 + csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; 134 + csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; 135 + 136 + m = UNSTUFF_BITS(resp, 99, 4); 137 + e = UNSTUFF_BITS(resp, 96, 3); 138 + csd->max_dtr = tran_exp[e] * tran_mant[m]; 139 + csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); 140 + 141 + e = UNSTUFF_BITS(resp, 47, 3); 142 + m = UNSTUFF_BITS(resp, 62, 12); 143 + csd->capacity = (1 + m) << (e + 2); 144 + 145 + csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); 146 + csd->read_partial = UNSTUFF_BITS(resp, 79, 1); 147 + csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); 148 + csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); 149 + csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); 150 + csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); 151 + csd->write_partial = UNSTUFF_BITS(resp, 21, 1); 152 + } 153 + 154 + /* 155 + * Read and decode extended CSD. Switch to high-speed and wide bus 156 + * if supported. 157 + */ 158 + static int mmc_process_ext_csd(struct mmc_card *card) 159 + { 160 + int err; 161 + u8 *ext_csd; 162 + 163 + BUG_ON(!card); 164 + 165 + err = MMC_ERR_FAILED; 166 + 167 + if (card->csd.mmca_vsn < CSD_SPEC_VER_4) 168 + return MMC_ERR_NONE; 169 + 170 + /* 171 + * As the ext_csd is so large and mostly unused, we don't store the 172 + * raw block in mmc_card. 173 + */ 174 + ext_csd = kmalloc(512, GFP_KERNEL); 175 + if (!ext_csd) { 176 + printk(KERN_ERR "%s: could not allocate a buffer to " 177 + "receive the ext_csd. mmc v4 cards will be " 178 + "treated as v3.\n", mmc_hostname(card->host)); 179 + return MMC_ERR_FAILED; 180 + } 181 + 182 + err = mmc_send_ext_csd(card, ext_csd); 183 + if (err != MMC_ERR_NONE) { 184 + /* 185 + * High capacity cards should have this "magic" size 186 + * stored in their CSD. 187 + */ 188 + if (card->csd.capacity == (4096 * 512)) { 189 + printk(KERN_ERR "%s: unable to read EXT_CSD " 190 + "on a possible high capacity card. " 191 + "Card will be ignored.\n", 192 + mmc_hostname(card->host)); 193 + } else { 194 + printk(KERN_WARNING "%s: unable to read " 195 + "EXT_CSD, performance might " 196 + "suffer.\n", 197 + mmc_hostname(card->host)); 198 + err = MMC_ERR_NONE; 199 + } 200 + goto out; 201 + } 202 + 203 + card->ext_csd.sectors = 204 + ext_csd[EXT_CSD_SEC_CNT + 0] << 0 | 205 + ext_csd[EXT_CSD_SEC_CNT + 1] << 8 | 206 + ext_csd[EXT_CSD_SEC_CNT + 2] << 16 | 207 + ext_csd[EXT_CSD_SEC_CNT + 3] << 24; 208 + if (card->ext_csd.sectors) 209 + mmc_card_set_blockaddr(card); 210 + 211 + switch (ext_csd[EXT_CSD_CARD_TYPE]) { 212 + case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26: 213 + card->ext_csd.hs_max_dtr = 52000000; 214 + break; 215 + case EXT_CSD_CARD_TYPE_26: 216 + card->ext_csd.hs_max_dtr = 26000000; 217 + break; 218 + default: 219 + /* MMC v4 spec says this cannot happen */ 220 + printk(KERN_WARNING "%s: card is mmc v4 but doesn't " 221 + "support any high-speed modes.\n", 222 + mmc_hostname(card->host)); 223 + goto out; 224 + } 225 + 226 + if (card->host->caps & MMC_CAP_MMC_HIGHSPEED) { 227 + /* Activate highspeed support. */ 228 + err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, 229 + EXT_CSD_HS_TIMING, 1); 230 + if (err != MMC_ERR_NONE) { 231 + printk(KERN_WARNING "%s: failed to switch " 232 + "card to mmc v4 high-speed mode.\n", 233 + mmc_hostname(card->host)); 234 + err = MMC_ERR_NONE; 235 + goto out; 236 + } 237 + 238 + mmc_card_set_highspeed(card); 239 + 240 + mmc_set_timing(card->host, MMC_TIMING_MMC_HS); 241 + } 242 + 243 + /* Check for host support for wide-bus modes. */ 244 + if (card->host->caps & MMC_CAP_4_BIT_DATA) { 245 + /* Activate 4-bit support. */ 246 + err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, 247 + EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_4); 248 + if (err != MMC_ERR_NONE) { 249 + printk(KERN_WARNING "%s: failed to switch " 250 + "card to mmc v4 4-bit bus mode.\n", 251 + mmc_hostname(card->host)); 252 + err = MMC_ERR_NONE; 253 + goto out; 254 + } 255 + 256 + mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4); 257 + } 258 + 259 + out: 260 + kfree(ext_csd); 261 + 262 + return err; 263 + } 264 + 265 + /* 266 + * Host is being removed. Free up the current card. 267 + */ 268 + static void mmc_remove(struct mmc_host *host) 269 + { 270 + BUG_ON(!host); 271 + BUG_ON(!host->card); 272 + 273 + mmc_remove_card(host->card); 274 + host->card = NULL; 275 + } 276 + 277 + /* 278 + * Card detection callback from host. 279 + */ 280 + static void mmc_detect(struct mmc_host *host) 281 + { 282 + int err; 283 + 284 + BUG_ON(!host); 285 + BUG_ON(!host->card); 286 + 287 + mmc_claim_host(host); 288 + 289 + /* 290 + * Just check if our card has been removed. 291 + */ 292 + err = mmc_send_status(host->card, NULL); 293 + 294 + mmc_release_host(host); 295 + 296 + if (err != MMC_ERR_NONE) { 297 + mmc_remove_card(host->card); 298 + host->card = NULL; 299 + 300 + mmc_claim_host(host); 301 + mmc_detach_bus(host); 302 + mmc_release_host(host); 303 + } 304 + } 305 + 306 + static const struct mmc_bus_ops mmc_ops = { 307 + .remove = mmc_remove, 308 + .detect = mmc_detect, 309 + }; 310 + 311 + /* 312 + * Starting point for MMC card init. 313 + */ 314 + int mmc_attach_mmc(struct mmc_host *host, u32 ocr) 315 + { 316 + struct mmc_card *card; 317 + int err; 318 + u32 cid[4]; 319 + unsigned int max_dtr; 320 + 321 + BUG_ON(!host); 322 + BUG_ON(!host->claimed); 323 + 324 + mmc_attach_bus(host, &mmc_ops); 325 + 326 + host->ocr = mmc_select_voltage(host, ocr); 327 + 328 + /* 329 + * Can we support the voltage of the card? 330 + */ 331 + if (!host->ocr) 332 + goto err; 333 + 334 + /* 335 + * Since we're changing the OCR value, we seem to 336 + * need to tell some cards to go back to the idle 337 + * state. We wait 1ms to give cards time to 338 + * respond. 339 + */ 340 + mmc_go_idle(host); 341 + 342 + /* The extra bit indicates that we support high capacity */ 343 + mmc_send_op_cond(host, host->ocr | (1 << 30), NULL); 344 + 345 + /* 346 + * Fetch CID from card. 347 + */ 348 + err = mmc_all_send_cid(host, cid); 349 + if (err != MMC_ERR_NONE) 350 + goto err; 351 + 352 + /* 353 + * Allocate card structure. 354 + */ 355 + card = mmc_alloc_card(host); 356 + if (IS_ERR(card)) 357 + goto err; 358 + 359 + card->type = MMC_TYPE_MMC; 360 + card->rca = 1; 361 + memcpy(card->raw_cid, cid, sizeof(card->raw_cid)); 362 + 363 + /* 364 + * Set card RCA. 365 + */ 366 + err = mmc_set_relative_addr(card); 367 + if (err != MMC_ERR_NONE) 368 + goto free_card; 369 + 370 + mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); 371 + 372 + /* 373 + * Fetch CSD from card. 374 + */ 375 + err = mmc_send_csd(card, card->raw_csd); 376 + if (err != MMC_ERR_NONE) 377 + goto free_card; 378 + 379 + mmc_decode_csd(card); 380 + mmc_decode_cid(card); 381 + 382 + /* 383 + * Fetch and process extened CSD. 384 + * This will switch into high-speed and wide bus modes, 385 + * as available. 386 + */ 387 + err = mmc_select_card(card); 388 + if (err != MMC_ERR_NONE) 389 + goto free_card; 390 + 391 + err = mmc_process_ext_csd(card); 392 + if (err != MMC_ERR_NONE) 393 + goto free_card; 394 + 395 + /* 396 + * Compute bus speed. 397 + */ 398 + max_dtr = (unsigned int)-1; 399 + 400 + if (mmc_card_highspeed(card)) { 401 + if (max_dtr > card->ext_csd.hs_max_dtr) 402 + max_dtr = card->ext_csd.hs_max_dtr; 403 + } else if (max_dtr > card->csd.max_dtr) { 404 + max_dtr = card->csd.max_dtr; 405 + } 406 + 407 + mmc_set_clock(host, max_dtr); 408 + 409 + host->card = card; 410 + 411 + mmc_release_host(host); 412 + 413 + err = mmc_register_card(card); 414 + if (err) 415 + goto reclaim_host; 416 + 417 + return 0; 418 + 419 + reclaim_host: 420 + mmc_claim_host(host); 421 + free_card: 422 + mmc_remove_card(card); 423 + host->card = NULL; 424 + err: 425 + mmc_detach_bus(host); 426 + mmc_release_host(host); 427 + 428 + return 0; 429 + } 430 +
+431
drivers/mmc/core/sd.c
··· 1 + /* 2 + * linux/drivers/mmc/sd.c 3 + * 4 + * Copyright (C) 2003-2004 Russell King, All Rights Reserved. 5 + * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved. 6 + * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved. 7 + * 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. 11 + */ 12 + 13 + #include <linux/err.h> 14 + 15 + #include <linux/mmc/host.h> 16 + #include <linux/mmc/card.h> 17 + #include <linux/mmc/mmc.h> 18 + 19 + #include "core.h" 20 + #include "sysfs.h" 21 + #include "mmc_ops.h" 22 + #include "sd_ops.h" 23 + 24 + #include "core.h" 25 + 26 + static const unsigned int tran_exp[] = { 27 + 10000, 100000, 1000000, 10000000, 28 + 0, 0, 0, 0 29 + }; 30 + 31 + static const unsigned char tran_mant[] = { 32 + 0, 10, 12, 13, 15, 20, 25, 30, 33 + 35, 40, 45, 50, 55, 60, 70, 80, 34 + }; 35 + 36 + static const unsigned int tacc_exp[] = { 37 + 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 38 + }; 39 + 40 + static const unsigned int tacc_mant[] = { 41 + 0, 10, 12, 13, 15, 20, 25, 30, 42 + 35, 40, 45, 50, 55, 60, 70, 80, 43 + }; 44 + 45 + #define UNSTUFF_BITS(resp,start,size) \ 46 + ({ \ 47 + const int __size = size; \ 48 + const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \ 49 + const int __off = 3 - ((start) / 32); \ 50 + const int __shft = (start) & 31; \ 51 + u32 __res; \ 52 + \ 53 + __res = resp[__off] >> __shft; \ 54 + if (__size + __shft > 32) \ 55 + __res |= resp[__off-1] << ((32 - __shft) % 32); \ 56 + __res & __mask; \ 57 + }) 58 + 59 + /* 60 + * Given the decoded CSD structure, decode the raw CID to our CID structure. 61 + */ 62 + static void mmc_decode_cid(struct mmc_card *card) 63 + { 64 + u32 *resp = card->raw_cid; 65 + 66 + memset(&card->cid, 0, sizeof(struct mmc_cid)); 67 + 68 + /* 69 + * SD doesn't currently have a version field so we will 70 + * have to assume we can parse this. 71 + */ 72 + card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); 73 + card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); 74 + card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); 75 + card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); 76 + card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); 77 + card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); 78 + card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); 79 + card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4); 80 + card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4); 81 + card->cid.serial = UNSTUFF_BITS(resp, 24, 32); 82 + card->cid.year = UNSTUFF_BITS(resp, 12, 8); 83 + card->cid.month = UNSTUFF_BITS(resp, 8, 4); 84 + 85 + card->cid.year += 2000; /* SD cards year offset */ 86 + } 87 + 88 + /* 89 + * Given a 128-bit response, decode to our card CSD structure. 90 + */ 91 + static void mmc_decode_csd(struct mmc_card *card) 92 + { 93 + struct mmc_csd *csd = &card->csd; 94 + unsigned int e, m, csd_struct; 95 + u32 *resp = card->raw_csd; 96 + 97 + csd_struct = UNSTUFF_BITS(resp, 126, 2); 98 + 99 + switch (csd_struct) { 100 + case 0: 101 + m = UNSTUFF_BITS(resp, 115, 4); 102 + e = UNSTUFF_BITS(resp, 112, 3); 103 + csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; 104 + csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; 105 + 106 + m = UNSTUFF_BITS(resp, 99, 4); 107 + e = UNSTUFF_BITS(resp, 96, 3); 108 + csd->max_dtr = tran_exp[e] * tran_mant[m]; 109 + csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); 110 + 111 + e = UNSTUFF_BITS(resp, 47, 3); 112 + m = UNSTUFF_BITS(resp, 62, 12); 113 + csd->capacity = (1 + m) << (e + 2); 114 + 115 + csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); 116 + csd->read_partial = UNSTUFF_BITS(resp, 79, 1); 117 + csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); 118 + csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); 119 + csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); 120 + csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); 121 + csd->write_partial = UNSTUFF_BITS(resp, 21, 1); 122 + break; 123 + case 1: 124 + /* 125 + * This is a block-addressed SDHC card. Most 126 + * interesting fields are unused and have fixed 127 + * values. To avoid getting tripped by buggy cards, 128 + * we assume those fixed values ourselves. 129 + */ 130 + mmc_card_set_blockaddr(card); 131 + 132 + csd->tacc_ns = 0; /* Unused */ 133 + csd->tacc_clks = 0; /* Unused */ 134 + 135 + m = UNSTUFF_BITS(resp, 99, 4); 136 + e = UNSTUFF_BITS(resp, 96, 3); 137 + csd->max_dtr = tran_exp[e] * tran_mant[m]; 138 + csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); 139 + 140 + m = UNSTUFF_BITS(resp, 48, 22); 141 + csd->capacity = (1 + m) << 10; 142 + 143 + csd->read_blkbits = 9; 144 + csd->read_partial = 0; 145 + csd->write_misalign = 0; 146 + csd->read_misalign = 0; 147 + csd->r2w_factor = 4; /* Unused */ 148 + csd->write_blkbits = 9; 149 + csd->write_partial = 0; 150 + break; 151 + default: 152 + printk("%s: unrecognised CSD structure version %d\n", 153 + mmc_hostname(card->host), csd_struct); 154 + mmc_card_set_bad(card); 155 + return; 156 + } 157 + } 158 + 159 + /* 160 + * Given a 64-bit response, decode to our card SCR structure. 161 + */ 162 + static void mmc_decode_scr(struct mmc_card *card) 163 + { 164 + struct sd_scr *scr = &card->scr; 165 + unsigned int scr_struct; 166 + u32 resp[4]; 167 + 168 + BUG_ON(!mmc_card_sd(card)); 169 + 170 + resp[3] = card->raw_scr[1]; 171 + resp[2] = card->raw_scr[0]; 172 + 173 + scr_struct = UNSTUFF_BITS(resp, 60, 4); 174 + if (scr_struct != 0) { 175 + printk("%s: unrecognised SCR structure version %d\n", 176 + mmc_hostname(card->host), scr_struct); 177 + mmc_card_set_bad(card); 178 + return; 179 + } 180 + 181 + scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4); 182 + scr->bus_widths = UNSTUFF_BITS(resp, 48, 4); 183 + } 184 + 185 + /* 186 + * Test if the card supports high-speed mode and, if so, switch to it. 187 + */ 188 + static int mmc_switch_hs(struct mmc_card *card) 189 + { 190 + int err; 191 + u8 *status; 192 + 193 + if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED)) 194 + return MMC_ERR_NONE; 195 + 196 + err = MMC_ERR_FAILED; 197 + 198 + status = kmalloc(64, GFP_KERNEL); 199 + if (!status) { 200 + printk("%s: could not allocate a buffer for switch " 201 + "capabilities.\n", 202 + mmc_hostname(card->host)); 203 + return err; 204 + } 205 + 206 + err = mmc_sd_switch(card, 0, 0, 1, status); 207 + if (err != MMC_ERR_NONE) { 208 + /* 209 + * Card not supporting high-speed will ignore the 210 + * command. 211 + */ 212 + if (err == MMC_ERR_TIMEOUT) 213 + err = MMC_ERR_NONE; 214 + goto out; 215 + } 216 + 217 + if (status[13] & 0x02) 218 + card->sw_caps.hs_max_dtr = 50000000; 219 + 220 + err = mmc_sd_switch(card, 1, 0, 1, status); 221 + if (err != MMC_ERR_NONE) 222 + goto out; 223 + 224 + if ((status[16] & 0xF) != 1) { 225 + printk(KERN_WARNING "%s: Problem switching card " 226 + "into high-speed mode!\n", 227 + mmc_hostname(card->host)); 228 + } else { 229 + mmc_card_set_highspeed(card); 230 + mmc_set_timing(card->host, MMC_TIMING_SD_HS); 231 + } 232 + 233 + out: 234 + kfree(status); 235 + 236 + return err; 237 + } 238 + 239 + /* 240 + * Host is being removed. Free up the current card. 241 + */ 242 + static void mmc_sd_remove(struct mmc_host *host) 243 + { 244 + BUG_ON(!host); 245 + BUG_ON(!host->card); 246 + 247 + mmc_remove_card(host->card); 248 + host->card = NULL; 249 + } 250 + 251 + /* 252 + * Card detection callback from host. 253 + */ 254 + static void mmc_sd_detect(struct mmc_host *host) 255 + { 256 + int err; 257 + 258 + BUG_ON(!host); 259 + BUG_ON(!host->card); 260 + 261 + mmc_claim_host(host); 262 + 263 + /* 264 + * Just check if our card has been removed. 265 + */ 266 + err = mmc_send_status(host->card, NULL); 267 + 268 + mmc_release_host(host); 269 + 270 + if (err != MMC_ERR_NONE) { 271 + mmc_remove_card(host->card); 272 + host->card = NULL; 273 + 274 + mmc_claim_host(host); 275 + mmc_detach_bus(host); 276 + mmc_release_host(host); 277 + } 278 + } 279 + 280 + static const struct mmc_bus_ops mmc_sd_ops = { 281 + .remove = mmc_sd_remove, 282 + .detect = mmc_sd_detect, 283 + }; 284 + 285 + /* 286 + * Starting point for SD card init. 287 + */ 288 + int mmc_attach_sd(struct mmc_host *host, u32 ocr) 289 + { 290 + struct mmc_card *card; 291 + int err; 292 + u32 cid[4]; 293 + unsigned int max_dtr; 294 + 295 + BUG_ON(!host); 296 + BUG_ON(!host->claimed); 297 + 298 + mmc_attach_bus(host, &mmc_sd_ops); 299 + 300 + host->ocr = mmc_select_voltage(host, ocr); 301 + 302 + /* 303 + * Can we support the voltage(s) of the card(s)? 304 + */ 305 + if (!host->ocr) 306 + goto err; 307 + 308 + /* 309 + * Since we're changing the OCR value, we seem to 310 + * need to tell some cards to go back to the idle 311 + * state. We wait 1ms to give cards time to 312 + * respond. 313 + */ 314 + mmc_go_idle(host); 315 + 316 + /* 317 + * If SD_SEND_IF_COND indicates an SD 2.0 318 + * compliant card and we should set bit 30 319 + * of the ocr to indicate that we can handle 320 + * block-addressed SDHC cards. 321 + */ 322 + err = mmc_send_if_cond(host, host->ocr); 323 + if (err == MMC_ERR_NONE) 324 + ocr = host->ocr | (1 << 30); 325 + 326 + mmc_send_app_op_cond(host, ocr, NULL); 327 + 328 + /* 329 + * Fetch CID from card. 330 + */ 331 + err = mmc_all_send_cid(host, cid); 332 + if (err != MMC_ERR_NONE) 333 + goto err; 334 + 335 + /* 336 + * Allocate card structure. 337 + */ 338 + card = mmc_alloc_card(host); 339 + if (IS_ERR(card)) 340 + goto err; 341 + 342 + card->type = MMC_TYPE_SD; 343 + memcpy(card->raw_cid, cid, sizeof(card->raw_cid)); 344 + 345 + /* 346 + * Set card RCA. 347 + */ 348 + err = mmc_send_relative_addr(host, &card->rca); 349 + if (err != MMC_ERR_NONE) 350 + goto free_card; 351 + 352 + mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); 353 + 354 + /* 355 + * Fetch CSD from card. 356 + */ 357 + err = mmc_send_csd(card, card->raw_csd); 358 + if (err != MMC_ERR_NONE) 359 + goto free_card; 360 + 361 + mmc_decode_csd(card); 362 + mmc_decode_cid(card); 363 + 364 + /* 365 + * Fetch SCR from card. 366 + */ 367 + err = mmc_select_card(card); 368 + if (err != MMC_ERR_NONE) 369 + goto free_card; 370 + 371 + err = mmc_app_send_scr(card, card->raw_scr); 372 + if (err != MMC_ERR_NONE) 373 + goto free_card; 374 + 375 + mmc_decode_scr(card); 376 + 377 + /* 378 + * Check if card can be switched into high-speed mode. 379 + */ 380 + err = mmc_switch_hs(card); 381 + if (err != MMC_ERR_NONE) 382 + goto free_card; 383 + 384 + /* 385 + * Compute bus speed. 386 + */ 387 + max_dtr = (unsigned int)-1; 388 + 389 + if (mmc_card_highspeed(card)) { 390 + if (max_dtr > card->sw_caps.hs_max_dtr) 391 + max_dtr = card->sw_caps.hs_max_dtr; 392 + } else if (max_dtr > card->csd.max_dtr) { 393 + max_dtr = card->csd.max_dtr; 394 + } 395 + 396 + mmc_set_clock(host, max_dtr); 397 + 398 + /* 399 + * Switch to wider bus (if supported). 400 + */ 401 + if ((host->caps && MMC_CAP_4_BIT_DATA) && 402 + (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) { 403 + err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4); 404 + if (err != MMC_ERR_NONE) 405 + goto free_card; 406 + 407 + mmc_set_bus_width(host, MMC_BUS_WIDTH_4); 408 + } 409 + 410 + host->card = card; 411 + 412 + mmc_release_host(host); 413 + 414 + err = mmc_register_card(card); 415 + if (err) 416 + goto reclaim_host; 417 + 418 + return 0; 419 + 420 + reclaim_host: 421 + mmc_claim_host(host); 422 + free_card: 423 + mmc_remove_card(card); 424 + host->card = NULL; 425 + err: 426 + mmc_detach_bus(host); 427 + mmc_release_host(host); 428 + 429 + return 0; 430 + } 431 +
+6 -1
include/linux/mmc/host.h
··· 131 131 unsigned int max_blk_count; /* maximum number of blocks in one req */ 132 132 133 133 /* private data */ 134 + spinlock_t lock; /* lock for claim and bus ops */ 135 + 134 136 struct mmc_ios ios; /* current io bus settings */ 135 137 u32 ocr; /* the current OCR setting */ 136 138 ··· 143 141 struct mmc_card *card; /* device attached to this host */ 144 142 145 143 wait_queue_head_t wq; 146 - spinlock_t lock; /* claimed lock */ 147 144 unsigned int claimed:1; /* host exclusively claimed */ 148 145 149 146 struct delayed_work detect; 150 147 #ifdef CONFIG_MMC_DEBUG 151 148 unsigned int removed:1; /* host is being removed */ 152 149 #endif 150 + 151 + const struct mmc_bus_ops *bus_ops; /* current bus driver */ 152 + unsigned int bus_refs; /* reference counter */ 153 + unsigned int bus_dead:1; /* bus has been released */ 153 154 154 155 unsigned long private[0] ____cacheline_aligned; 155 156 };