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.
26 EFI_BLOCK_IO_MEDIA gMMCHSMedia
= {
27 SIGNATURE_32('s','d','i','o'), // MediaId
28 TRUE
, // RemovableMedia
29 FALSE
, // MediaPresent
30 FALSE
, // LogicalPartition
32 FALSE
, // WriteCaching
40 VENDOR_DEVICE_PATH Mmc
;
44 MMCHS_DEVICE_PATH gMmcHsDevicePath
= {
48 (UINT8
)(sizeof(VENDOR_DEVICE_PATH
)),
49 (UINT8
)((sizeof(VENDOR_DEVICE_PATH
)) >> 8),
50 0xb615f1f5, 0x5088, 0x43cd, 0x80, 0x9c, 0xa1, 0x6e, 0x52, 0x48, 0x7d, 0x00
54 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
55 { sizeof (EFI_DEVICE_PATH_PROTOCOL
), 0 }
60 EMBEDDED_EXTERNAL_DEVICE
*gTPS65950
;
61 EFI_EVENT gTimerEvent
;
62 BOOLEAN gMediaChange
= FALSE
;
77 gCardInfo
.CIDData
.MDT
= ((Response0
>> 8) & 0xFFF);
78 gCardInfo
.CIDData
.PSN
= (((Response0
>> 24) & 0xFF) | ((Response1
& 0xFFFFFF) << 8));
79 gCardInfo
.CIDData
.PRV
= ((Response1
>> 24) & 0xFF);
80 gCardInfo
.CIDData
.PNM
[4] = ((Response2
) & 0xFF);
81 gCardInfo
.CIDData
.PNM
[3] = ((Response2
>> 8) & 0xFF);
82 gCardInfo
.CIDData
.PNM
[2] = ((Response2
>> 16) & 0xFF);
83 gCardInfo
.CIDData
.PNM
[1] = ((Response2
>> 24) & 0xFF);
84 gCardInfo
.CIDData
.PNM
[0] = ((Response3
) & 0xFF);
85 gCardInfo
.CIDData
.OID
= ((Response3
>> 8) & 0xFFFF);
86 gCardInfo
.CIDData
.MID
= ((Response3
>> 24) & 0xFF);
91 UpdateMMCHSClkFrequency (
95 //Set Clock enable to 0x0 to not provide the clock to the card
96 MmioAnd32 (MMCHS_SYSCTL
, ~CEN
);
98 //Set new clock frequency.
99 MmioAndThenOr32 (MMCHS_SYSCTL
, ~CLKD_MASK
, NewCLKD
<< 6);
101 //Poll till Internal Clock Stable
102 while ((MmioRead32 (MMCHS_SYSCTL
) & ICS_MASK
) != ICS
);
104 //Set Clock enable to 0x1 to provide the clock to the card
105 MmioOr32 (MMCHS_SYSCTL
, CEN
);
112 UINTN CmdInterruptEnableVal
,
117 UINTN RetryCount
= 0;
119 //Check if command line is in use or not. Poll till command line is available.
120 while ((MmioRead32 (MMCHS_PSTATE
) & DATI_MASK
) == DATI_NOT_ALLOWED
);
122 //Provide the block size.
123 MmioWrite32 (MMCHS_BLK
, BLEN_512BYTES
);
125 //Setting Data timeout counter value to max value.
126 MmioAndThenOr32 (MMCHS_SYSCTL
, ~DTO_MASK
, DTO_VAL
);
128 //Clear Status register.
129 MmioWrite32 (MMCHS_STAT
, 0xFFFFFFFF);
131 //Set command argument register
132 MmioWrite32 (MMCHS_ARG
, CmdArgument
);
134 //Enable interrupt enable events to occur
135 MmioWrite32 (MMCHS_IE
, CmdInterruptEnableVal
);
138 MmioWrite32 (MMCHS_CMD
, Cmd
);
140 //Check for the command status.
141 while (RetryCount
< MAX_RETRY_COUNT
) {
143 MmcStatus
= MmioRead32 (MMCHS_STAT
);
144 } while (MmcStatus
== 0);
146 //Read status of command response
147 if ((MmcStatus
& ERRI
) != 0) {
149 //Perform soft-reset for mmci_cmd line.
150 MmioOr32 (MMCHS_SYSCTL
, SRC
);
151 while ((MmioRead32 (MMCHS_SYSCTL
) & SRC
));
153 DEBUG ((EFI_D_INFO
, "MmcStatus: %x\n", MmcStatus
));
154 return EFI_DEVICE_ERROR
;
157 //Check if command is completed.
158 if ((MmcStatus
& CC
) == CC
) {
159 MmioWrite32 (MMCHS_STAT
, CC
);
166 if (RetryCount
== MAX_RETRY_COUNT
) {
175 GetBlockInformation (
180 CSD_SDV2
*CsdSDV2Data
;
183 if (gCardInfo
.CardType
== SD_CARD_2_HIGH
) {
184 CsdSDV2Data
= (CSD_SDV2
*)&gCardInfo
.CSDData
;
186 //Populate BlockSize.
187 *BlockSize
= (0x1UL
<< CsdSDV2Data
->READ_BL_LEN
);
189 //Calculate Total number of blocks.
190 CardSize
= CsdSDV2Data
->C_SIZELow16
| (CsdSDV2Data
->C_SIZEHigh6
<< 2);
191 *NumBlocks
= ((CardSize
+ 1) * 1024);
193 //Populate BlockSize.
194 *BlockSize
= (0x1UL
<< gCardInfo
.CSDData
.READ_BL_LEN
);
196 //Calculate Total number of blocks.
197 CardSize
= gCardInfo
.CSDData
.C_SIZELow2
| (gCardInfo
.CSDData
.C_SIZEHigh10
<< 2);
198 *NumBlocks
= (CardSize
+ 1) * (1 << (gCardInfo
.CSDData
.C_SIZE_MULT
+ 2));
201 //For >=2G card, BlockSize may be 1K, but the transfer size is 512 bytes.
202 if (*BlockSize
> 512) {
203 *NumBlocks
= MultU64x32(*NumBlocks
, *BlockSize
/2);
207 DEBUG ((EFI_D_INFO
, "Card type: %x, BlockSize: %x, NumBlocks: %x\n", gCardInfo
.CardType
, *BlockSize
, *NumBlocks
));
213 UINTN
*ClockFrequencySelect
216 UINT8 MaxDataTransferRate
;
217 UINTN TransferRateValue
= 0;
218 UINTN TimeValue
= 0 ;
221 MaxDataTransferRate
= gCardInfo
.CSDData
.TRAN_SPEED
;
223 // For SD Cards we would need to send CMD6 to set
224 // speeds abouve 25MHz. High Speed mode 50 MHz and up
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();
601 PerformCardConfiguration (
605 UINTN CmdArgument
= 0;
609 CmdArgument
= gCardInfo
.RCA
<< 16;
610 Status
= SendCmd (CMD7
, CMD7_INT_EN
, CmdArgument
);
611 if (EFI_ERROR(Status
)) {
612 DEBUG ((EFI_D_ERROR
, "CMD7 fails. Status: %x\n", Status
));
616 if ((gCardInfo
.CardType
!= UNKNOWN_CARD
) && (gCardInfo
.CardType
!= MMC_CARD
)) {
617 // We could read SCR register, but SD Card Phys spec stats any SD Card shall
618 // set SCR.SD_BUS_WIDTHS to support 4-bit mode, so why bother?
620 // Send ACMD6 (application specific commands must be prefixed with CMD55)
621 Status
= SendCmd (CMD55
, CMD55_INT_EN
, CmdArgument
);
622 if (!EFI_ERROR (Status
)) {
623 // set device into 4-bit data bus mode
624 Status
= SendCmd (ACMD6
, ACMD6_INT_EN
, 0x2);
625 if (!EFI_ERROR (Status
)) {
626 // Set host controler into 4-bit mode
627 MmioOr32 (MMCHS_HCTL
, DTW_4_BIT
);
628 DEBUG ((EFI_D_INFO
, "SD Memory Card set to 4-bit mode\n"));
633 //Send CMD16 to set the block length
634 CmdArgument
= gCardInfo
.BlockSize
;
635 Status
= SendCmd (CMD16
, CMD16_INT_EN
, CmdArgument
);
636 if (EFI_ERROR(Status
)) {
637 DEBUG ((EFI_D_ERROR
, "CMD16 fails. Status: %x\n", Status
));
641 //Change MMCHS clock frequency to what detected card can support.
642 UpdateMMCHSClkFrequency(gCardInfo
.ClockFrequencySelect
);
650 IN EFI_BLOCK_IO_PROTOCOL
*This
,
655 UINTN
*DataBuffer
= Buffer
;
656 UINTN DataSize
= This
->Media
->BlockSize
/4;
658 UINTN RetryCount
= 0;
660 //Check controller status to make sure there is no error.
661 while (RetryCount
< MAX_RETRY_COUNT
) {
664 MmcStatus
= MmioRead32 (MMCHS_STAT
);
665 } while(MmcStatus
== 0);
667 //Check if Buffer read ready (BRR) bit is set?
668 if (MmcStatus
& BRR
) {
671 MmioOr32 (MMCHS_STAT
, BRR
);
673 //Read block worth of data.
674 for (Count
= 0; Count
< DataSize
; Count
++) {
675 *DataBuffer
++ = MmioRead32 (MMCHS_DATA
);
682 if (RetryCount
== MAX_RETRY_COUNT
) {
692 IN EFI_BLOCK_IO_PROTOCOL
*This
,
697 UINTN
*DataBuffer
= Buffer
;
698 UINTN DataSize
= This
->Media
->BlockSize
/4;
700 UINTN RetryCount
= 0;
702 //Check controller status to make sure there is no error.
703 while (RetryCount
< MAX_RETRY_COUNT
) {
706 MmcStatus
= MmioRead32 (MMCHS_STAT
);
707 } while(MmcStatus
== 0);
709 //Check if Buffer write ready (BWR) bit is set?
710 if (MmcStatus
& BWR
) {
713 MmioOr32 (MMCHS_STAT
, BWR
);
715 //Write block worth of data.
716 for (Count
= 0; Count
< DataSize
; Count
++) {
717 MmioWrite32 (MMCHS_DATA
, *DataBuffer
++);
725 if (RetryCount
== MAX_RETRY_COUNT
) {
734 IN EFI_BLOCK_IO_PROTOCOL
*This
,
738 IN OPERATION_TYPE OperationType
744 UINTN CmdInterruptEnable
;
747 EFI_PHYSICAL_ADDRESS BufferAddress
;
749 DMA_MAP_OPERATION DmaOperation
;
750 EFI_STATUS MmcStatus
;
751 UINTN RetryCount
= 0;
754 // Map passed in buffer for DMA xfer
755 DmaSize
= BlockCount
* This
->Media
->BlockSize
;
756 Status
= DmaMap (DmaOperation
, Buffer
, &DmaSize
, &BufferAddress
, &BufferMap
);
757 if (EFI_ERROR (Status
)) {
761 ZeroMem (&DmaOperation
, sizeof (DMA_MAP_OPERATION
));
764 Dma4
.DataType
= 2; // DMA4_CSDPi[1:0] 32-bit elements from MMCHS_DATA
766 Dma4
.SourceEndiansim
= 0; // DMA4_CSDPi[21]
768 Dma4
.DestinationEndianism
= 0; // DMA4_CSDPi[19]
770 Dma4
.SourcePacked
= 0; // DMA4_CSDPi[6]
772 Dma4
.DestinationPacked
= 0; // DMA4_CSDPi[13]
774 Dma4
.NumberOfElementPerFrame
= This
->Media
->BlockSize
/4; // DMA4_CENi (TRM 4K is optimum value)
776 Dma4
.NumberOfFramePerTransferBlock
= BlockCount
; // DMA4_CFNi
778 Dma4
.ReadPriority
= 0; // DMA4_CCRi[6] Low priority read
780 Dma4
.WritePriority
= 0; // DMA4_CCRi[23] Prefetech disabled
783 //Populate the command information based on the operation type.
784 if (OperationType
== READ
) {
785 Cmd
= CMD18
; //Multiple block read
786 CmdInterruptEnable
= CMD18_INT_EN
;
787 DmaOperation
= MapOperationBusMasterCommonBuffer
;
789 Dma4
.ReadPortAccessType
=0 ; // DMA4_CSDPi[8:7] Can not burst MMCHS_DATA reg
791 Dma4
.WritePortAccessType
= 3; // DMA4_CSDPi[15:14] Memory burst 16x32
793 Dma4
.WriteMode
= 1; // DMA4_CSDPi[17:16] Write posted
797 Dma4
.SourceStartAddress
= MMCHS_DATA
; // DMA4_CSSAi
799 Dma4
.DestinationStartAddress
= (UINT32
)BufferAddress
; // DMA4_CDSAi
801 Dma4
.SourceElementIndex
= 1; // DMA4_CSEi
803 Dma4
.SourceFrameIndex
= 0x200; // DMA4_CSFi
805 Dma4
.DestinationElementIndex
= 1; // DMA4_CDEi
807 Dma4
.DestinationFrameIndex
= 0; // DMA4_CDFi
811 Dma4
.ReadPortAccessMode
= 0; // DMA4_CCRi[13:12] Always read MMCHS_DATA
813 Dma4
.WritePortAccessMode
= 1; // DMA4_CCRi[15:14] Post increment memory address
815 Dma4
.ReadRequestNumber
= 0x1e; // DMA4_CCRi[4:0] Syncro with MMCA_DMA_RX (61)
817 Dma4
.WriteRequestNumber
= 1; // DMA4_CCRi[20:19] Syncro upper 0x3e == 62 (one based)
819 } else if (OperationType
== WRITE
) {
820 Cmd
= CMD25
; //Multiple block write
821 CmdInterruptEnable
= CMD25_INT_EN
;
822 DmaOperation
= MapOperationBusMasterRead
;
824 Dma4
.ReadPortAccessType
= 3; // DMA4_CSDPi[8:7] Memory burst 16x32
826 Dma4
.WritePortAccessType
= 0; // DMA4_CSDPi[15:14] Can not burst MMCHS_DATA reg
828 Dma4
.WriteMode
= 1; // DMA4_CSDPi[17:16] Write posted ???
832 Dma4
.SourceStartAddress
= (UINT32
)BufferAddress
; // DMA4_CSSAi
834 Dma4
.DestinationStartAddress
= MMCHS_DATA
; // DMA4_CDSAi
836 Dma4
.SourceElementIndex
= 1; // DMA4_CSEi
838 Dma4
.SourceFrameIndex
= 0x200; // DMA4_CSFi
840 Dma4
.DestinationElementIndex
= 1; // DMA4_CDEi
842 Dma4
.DestinationFrameIndex
= 0; // DMA4_CDFi
846 Dma4
.ReadPortAccessMode
= 1; // DMA4_CCRi[13:12] Post increment memory address
848 Dma4
.WritePortAccessMode
= 0; // DMA4_CCRi[15:14] Always write MMCHS_DATA
850 Dma4
.ReadRequestNumber
= 0x1d; // DMA4_CCRi[4:0] Syncro with MMCA_DMA_TX (60)
852 Dma4
.WriteRequestNumber
= 1; // DMA4_CCRi[20:19] Syncro upper 0x3d == 61 (one based)
855 return EFI_INVALID_PARAMETER
;
859 EnableDmaChannel (2, &Dma4
);
862 //Set command argument based on the card access mode (Byte mode or Block mode)
863 if (gCardInfo
.OCRData
.AccessMode
& BIT1
) {
866 CmdArgument
= Lba
* This
->Media
->BlockSize
;
870 Status
= SendCmd (Cmd
, CmdInterruptEnable
, CmdArgument
);
871 if (EFI_ERROR (Status
)) {
872 DEBUG ((EFI_D_ERROR
, "CMD fails. Status: %x\n", Status
));
876 //Check for the Transfer completion.
877 while (RetryCount
< MAX_RETRY_COUNT
) {
880 MmcStatus
= MmioRead32 (MMCHS_STAT
);
881 } while (MmcStatus
== 0);
883 //Check if Transfer complete (TC) bit is set?
884 if (MmcStatus
& TC
) {
887 DEBUG ((EFI_D_ERROR
, "MmcStatus for TC: %x\n", MmcStatus
));
888 //Check if DEB, DCRC or DTO interrupt occured.
889 if ((MmcStatus
& DEB
) | (MmcStatus
& DCRC
) | (MmcStatus
& DTO
)) {
890 //There was an error during the data transfer.
892 //Set SRD bit to 1 and wait until it return to 0x0.
893 MmioOr32 (MMCHS_SYSCTL
, SRD
);
894 while((MmioRead32 (MMCHS_SYSCTL
) & SRD
) != 0x0);
896 DisableDmaChannel (2, DMA4_CSR_BLOCK
, DMA4_CSR_ERR
);
897 DmaUnmap (BufferMap
);
898 return EFI_DEVICE_ERROR
;
904 DisableDmaChannel (2, DMA4_CSR_BLOCK
, DMA4_CSR_ERR
);
905 Status
= DmaUnmap (BufferMap
);
907 if (RetryCount
== MAX_RETRY_COUNT
) {
908 DEBUG ((EFI_D_ERROR
, "TransferBlockData timed out.\n"));
918 IN EFI_BLOCK_IO_PROTOCOL
*This
,
921 IN OPERATION_TYPE OperationType
926 UINTN RetryCount
= 0;
928 UINTN CmdInterruptEnable
= 0;
929 UINTN CmdArgument
= 0;
932 //Populate the command information based on the operation type.
933 if (OperationType
== READ
) {
934 Cmd
= CMD17
; //Single block read
935 CmdInterruptEnable
= CMD18_INT_EN
;
936 } else if (OperationType
== WRITE
) {
937 Cmd
= CMD24
; //Single block write
938 CmdInterruptEnable
= CMD24_INT_EN
;
941 //Set command argument based on the card access mode (Byte mode or Block mode)
942 if (gCardInfo
.OCRData
.AccessMode
& BIT1
) {
945 CmdArgument
= Lba
* This
->Media
->BlockSize
;
949 Status
= SendCmd (Cmd
, CmdInterruptEnable
, CmdArgument
);
950 if (EFI_ERROR(Status
)) {
951 DEBUG ((EFI_D_ERROR
, "CMD fails. Status: %x\n", Status
));
955 //Read or Write data.
956 if (OperationType
== READ
) {
957 Status
= ReadBlockData (This
, Buffer
);
958 if (EFI_ERROR(Status
)) {
959 DEBUG((EFI_D_ERROR
, "ReadBlockData fails.\n"));
962 } else if (OperationType
== WRITE
) {
963 Status
= WriteBlockData (This
, Buffer
);
964 if (EFI_ERROR(Status
)) {
965 DEBUG((EFI_D_ERROR
, "WriteBlockData fails.\n"));
970 //Check for the Transfer completion.
971 while (RetryCount
< MAX_RETRY_COUNT
) {
974 MmcStatus
= MmioRead32 (MMCHS_STAT
);
975 } while (MmcStatus
== 0);
977 //Check if Transfer complete (TC) bit is set?
978 if (MmcStatus
& TC
) {
981 DEBUG ((EFI_D_ERROR
, "MmcStatus for TC: %x\n", MmcStatus
));
982 //Check if DEB, DCRC or DTO interrupt occured.
983 if ((MmcStatus
& DEB
) | (MmcStatus
& DCRC
) | (MmcStatus
& DTO
)) {
984 //There was an error during the data transfer.
986 //Set SRD bit to 1 and wait until it return to 0x0.
987 MmioOr32 (MMCHS_SYSCTL
, SRD
);
988 while((MmioRead32 (MMCHS_SYSCTL
) & SRD
) != 0x0);
990 return EFI_DEVICE_ERROR
;
996 if (RetryCount
== MAX_RETRY_COUNT
) {
997 DEBUG ((EFI_D_ERROR
, "TransferBlockData timed out.\n"));
1013 // Card detect is a GPIO0 on the TPS65950
1015 Status
= gTPS65950
->Read (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID2
, GPIODATAIN1
), 1, &Data
);
1016 if (EFI_ERROR (Status
)) {
1020 if ((Data
& CARD_DETECT_BIT
) == CARD_DETECT_BIT
) {
1035 if (!CardPresent ()) {
1036 return EFI_NO_MEDIA
;
1039 //Initialize MMC host controller clocks.
1040 Status
= InitializeMMCHS ();
1041 if (EFI_ERROR(Status
)) {
1042 DEBUG ((EFI_D_ERROR
, "Initialize MMC host controller fails. Status: %x\n", Status
));
1046 //Software reset of the MMCHS host controller.
1047 MmioWrite32 (MMCHS_SYSCONFIG
, SOFTRESET
);
1049 while ((MmioRead32 (MMCHS_SYSSTATUS
) & RESETDONE_MASK
) != RESETDONE
);
1051 //Soft reset for all.
1052 MmioWrite32 (MMCHS_SYSCTL
, SRA
);
1054 while ((MmioRead32 (MMCHS_SYSCTL
) & SRA
) != 0x0);
1056 //Voltage capabilities initialization. Activate VS18 and VS30.
1057 MmioOr32 (MMCHS_CAPA
, (VS30
| VS18
));
1059 //Wakeup configuration
1060 MmioOr32 (MMCHS_SYSCONFIG
, ENAWAKEUP
);
1061 MmioOr32 (MMCHS_HCTL
, IWE
);
1063 //MMCHS Controller default initialization
1064 MmioOr32 (MMCHS_CON
, (OD
| DW8_1_4_BIT
| CEATA_OFF
));
1066 MmioWrite32 (MMCHS_HCTL
, (SDVS_3_0_V
| DTW_1_BIT
| SDBP_OFF
));
1068 //Enable internal clock
1069 MmioOr32 (MMCHS_SYSCTL
, ICE
);
1071 //Set the clock frequency to 80KHz.
1072 UpdateMMCHSClkFrequency (CLKD_80KHZ
);
1074 //Enable SD bus power.
1075 MmioOr32 (MMCHS_HCTL
, (SDBP_ON
));
1077 //Poll till SD bus power bit is set.
1078 while ((MmioRead32 (MMCHS_HCTL
) & SDBP_MASK
) != SDBP_ON
);
1081 Status
= PerformCardIdenfication ();
1082 if (EFI_ERROR(Status
)) {
1083 DEBUG ((EFI_D_ERROR
, "No MMC/SD card detected.\n"));
1087 //Get CSD (Card specific data) for the detected card.
1088 Status
= GetCardSpecificData();
1089 if (EFI_ERROR(Status
)) {
1093 //Configure the card in data transfer mode.
1094 Status
= PerformCardConfiguration();
1095 if (EFI_ERROR(Status
)) {
1099 //Patch the Media structure.
1100 gMMCHSMedia
.LastBlock
= (gCardInfo
.NumBlocks
- 1);
1101 gMMCHSMedia
.BlockSize
= gCardInfo
.BlockSize
;
1102 gMMCHSMedia
.ReadOnly
= (MmioRead32 (GPIO1_BASE
+ GPIO_DATAIN
) & BIT23
) == BIT23
;
1103 gMMCHSMedia
.MediaPresent
= TRUE
;
1104 gMMCHSMedia
.MediaId
++;
1106 DEBUG ((EFI_D_INFO
, "SD Card Media Change on Handle 0x%08x\n", gImageHandle
));
1111 #define MAX_MMCHS_TRANSFER_SIZE 0x4000
1115 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1118 IN UINTN BufferSize
,
1119 IN OPERATION_TYPE OperationType
1122 EFI_STATUS Status
= EFI_SUCCESS
;
1123 UINTN RetryCount
= 0;
1125 UINTN BytesToBeTranferedThisPass
= 0;
1126 UINTN BytesRemainingToBeTransfered
;
1137 Status
= DetectCard ();
1138 if (EFI_ERROR (Status
)) {
1139 // We detected a removal
1140 gMMCHSMedia
.MediaPresent
= FALSE
;
1141 gMMCHSMedia
.LastBlock
= 0;
1142 gMMCHSMedia
.BlockSize
= 512; // Should be zero but there is a bug in DiskIo
1143 gMMCHSMedia
.ReadOnly
= FALSE
;
1145 gMediaChange
= FALSE
;
1146 } else if (!gMMCHSMedia
.MediaPresent
) {
1147 Status
= EFI_NO_MEDIA
;
1152 DEBUG ((EFI_D_INFO
, "SD Card ReinstallProtocolInterface ()\n"));
1153 gBS
->ReinstallProtocolInterface (
1155 &gEfiBlockIoProtocolGuid
,
1159 return EFI_MEDIA_CHANGED
;
1162 if (EFI_ERROR (Status
)) {
1166 if (Buffer
== NULL
) {
1167 Status
= EFI_INVALID_PARAMETER
;
1171 if (Lba
> This
->Media
->LastBlock
) {
1172 Status
= EFI_INVALID_PARAMETER
;
1176 if ((BufferSize
% This
->Media
->BlockSize
) != 0) {
1177 Status
= EFI_BAD_BUFFER_SIZE
;
1181 //Check if the data lines are not in use.
1182 while ((RetryCount
++ < MAX_RETRY_COUNT
) && ((MmioRead32 (MMCHS_PSTATE
) & DATI_MASK
) != DATI_ALLOWED
));
1183 if (RetryCount
== MAX_RETRY_COUNT
) {
1184 Status
= EFI_TIMEOUT
;
1188 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1190 BytesRemainingToBeTransfered
= BufferSize
;
1191 while (BytesRemainingToBeTransfered
> 0) {
1194 Status
= EFI_NO_MEDIA
;
1195 DEBUG ((EFI_D_INFO
, "SdReadWrite() EFI_NO_MEDIA due to gMediaChange\n"));
1196 goto DoneRestoreTPL
;
1199 // Turn OFF DMA path until it is debugged
1200 // BytesToBeTranferedThisPass = (BytesToBeTranferedThisPass >= MAX_MMCHS_TRANSFER_SIZE) ? MAX_MMCHS_TRANSFER_SIZE : BytesRemainingToBeTransfered;
1201 BytesToBeTranferedThisPass
= This
->Media
->BlockSize
;
1203 BlockCount
= BytesToBeTranferedThisPass
/This
->Media
->BlockSize
;
1205 if (BlockCount
> 1) {
1206 Status
= DmaBlocks (This
, Lba
, Buffer
, BlockCount
, OperationType
);
1208 //Transfer a block worth of data.
1209 Status
= TransferBlock (This
, Lba
, Buffer
, OperationType
);
1212 if (EFI_ERROR(Status
)) {
1213 DEBUG ((EFI_D_ERROR
, "TransferBlockData fails. %x\n", Status
));
1214 goto DoneRestoreTPL
;
1217 BytesRemainingToBeTransfered
-= BytesToBeTranferedThisPass
;
1219 Buffer
= (UINT8
*)Buffer
+ This
->Media
->BlockSize
;
1224 gBS
->RestoreTPL (OldTpl
);
1235 Reset the Block Device.
1239 @param This Indicates a pointer to the calling context.
1241 @param ExtendedVerification Driver may perform diagnostics on reset.
1245 @retval EFI_SUCCESS The device was reset.
1247 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
1257 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1258 IN BOOLEAN ExtendedVerification
1267 Read BufferSize bytes from Lba into Buffer.
1271 @param This Indicates a pointer to the calling context.
1273 @param MediaId Id of the media, changes every time the media is replaced.
1275 @param Lba The starting Logical Block Address to read from
1277 @param BufferSize Size of Buffer, must be a multiple of device block size.
1279 @param Buffer A pointer to the destination buffer for the data. The caller is
1281 responsible for either having implicit or explicit ownership of the buffer.
1285 @retval EFI_SUCCESS The data was read correctly from the device.
1287 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
1289 @retval EFI_NO_MEDIA There is no media in the device.
1291 @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.
1293 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
1295 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
1297 or the buffer is not on proper alignment.
1305 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1308 IN UINTN BufferSize
,
1314 //Perform Read operation.
1315 Status
= SdReadWrite (This
, (UINTN
)Lba
, Buffer
, BufferSize
, READ
);
1324 Write BufferSize bytes from Lba into Buffer.
1328 @param This Indicates a pointer to the calling context.
1330 @param MediaId The media ID that the write request is for.
1332 @param Lba The starting logical block address to be written. The caller is
1334 responsible for writing to only legitimate locations.
1336 @param BufferSize Size of Buffer, must be a multiple of device block size.
1338 @param Buffer A pointer to the source buffer for the data.
1342 @retval EFI_SUCCESS The data was written correctly to the device.
1344 @retval EFI_WRITE_PROTECTED The device can not be written to.
1346 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
1348 @retval EFI_NO_MEDIA There is no media in the device.
1350 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
1352 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
1354 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
1356 or the buffer is not on proper alignment.
1364 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1367 IN UINTN BufferSize
,
1373 //Perform write operation.
1374 Status
= SdReadWrite (This
, (UINTN
)Lba
, Buffer
, BufferSize
, WRITE
);
1384 Flush the Block Device.
1388 @param This Indicates a pointer to the calling context.
1392 @retval EFI_SUCCESS All outstanding data was written to the device
1394 @retval EFI_DEVICE_ERROR The device reported an error while writting back the data
1396 @retval EFI_NO_MEDIA There is no media in the device.
1404 IN EFI_BLOCK_IO_PROTOCOL
*This
1411 EFI_BLOCK_IO_PROTOCOL gBlockIo
= {
1412 EFI_BLOCK_IO_INTERFACE_REVISION
, // Revision
1413 &gMMCHSMedia
, // *Media
1414 MMCHSReset
, // Reset
1415 MMCHSReadBlocks
, // ReadBlocks
1416 MMCHSWriteBlocks
, // WriteBlocks
1417 MMCHSFlushBlocks
// FlushBlocks
1423 Timer callback to convert card present hardware into a boolean that indicates
1425 a media change event has happened. If you just check the GPIO you could see
1427 card 1 and then check again after card 1 was removed and card 2 was inserted
1429 and you would still see media present. Thus you need the timer tick to catch
1435 @param Event Event whose notification function is being invoked.
1437 @param Context The pointer to the notification function's context,
1439 which is implementation-dependent. Not used.
1453 Present
= CardPresent ();
1454 if (gMMCHSMedia
.MediaPresent
) {
1455 if (!Present
&& !gMediaChange
) {
1456 gMediaChange
= TRUE
;
1459 if (Present
&& !gMediaChange
) {
1460 gMediaChange
= TRUE
;
1469 IN EFI_HANDLE ImageHandle
,
1470 IN EFI_SYSTEM_TABLE
*SystemTable
1475 Status
= gBS
->LocateProtocol (&gEmbeddedExternalDeviceProtocolGuid
, NULL
, (VOID
**)&gTPS65950
);
1476 ASSERT_EFI_ERROR(Status
);
1478 ZeroMem (&gCardInfo
, sizeof (CARD_INFO
));
1480 Status
= gBS
->CreateEvent (EVT_TIMER
| EVT_NOTIFY_SIGNAL
, TPL_CALLBACK
, TimerCallback
, NULL
, &gTimerEvent
);
1481 ASSERT_EFI_ERROR (Status
);
1483 Status
= gBS
->SetTimer (gTimerEvent
, TimerPeriodic
, FixedPcdGet32 (PcdMmchsTimerFreq100NanoSeconds
));
1484 ASSERT_EFI_ERROR (Status
);
1487 Status
= gBS
->InstallMultipleProtocolInterfaces (
1489 &gEfiBlockIoProtocolGuid
, &gBlockIo
,
1490 &gEfiDevicePathProtocolGuid
, &gMmcHsDevicePath
,