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 SPDX-License-Identifier: BSD-2-Clause-Patent
20 EFI_BLOCK_IO_MEDIA gMMCHSMedia
= {
21 SIGNATURE_32('s','d','i','o'), // MediaId
22 TRUE
, // RemovableMedia
23 FALSE
, // MediaPresent
24 FALSE
, // LogicalPartition
26 FALSE
, // WriteCaching
34 VENDOR_DEVICE_PATH Mmc
;
38 MMCHS_DEVICE_PATH gMmcHsDevicePath
= {
44 (UINT8
)(sizeof(VENDOR_DEVICE_PATH
)),
45 (UINT8
)((sizeof(VENDOR_DEVICE_PATH
)) >> 8),
48 { 0xb615f1f5, 0x5088, 0x43cd, { 0x80, 0x9c, 0xa1, 0x6e, 0x52, 0x48, 0x7d, 0x00 } },
52 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
53 { sizeof (EFI_DEVICE_PATH_PROTOCOL
), 0 }
58 EMBEDDED_EXTERNAL_DEVICE
*gTPS65950
;
59 EFI_EVENT gTimerEvent
;
60 BOOLEAN gMediaChange
= FALSE
;
75 gCardInfo
.CIDData
.MDT
= ((Response0
>> 8) & 0xFFF);
76 gCardInfo
.CIDData
.PSN
= (((Response0
>> 24) & 0xFF) | ((Response1
& 0xFFFFFF) << 8));
77 gCardInfo
.CIDData
.PRV
= ((Response1
>> 24) & 0xFF);
78 gCardInfo
.CIDData
.PNM
[4] = ((Response2
) & 0xFF);
79 gCardInfo
.CIDData
.PNM
[3] = ((Response2
>> 8) & 0xFF);
80 gCardInfo
.CIDData
.PNM
[2] = ((Response2
>> 16) & 0xFF);
81 gCardInfo
.CIDData
.PNM
[1] = ((Response2
>> 24) & 0xFF);
82 gCardInfo
.CIDData
.PNM
[0] = ((Response3
) & 0xFF);
83 gCardInfo
.CIDData
.OID
= ((Response3
>> 8) & 0xFFFF);
84 gCardInfo
.CIDData
.MID
= ((Response3
>> 24) & 0xFF);
89 UpdateMMCHSClkFrequency (
93 //Set Clock enable to 0x0 to not provide the clock to the card
94 MmioAnd32 (MMCHS_SYSCTL
, ~CEN
);
96 //Set new clock frequency.
97 MmioAndThenOr32 (MMCHS_SYSCTL
, ~CLKD_MASK
, NewCLKD
<< 6);
99 //Poll till Internal Clock Stable
100 while ((MmioRead32 (MMCHS_SYSCTL
) & ICS_MASK
) != ICS
);
102 //Set Clock enable to 0x1 to provide the clock to the card
103 MmioOr32 (MMCHS_SYSCTL
, CEN
);
110 UINTN CmdInterruptEnableVal
,
115 UINTN RetryCount
= 0;
117 //Check if command line is in use or not. Poll till command line is available.
118 while ((MmioRead32 (MMCHS_PSTATE
) & DATI_MASK
) == DATI_NOT_ALLOWED
);
120 //Provide the block size.
121 MmioWrite32 (MMCHS_BLK
, BLEN_512BYTES
);
123 //Setting Data timeout counter value to max value.
124 MmioAndThenOr32 (MMCHS_SYSCTL
, ~DTO_MASK
, DTO_VAL
);
126 //Clear Status register.
127 MmioWrite32 (MMCHS_STAT
, 0xFFFFFFFF);
129 //Set command argument register
130 MmioWrite32 (MMCHS_ARG
, CmdArgument
);
132 //Enable interrupt enable events to occur
133 MmioWrite32 (MMCHS_IE
, CmdInterruptEnableVal
);
136 MmioWrite32 (MMCHS_CMD
, Cmd
);
138 //Check for the command status.
139 while (RetryCount
< MAX_RETRY_COUNT
) {
141 MmcStatus
= MmioRead32 (MMCHS_STAT
);
142 } while (MmcStatus
== 0);
144 //Read status of command response
145 if ((MmcStatus
& ERRI
) != 0) {
147 //Perform soft-reset for mmci_cmd line.
148 MmioOr32 (MMCHS_SYSCTL
, SRC
);
149 while ((MmioRead32 (MMCHS_SYSCTL
) & SRC
));
151 DEBUG ((EFI_D_INFO
, "MmcStatus: %x\n", MmcStatus
));
152 return EFI_DEVICE_ERROR
;
155 //Check if command is completed.
156 if ((MmcStatus
& CC
) == CC
) {
157 MmioWrite32 (MMCHS_STAT
, CC
);
164 if (RetryCount
== MAX_RETRY_COUNT
) {
173 GetBlockInformation (
178 CSD_SDV2
*CsdSDV2Data
;
181 if (gCardInfo
.CardType
== SD_CARD_2_HIGH
) {
182 CsdSDV2Data
= (CSD_SDV2
*)&gCardInfo
.CSDData
;
184 //Populate BlockSize.
185 *BlockSize
= (0x1UL
<< CsdSDV2Data
->READ_BL_LEN
);
187 //Calculate Total number of blocks.
188 CardSize
= CsdSDV2Data
->C_SIZELow16
| (CsdSDV2Data
->C_SIZEHigh6
<< 2);
189 *NumBlocks
= ((CardSize
+ 1) * 1024);
191 //Populate BlockSize.
192 *BlockSize
= (0x1UL
<< gCardInfo
.CSDData
.READ_BL_LEN
);
194 //Calculate Total number of blocks.
195 CardSize
= gCardInfo
.CSDData
.C_SIZELow2
| (gCardInfo
.CSDData
.C_SIZEHigh10
<< 2);
196 *NumBlocks
= (CardSize
+ 1) * (1 << (gCardInfo
.CSDData
.C_SIZE_MULT
+ 2));
199 //For >=2G card, BlockSize may be 1K, but the transfer size is 512 bytes.
200 if (*BlockSize
> 512) {
201 *NumBlocks
= MultU64x32(*NumBlocks
, *BlockSize
/2);
205 DEBUG ((EFI_D_INFO
, "Card type: %x, BlockSize: %x, NumBlocks: %x\n", gCardInfo
.CardType
, *BlockSize
, *NumBlocks
));
211 UINTN
*ClockFrequencySelect
214 UINT8 MaxDataTransferRate
;
215 UINTN TransferRateValue
= 0;
216 UINTN TimeValue
= 0 ;
219 MaxDataTransferRate
= gCardInfo
.CSDData
.TRAN_SPEED
;
221 // For SD Cards we would need to send CMD6 to set
222 // speeds abouve 25MHz. High Speed mode 50 MHz and up
224 //Calculate Transfer rate unit (Bits 2:0 of TRAN_SPEED)
225 switch (MaxDataTransferRate
& 0x7) {
227 TransferRateValue
= 100 * 1000;
231 TransferRateValue
= 1 * 1000 * 1000;
235 TransferRateValue
= 10 * 1000 * 1000;
239 TransferRateValue
= 100 * 1000 * 1000;
243 DEBUG((EFI_D_ERROR
, "Invalid parameter.\n"));
247 //Calculate Time value (Bits 6:3 of TRAN_SPEED)
248 switch ((MaxDataTransferRate
>> 3) & 0xF) {
310 DEBUG((EFI_D_ERROR
, "Invalid parameter.\n"));
314 Frequency
= TransferRateValue
* TimeValue
/10;
316 //Calculate Clock divider value to program in MMCHS_SYSCTL[CLKD] field.
317 *ClockFrequencySelect
= ((MMC_REFERENCE_CLK
/Frequency
) + 1);
319 DEBUG ((EFI_D_INFO
, "MaxDataTransferRate: 0x%x, Frequency: %d KHz, ClockFrequencySelect: %x\n", MaxDataTransferRate
, Frequency
/1000, *ClockFrequencySelect
));
324 GetCardConfigurationData (
330 UINTN ClockFrequencySelect
;
332 //Calculate BlockSize and Total number of blocks in the detected card.
333 GetBlockInformation(&BlockSize
, &NumBlocks
);
334 gCardInfo
.BlockSize
= BlockSize
;
335 gCardInfo
.NumBlocks
= NumBlocks
;
337 //Calculate Card clock divider value.
338 CalculateCardCLKD(&ClockFrequencySelect
);
339 gCardInfo
.ClockFrequencySelect
= ClockFrequencySelect
;
351 //Select Device group to belong to P1 device group in Power IC.
353 Status
= gTPS65950
->Write (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4
, VMMC1_DEV_GRP
), 1, &Data
);
354 ASSERT_EFI_ERROR(Status
);
356 //Configure voltage regulator for MMC1 in Power IC to output 3.0 voltage.
358 Status
= gTPS65950
->Write (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4
, VMMC1_DEDICATED_REG
), 1, &Data
);
359 ASSERT_EFI_ERROR(Status
);
361 //After ramping up voltage, set VDDS stable bit to indicate that voltage level is stable.
362 MmioOr32 (CONTROL_PBIAS_LITE
, (PBIASLITEVMODE0
| PBIASLITEPWRDNZ0
| PBIASSPEEDCTRL0
| PBIASLITEVMODE1
| PBIASLITEWRDNZ1
));
365 MmioAndThenOr32 (GPIO1_BASE
+ GPIO_OE
, ~BIT23
, BIT23
);
367 // Enable Card Detect
368 Data
= CARD_DETECT_ENABLE
;
369 gTPS65950
->Write (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID2
, TPS65950_GPIO_CTRL
), 1, &Data
);
377 PerformCardIdenfication (
382 UINTN CmdArgument
= 0;
384 UINTN RetryCount
= 0;
385 BOOLEAN SDCmd8Supported
= FALSE
;
388 MmioWrite32 (MMCHS_IE
, (BADA_EN
| CERR_EN
| DEB_EN
| DCRC_EN
| DTO_EN
| CIE_EN
|
389 CEB_EN
| CCRC_EN
| CTO_EN
| BRR_EN
| BWR_EN
| TC_EN
| CC_EN
));
391 //Controller INIT procedure start.
392 MmioOr32 (MMCHS_CON
, INIT
);
393 MmioWrite32 (MMCHS_CMD
, 0x00000000);
394 while (!(MmioRead32 (MMCHS_STAT
) & CC
));
399 //Set CC bit to 0x1 to clear the flag
400 MmioOr32 (MMCHS_STAT
, CC
);
402 //Retry INIT procedure.
403 MmioWrite32 (MMCHS_CMD
, 0x00000000);
404 while (!(MmioRead32 (MMCHS_STAT
) & CC
));
406 //End initialization sequence
407 MmioAnd32 (MMCHS_CON
, ~INIT
);
409 MmioOr32 (MMCHS_HCTL
, (SDVS_3_0_V
| DTW_1_BIT
| SDBP_ON
));
411 //Change clock frequency to 400KHz to fit protocol
412 UpdateMMCHSClkFrequency(CLKD_400KHZ
);
414 MmioOr32 (MMCHS_CON
, OD
);
417 Status
= SendCmd (CMD0
, CMD0_INT_EN
, CmdArgument
);
418 if (EFI_ERROR(Status
)) {
419 DEBUG ((EFI_D_ERROR
, "Cmd0 fails.\n"));
423 DEBUG ((EFI_D_INFO
, "CMD0 response: %x\n", MmioRead32 (MMCHS_RSP10
)));
426 Status
= SendCmd (CMD5
, CMD5_INT_EN
, CmdArgument
);
427 if (Status
== EFI_SUCCESS
) {
428 DEBUG ((EFI_D_ERROR
, "CMD5 Success. SDIO card. Follow SDIO card specification.\n"));
429 DEBUG ((EFI_D_INFO
, "CMD5 response: %x\n", MmioRead32 (MMCHS_RSP10
)));
430 //NOTE: Returning unsupported error for now. Need to implement SDIO specification.
431 return EFI_UNSUPPORTED
;
433 DEBUG ((EFI_D_INFO
, "CMD5 fails. Not an SDIO card.\n"));
436 MmioOr32 (MMCHS_SYSCTL
, SRC
);
438 while ((MmioRead32 (MMCHS_SYSCTL
) & SRC
));
440 //Send CMD8 command. (New v2.00 command for Voltage check)
441 //Only 2.7V - 3.6V is supported for SD2.0, only SD 2.0 card can pass.
442 //MMC & SD1.1 card will fail this command.
443 CmdArgument
= CMD8_ARG
;
444 Status
= SendCmd (CMD8
, CMD8_INT_EN
, CmdArgument
);
445 if (Status
== EFI_SUCCESS
) {
446 Response
= MmioRead32 (MMCHS_RSP10
);
447 DEBUG ((EFI_D_INFO
, "CMD8 success. CMD8 response: %x\n", Response
));
448 if (Response
!= CmdArgument
) {
449 return EFI_DEVICE_ERROR
;
451 DEBUG ((EFI_D_INFO
, "Card is SD2.0\n"));
452 SDCmd8Supported
= TRUE
; //Supports high capacity.
454 DEBUG ((EFI_D_INFO
, "CMD8 fails. Not an SD2.0 card.\n"));
457 MmioOr32 (MMCHS_SYSCTL
, SRC
);
459 while ((MmioRead32 (MMCHS_SYSCTL
) & SRC
));
461 //Poll till card is busy
462 while (RetryCount
< MAX_RETRY_COUNT
) {
463 //Send CMD55 command.
465 Status
= SendCmd (CMD55
, CMD55_INT_EN
, CmdArgument
);
466 if (Status
== EFI_SUCCESS
) {
467 DEBUG ((EFI_D_INFO
, "CMD55 success. CMD55 response: %x\n", MmioRead32 (MMCHS_RSP10
)));
468 gCardInfo
.CardType
= SD_CARD
;
470 DEBUG ((EFI_D_INFO
, "CMD55 fails.\n"));
471 gCardInfo
.CardType
= MMC_CARD
;
474 //Send appropriate command for the card type which got detected.
475 if (gCardInfo
.CardType
== SD_CARD
) {
476 CmdArgument
= ((UINTN
*) &(gCardInfo
.OCRData
))[0];
479 if (SDCmd8Supported
) {
483 Status
= SendCmd (ACMD41
, ACMD41_INT_EN
, CmdArgument
);
484 if (EFI_ERROR(Status
)) {
485 DEBUG ((EFI_D_INFO
, "ACMD41 fails.\n"));
488 ((UINT32
*) &(gCardInfo
.OCRData
))[0] = MmioRead32 (MMCHS_RSP10
);
489 DEBUG ((EFI_D_INFO
, "SD card detected. ACMD41 OCR: %x\n", ((UINT32
*) &(gCardInfo
.OCRData
))[0]));
490 } else if (gCardInfo
.CardType
== MMC_CARD
) {
492 Status
= SendCmd (CMD1
, CMD1_INT_EN
, CmdArgument
);
493 if (EFI_ERROR(Status
)) {
494 DEBUG ((EFI_D_INFO
, "CMD1 fails.\n"));
497 Response
= MmioRead32 (MMCHS_RSP10
);
498 DEBUG ((EFI_D_INFO
, "MMC card detected.. CMD1 response: %x\n", Response
));
500 //NOTE: For now, I am skipping this since I only have an SD card.
501 //Compare card OCR and host OCR (Section 22.6.1.3.2.4)
502 return EFI_UNSUPPORTED
; //For now, MMC is not supported.
505 //Poll the card until it is out of its power-up sequence.
506 if (gCardInfo
.OCRData
.Busy
== 1) {
508 if (SDCmd8Supported
) {
509 gCardInfo
.CardType
= SD_CARD_2
;
512 //Card is ready. Check CCS (Card capacity status) bit (bit#30).
513 //SD 2.0 standard card will response with CCS 0, SD high capacity card will respond with CCS 1.
514 if (gCardInfo
.OCRData
.AccessMode
& BIT1
) {
515 gCardInfo
.CardType
= SD_CARD_2_HIGH
;
516 DEBUG ((EFI_D_INFO
, "High capacity card.\n"));
518 DEBUG ((EFI_D_INFO
, "Standard capacity card.\n"));
528 if (RetryCount
== MAX_RETRY_COUNT
) {
529 DEBUG ((EFI_D_ERROR
, "Timeout error. RetryCount: %d\n", RetryCount
));
535 Status
= SendCmd (CMD2
, CMD2_INT_EN
, CmdArgument
);
536 if (EFI_ERROR(Status
)) {
537 DEBUG ((EFI_D_ERROR
, "CMD2 fails. Status: %x\n", Status
));
541 DEBUG ((EFI_D_INFO
, "CMD2 response: %x %x %x %x\n", MmioRead32 (MMCHS_RSP10
), MmioRead32 (MMCHS_RSP32
), MmioRead32 (MMCHS_RSP54
), MmioRead32 (MMCHS_RSP76
)));
543 //Parse CID register data.
544 ParseCardCIDData(MmioRead32 (MMCHS_RSP10
), MmioRead32 (MMCHS_RSP32
), MmioRead32 (MMCHS_RSP54
), MmioRead32 (MMCHS_RSP76
));
548 Status
= SendCmd (CMD3
, CMD3_INT_EN
, CmdArgument
);
549 if (EFI_ERROR(Status
)) {
550 DEBUG ((EFI_D_ERROR
, "CMD3 fails. Status: %x\n", Status
));
554 //Set RCA for the detected card. RCA is CMD3 response.
555 gCardInfo
.RCA
= (MmioRead32 (MMCHS_RSP10
) >> 16);
556 DEBUG ((EFI_D_INFO
, "CMD3 response: RCA %x\n", gCardInfo
.RCA
));
558 //MMC Bus setting change after card identification.
559 MmioAnd32 (MMCHS_CON
, ~OD
);
560 MmioOr32 (MMCHS_HCTL
, SDVS_3_0_V
);
561 UpdateMMCHSClkFrequency(CLKD_400KHZ
); //Set the clock frequency to 400KHz.
568 GetCardSpecificData (
575 //Send CMD9 to retrieve CSD.
576 CmdArgument
= gCardInfo
.RCA
<< 16;
577 Status
= SendCmd (CMD9
, CMD9_INT_EN
, CmdArgument
);
578 if (EFI_ERROR(Status
)) {
579 DEBUG ((EFI_D_ERROR
, "CMD9 fails. Status: %x\n", Status
));
583 //Populate 128-bit CSD register data.
584 ((UINT32
*)&(gCardInfo
.CSDData
))[0] = MmioRead32 (MMCHS_RSP10
);
585 ((UINT32
*)&(gCardInfo
.CSDData
))[1] = MmioRead32 (MMCHS_RSP32
);
586 ((UINT32
*)&(gCardInfo
.CSDData
))[2] = MmioRead32 (MMCHS_RSP54
);
587 ((UINT32
*)&(gCardInfo
.CSDData
))[3] = MmioRead32 (MMCHS_RSP76
);
589 DEBUG ((EFI_D_INFO
, "CMD9 response: %x %x %x %x\n", MmioRead32 (MMCHS_RSP10
), MmioRead32 (MMCHS_RSP32
), MmioRead32 (MMCHS_RSP54
), MmioRead32 (MMCHS_RSP76
)));
591 //Calculate total number of blocks and max. data transfer rate supported by the detected card.
592 GetCardConfigurationData();
599 PerformCardConfiguration (
603 UINTN CmdArgument
= 0;
607 CmdArgument
= gCardInfo
.RCA
<< 16;
608 Status
= SendCmd (CMD7
, CMD7_INT_EN
, CmdArgument
);
609 if (EFI_ERROR(Status
)) {
610 DEBUG ((EFI_D_ERROR
, "CMD7 fails. Status: %x\n", Status
));
614 if ((gCardInfo
.CardType
!= UNKNOWN_CARD
) && (gCardInfo
.CardType
!= MMC_CARD
)) {
615 // We could read SCR register, but SD Card Phys spec stats any SD Card shall
616 // set SCR.SD_BUS_WIDTHS to support 4-bit mode, so why bother?
618 // Send ACMD6 (application specific commands must be prefixed with CMD55)
619 Status
= SendCmd (CMD55
, CMD55_INT_EN
, CmdArgument
);
620 if (!EFI_ERROR (Status
)) {
621 // set device into 4-bit data bus mode
622 Status
= SendCmd (ACMD6
, ACMD6_INT_EN
, 0x2);
623 if (!EFI_ERROR (Status
)) {
624 // Set host controler into 4-bit mode
625 MmioOr32 (MMCHS_HCTL
, DTW_4_BIT
);
626 DEBUG ((EFI_D_INFO
, "SD Memory Card set to 4-bit mode\n"));
631 //Send CMD16 to set the block length
632 CmdArgument
= gCardInfo
.BlockSize
;
633 Status
= SendCmd (CMD16
, CMD16_INT_EN
, CmdArgument
);
634 if (EFI_ERROR(Status
)) {
635 DEBUG ((EFI_D_ERROR
, "CMD16 fails. Status: %x\n", Status
));
639 //Change MMCHS clock frequency to what detected card can support.
640 UpdateMMCHSClkFrequency(gCardInfo
.ClockFrequencySelect
);
648 IN EFI_BLOCK_IO_PROTOCOL
*This
,
653 UINTN
*DataBuffer
= Buffer
;
654 UINTN DataSize
= This
->Media
->BlockSize
/4;
656 UINTN RetryCount
= 0;
658 //Check controller status to make sure there is no error.
659 while (RetryCount
< MAX_RETRY_COUNT
) {
662 MmcStatus
= MmioRead32 (MMCHS_STAT
);
663 } while(MmcStatus
== 0);
665 //Check if Buffer read ready (BRR) bit is set?
666 if (MmcStatus
& BRR
) {
669 MmioOr32 (MMCHS_STAT
, BRR
);
671 //Read block worth of data.
672 for (Count
= 0; Count
< DataSize
; Count
++) {
673 *DataBuffer
++ = MmioRead32 (MMCHS_DATA
);
680 if (RetryCount
== MAX_RETRY_COUNT
) {
690 IN EFI_BLOCK_IO_PROTOCOL
*This
,
695 UINTN
*DataBuffer
= Buffer
;
696 UINTN DataSize
= This
->Media
->BlockSize
/4;
698 UINTN RetryCount
= 0;
700 //Check controller status to make sure there is no error.
701 while (RetryCount
< MAX_RETRY_COUNT
) {
704 MmcStatus
= MmioRead32 (MMCHS_STAT
);
705 } while(MmcStatus
== 0);
707 //Check if Buffer write ready (BWR) bit is set?
708 if (MmcStatus
& BWR
) {
711 MmioOr32 (MMCHS_STAT
, BWR
);
713 //Write block worth of data.
714 for (Count
= 0; Count
< DataSize
; Count
++) {
715 MmioWrite32 (MMCHS_DATA
, *DataBuffer
++);
723 if (RetryCount
== MAX_RETRY_COUNT
) {
732 IN EFI_BLOCK_IO_PROTOCOL
*This
,
736 IN OPERATION_TYPE OperationType
742 UINTN CmdInterruptEnable
;
745 EFI_PHYSICAL_ADDRESS BufferAddress
;
747 DMA_MAP_OPERATION DmaOperation
;
748 EFI_STATUS MmcStatus
;
749 UINTN RetryCount
= 0;
752 // Map passed in buffer for DMA xfer
753 DmaSize
= BlockCount
* This
->Media
->BlockSize
;
754 Status
= DmaMap (DmaOperation
, Buffer
, &DmaSize
, &BufferAddress
, &BufferMap
);
755 if (EFI_ERROR (Status
)) {
759 ZeroMem (&DmaOperation
, sizeof (DMA_MAP_OPERATION
));
762 Dma4
.DataType
= 2; // DMA4_CSDPi[1:0] 32-bit elements from MMCHS_DATA
764 Dma4
.SourceEndiansim
= 0; // DMA4_CSDPi[21]
766 Dma4
.DestinationEndianism
= 0; // DMA4_CSDPi[19]
768 Dma4
.SourcePacked
= 0; // DMA4_CSDPi[6]
770 Dma4
.DestinationPacked
= 0; // DMA4_CSDPi[13]
772 Dma4
.NumberOfElementPerFrame
= This
->Media
->BlockSize
/4; // DMA4_CENi (TRM 4K is optimum value)
774 Dma4
.NumberOfFramePerTransferBlock
= BlockCount
; // DMA4_CFNi
776 Dma4
.ReadPriority
= 0; // DMA4_CCRi[6] Low priority read
778 Dma4
.WritePriority
= 0; // DMA4_CCRi[23] Prefetech disabled
781 //Populate the command information based on the operation type.
782 if (OperationType
== READ
) {
783 Cmd
= CMD18
; //Multiple block read
784 CmdInterruptEnable
= CMD18_INT_EN
;
785 DmaOperation
= MapOperationBusMasterCommonBuffer
;
787 Dma4
.ReadPortAccessType
=0 ; // DMA4_CSDPi[8:7] Can not burst MMCHS_DATA reg
789 Dma4
.WritePortAccessType
= 3; // DMA4_CSDPi[15:14] Memory burst 16x32
791 Dma4
.WriteMode
= 1; // DMA4_CSDPi[17:16] Write posted
795 Dma4
.SourceStartAddress
= MMCHS_DATA
; // DMA4_CSSAi
797 Dma4
.DestinationStartAddress
= (UINT32
)BufferAddress
; // DMA4_CDSAi
799 Dma4
.SourceElementIndex
= 1; // DMA4_CSEi
801 Dma4
.SourceFrameIndex
= 0x200; // DMA4_CSFi
803 Dma4
.DestinationElementIndex
= 1; // DMA4_CDEi
805 Dma4
.DestinationFrameIndex
= 0; // DMA4_CDFi
809 Dma4
.ReadPortAccessMode
= 0; // DMA4_CCRi[13:12] Always read MMCHS_DATA
811 Dma4
.WritePortAccessMode
= 1; // DMA4_CCRi[15:14] Post increment memory address
813 Dma4
.ReadRequestNumber
= 0x1e; // DMA4_CCRi[4:0] Syncro with MMCA_DMA_RX (61)
815 Dma4
.WriteRequestNumber
= 1; // DMA4_CCRi[20:19] Syncro upper 0x3e == 62 (one based)
817 } else if (OperationType
== WRITE
) {
818 Cmd
= CMD25
; //Multiple block write
819 CmdInterruptEnable
= CMD25_INT_EN
;
820 DmaOperation
= MapOperationBusMasterRead
;
822 Dma4
.ReadPortAccessType
= 3; // DMA4_CSDPi[8:7] Memory burst 16x32
824 Dma4
.WritePortAccessType
= 0; // DMA4_CSDPi[15:14] Can not burst MMCHS_DATA reg
826 Dma4
.WriteMode
= 1; // DMA4_CSDPi[17:16] Write posted ???
830 Dma4
.SourceStartAddress
= (UINT32
)BufferAddress
; // DMA4_CSSAi
832 Dma4
.DestinationStartAddress
= MMCHS_DATA
; // DMA4_CDSAi
834 Dma4
.SourceElementIndex
= 1; // DMA4_CSEi
836 Dma4
.SourceFrameIndex
= 0x200; // DMA4_CSFi
838 Dma4
.DestinationElementIndex
= 1; // DMA4_CDEi
840 Dma4
.DestinationFrameIndex
= 0; // DMA4_CDFi
844 Dma4
.ReadPortAccessMode
= 1; // DMA4_CCRi[13:12] Post increment memory address
846 Dma4
.WritePortAccessMode
= 0; // DMA4_CCRi[15:14] Always write MMCHS_DATA
848 Dma4
.ReadRequestNumber
= 0x1d; // DMA4_CCRi[4:0] Syncro with MMCA_DMA_TX (60)
850 Dma4
.WriteRequestNumber
= 1; // DMA4_CCRi[20:19] Syncro upper 0x3d == 61 (one based)
853 return EFI_INVALID_PARAMETER
;
857 EnableDmaChannel (2, &Dma4
);
860 //Set command argument based on the card access mode (Byte mode or Block mode)
861 if (gCardInfo
.OCRData
.AccessMode
& BIT1
) {
864 CmdArgument
= Lba
* This
->Media
->BlockSize
;
868 Status
= SendCmd (Cmd
, CmdInterruptEnable
, CmdArgument
);
869 if (EFI_ERROR (Status
)) {
870 DEBUG ((EFI_D_ERROR
, "CMD fails. Status: %x\n", Status
));
874 //Check for the Transfer completion.
875 while (RetryCount
< MAX_RETRY_COUNT
) {
878 MmcStatus
= MmioRead32 (MMCHS_STAT
);
879 } while (MmcStatus
== 0);
881 //Check if Transfer complete (TC) bit is set?
882 if (MmcStatus
& TC
) {
885 DEBUG ((EFI_D_ERROR
, "MmcStatus for TC: %x\n", MmcStatus
));
886 //Check if DEB, DCRC or DTO interrupt occured.
887 if ((MmcStatus
& DEB
) | (MmcStatus
& DCRC
) | (MmcStatus
& DTO
)) {
888 //There was an error during the data transfer.
890 //Set SRD bit to 1 and wait until it return to 0x0.
891 MmioOr32 (MMCHS_SYSCTL
, SRD
);
892 while((MmioRead32 (MMCHS_SYSCTL
) & SRD
) != 0x0);
894 DisableDmaChannel (2, DMA4_CSR_BLOCK
, DMA4_CSR_ERR
);
895 DmaUnmap (BufferMap
);
896 return EFI_DEVICE_ERROR
;
902 DisableDmaChannel (2, DMA4_CSR_BLOCK
, DMA4_CSR_ERR
);
903 Status
= DmaUnmap (BufferMap
);
905 if (RetryCount
== MAX_RETRY_COUNT
) {
906 DEBUG ((EFI_D_ERROR
, "TransferBlockData timed out.\n"));
916 IN EFI_BLOCK_IO_PROTOCOL
*This
,
919 IN OPERATION_TYPE OperationType
924 UINTN RetryCount
= 0;
926 UINTN CmdInterruptEnable
= 0;
927 UINTN CmdArgument
= 0;
930 //Populate the command information based on the operation type.
931 if (OperationType
== READ
) {
932 Cmd
= CMD17
; //Single block read
933 CmdInterruptEnable
= CMD18_INT_EN
;
934 } else if (OperationType
== WRITE
) {
935 Cmd
= CMD24
; //Single block write
936 CmdInterruptEnable
= CMD24_INT_EN
;
939 //Set command argument based on the card access mode (Byte mode or Block mode)
940 if (gCardInfo
.OCRData
.AccessMode
& BIT1
) {
943 CmdArgument
= Lba
* This
->Media
->BlockSize
;
947 Status
= SendCmd (Cmd
, CmdInterruptEnable
, CmdArgument
);
948 if (EFI_ERROR(Status
)) {
949 DEBUG ((EFI_D_ERROR
, "CMD fails. Status: %x\n", Status
));
953 //Read or Write data.
954 if (OperationType
== READ
) {
955 Status
= ReadBlockData (This
, Buffer
);
956 if (EFI_ERROR(Status
)) {
957 DEBUG((EFI_D_ERROR
, "ReadBlockData fails.\n"));
960 } else if (OperationType
== WRITE
) {
961 Status
= WriteBlockData (This
, Buffer
);
962 if (EFI_ERROR(Status
)) {
963 DEBUG((EFI_D_ERROR
, "WriteBlockData fails.\n"));
968 //Check for the Transfer completion.
969 while (RetryCount
< MAX_RETRY_COUNT
) {
972 MmcStatus
= MmioRead32 (MMCHS_STAT
);
973 } while (MmcStatus
== 0);
975 //Check if Transfer complete (TC) bit is set?
976 if (MmcStatus
& TC
) {
979 DEBUG ((EFI_D_ERROR
, "MmcStatus for TC: %x\n", MmcStatus
));
980 //Check if DEB, DCRC or DTO interrupt occured.
981 if ((MmcStatus
& DEB
) | (MmcStatus
& DCRC
) | (MmcStatus
& DTO
)) {
982 //There was an error during the data transfer.
984 //Set SRD bit to 1 and wait until it return to 0x0.
985 MmioOr32 (MMCHS_SYSCTL
, SRD
);
986 while((MmioRead32 (MMCHS_SYSCTL
) & SRD
) != 0x0);
988 return EFI_DEVICE_ERROR
;
994 if (RetryCount
== MAX_RETRY_COUNT
) {
995 DEBUG ((EFI_D_ERROR
, "TransferBlockData timed out.\n"));
1011 // Card detect is a GPIO0 on the TPS65950
1013 Status
= gTPS65950
->Read (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID2
, GPIODATAIN1
), 1, &Data
);
1014 if (EFI_ERROR (Status
)) {
1018 if ((Data
& CARD_DETECT_BIT
) == CARD_DETECT_BIT
) {
1033 if (!CardPresent ()) {
1034 return EFI_NO_MEDIA
;
1037 //Initialize MMC host controller clocks.
1038 Status
= InitializeMMCHS ();
1039 if (EFI_ERROR(Status
)) {
1040 DEBUG ((EFI_D_ERROR
, "Initialize MMC host controller fails. Status: %x\n", Status
));
1044 //Software reset of the MMCHS host controller.
1045 MmioWrite32 (MMCHS_SYSCONFIG
, SOFTRESET
);
1047 while ((MmioRead32 (MMCHS_SYSSTATUS
) & RESETDONE_MASK
) != RESETDONE
);
1049 //Soft reset for all.
1050 MmioWrite32 (MMCHS_SYSCTL
, SRA
);
1052 while ((MmioRead32 (MMCHS_SYSCTL
) & SRA
) != 0x0);
1054 //Voltage capabilities initialization. Activate VS18 and VS30.
1055 MmioOr32 (MMCHS_CAPA
, (VS30
| VS18
));
1057 //Wakeup configuration
1058 MmioOr32 (MMCHS_SYSCONFIG
, ENAWAKEUP
);
1059 MmioOr32 (MMCHS_HCTL
, IWE
);
1061 //MMCHS Controller default initialization
1062 MmioOr32 (MMCHS_CON
, (OD
| DW8_1_4_BIT
| CEATA_OFF
));
1064 MmioWrite32 (MMCHS_HCTL
, (SDVS_3_0_V
| DTW_1_BIT
| SDBP_OFF
));
1066 //Enable internal clock
1067 MmioOr32 (MMCHS_SYSCTL
, ICE
);
1069 //Set the clock frequency to 80KHz.
1070 UpdateMMCHSClkFrequency (CLKD_80KHZ
);
1072 //Enable SD bus power.
1073 MmioOr32 (MMCHS_HCTL
, (SDBP_ON
));
1075 //Poll till SD bus power bit is set.
1076 while ((MmioRead32 (MMCHS_HCTL
) & SDBP_MASK
) != SDBP_ON
);
1079 Status
= PerformCardIdenfication ();
1080 if (EFI_ERROR(Status
)) {
1081 DEBUG ((EFI_D_ERROR
, "No MMC/SD card detected.\n"));
1085 //Get CSD (Card specific data) for the detected card.
1086 Status
= GetCardSpecificData();
1087 if (EFI_ERROR(Status
)) {
1091 //Configure the card in data transfer mode.
1092 Status
= PerformCardConfiguration();
1093 if (EFI_ERROR(Status
)) {
1097 //Patch the Media structure.
1098 gMMCHSMedia
.LastBlock
= (gCardInfo
.NumBlocks
- 1);
1099 gMMCHSMedia
.BlockSize
= gCardInfo
.BlockSize
;
1100 gMMCHSMedia
.ReadOnly
= (MmioRead32 (GPIO1_BASE
+ GPIO_DATAIN
) & BIT23
) == BIT23
;
1101 gMMCHSMedia
.MediaPresent
= TRUE
;
1102 gMMCHSMedia
.MediaId
++;
1104 DEBUG ((EFI_D_INFO
, "SD Card Media Change on Handle 0x%08x\n", gImageHandle
));
1109 #define MAX_MMCHS_TRANSFER_SIZE 0x4000
1113 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1116 IN UINTN BufferSize
,
1117 IN OPERATION_TYPE OperationType
1120 EFI_STATUS Status
= EFI_SUCCESS
;
1121 UINTN RetryCount
= 0;
1123 UINTN BytesToBeTranferedThisPass
= 0;
1124 UINTN BytesRemainingToBeTransfered
;
1135 Status
= DetectCard ();
1136 if (EFI_ERROR (Status
)) {
1137 // We detected a removal
1138 gMMCHSMedia
.MediaPresent
= FALSE
;
1139 gMMCHSMedia
.LastBlock
= 0;
1140 gMMCHSMedia
.BlockSize
= 512; // Should be zero but there is a bug in DiskIo
1141 gMMCHSMedia
.ReadOnly
= FALSE
;
1143 gMediaChange
= FALSE
;
1144 } else if (!gMMCHSMedia
.MediaPresent
) {
1145 Status
= EFI_NO_MEDIA
;
1150 DEBUG ((EFI_D_INFO
, "SD Card ReinstallProtocolInterface ()\n"));
1151 gBS
->ReinstallProtocolInterface (
1153 &gEfiBlockIoProtocolGuid
,
1157 return EFI_MEDIA_CHANGED
;
1160 if (EFI_ERROR (Status
)) {
1164 if (Buffer
== NULL
) {
1165 Status
= EFI_INVALID_PARAMETER
;
1169 if (Lba
> This
->Media
->LastBlock
) {
1170 Status
= EFI_INVALID_PARAMETER
;
1174 if ((BufferSize
% This
->Media
->BlockSize
) != 0) {
1175 Status
= EFI_BAD_BUFFER_SIZE
;
1179 //Check if the data lines are not in use.
1180 while ((RetryCount
++ < MAX_RETRY_COUNT
) && ((MmioRead32 (MMCHS_PSTATE
) & DATI_MASK
) != DATI_ALLOWED
));
1181 if (RetryCount
== MAX_RETRY_COUNT
) {
1182 Status
= EFI_TIMEOUT
;
1186 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1188 BytesRemainingToBeTransfered
= BufferSize
;
1189 while (BytesRemainingToBeTransfered
> 0) {
1192 Status
= EFI_NO_MEDIA
;
1193 DEBUG ((EFI_D_INFO
, "SdReadWrite() EFI_NO_MEDIA due to gMediaChange\n"));
1194 goto DoneRestoreTPL
;
1197 // Turn OFF DMA path until it is debugged
1198 // BytesToBeTranferedThisPass = (BytesToBeTranferedThisPass >= MAX_MMCHS_TRANSFER_SIZE) ? MAX_MMCHS_TRANSFER_SIZE : BytesRemainingToBeTransfered;
1199 BytesToBeTranferedThisPass
= This
->Media
->BlockSize
;
1201 BlockCount
= BytesToBeTranferedThisPass
/This
->Media
->BlockSize
;
1203 if (BlockCount
> 1) {
1204 Status
= DmaBlocks (This
, Lba
, Buffer
, BlockCount
, OperationType
);
1206 //Transfer a block worth of data.
1207 Status
= TransferBlock (This
, Lba
, Buffer
, OperationType
);
1210 if (EFI_ERROR(Status
)) {
1211 DEBUG ((EFI_D_ERROR
, "TransferBlockData fails. %x\n", Status
));
1212 goto DoneRestoreTPL
;
1215 BytesRemainingToBeTransfered
-= BytesToBeTranferedThisPass
;
1217 Buffer
= (UINT8
*)Buffer
+ This
->Media
->BlockSize
;
1222 gBS
->RestoreTPL (OldTpl
);
1233 Reset the Block Device.
1237 @param This Indicates a pointer to the calling context.
1239 @param ExtendedVerification Driver may perform diagnostics on reset.
1243 @retval EFI_SUCCESS The device was reset.
1245 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
1255 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1256 IN BOOLEAN ExtendedVerification
1265 Read BufferSize bytes from Lba into Buffer.
1269 @param This Indicates a pointer to the calling context.
1271 @param MediaId Id of the media, changes every time the media is replaced.
1273 @param Lba The starting Logical Block Address to read from
1275 @param BufferSize Size of Buffer, must be a multiple of device block size.
1277 @param Buffer A pointer to the destination buffer for the data. The caller is
1279 responsible for either having implicit or explicit ownership of the buffer.
1283 @retval EFI_SUCCESS The data was read correctly from the device.
1285 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
1287 @retval EFI_NO_MEDIA There is no media in the device.
1289 @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.
1291 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
1293 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
1295 or the buffer is not on proper alignment.
1303 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1306 IN UINTN BufferSize
,
1312 //Perform Read operation.
1313 Status
= SdReadWrite (This
, (UINTN
)Lba
, Buffer
, BufferSize
, READ
);
1322 Write BufferSize bytes from Lba into Buffer.
1326 @param This Indicates a pointer to the calling context.
1328 @param MediaId The media ID that the write request is for.
1330 @param Lba The starting logical block address to be written. The caller is
1332 responsible for writing to only legitimate locations.
1334 @param BufferSize Size of Buffer, must be a multiple of device block size.
1336 @param Buffer A pointer to the source buffer for the data.
1340 @retval EFI_SUCCESS The data was written correctly to the device.
1342 @retval EFI_WRITE_PROTECTED The device can not be written to.
1344 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
1346 @retval EFI_NO_MEDIA There is no media in the device.
1348 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
1350 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
1352 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
1354 or the buffer is not on proper alignment.
1362 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1365 IN UINTN BufferSize
,
1371 //Perform write operation.
1372 Status
= SdReadWrite (This
, (UINTN
)Lba
, Buffer
, BufferSize
, WRITE
);
1382 Flush the Block Device.
1386 @param This Indicates a pointer to the calling context.
1390 @retval EFI_SUCCESS All outstanding data was written to the device
1392 @retval EFI_DEVICE_ERROR The device reported an error while writting back the data
1394 @retval EFI_NO_MEDIA There is no media in the device.
1402 IN EFI_BLOCK_IO_PROTOCOL
*This
1409 EFI_BLOCK_IO_PROTOCOL gBlockIo
= {
1410 EFI_BLOCK_IO_INTERFACE_REVISION
, // Revision
1411 &gMMCHSMedia
, // *Media
1412 MMCHSReset
, // Reset
1413 MMCHSReadBlocks
, // ReadBlocks
1414 MMCHSWriteBlocks
, // WriteBlocks
1415 MMCHSFlushBlocks
// FlushBlocks
1421 Timer callback to convert card present hardware into a boolean that indicates
1423 a media change event has happened. If you just check the GPIO you could see
1425 card 1 and then check again after card 1 was removed and card 2 was inserted
1427 and you would still see media present. Thus you need the timer tick to catch
1433 @param Event Event whose notification function is being invoked.
1435 @param Context The pointer to the notification function's context,
1437 which is implementation-dependent. Not used.
1451 Present
= CardPresent ();
1452 if (gMMCHSMedia
.MediaPresent
) {
1453 if (!Present
&& !gMediaChange
) {
1454 gMediaChange
= TRUE
;
1457 if (Present
&& !gMediaChange
) {
1458 gMediaChange
= TRUE
;
1467 IN EFI_HANDLE ImageHandle
,
1468 IN EFI_SYSTEM_TABLE
*SystemTable
1473 Status
= gBS
->LocateProtocol (&gEmbeddedExternalDeviceProtocolGuid
, NULL
, (VOID
**)&gTPS65950
);
1474 ASSERT_EFI_ERROR(Status
);
1476 ZeroMem (&gCardInfo
, sizeof (CARD_INFO
));
1478 Status
= gBS
->CreateEvent (EVT_TIMER
| EVT_NOTIFY_SIGNAL
, TPL_CALLBACK
, TimerCallback
, NULL
, &gTimerEvent
);
1479 ASSERT_EFI_ERROR (Status
);
1481 Status
= gBS
->SetTimer (gTimerEvent
, TimerPeriodic
, FixedPcdGet32 (PcdMmchsTimerFreq100NanoSeconds
));
1482 ASSERT_EFI_ERROR (Status
);
1485 Status
= gBS
->InstallMultipleProtocolInterfaces (
1487 &gEfiBlockIoProtocolGuid
, &gBlockIo
,
1488 &gEfiDevicePathProtocolGuid
, &gMmcHsDevicePath
,