2 MMC/SD Card driver for OMAP 35xx (SDIO not supported)
4 This driver always produces a BlockIo protocol but it starts off with no Media
5 present. A TimerCallBack detects when media is inserted or removed and after
6 a media change event a call to BlockIo ReadBlocks/WriteBlocks will cause the
7 media to be detected (or removed) and the BlockIo Media structure will get
8 updated. No MMC/SD Card harward registers are updated until the first BlockIo
9 ReadBlocks/WriteBlocks after media has been insterted (booting with a card
10 plugged in counts as an insertion event).
12 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
14 This program and the accompanying materials
15 are licensed and made available under the terms and conditions of the BSD License
16 which accompanies this distribution. The full text of the license may be found at
17 http://opensource.org/licenses/bsd-license.php
19 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
20 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
28 EFI_BLOCK_IO_MEDIA gMMCHSMedia
= {
29 SIGNATURE_32('s','d','i','o'), // MediaId
30 TRUE
, // RemovableMedia
31 FALSE
, // MediaPresent
32 FALSE
, // LogicalPartition
34 FALSE
, // WriteCaching
42 VENDOR_DEVICE_PATH Mmc
;
46 MMCHS_DEVICE_PATH gMmcHsDevicePath
= {
50 (UINT8
)(sizeof(VENDOR_DEVICE_PATH
)),
51 (UINT8
)((sizeof(VENDOR_DEVICE_PATH
)) >> 8),
52 0xb615f1f5, 0x5088, 0x43cd, 0x80, 0x9c, 0xa1, 0x6e, 0x52, 0x48, 0x7d, 0x00
56 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
57 sizeof (EFI_DEVICE_PATH_PROTOCOL
),
63 EMBEDDED_EXTERNAL_DEVICE
*gTPS65950
;
64 EFI_EVENT gTimerEvent
;
65 BOOLEAN gMediaChange
= FALSE
;
80 gCardInfo
.CIDData
.MDT
= ((Response0
>> 8) & 0xFFF);
81 gCardInfo
.CIDData
.PSN
= (((Response0
>> 24) & 0xFF) | ((Response1
& 0xFFFFFF) << 8));
82 gCardInfo
.CIDData
.PRV
= ((Response1
>> 24) & 0xFF);
83 gCardInfo
.CIDData
.PNM
[4] = ((Response2
) & 0xFF);
84 gCardInfo
.CIDData
.PNM
[3] = ((Response2
>> 8) & 0xFF);
85 gCardInfo
.CIDData
.PNM
[2] = ((Response2
>> 16) & 0xFF);
86 gCardInfo
.CIDData
.PNM
[1] = ((Response2
>> 24) & 0xFF);
87 gCardInfo
.CIDData
.PNM
[0] = ((Response3
) & 0xFF);
88 gCardInfo
.CIDData
.OID
= ((Response3
>> 8) & 0xFFFF);
89 gCardInfo
.CIDData
.MID
= ((Response3
>> 24) & 0xFF);
94 UpdateMMCHSClkFrequency (
98 //Set Clock enable to 0x0 to not provide the clock to the card
99 MmioAnd32 (MMCHS_SYSCTL
, ~CEN
);
101 //Set new clock frequency.
102 MmioAndThenOr32 (MMCHS_SYSCTL
, ~CLKD_MASK
, NewCLKD
<< 6);
104 //Poll till Internal Clock Stable
105 while ((MmioRead32 (MMCHS_SYSCTL
) & ICS_MASK
) != ICS
);
107 //Set Clock enable to 0x1 to provide the clock to the card
108 MmioOr32 (MMCHS_SYSCTL
, CEN
);
115 UINTN CmdInterruptEnableVal
,
120 UINTN RetryCount
= 0;
122 //Check if command line is in use or not. Poll till command line is available.
123 while ((MmioRead32 (MMCHS_PSTATE
) & DATI_MASK
) == DATI_NOT_ALLOWED
);
125 //Provide the block size.
126 MmioWrite32 (MMCHS_BLK
, BLEN_512BYTES
);
128 //Setting Data timeout counter value to max value.
129 MmioAndThenOr32 (MMCHS_SYSCTL
, ~DTO_MASK
, DTO_VAL
);
131 //Clear Status register.
132 MmioWrite32 (MMCHS_STAT
, 0xFFFFFFFF);
134 //Set command argument register
135 MmioWrite32 (MMCHS_ARG
, CmdArgument
);
137 //Enable interrupt enable events to occur
138 MmioWrite32 (MMCHS_IE
, CmdInterruptEnableVal
);
141 MmioWrite32 (MMCHS_CMD
, Cmd
);
143 //Check for the command status.
144 while (RetryCount
< MAX_RETRY_COUNT
) {
146 MmcStatus
= MmioRead32 (MMCHS_STAT
);
147 } while (MmcStatus
== 0);
149 //Read status of command response
150 if ((MmcStatus
& ERRI
) != 0) {
152 //Perform soft-reset for mmci_cmd line.
153 MmioOr32 (MMCHS_SYSCTL
, SRC
);
154 while ((MmioRead32 (MMCHS_SYSCTL
) & SRC
));
156 DEBUG ((EFI_D_INFO
, "MmcStatus: %x\n", MmcStatus
));
157 return EFI_DEVICE_ERROR
;
160 //Check if command is completed.
161 if ((MmcStatus
& CC
) == CC
) {
162 MmioWrite32 (MMCHS_STAT
, CC
);
169 if (RetryCount
== MAX_RETRY_COUNT
) {
178 GetBlockInformation (
183 CSD_SDV2
*CsdSDV2Data
;
186 if (gCardInfo
.CardType
== SD_CARD_2_HIGH
) {
187 CsdSDV2Data
= (CSD_SDV2
*)&gCardInfo
.CSDData
;
189 //Populate BlockSize.
190 *BlockSize
= (0x1UL
<< CsdSDV2Data
->READ_BL_LEN
);
192 //Calculate Total number of blocks.
193 CardSize
= CsdSDV2Data
->C_SIZELow16
| (CsdSDV2Data
->C_SIZEHigh6
<< 2);
194 *NumBlocks
= ((CardSize
+ 1) * 1024);
196 //Populate BlockSize.
197 *BlockSize
= (0x1UL
<< gCardInfo
.CSDData
.READ_BL_LEN
);
199 //Calculate Total number of blocks.
200 CardSize
= gCardInfo
.CSDData
.C_SIZELow2
| (gCardInfo
.CSDData
.C_SIZEHigh10
<< 2);
201 *NumBlocks
= (CardSize
+ 1) * (1 << (gCardInfo
.CSDData
.C_SIZE_MULT
+ 2));
204 //For >=2G card, BlockSize may be 1K, but the transfer size is 512 bytes.
205 if (*BlockSize
> 512) {
206 *NumBlocks
= MultU64x32(*NumBlocks
, *BlockSize
/2);
210 DEBUG ((EFI_D_INFO
, "Card type: %x, BlockSize: %x, NumBlocks: %x\n", gCardInfo
.CardType
, *BlockSize
, *NumBlocks
));
216 UINTN
*ClockFrequencySelect
219 UINT8 MaxDataTransferRate
;
220 UINTN TransferRateValue
= 0;
221 UINTN TimeValue
= 0 ;
224 MaxDataTransferRate
= gCardInfo
.CSDData
.TRAN_SPEED
;
226 //Calculate Transfer rate unit (Bits 2:0 of TRAN_SPEED)
227 switch (MaxDataTransferRate
& 0x7) {
229 TransferRateValue
= 100 * 1000;
233 TransferRateValue
= 1 * 1000 * 1000;
237 TransferRateValue
= 10 * 1000 * 1000;
241 TransferRateValue
= 100 * 1000 * 1000;
245 DEBUG((EFI_D_ERROR
, "Invalid parameter.\n"));
249 //Calculate Time value (Bits 6:3 of TRAN_SPEED)
250 switch ((MaxDataTransferRate
>> 3) & 0xF) {
312 DEBUG((EFI_D_ERROR
, "Invalid parameter.\n"));
316 Frequency
= TransferRateValue
* TimeValue
/10;
318 //Calculate Clock divider value to program in MMCHS_SYSCTL[CLKD] field.
319 *ClockFrequencySelect
= ((MMC_REFERENCE_CLK
/Frequency
) + 1);
321 DEBUG ((EFI_D_INFO
, "MaxDataTransferRate: 0x%x, Frequency: %d KHz, ClockFrequencySelect: %x\n", MaxDataTransferRate
, Frequency
/1000, *ClockFrequencySelect
));
326 GetCardConfigurationData (
332 UINTN ClockFrequencySelect
;
334 //Calculate BlockSize and Total number of blocks in the detected card.
335 GetBlockInformation(&BlockSize
, &NumBlocks
);
336 gCardInfo
.BlockSize
= BlockSize
;
337 gCardInfo
.NumBlocks
= NumBlocks
;
339 //Calculate Card clock divider value.
340 CalculateCardCLKD(&ClockFrequencySelect
);
341 gCardInfo
.ClockFrequencySelect
= ClockFrequencySelect
;
353 //Select Device group to belong to P1 device group in Power IC.
355 Status
= gTPS65950
->Write (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4
, VMMC1_DEV_GRP
), 1, &Data
);
356 ASSERT_EFI_ERROR(Status
);
358 //Configure voltage regulator for MMC1 in Power IC to output 3.0 voltage.
360 Status
= gTPS65950
->Write (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4
, VMMC1_DEDICATED_REG
), 1, &Data
);
361 ASSERT_EFI_ERROR(Status
);
363 //After ramping up voltage, set VDDS stable bit to indicate that voltage level is stable.
364 MmioOr32 (CONTROL_PBIAS_LITE
, (PBIASLITEVMODE0
| PBIASLITEPWRDNZ0
| PBIASSPEEDCTRL0
| PBIASLITEVMODE1
| PBIASLITEWRDNZ1
));
367 MmioAndThenOr32 (GPIO1_BASE
+ GPIO_OE
, ~BIT23
, BIT23
);
369 // Enable Card Detect
370 Data
= CARD_DETECT_ENABLE
;
371 gTPS65950
->Write (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID2
, TPS65950_GPIO_CTRL
), 1, &Data
);
379 PerformCardIdenfication (
384 UINTN CmdArgument
= 0;
386 UINTN RetryCount
= 0;
387 BOOLEAN SDCmd8Supported
= FALSE
;
390 MmioWrite32 (MMCHS_IE
, (BADA_EN
| CERR_EN
| DEB_EN
| DCRC_EN
| DTO_EN
| CIE_EN
|
391 CEB_EN
| CCRC_EN
| CTO_EN
| BRR_EN
| BWR_EN
| TC_EN
| CC_EN
));
393 //Controller INIT procedure start.
394 MmioOr32 (MMCHS_CON
, INIT
);
395 MmioWrite32 (MMCHS_CMD
, 0x00000000);
396 while (!(MmioRead32 (MMCHS_STAT
) & CC
));
401 //Set CC bit to 0x1 to clear the flag
402 MmioOr32 (MMCHS_STAT
, CC
);
404 //Retry INIT procedure.
405 MmioWrite32 (MMCHS_CMD
, 0x00000000);
406 while (!(MmioRead32 (MMCHS_STAT
) & CC
));
408 //End initialization sequence
409 MmioAnd32 (MMCHS_CON
, ~INIT
);
411 MmioOr32 (MMCHS_HCTL
, (SDVS_3_0_V
| DTW_1_BIT
| SDBP_ON
));
413 //Change clock frequency to 400KHz to fit protocol
414 UpdateMMCHSClkFrequency(CLKD_400KHZ
);
416 MmioOr32 (MMCHS_CON
, OD
);
419 Status
= SendCmd (CMD0
, CMD0_INT_EN
, CmdArgument
);
420 if (EFI_ERROR(Status
)) {
421 DEBUG ((EFI_D_ERROR
, "Cmd0 fails.\n"));
425 DEBUG ((EFI_D_INFO
, "CMD0 response: %x\n", MmioRead32 (MMCHS_RSP10
)));
428 Status
= SendCmd (CMD5
, CMD5_INT_EN
, CmdArgument
);
429 if (Status
== EFI_SUCCESS
) {
430 DEBUG ((EFI_D_ERROR
, "CMD5 Success. SDIO card. Follow SDIO card specification.\n"));
431 DEBUG ((EFI_D_INFO
, "CMD5 response: %x\n", MmioRead32 (MMCHS_RSP10
)));
432 //NOTE: Returning unsupported error for now. Need to implement SDIO specification.
433 return EFI_UNSUPPORTED
;
435 DEBUG ((EFI_D_INFO
, "CMD5 fails. Not an SDIO card.\n"));
438 MmioOr32 (MMCHS_SYSCTL
, SRC
);
440 while ((MmioRead32 (MMCHS_SYSCTL
) & SRC
));
442 //Send CMD8 command. (New v2.00 command for Voltage check)
443 //Only 2.7V - 3.6V is supported for SD2.0, only SD 2.0 card can pass.
444 //MMC & SD1.1 card will fail this command.
445 CmdArgument
= CMD8_ARG
;
446 Status
= SendCmd (CMD8
, CMD8_INT_EN
, CmdArgument
);
447 if (Status
== EFI_SUCCESS
) {
448 Response
= MmioRead32 (MMCHS_RSP10
);
449 DEBUG ((EFI_D_INFO
, "CMD8 success. CMD8 response: %x\n", Response
));
450 if (Response
!= CmdArgument
) {
451 return EFI_DEVICE_ERROR
;
453 DEBUG ((EFI_D_INFO
, "Card is SD2.0\n"));
454 SDCmd8Supported
= TRUE
; //Supports high capacity.
456 DEBUG ((EFI_D_INFO
, "CMD8 fails. Not an SD2.0 card.\n"));
459 MmioOr32 (MMCHS_SYSCTL
, SRC
);
461 while ((MmioRead32 (MMCHS_SYSCTL
) & SRC
));
463 //Poll till card is busy
464 while (RetryCount
< MAX_RETRY_COUNT
) {
465 //Send CMD55 command.
467 Status
= SendCmd (CMD55
, CMD55_INT_EN
, CmdArgument
);
468 if (Status
== EFI_SUCCESS
) {
469 DEBUG ((EFI_D_INFO
, "CMD55 success. CMD55 response: %x\n", MmioRead32 (MMCHS_RSP10
)));
470 gCardInfo
.CardType
= SD_CARD
;
472 DEBUG ((EFI_D_INFO
, "CMD55 fails.\n"));
473 gCardInfo
.CardType
= MMC_CARD
;
476 //Send appropriate command for the card type which got detected.
477 if (gCardInfo
.CardType
== SD_CARD
) {
478 CmdArgument
= ((UINTN
*) &(gCardInfo
.OCRData
))[0];
481 if (SDCmd8Supported
) {
485 Status
= SendCmd (ACMD41
, ACMD41_INT_EN
, CmdArgument
);
486 if (EFI_ERROR(Status
)) {
487 DEBUG ((EFI_D_INFO
, "ACMD41 fails.\n"));
490 ((UINT32
*) &(gCardInfo
.OCRData
))[0] = MmioRead32 (MMCHS_RSP10
);
491 DEBUG ((EFI_D_INFO
, "SD card detected. ACMD41 OCR: %x\n", ((UINT32
*) &(gCardInfo
.OCRData
))[0]));
492 } else if (gCardInfo
.CardType
== MMC_CARD
) {
494 Status
= SendCmd (CMD1
, CMD1_INT_EN
, CmdArgument
);
495 if (EFI_ERROR(Status
)) {
496 DEBUG ((EFI_D_INFO
, "CMD1 fails.\n"));
499 Response
= MmioRead32 (MMCHS_RSP10
);
500 DEBUG ((EFI_D_INFO
, "MMC card detected.. CMD1 response: %x\n", Response
));
502 //NOTE: For now, I am skipping this since I only have an SD card.
503 //Compare card OCR and host OCR (Section 22.6.1.3.2.4)
504 return EFI_UNSUPPORTED
; //For now, MMC is not supported.
507 //Poll the card until it is out of its power-up sequence.
508 if (gCardInfo
.OCRData
.Busy
== 1) {
510 if (SDCmd8Supported
) {
511 gCardInfo
.CardType
= SD_CARD_2
;
514 //Card is ready. Check CCS (Card capacity status) bit (bit#30).
515 //SD 2.0 standard card will response with CCS 0, SD high capacity card will respond with CCS 1.
516 if (gCardInfo
.OCRData
.AccessMode
& BIT1
) {
517 gCardInfo
.CardType
= SD_CARD_2_HIGH
;
518 DEBUG ((EFI_D_INFO
, "High capacity card.\n"));
520 DEBUG ((EFI_D_INFO
, "Standard capacity card.\n"));
530 if (RetryCount
== MAX_RETRY_COUNT
) {
531 DEBUG ((EFI_D_ERROR
, "Timeout error. RetryCount: %d\n", RetryCount
));
537 Status
= SendCmd (CMD2
, CMD2_INT_EN
, CmdArgument
);
538 if (EFI_ERROR(Status
)) {
539 DEBUG ((EFI_D_ERROR
, "CMD2 fails. Status: %x\n", Status
));
543 DEBUG ((EFI_D_INFO
, "CMD2 response: %x %x %x %x\n", MmioRead32 (MMCHS_RSP10
), MmioRead32 (MMCHS_RSP32
), MmioRead32 (MMCHS_RSP54
), MmioRead32 (MMCHS_RSP76
)));
545 //Parse CID register data.
546 ParseCardCIDData(MmioRead32 (MMCHS_RSP10
), MmioRead32 (MMCHS_RSP32
), MmioRead32 (MMCHS_RSP54
), MmioRead32 (MMCHS_RSP76
));
550 Status
= SendCmd (CMD3
, CMD3_INT_EN
, CmdArgument
);
551 if (EFI_ERROR(Status
)) {
552 DEBUG ((EFI_D_ERROR
, "CMD3 fails. Status: %x\n", Status
));
556 //Set RCA for the detected card. RCA is CMD3 response.
557 gCardInfo
.RCA
= (MmioRead32 (MMCHS_RSP10
) >> 16);
558 DEBUG ((EFI_D_INFO
, "CMD3 response: RCA %x\n", gCardInfo
.RCA
));
560 //MMC Bus setting change after card identification.
561 MmioAnd32 (MMCHS_CON
, ~OD
);
562 MmioOr32 (MMCHS_HCTL
, SDVS_3_0_V
);
563 UpdateMMCHSClkFrequency(CLKD_400KHZ
); //Set the clock frequency to 400KHz.
570 GetCardSpecificData (
577 //Send CMD9 to retrieve CSD.
578 CmdArgument
= gCardInfo
.RCA
<< 16;
579 Status
= SendCmd (CMD9
, CMD9_INT_EN
, CmdArgument
);
580 if (EFI_ERROR(Status
)) {
581 DEBUG ((EFI_D_ERROR
, "CMD9 fails. Status: %x\n", Status
));
585 //Populate 128-bit CSD register data.
586 ((UINT32
*)&(gCardInfo
.CSDData
))[0] = MmioRead32 (MMCHS_RSP10
);
587 ((UINT32
*)&(gCardInfo
.CSDData
))[1] = MmioRead32 (MMCHS_RSP32
);
588 ((UINT32
*)&(gCardInfo
.CSDData
))[2] = MmioRead32 (MMCHS_RSP54
);
589 ((UINT32
*)&(gCardInfo
.CSDData
))[3] = MmioRead32 (MMCHS_RSP76
);
591 DEBUG ((EFI_D_INFO
, "CMD9 response: %x %x %x %x\n", MmioRead32 (MMCHS_RSP10
), MmioRead32 (MMCHS_RSP32
), MmioRead32 (MMCHS_RSP54
), MmioRead32 (MMCHS_RSP76
)));
593 //Calculate total number of blocks and max. data transfer rate supported by the detected card.
594 GetCardConfigurationData();
596 //Change MMCHS clock frequency to what detected card can support.
597 UpdateMMCHSClkFrequency(gCardInfo
.ClockFrequencySelect
);
604 PerformCardConfiguration (
608 UINTN CmdArgument
= 0;
612 CmdArgument
= gCardInfo
.RCA
<< 16;
613 Status
= SendCmd (CMD7
, CMD7_INT_EN
, CmdArgument
);
614 if (EFI_ERROR(Status
)) {
615 DEBUG ((EFI_D_ERROR
, "CMD7 fails. Status: %x\n", Status
));
619 //Send CMD16 to set the block length
620 CmdArgument
= gCardInfo
.BlockSize
;
621 Status
= SendCmd (CMD16
, CMD16_INT_EN
, CmdArgument
);
622 if (EFI_ERROR(Status
)) {
623 DEBUG ((EFI_D_ERROR
, "CMD16 fails. Status: %x\n", Status
));
633 IN EFI_BLOCK_IO_PROTOCOL
*This
,
638 UINTN
*DataBuffer
= Buffer
;
639 UINTN DataSize
= This
->Media
->BlockSize
/4;
641 UINTN RetryCount
= 0;
643 //Check controller status to make sure there is no error.
644 while (RetryCount
< MAX_RETRY_COUNT
) {
647 MmcStatus
= MmioRead32 (MMCHS_STAT
);
648 } while(MmcStatus
== 0);
650 //Check if Buffer read ready (BRR) bit is set?
651 if (MmcStatus
& BRR
) {
654 MmioOr32 (MMCHS_STAT
, BRR
);
656 //Read block worth of data.
657 for (Count
= 0; Count
< DataSize
; Count
++) {
658 *DataBuffer
++ = MmioRead32 (MMCHS_DATA
);
665 if (RetryCount
== MAX_RETRY_COUNT
) {
675 IN EFI_BLOCK_IO_PROTOCOL
*This
,
680 UINTN
*DataBuffer
= Buffer
;
681 UINTN DataSize
= This
->Media
->BlockSize
/4;
683 UINTN RetryCount
= 0;
685 //Check controller status to make sure there is no error.
686 while (RetryCount
< MAX_RETRY_COUNT
) {
689 MmcStatus
= MmioRead32 (MMCHS_STAT
);
690 } while(MmcStatus
== 0);
692 //Check if Buffer write ready (BWR) bit is set?
693 if (MmcStatus
& BWR
) {
696 MmioOr32 (MMCHS_STAT
, BWR
);
698 //Write block worth of data.
699 for (Count
= 0; Count
< DataSize
; Count
++) {
700 MmioWrite32 (MMCHS_DATA
, *DataBuffer
++);
708 if (RetryCount
== MAX_RETRY_COUNT
) {
718 IN EFI_BLOCK_IO_PROTOCOL
*This
,
720 IN OPERATION_TYPE OperationType
725 UINTN RetryCount
= 0;
727 //Read or Write data.
728 if (OperationType
== READ
) {
729 Status
= ReadBlockData(This
, Buffer
);
730 if (EFI_ERROR(Status
)) {
731 DEBUG((EFI_D_ERROR
, "ReadBlockData fails.\n"));
734 } else if (OperationType
== WRITE
) {
735 Status
= WriteBlockData(This
, Buffer
);
736 if (EFI_ERROR(Status
)) {
737 DEBUG((EFI_D_ERROR
, "WriteBlockData fails.\n"));
742 //Check for the Transfer completion.
743 while (RetryCount
< MAX_RETRY_COUNT
) {
746 MmcStatus
= MmioRead32 (MMCHS_STAT
);
747 } while (MmcStatus
== 0);
749 //Check if Transfer complete (TC) bit is set?
750 if (MmcStatus
& TC
) {
753 DEBUG ((EFI_D_ERROR
, "MmcStatus for TC: %x\n", MmcStatus
));
754 //Check if DEB, DCRC or DTO interrupt occured.
755 if ((MmcStatus
& DEB
) | (MmcStatus
& DCRC
) | (MmcStatus
& DTO
)) {
756 //There was an error during the data transfer.
758 //Set SRD bit to 1 and wait until it return to 0x0.
759 MmioOr32 (MMCHS_SYSCTL
, SRD
);
760 while((MmioRead32 (MMCHS_SYSCTL
) & SRD
) != 0x0);
762 return EFI_DEVICE_ERROR
;
768 if (RetryCount
== MAX_RETRY_COUNT
) {
769 DEBUG ((EFI_D_ERROR
, "TransferBlockData timed out.\n"));
785 // Card detect is a GPIO0 on the TPS65950
787 Status
= gTPS65950
->Read (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID2
, GPIODATAIN1
), 1, &Data
);
788 if (EFI_ERROR (Status
)) {
792 if ((Data
& CARD_DETECT_BIT
) == CARD_DETECT_BIT
) {
807 if (!CardPresent ()) {
811 //Initialize MMC host controller clocks.
812 Status
= InitializeMMCHS ();
813 if (EFI_ERROR(Status
)) {
814 DEBUG ((EFI_D_ERROR
, "Initialize MMC host controller fails. Status: %x\n", Status
));
818 //Software reset of the MMCHS host controller.
819 MmioWrite32 (MMCHS_SYSCONFIG
, SOFTRESET
);
821 while ((MmioRead32 (MMCHS_SYSSTATUS
) & RESETDONE_MASK
) != RESETDONE
);
823 //Soft reset for all.
824 MmioWrite32 (MMCHS_SYSCTL
, SRA
);
826 while ((MmioRead32 (MMCHS_SYSCTL
) & SRA
) != 0x0);
828 //Voltage capabilities initialization. Activate VS18 and VS30.
829 MmioOr32 (MMCHS_CAPA
, (VS30
| VS18
));
831 //Wakeup configuration
832 MmioOr32 (MMCHS_SYSCONFIG
, ENAWAKEUP
);
833 MmioOr32 (MMCHS_HCTL
, IWE
);
835 //MMCHS Controller default initialization
836 MmioOr32 (MMCHS_CON
, (OD
| DW8_1_4_BIT
| CEATA_OFF
));
838 MmioWrite32 (MMCHS_HCTL
, (SDVS_3_0_V
| DTW_1_BIT
| SDBP_OFF
));
840 //Enable internal clock
841 MmioOr32 (MMCHS_SYSCTL
, ICE
);
843 //Set the clock frequency to 80KHz.
844 UpdateMMCHSClkFrequency (CLKD_80KHZ
);
846 //Enable SD bus power.
847 MmioOr32 (MMCHS_HCTL
, (SDBP_ON
));
849 //Poll till SD bus power bit is set.
850 while ((MmioRead32 (MMCHS_HCTL
) & SDBP_MASK
) != SDBP_ON
);
853 Status
= PerformCardIdenfication ();
854 if (EFI_ERROR(Status
)) {
855 DEBUG ((EFI_D_ERROR
, "No MMC/SD card detected.\n"));
859 //Get CSD (Card specific data) for the detected card.
860 Status
= GetCardSpecificData();
861 if (EFI_ERROR(Status
)) {
865 //Configure the card in data transfer mode.
866 Status
= PerformCardConfiguration();
867 if (EFI_ERROR(Status
)) {
871 //Patch the Media structure.
872 gMMCHSMedia
.LastBlock
= (gCardInfo
.NumBlocks
- 1);
873 gMMCHSMedia
.BlockSize
= gCardInfo
.BlockSize
;
874 gMMCHSMedia
.ReadOnly
= (MmioRead32 (GPIO1_BASE
+ GPIO_DATAIN
) & BIT23
) == BIT23
;
875 gMMCHSMedia
.MediaPresent
= TRUE
;
876 gMMCHSMedia
.MediaId
++;
877 gMediaChange
= FALSE
;
885 IN EFI_BLOCK_IO_PROTOCOL
*This
,
889 IN OPERATION_TYPE OperationType
892 EFI_STATUS Status
= EFI_SUCCESS
;
893 UINTN RetryCount
= 0;
896 UINTN CmdInterruptEnable
= 0;
897 UINTN CmdArgument
= 0;
899 BOOLEAN MediaPresentLastTime
;
902 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
905 if (Buffer
== NULL
) {
906 Status
= EFI_INVALID_PARAMETER
;
911 MediaPresentLastTime
= gMMCHSMedia
.MediaPresent
;
914 Status
= DetectCard ();
915 if (EFI_ERROR (Status
)) {
916 // We detected a removal
917 gMMCHSMedia
.MediaPresent
= FALSE
;
918 gMMCHSMedia
.LastBlock
= 0;
919 gMMCHSMedia
.BlockSize
= 512; // Should be zero but there is a bug in DiskIo
920 gMMCHSMedia
.ReadOnly
= FALSE
;
924 gMediaChange
= FALSE
;
925 } else if (!gMMCHSMedia
.MediaPresent
) {
926 Status
= EFI_NO_MEDIA
;
930 if ((MediaPresentLastTime
!= gMMCHSMedia
.MediaPresent
) || Update
) {
931 gBS
->ReinstallProtocolInterface (
933 &gEfiBlockIoProtocolGuid
,
939 if (EFI_ERROR (Status
)) {
943 if (Lba
> This
->Media
->LastBlock
) {
944 Status
= EFI_INVALID_PARAMETER
;
948 if ((BufferSize
% This
->Media
->BlockSize
) != 0) {
949 Status
= EFI_BAD_BUFFER_SIZE
;
953 //Check if the data lines are not in use.
954 while ((RetryCount
++ < MAX_RETRY_COUNT
) && ((MmioRead32 (MMCHS_PSTATE
) & DATI_MASK
) != DATI_ALLOWED
));
955 if (RetryCount
== MAX_RETRY_COUNT
) {
956 Status
= EFI_TIMEOUT
;
960 //Populate the command information based on the operation type.
961 if (OperationType
== READ
) {
962 Cmd
= CMD17
; //Single block read
963 CmdInterruptEnable
= CMD17_INT_EN
;
964 } else if (OperationType
== WRITE
) {
965 Cmd
= CMD24
; //Single block write
966 CmdInterruptEnable
= CMD24_INT_EN
;
969 //Calculate total number of blocks its going to read.
970 NumBlocks
= (BufferSize
+ (This
->Media
->BlockSize
- 1))/This
->Media
->BlockSize
;
972 //Set command argument based on the card access mode (Byte mode or Block mode)
973 if (gCardInfo
.OCRData
.AccessMode
& BIT1
) {
974 CmdArgument
= (UINTN
)Lba
;
976 CmdArgument
= (UINTN
)Lba
* This
->Media
->BlockSize
;
981 Status
= SendCmd (Cmd
, CmdInterruptEnable
, CmdArgument
);
982 if (EFI_ERROR(Status
)) {
983 DEBUG ((EFI_D_ERROR
, "CMD fails. Status: %x\n", Status
));
987 //Transfer a block worth of data.
988 Status
= TransferBlockData(This
, Buffer
, OperationType
);
989 if (EFI_ERROR(Status
)) {
990 DEBUG ((EFI_D_ERROR
, "TransferBlockData fails. %x\n", Status
));
994 //Adjust command argument.
995 if (gCardInfo
.OCRData
.AccessMode
& BIT1
) {
996 CmdArgument
++; //Increase BlockIndex by one.
998 CmdArgument
+= This
->Media
->BlockSize
; //Increase BlockIndex by BlockSize
1002 Buffer
= (UINT8
*)Buffer
+ This
->Media
->BlockSize
;
1007 gBS
->RestoreTPL (OldTpl
);
1013 Reset the Block Device.
1015 @param This Indicates a pointer to the calling context.
1016 @param ExtendedVerification Driver may perform diagnostics on reset.
1018 @retval EFI_SUCCESS The device was reset.
1019 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
1026 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1027 IN BOOLEAN ExtendedVerification
1035 Read BufferSize bytes from Lba into Buffer.
1037 @param This Indicates a pointer to the calling context.
1038 @param MediaId Id of the media, changes every time the media is replaced.
1039 @param Lba The starting Logical Block Address to read from
1040 @param BufferSize Size of Buffer, must be a multiple of device block size.
1041 @param Buffer A pointer to the destination buffer for the data. The caller is
1042 responsible for either having implicit or explicit ownership of the buffer.
1044 @retval EFI_SUCCESS The data was read correctly from the device.
1045 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
1046 @retval EFI_NO_MEDIA There is no media in the device.
1047 @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.
1048 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
1049 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
1050 or the buffer is not on proper alignment.
1057 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1060 IN UINTN BufferSize
,
1066 //Perform Read operation.
1067 Status
= SdReadWrite (This
, (UINTN
)Lba
, Buffer
, BufferSize
, READ
);
1074 Write BufferSize bytes from Lba into Buffer.
1076 @param This Indicates a pointer to the calling context.
1077 @param MediaId The media ID that the write request is for.
1078 @param Lba The starting logical block address to be written. The caller is
1079 responsible for writing to only legitimate locations.
1080 @param BufferSize Size of Buffer, must be a multiple of device block size.
1081 @param Buffer A pointer to the source buffer for the data.
1083 @retval EFI_SUCCESS The data was written correctly to the device.
1084 @retval EFI_WRITE_PROTECTED The device can not be written to.
1085 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
1086 @retval EFI_NO_MEDIA There is no media in the device.
1087 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
1088 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
1089 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
1090 or the buffer is not on proper alignment.
1096 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1099 IN UINTN BufferSize
,
1105 //Perform write operation.
1106 Status
= SdReadWrite (This
, (UINTN
)Lba
, Buffer
, BufferSize
, WRITE
);
1113 Flush the Block Device.
1115 @param This Indicates a pointer to the calling context.
1117 @retval EFI_SUCCESS All outstanding data was written to the device
1118 @retval EFI_DEVICE_ERROR The device reported an error while writting back the data
1119 @retval EFI_NO_MEDIA There is no media in the device.
1125 IN EFI_BLOCK_IO_PROTOCOL
*This
1132 EFI_BLOCK_IO_PROTOCOL gBlockIo
= {
1133 EFI_BLOCK_IO_INTERFACE_REVISION
, // Revision
1134 &gMMCHSMedia
, // *Media
1135 MMCHSReset
, // Reset
1136 MMCHSReadBlocks
, // ReadBlocks
1137 MMCHSWriteBlocks
, // WriteBlocks
1138 MMCHSFlushBlocks
// FlushBlocks
1143 Timer callback to convert card present hardware into a boolean that indicates
1144 a media change event has happened. If you just check the GPIO you could see
1145 card 1 and then check again after card 1 was removed and card 2 was inserted
1146 and you would still see media present. Thus you need the timer tick to catch
1149 @param Event Event whose notification function is being invoked.
1150 @param Context The pointer to the notification function's context,
1151 which is implementation-dependent. Not used.
1163 Present
= CardPresent ();
1164 if (gMMCHSMedia
.MediaPresent
) {
1165 if (!Present
&& !gMediaChange
) {
1166 gMediaChange
= TRUE
;
1169 if (Present
&& !gMediaChange
) {
1170 gMediaChange
= TRUE
;
1179 IN EFI_HANDLE ImageHandle
,
1180 IN EFI_SYSTEM_TABLE
*SystemTable
1185 Status
= gBS
->LocateProtocol (&gEmbeddedExternalDeviceProtocolGuid
, NULL
, (VOID
**)&gTPS65950
);
1186 ASSERT_EFI_ERROR(Status
);
1188 ZeroMem (&gCardInfo
, sizeof (CARD_INFO
));
1190 Status
= gBS
->CreateEvent (EVT_TIMER
| EVT_NOTIFY_SIGNAL
, TPL_CALLBACK
, TimerCallback
, NULL
, &gTimerEvent
);
1191 ASSERT_EFI_ERROR (Status
);
1193 Status
= gBS
->SetTimer (gTimerEvent
, TimerPeriodic
, 1000000); // make me a PCD
1194 ASSERT_EFI_ERROR (Status
);
1197 Status
= gBS
->InstallMultipleProtocolInterfaces (
1199 &gEfiBlockIoProtocolGuid
, &gBlockIo
,
1200 &gEfiDevicePathProtocolGuid
, &gMmcHsDevicePath
,