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
= {
50 (UINT8
)(sizeof(VENDOR_DEVICE_PATH
)),
51 (UINT8
)((sizeof(VENDOR_DEVICE_PATH
)) >> 8),
54 { 0xb615f1f5, 0x5088, 0x43cd, { 0x80, 0x9c, 0xa1, 0x6e, 0x52, 0x48, 0x7d, 0x00 } },
58 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
59 { sizeof (EFI_DEVICE_PATH_PROTOCOL
), 0 }
64 EMBEDDED_EXTERNAL_DEVICE
*gTPS65950
;
65 EFI_EVENT gTimerEvent
;
66 BOOLEAN gMediaChange
= FALSE
;
81 gCardInfo
.CIDData
.MDT
= ((Response0
>> 8) & 0xFFF);
82 gCardInfo
.CIDData
.PSN
= (((Response0
>> 24) & 0xFF) | ((Response1
& 0xFFFFFF) << 8));
83 gCardInfo
.CIDData
.PRV
= ((Response1
>> 24) & 0xFF);
84 gCardInfo
.CIDData
.PNM
[4] = ((Response2
) & 0xFF);
85 gCardInfo
.CIDData
.PNM
[3] = ((Response2
>> 8) & 0xFF);
86 gCardInfo
.CIDData
.PNM
[2] = ((Response2
>> 16) & 0xFF);
87 gCardInfo
.CIDData
.PNM
[1] = ((Response2
>> 24) & 0xFF);
88 gCardInfo
.CIDData
.PNM
[0] = ((Response3
) & 0xFF);
89 gCardInfo
.CIDData
.OID
= ((Response3
>> 8) & 0xFFFF);
90 gCardInfo
.CIDData
.MID
= ((Response3
>> 24) & 0xFF);
95 UpdateMMCHSClkFrequency (
99 //Set Clock enable to 0x0 to not provide the clock to the card
100 MmioAnd32 (MMCHS_SYSCTL
, ~CEN
);
102 //Set new clock frequency.
103 MmioAndThenOr32 (MMCHS_SYSCTL
, ~CLKD_MASK
, NewCLKD
<< 6);
105 //Poll till Internal Clock Stable
106 while ((MmioRead32 (MMCHS_SYSCTL
) & ICS_MASK
) != ICS
);
108 //Set Clock enable to 0x1 to provide the clock to the card
109 MmioOr32 (MMCHS_SYSCTL
, CEN
);
116 UINTN CmdInterruptEnableVal
,
121 UINTN RetryCount
= 0;
123 //Check if command line is in use or not. Poll till command line is available.
124 while ((MmioRead32 (MMCHS_PSTATE
) & DATI_MASK
) == DATI_NOT_ALLOWED
);
126 //Provide the block size.
127 MmioWrite32 (MMCHS_BLK
, BLEN_512BYTES
);
129 //Setting Data timeout counter value to max value.
130 MmioAndThenOr32 (MMCHS_SYSCTL
, ~DTO_MASK
, DTO_VAL
);
132 //Clear Status register.
133 MmioWrite32 (MMCHS_STAT
, 0xFFFFFFFF);
135 //Set command argument register
136 MmioWrite32 (MMCHS_ARG
, CmdArgument
);
138 //Enable interrupt enable events to occur
139 MmioWrite32 (MMCHS_IE
, CmdInterruptEnableVal
);
142 MmioWrite32 (MMCHS_CMD
, Cmd
);
144 //Check for the command status.
145 while (RetryCount
< MAX_RETRY_COUNT
) {
147 MmcStatus
= MmioRead32 (MMCHS_STAT
);
148 } while (MmcStatus
== 0);
150 //Read status of command response
151 if ((MmcStatus
& ERRI
) != 0) {
153 //Perform soft-reset for mmci_cmd line.
154 MmioOr32 (MMCHS_SYSCTL
, SRC
);
155 while ((MmioRead32 (MMCHS_SYSCTL
) & SRC
));
157 DEBUG ((EFI_D_INFO
, "MmcStatus: %x\n", MmcStatus
));
158 return EFI_DEVICE_ERROR
;
161 //Check if command is completed.
162 if ((MmcStatus
& CC
) == CC
) {
163 MmioWrite32 (MMCHS_STAT
, CC
);
170 if (RetryCount
== MAX_RETRY_COUNT
) {
179 GetBlockInformation (
184 CSD_SDV2
*CsdSDV2Data
;
187 if (gCardInfo
.CardType
== SD_CARD_2_HIGH
) {
188 CsdSDV2Data
= (CSD_SDV2
*)&gCardInfo
.CSDData
;
190 //Populate BlockSize.
191 *BlockSize
= (0x1UL
<< CsdSDV2Data
->READ_BL_LEN
);
193 //Calculate Total number of blocks.
194 CardSize
= CsdSDV2Data
->C_SIZELow16
| (CsdSDV2Data
->C_SIZEHigh6
<< 2);
195 *NumBlocks
= ((CardSize
+ 1) * 1024);
197 //Populate BlockSize.
198 *BlockSize
= (0x1UL
<< gCardInfo
.CSDData
.READ_BL_LEN
);
200 //Calculate Total number of blocks.
201 CardSize
= gCardInfo
.CSDData
.C_SIZELow2
| (gCardInfo
.CSDData
.C_SIZEHigh10
<< 2);
202 *NumBlocks
= (CardSize
+ 1) * (1 << (gCardInfo
.CSDData
.C_SIZE_MULT
+ 2));
205 //For >=2G card, BlockSize may be 1K, but the transfer size is 512 bytes.
206 if (*BlockSize
> 512) {
207 *NumBlocks
= MultU64x32(*NumBlocks
, *BlockSize
/2);
211 DEBUG ((EFI_D_INFO
, "Card type: %x, BlockSize: %x, NumBlocks: %x\n", gCardInfo
.CardType
, *BlockSize
, *NumBlocks
));
217 UINTN
*ClockFrequencySelect
220 UINT8 MaxDataTransferRate
;
221 UINTN TransferRateValue
= 0;
222 UINTN TimeValue
= 0 ;
225 MaxDataTransferRate
= gCardInfo
.CSDData
.TRAN_SPEED
;
227 // For SD Cards we would need to send CMD6 to set
228 // speeds abouve 25MHz. High Speed mode 50 MHz and up
230 //Calculate Transfer rate unit (Bits 2:0 of TRAN_SPEED)
231 switch (MaxDataTransferRate
& 0x7) {
233 TransferRateValue
= 100 * 1000;
237 TransferRateValue
= 1 * 1000 * 1000;
241 TransferRateValue
= 10 * 1000 * 1000;
245 TransferRateValue
= 100 * 1000 * 1000;
249 DEBUG((EFI_D_ERROR
, "Invalid parameter.\n"));
253 //Calculate Time value (Bits 6:3 of TRAN_SPEED)
254 switch ((MaxDataTransferRate
>> 3) & 0xF) {
316 DEBUG((EFI_D_ERROR
, "Invalid parameter.\n"));
320 Frequency
= TransferRateValue
* TimeValue
/10;
322 //Calculate Clock divider value to program in MMCHS_SYSCTL[CLKD] field.
323 *ClockFrequencySelect
= ((MMC_REFERENCE_CLK
/Frequency
) + 1);
325 DEBUG ((EFI_D_INFO
, "MaxDataTransferRate: 0x%x, Frequency: %d KHz, ClockFrequencySelect: %x\n", MaxDataTransferRate
, Frequency
/1000, *ClockFrequencySelect
));
330 GetCardConfigurationData (
336 UINTN ClockFrequencySelect
;
338 //Calculate BlockSize and Total number of blocks in the detected card.
339 GetBlockInformation(&BlockSize
, &NumBlocks
);
340 gCardInfo
.BlockSize
= BlockSize
;
341 gCardInfo
.NumBlocks
= NumBlocks
;
343 //Calculate Card clock divider value.
344 CalculateCardCLKD(&ClockFrequencySelect
);
345 gCardInfo
.ClockFrequencySelect
= ClockFrequencySelect
;
357 //Select Device group to belong to P1 device group in Power IC.
359 Status
= gTPS65950
->Write (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4
, VMMC1_DEV_GRP
), 1, &Data
);
360 ASSERT_EFI_ERROR(Status
);
362 //Configure voltage regulator for MMC1 in Power IC to output 3.0 voltage.
364 Status
= gTPS65950
->Write (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4
, VMMC1_DEDICATED_REG
), 1, &Data
);
365 ASSERT_EFI_ERROR(Status
);
367 //After ramping up voltage, set VDDS stable bit to indicate that voltage level is stable.
368 MmioOr32 (CONTROL_PBIAS_LITE
, (PBIASLITEVMODE0
| PBIASLITEPWRDNZ0
| PBIASSPEEDCTRL0
| PBIASLITEVMODE1
| PBIASLITEWRDNZ1
));
371 MmioAndThenOr32 (GPIO1_BASE
+ GPIO_OE
, ~BIT23
, BIT23
);
373 // Enable Card Detect
374 Data
= CARD_DETECT_ENABLE
;
375 gTPS65950
->Write (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID2
, TPS65950_GPIO_CTRL
), 1, &Data
);
383 PerformCardIdenfication (
388 UINTN CmdArgument
= 0;
390 UINTN RetryCount
= 0;
391 BOOLEAN SDCmd8Supported
= FALSE
;
394 MmioWrite32 (MMCHS_IE
, (BADA_EN
| CERR_EN
| DEB_EN
| DCRC_EN
| DTO_EN
| CIE_EN
|
395 CEB_EN
| CCRC_EN
| CTO_EN
| BRR_EN
| BWR_EN
| TC_EN
| CC_EN
));
397 //Controller INIT procedure start.
398 MmioOr32 (MMCHS_CON
, INIT
);
399 MmioWrite32 (MMCHS_CMD
, 0x00000000);
400 while (!(MmioRead32 (MMCHS_STAT
) & CC
));
405 //Set CC bit to 0x1 to clear the flag
406 MmioOr32 (MMCHS_STAT
, CC
);
408 //Retry INIT procedure.
409 MmioWrite32 (MMCHS_CMD
, 0x00000000);
410 while (!(MmioRead32 (MMCHS_STAT
) & CC
));
412 //End initialization sequence
413 MmioAnd32 (MMCHS_CON
, ~INIT
);
415 MmioOr32 (MMCHS_HCTL
, (SDVS_3_0_V
| DTW_1_BIT
| SDBP_ON
));
417 //Change clock frequency to 400KHz to fit protocol
418 UpdateMMCHSClkFrequency(CLKD_400KHZ
);
420 MmioOr32 (MMCHS_CON
, OD
);
423 Status
= SendCmd (CMD0
, CMD0_INT_EN
, CmdArgument
);
424 if (EFI_ERROR(Status
)) {
425 DEBUG ((EFI_D_ERROR
, "Cmd0 fails.\n"));
429 DEBUG ((EFI_D_INFO
, "CMD0 response: %x\n", MmioRead32 (MMCHS_RSP10
)));
432 Status
= SendCmd (CMD5
, CMD5_INT_EN
, CmdArgument
);
433 if (Status
== EFI_SUCCESS
) {
434 DEBUG ((EFI_D_ERROR
, "CMD5 Success. SDIO card. Follow SDIO card specification.\n"));
435 DEBUG ((EFI_D_INFO
, "CMD5 response: %x\n", MmioRead32 (MMCHS_RSP10
)));
436 //NOTE: Returning unsupported error for now. Need to implement SDIO specification.
437 return EFI_UNSUPPORTED
;
439 DEBUG ((EFI_D_INFO
, "CMD5 fails. Not an SDIO card.\n"));
442 MmioOr32 (MMCHS_SYSCTL
, SRC
);
444 while ((MmioRead32 (MMCHS_SYSCTL
) & SRC
));
446 //Send CMD8 command. (New v2.00 command for Voltage check)
447 //Only 2.7V - 3.6V is supported for SD2.0, only SD 2.0 card can pass.
448 //MMC & SD1.1 card will fail this command.
449 CmdArgument
= CMD8_ARG
;
450 Status
= SendCmd (CMD8
, CMD8_INT_EN
, CmdArgument
);
451 if (Status
== EFI_SUCCESS
) {
452 Response
= MmioRead32 (MMCHS_RSP10
);
453 DEBUG ((EFI_D_INFO
, "CMD8 success. CMD8 response: %x\n", Response
));
454 if (Response
!= CmdArgument
) {
455 return EFI_DEVICE_ERROR
;
457 DEBUG ((EFI_D_INFO
, "Card is SD2.0\n"));
458 SDCmd8Supported
= TRUE
; //Supports high capacity.
460 DEBUG ((EFI_D_INFO
, "CMD8 fails. Not an SD2.0 card.\n"));
463 MmioOr32 (MMCHS_SYSCTL
, SRC
);
465 while ((MmioRead32 (MMCHS_SYSCTL
) & SRC
));
467 //Poll till card is busy
468 while (RetryCount
< MAX_RETRY_COUNT
) {
469 //Send CMD55 command.
471 Status
= SendCmd (CMD55
, CMD55_INT_EN
, CmdArgument
);
472 if (Status
== EFI_SUCCESS
) {
473 DEBUG ((EFI_D_INFO
, "CMD55 success. CMD55 response: %x\n", MmioRead32 (MMCHS_RSP10
)));
474 gCardInfo
.CardType
= SD_CARD
;
476 DEBUG ((EFI_D_INFO
, "CMD55 fails.\n"));
477 gCardInfo
.CardType
= MMC_CARD
;
480 //Send appropriate command for the card type which got detected.
481 if (gCardInfo
.CardType
== SD_CARD
) {
482 CmdArgument
= ((UINTN
*) &(gCardInfo
.OCRData
))[0];
485 if (SDCmd8Supported
) {
489 Status
= SendCmd (ACMD41
, ACMD41_INT_EN
, CmdArgument
);
490 if (EFI_ERROR(Status
)) {
491 DEBUG ((EFI_D_INFO
, "ACMD41 fails.\n"));
494 ((UINT32
*) &(gCardInfo
.OCRData
))[0] = MmioRead32 (MMCHS_RSP10
);
495 DEBUG ((EFI_D_INFO
, "SD card detected. ACMD41 OCR: %x\n", ((UINT32
*) &(gCardInfo
.OCRData
))[0]));
496 } else if (gCardInfo
.CardType
== MMC_CARD
) {
498 Status
= SendCmd (CMD1
, CMD1_INT_EN
, CmdArgument
);
499 if (EFI_ERROR(Status
)) {
500 DEBUG ((EFI_D_INFO
, "CMD1 fails.\n"));
503 Response
= MmioRead32 (MMCHS_RSP10
);
504 DEBUG ((EFI_D_INFO
, "MMC card detected.. CMD1 response: %x\n", Response
));
506 //NOTE: For now, I am skipping this since I only have an SD card.
507 //Compare card OCR and host OCR (Section 22.6.1.3.2.4)
508 return EFI_UNSUPPORTED
; //For now, MMC is not supported.
511 //Poll the card until it is out of its power-up sequence.
512 if (gCardInfo
.OCRData
.Busy
== 1) {
514 if (SDCmd8Supported
) {
515 gCardInfo
.CardType
= SD_CARD_2
;
518 //Card is ready. Check CCS (Card capacity status) bit (bit#30).
519 //SD 2.0 standard card will response with CCS 0, SD high capacity card will respond with CCS 1.
520 if (gCardInfo
.OCRData
.AccessMode
& BIT1
) {
521 gCardInfo
.CardType
= SD_CARD_2_HIGH
;
522 DEBUG ((EFI_D_INFO
, "High capacity card.\n"));
524 DEBUG ((EFI_D_INFO
, "Standard capacity card.\n"));
534 if (RetryCount
== MAX_RETRY_COUNT
) {
535 DEBUG ((EFI_D_ERROR
, "Timeout error. RetryCount: %d\n", RetryCount
));
541 Status
= SendCmd (CMD2
, CMD2_INT_EN
, CmdArgument
);
542 if (EFI_ERROR(Status
)) {
543 DEBUG ((EFI_D_ERROR
, "CMD2 fails. Status: %x\n", Status
));
547 DEBUG ((EFI_D_INFO
, "CMD2 response: %x %x %x %x\n", MmioRead32 (MMCHS_RSP10
), MmioRead32 (MMCHS_RSP32
), MmioRead32 (MMCHS_RSP54
), MmioRead32 (MMCHS_RSP76
)));
549 //Parse CID register data.
550 ParseCardCIDData(MmioRead32 (MMCHS_RSP10
), MmioRead32 (MMCHS_RSP32
), MmioRead32 (MMCHS_RSP54
), MmioRead32 (MMCHS_RSP76
));
554 Status
= SendCmd (CMD3
, CMD3_INT_EN
, CmdArgument
);
555 if (EFI_ERROR(Status
)) {
556 DEBUG ((EFI_D_ERROR
, "CMD3 fails. Status: %x\n", Status
));
560 //Set RCA for the detected card. RCA is CMD3 response.
561 gCardInfo
.RCA
= (MmioRead32 (MMCHS_RSP10
) >> 16);
562 DEBUG ((EFI_D_INFO
, "CMD3 response: RCA %x\n", gCardInfo
.RCA
));
564 //MMC Bus setting change after card identification.
565 MmioAnd32 (MMCHS_CON
, ~OD
);
566 MmioOr32 (MMCHS_HCTL
, SDVS_3_0_V
);
567 UpdateMMCHSClkFrequency(CLKD_400KHZ
); //Set the clock frequency to 400KHz.
574 GetCardSpecificData (
581 //Send CMD9 to retrieve CSD.
582 CmdArgument
= gCardInfo
.RCA
<< 16;
583 Status
= SendCmd (CMD9
, CMD9_INT_EN
, CmdArgument
);
584 if (EFI_ERROR(Status
)) {
585 DEBUG ((EFI_D_ERROR
, "CMD9 fails. Status: %x\n", Status
));
589 //Populate 128-bit CSD register data.
590 ((UINT32
*)&(gCardInfo
.CSDData
))[0] = MmioRead32 (MMCHS_RSP10
);
591 ((UINT32
*)&(gCardInfo
.CSDData
))[1] = MmioRead32 (MMCHS_RSP32
);
592 ((UINT32
*)&(gCardInfo
.CSDData
))[2] = MmioRead32 (MMCHS_RSP54
);
593 ((UINT32
*)&(gCardInfo
.CSDData
))[3] = MmioRead32 (MMCHS_RSP76
);
595 DEBUG ((EFI_D_INFO
, "CMD9 response: %x %x %x %x\n", MmioRead32 (MMCHS_RSP10
), MmioRead32 (MMCHS_RSP32
), MmioRead32 (MMCHS_RSP54
), MmioRead32 (MMCHS_RSP76
)));
597 //Calculate total number of blocks and max. data transfer rate supported by the detected card.
598 GetCardConfigurationData();
605 PerformCardConfiguration (
609 UINTN CmdArgument
= 0;
613 CmdArgument
= gCardInfo
.RCA
<< 16;
614 Status
= SendCmd (CMD7
, CMD7_INT_EN
, CmdArgument
);
615 if (EFI_ERROR(Status
)) {
616 DEBUG ((EFI_D_ERROR
, "CMD7 fails. Status: %x\n", Status
));
620 if ((gCardInfo
.CardType
!= UNKNOWN_CARD
) && (gCardInfo
.CardType
!= MMC_CARD
)) {
621 // We could read SCR register, but SD Card Phys spec stats any SD Card shall
622 // set SCR.SD_BUS_WIDTHS to support 4-bit mode, so why bother?
624 // Send ACMD6 (application specific commands must be prefixed with CMD55)
625 Status
= SendCmd (CMD55
, CMD55_INT_EN
, CmdArgument
);
626 if (!EFI_ERROR (Status
)) {
627 // set device into 4-bit data bus mode
628 Status
= SendCmd (ACMD6
, ACMD6_INT_EN
, 0x2);
629 if (!EFI_ERROR (Status
)) {
630 // Set host controler into 4-bit mode
631 MmioOr32 (MMCHS_HCTL
, DTW_4_BIT
);
632 DEBUG ((EFI_D_INFO
, "SD Memory Card set to 4-bit mode\n"));
637 //Send CMD16 to set the block length
638 CmdArgument
= gCardInfo
.BlockSize
;
639 Status
= SendCmd (CMD16
, CMD16_INT_EN
, CmdArgument
);
640 if (EFI_ERROR(Status
)) {
641 DEBUG ((EFI_D_ERROR
, "CMD16 fails. Status: %x\n", Status
));
645 //Change MMCHS clock frequency to what detected card can support.
646 UpdateMMCHSClkFrequency(gCardInfo
.ClockFrequencySelect
);
654 IN EFI_BLOCK_IO_PROTOCOL
*This
,
659 UINTN
*DataBuffer
= Buffer
;
660 UINTN DataSize
= This
->Media
->BlockSize
/4;
662 UINTN RetryCount
= 0;
664 //Check controller status to make sure there is no error.
665 while (RetryCount
< MAX_RETRY_COUNT
) {
668 MmcStatus
= MmioRead32 (MMCHS_STAT
);
669 } while(MmcStatus
== 0);
671 //Check if Buffer read ready (BRR) bit is set?
672 if (MmcStatus
& BRR
) {
675 MmioOr32 (MMCHS_STAT
, BRR
);
677 //Read block worth of data.
678 for (Count
= 0; Count
< DataSize
; Count
++) {
679 *DataBuffer
++ = MmioRead32 (MMCHS_DATA
);
686 if (RetryCount
== MAX_RETRY_COUNT
) {
696 IN EFI_BLOCK_IO_PROTOCOL
*This
,
701 UINTN
*DataBuffer
= Buffer
;
702 UINTN DataSize
= This
->Media
->BlockSize
/4;
704 UINTN RetryCount
= 0;
706 //Check controller status to make sure there is no error.
707 while (RetryCount
< MAX_RETRY_COUNT
) {
710 MmcStatus
= MmioRead32 (MMCHS_STAT
);
711 } while(MmcStatus
== 0);
713 //Check if Buffer write ready (BWR) bit is set?
714 if (MmcStatus
& BWR
) {
717 MmioOr32 (MMCHS_STAT
, BWR
);
719 //Write block worth of data.
720 for (Count
= 0; Count
< DataSize
; Count
++) {
721 MmioWrite32 (MMCHS_DATA
, *DataBuffer
++);
729 if (RetryCount
== MAX_RETRY_COUNT
) {
738 IN EFI_BLOCK_IO_PROTOCOL
*This
,
742 IN OPERATION_TYPE OperationType
748 UINTN CmdInterruptEnable
;
751 EFI_PHYSICAL_ADDRESS BufferAddress
;
753 DMA_MAP_OPERATION DmaOperation
;
754 EFI_STATUS MmcStatus
;
755 UINTN RetryCount
= 0;
758 // Map passed in buffer for DMA xfer
759 DmaSize
= BlockCount
* This
->Media
->BlockSize
;
760 Status
= DmaMap (DmaOperation
, Buffer
, &DmaSize
, &BufferAddress
, &BufferMap
);
761 if (EFI_ERROR (Status
)) {
765 ZeroMem (&DmaOperation
, sizeof (DMA_MAP_OPERATION
));
768 Dma4
.DataType
= 2; // DMA4_CSDPi[1:0] 32-bit elements from MMCHS_DATA
770 Dma4
.SourceEndiansim
= 0; // DMA4_CSDPi[21]
772 Dma4
.DestinationEndianism
= 0; // DMA4_CSDPi[19]
774 Dma4
.SourcePacked
= 0; // DMA4_CSDPi[6]
776 Dma4
.DestinationPacked
= 0; // DMA4_CSDPi[13]
778 Dma4
.NumberOfElementPerFrame
= This
->Media
->BlockSize
/4; // DMA4_CENi (TRM 4K is optimum value)
780 Dma4
.NumberOfFramePerTransferBlock
= BlockCount
; // DMA4_CFNi
782 Dma4
.ReadPriority
= 0; // DMA4_CCRi[6] Low priority read
784 Dma4
.WritePriority
= 0; // DMA4_CCRi[23] Prefetech disabled
787 //Populate the command information based on the operation type.
788 if (OperationType
== READ
) {
789 Cmd
= CMD18
; //Multiple block read
790 CmdInterruptEnable
= CMD18_INT_EN
;
791 DmaOperation
= MapOperationBusMasterCommonBuffer
;
793 Dma4
.ReadPortAccessType
=0 ; // DMA4_CSDPi[8:7] Can not burst MMCHS_DATA reg
795 Dma4
.WritePortAccessType
= 3; // DMA4_CSDPi[15:14] Memory burst 16x32
797 Dma4
.WriteMode
= 1; // DMA4_CSDPi[17:16] Write posted
801 Dma4
.SourceStartAddress
= MMCHS_DATA
; // DMA4_CSSAi
803 Dma4
.DestinationStartAddress
= (UINT32
)BufferAddress
; // DMA4_CDSAi
805 Dma4
.SourceElementIndex
= 1; // DMA4_CSEi
807 Dma4
.SourceFrameIndex
= 0x200; // DMA4_CSFi
809 Dma4
.DestinationElementIndex
= 1; // DMA4_CDEi
811 Dma4
.DestinationFrameIndex
= 0; // DMA4_CDFi
815 Dma4
.ReadPortAccessMode
= 0; // DMA4_CCRi[13:12] Always read MMCHS_DATA
817 Dma4
.WritePortAccessMode
= 1; // DMA4_CCRi[15:14] Post increment memory address
819 Dma4
.ReadRequestNumber
= 0x1e; // DMA4_CCRi[4:0] Syncro with MMCA_DMA_RX (61)
821 Dma4
.WriteRequestNumber
= 1; // DMA4_CCRi[20:19] Syncro upper 0x3e == 62 (one based)
823 } else if (OperationType
== WRITE
) {
824 Cmd
= CMD25
; //Multiple block write
825 CmdInterruptEnable
= CMD25_INT_EN
;
826 DmaOperation
= MapOperationBusMasterRead
;
828 Dma4
.ReadPortAccessType
= 3; // DMA4_CSDPi[8:7] Memory burst 16x32
830 Dma4
.WritePortAccessType
= 0; // DMA4_CSDPi[15:14] Can not burst MMCHS_DATA reg
832 Dma4
.WriteMode
= 1; // DMA4_CSDPi[17:16] Write posted ???
836 Dma4
.SourceStartAddress
= (UINT32
)BufferAddress
; // DMA4_CSSAi
838 Dma4
.DestinationStartAddress
= MMCHS_DATA
; // DMA4_CDSAi
840 Dma4
.SourceElementIndex
= 1; // DMA4_CSEi
842 Dma4
.SourceFrameIndex
= 0x200; // DMA4_CSFi
844 Dma4
.DestinationElementIndex
= 1; // DMA4_CDEi
846 Dma4
.DestinationFrameIndex
= 0; // DMA4_CDFi
850 Dma4
.ReadPortAccessMode
= 1; // DMA4_CCRi[13:12] Post increment memory address
852 Dma4
.WritePortAccessMode
= 0; // DMA4_CCRi[15:14] Always write MMCHS_DATA
854 Dma4
.ReadRequestNumber
= 0x1d; // DMA4_CCRi[4:0] Syncro with MMCA_DMA_TX (60)
856 Dma4
.WriteRequestNumber
= 1; // DMA4_CCRi[20:19] Syncro upper 0x3d == 61 (one based)
859 return EFI_INVALID_PARAMETER
;
863 EnableDmaChannel (2, &Dma4
);
866 //Set command argument based on the card access mode (Byte mode or Block mode)
867 if (gCardInfo
.OCRData
.AccessMode
& BIT1
) {
870 CmdArgument
= Lba
* This
->Media
->BlockSize
;
874 Status
= SendCmd (Cmd
, CmdInterruptEnable
, CmdArgument
);
875 if (EFI_ERROR (Status
)) {
876 DEBUG ((EFI_D_ERROR
, "CMD fails. Status: %x\n", Status
));
880 //Check for the Transfer completion.
881 while (RetryCount
< MAX_RETRY_COUNT
) {
884 MmcStatus
= MmioRead32 (MMCHS_STAT
);
885 } while (MmcStatus
== 0);
887 //Check if Transfer complete (TC) bit is set?
888 if (MmcStatus
& TC
) {
891 DEBUG ((EFI_D_ERROR
, "MmcStatus for TC: %x\n", MmcStatus
));
892 //Check if DEB, DCRC or DTO interrupt occured.
893 if ((MmcStatus
& DEB
) | (MmcStatus
& DCRC
) | (MmcStatus
& DTO
)) {
894 //There was an error during the data transfer.
896 //Set SRD bit to 1 and wait until it return to 0x0.
897 MmioOr32 (MMCHS_SYSCTL
, SRD
);
898 while((MmioRead32 (MMCHS_SYSCTL
) & SRD
) != 0x0);
900 DisableDmaChannel (2, DMA4_CSR_BLOCK
, DMA4_CSR_ERR
);
901 DmaUnmap (BufferMap
);
902 return EFI_DEVICE_ERROR
;
908 DisableDmaChannel (2, DMA4_CSR_BLOCK
, DMA4_CSR_ERR
);
909 Status
= DmaUnmap (BufferMap
);
911 if (RetryCount
== MAX_RETRY_COUNT
) {
912 DEBUG ((EFI_D_ERROR
, "TransferBlockData timed out.\n"));
922 IN EFI_BLOCK_IO_PROTOCOL
*This
,
925 IN OPERATION_TYPE OperationType
930 UINTN RetryCount
= 0;
932 UINTN CmdInterruptEnable
= 0;
933 UINTN CmdArgument
= 0;
936 //Populate the command information based on the operation type.
937 if (OperationType
== READ
) {
938 Cmd
= CMD17
; //Single block read
939 CmdInterruptEnable
= CMD18_INT_EN
;
940 } else if (OperationType
== WRITE
) {
941 Cmd
= CMD24
; //Single block write
942 CmdInterruptEnable
= CMD24_INT_EN
;
945 //Set command argument based on the card access mode (Byte mode or Block mode)
946 if (gCardInfo
.OCRData
.AccessMode
& BIT1
) {
949 CmdArgument
= Lba
* This
->Media
->BlockSize
;
953 Status
= SendCmd (Cmd
, CmdInterruptEnable
, CmdArgument
);
954 if (EFI_ERROR(Status
)) {
955 DEBUG ((EFI_D_ERROR
, "CMD fails. Status: %x\n", Status
));
959 //Read or Write data.
960 if (OperationType
== READ
) {
961 Status
= ReadBlockData (This
, Buffer
);
962 if (EFI_ERROR(Status
)) {
963 DEBUG((EFI_D_ERROR
, "ReadBlockData fails.\n"));
966 } else if (OperationType
== WRITE
) {
967 Status
= WriteBlockData (This
, Buffer
);
968 if (EFI_ERROR(Status
)) {
969 DEBUG((EFI_D_ERROR
, "WriteBlockData fails.\n"));
974 //Check for the Transfer completion.
975 while (RetryCount
< MAX_RETRY_COUNT
) {
978 MmcStatus
= MmioRead32 (MMCHS_STAT
);
979 } while (MmcStatus
== 0);
981 //Check if Transfer complete (TC) bit is set?
982 if (MmcStatus
& TC
) {
985 DEBUG ((EFI_D_ERROR
, "MmcStatus for TC: %x\n", MmcStatus
));
986 //Check if DEB, DCRC or DTO interrupt occured.
987 if ((MmcStatus
& DEB
) | (MmcStatus
& DCRC
) | (MmcStatus
& DTO
)) {
988 //There was an error during the data transfer.
990 //Set SRD bit to 1 and wait until it return to 0x0.
991 MmioOr32 (MMCHS_SYSCTL
, SRD
);
992 while((MmioRead32 (MMCHS_SYSCTL
) & SRD
) != 0x0);
994 return EFI_DEVICE_ERROR
;
1000 if (RetryCount
== MAX_RETRY_COUNT
) {
1001 DEBUG ((EFI_D_ERROR
, "TransferBlockData timed out.\n"));
1017 // Card detect is a GPIO0 on the TPS65950
1019 Status
= gTPS65950
->Read (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID2
, GPIODATAIN1
), 1, &Data
);
1020 if (EFI_ERROR (Status
)) {
1024 if ((Data
& CARD_DETECT_BIT
) == CARD_DETECT_BIT
) {
1039 if (!CardPresent ()) {
1040 return EFI_NO_MEDIA
;
1043 //Initialize MMC host controller clocks.
1044 Status
= InitializeMMCHS ();
1045 if (EFI_ERROR(Status
)) {
1046 DEBUG ((EFI_D_ERROR
, "Initialize MMC host controller fails. Status: %x\n", Status
));
1050 //Software reset of the MMCHS host controller.
1051 MmioWrite32 (MMCHS_SYSCONFIG
, SOFTRESET
);
1053 while ((MmioRead32 (MMCHS_SYSSTATUS
) & RESETDONE_MASK
) != RESETDONE
);
1055 //Soft reset for all.
1056 MmioWrite32 (MMCHS_SYSCTL
, SRA
);
1058 while ((MmioRead32 (MMCHS_SYSCTL
) & SRA
) != 0x0);
1060 //Voltage capabilities initialization. Activate VS18 and VS30.
1061 MmioOr32 (MMCHS_CAPA
, (VS30
| VS18
));
1063 //Wakeup configuration
1064 MmioOr32 (MMCHS_SYSCONFIG
, ENAWAKEUP
);
1065 MmioOr32 (MMCHS_HCTL
, IWE
);
1067 //MMCHS Controller default initialization
1068 MmioOr32 (MMCHS_CON
, (OD
| DW8_1_4_BIT
| CEATA_OFF
));
1070 MmioWrite32 (MMCHS_HCTL
, (SDVS_3_0_V
| DTW_1_BIT
| SDBP_OFF
));
1072 //Enable internal clock
1073 MmioOr32 (MMCHS_SYSCTL
, ICE
);
1075 //Set the clock frequency to 80KHz.
1076 UpdateMMCHSClkFrequency (CLKD_80KHZ
);
1078 //Enable SD bus power.
1079 MmioOr32 (MMCHS_HCTL
, (SDBP_ON
));
1081 //Poll till SD bus power bit is set.
1082 while ((MmioRead32 (MMCHS_HCTL
) & SDBP_MASK
) != SDBP_ON
);
1085 Status
= PerformCardIdenfication ();
1086 if (EFI_ERROR(Status
)) {
1087 DEBUG ((EFI_D_ERROR
, "No MMC/SD card detected.\n"));
1091 //Get CSD (Card specific data) for the detected card.
1092 Status
= GetCardSpecificData();
1093 if (EFI_ERROR(Status
)) {
1097 //Configure the card in data transfer mode.
1098 Status
= PerformCardConfiguration();
1099 if (EFI_ERROR(Status
)) {
1103 //Patch the Media structure.
1104 gMMCHSMedia
.LastBlock
= (gCardInfo
.NumBlocks
- 1);
1105 gMMCHSMedia
.BlockSize
= gCardInfo
.BlockSize
;
1106 gMMCHSMedia
.ReadOnly
= (MmioRead32 (GPIO1_BASE
+ GPIO_DATAIN
) & BIT23
) == BIT23
;
1107 gMMCHSMedia
.MediaPresent
= TRUE
;
1108 gMMCHSMedia
.MediaId
++;
1110 DEBUG ((EFI_D_INFO
, "SD Card Media Change on Handle 0x%08x\n", gImageHandle
));
1115 #define MAX_MMCHS_TRANSFER_SIZE 0x4000
1119 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1122 IN UINTN BufferSize
,
1123 IN OPERATION_TYPE OperationType
1126 EFI_STATUS Status
= EFI_SUCCESS
;
1127 UINTN RetryCount
= 0;
1129 UINTN BytesToBeTranferedThisPass
= 0;
1130 UINTN BytesRemainingToBeTransfered
;
1141 Status
= DetectCard ();
1142 if (EFI_ERROR (Status
)) {
1143 // We detected a removal
1144 gMMCHSMedia
.MediaPresent
= FALSE
;
1145 gMMCHSMedia
.LastBlock
= 0;
1146 gMMCHSMedia
.BlockSize
= 512; // Should be zero but there is a bug in DiskIo
1147 gMMCHSMedia
.ReadOnly
= FALSE
;
1149 gMediaChange
= FALSE
;
1150 } else if (!gMMCHSMedia
.MediaPresent
) {
1151 Status
= EFI_NO_MEDIA
;
1156 DEBUG ((EFI_D_INFO
, "SD Card ReinstallProtocolInterface ()\n"));
1157 gBS
->ReinstallProtocolInterface (
1159 &gEfiBlockIoProtocolGuid
,
1163 return EFI_MEDIA_CHANGED
;
1166 if (EFI_ERROR (Status
)) {
1170 if (Buffer
== NULL
) {
1171 Status
= EFI_INVALID_PARAMETER
;
1175 if (Lba
> This
->Media
->LastBlock
) {
1176 Status
= EFI_INVALID_PARAMETER
;
1180 if ((BufferSize
% This
->Media
->BlockSize
) != 0) {
1181 Status
= EFI_BAD_BUFFER_SIZE
;
1185 //Check if the data lines are not in use.
1186 while ((RetryCount
++ < MAX_RETRY_COUNT
) && ((MmioRead32 (MMCHS_PSTATE
) & DATI_MASK
) != DATI_ALLOWED
));
1187 if (RetryCount
== MAX_RETRY_COUNT
) {
1188 Status
= EFI_TIMEOUT
;
1192 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1194 BytesRemainingToBeTransfered
= BufferSize
;
1195 while (BytesRemainingToBeTransfered
> 0) {
1198 Status
= EFI_NO_MEDIA
;
1199 DEBUG ((EFI_D_INFO
, "SdReadWrite() EFI_NO_MEDIA due to gMediaChange\n"));
1200 goto DoneRestoreTPL
;
1203 // Turn OFF DMA path until it is debugged
1204 // BytesToBeTranferedThisPass = (BytesToBeTranferedThisPass >= MAX_MMCHS_TRANSFER_SIZE) ? MAX_MMCHS_TRANSFER_SIZE : BytesRemainingToBeTransfered;
1205 BytesToBeTranferedThisPass
= This
->Media
->BlockSize
;
1207 BlockCount
= BytesToBeTranferedThisPass
/This
->Media
->BlockSize
;
1209 if (BlockCount
> 1) {
1210 Status
= DmaBlocks (This
, Lba
, Buffer
, BlockCount
, OperationType
);
1212 //Transfer a block worth of data.
1213 Status
= TransferBlock (This
, Lba
, Buffer
, OperationType
);
1216 if (EFI_ERROR(Status
)) {
1217 DEBUG ((EFI_D_ERROR
, "TransferBlockData fails. %x\n", Status
));
1218 goto DoneRestoreTPL
;
1221 BytesRemainingToBeTransfered
-= BytesToBeTranferedThisPass
;
1223 Buffer
= (UINT8
*)Buffer
+ This
->Media
->BlockSize
;
1228 gBS
->RestoreTPL (OldTpl
);
1239 Reset the Block Device.
1243 @param This Indicates a pointer to the calling context.
1245 @param ExtendedVerification Driver may perform diagnostics on reset.
1249 @retval EFI_SUCCESS The device was reset.
1251 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
1261 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1262 IN BOOLEAN ExtendedVerification
1271 Read BufferSize bytes from Lba into Buffer.
1275 @param This Indicates a pointer to the calling context.
1277 @param MediaId Id of the media, changes every time the media is replaced.
1279 @param Lba The starting Logical Block Address to read from
1281 @param BufferSize Size of Buffer, must be a multiple of device block size.
1283 @param Buffer A pointer to the destination buffer for the data. The caller is
1285 responsible for either having implicit or explicit ownership of the buffer.
1289 @retval EFI_SUCCESS The data was read correctly from the device.
1291 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
1293 @retval EFI_NO_MEDIA There is no media in the device.
1295 @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.
1297 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
1299 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
1301 or the buffer is not on proper alignment.
1309 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1312 IN UINTN BufferSize
,
1318 //Perform Read operation.
1319 Status
= SdReadWrite (This
, (UINTN
)Lba
, Buffer
, BufferSize
, READ
);
1328 Write BufferSize bytes from Lba into Buffer.
1332 @param This Indicates a pointer to the calling context.
1334 @param MediaId The media ID that the write request is for.
1336 @param Lba The starting logical block address to be written. The caller is
1338 responsible for writing to only legitimate locations.
1340 @param BufferSize Size of Buffer, must be a multiple of device block size.
1342 @param Buffer A pointer to the source buffer for the data.
1346 @retval EFI_SUCCESS The data was written correctly to the device.
1348 @retval EFI_WRITE_PROTECTED The device can not be written to.
1350 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
1352 @retval EFI_NO_MEDIA There is no media in the device.
1354 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
1356 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
1358 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
1360 or the buffer is not on proper alignment.
1368 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1371 IN UINTN BufferSize
,
1377 //Perform write operation.
1378 Status
= SdReadWrite (This
, (UINTN
)Lba
, Buffer
, BufferSize
, WRITE
);
1388 Flush the Block Device.
1392 @param This Indicates a pointer to the calling context.
1396 @retval EFI_SUCCESS All outstanding data was written to the device
1398 @retval EFI_DEVICE_ERROR The device reported an error while writting back the data
1400 @retval EFI_NO_MEDIA There is no media in the device.
1408 IN EFI_BLOCK_IO_PROTOCOL
*This
1415 EFI_BLOCK_IO_PROTOCOL gBlockIo
= {
1416 EFI_BLOCK_IO_INTERFACE_REVISION
, // Revision
1417 &gMMCHSMedia
, // *Media
1418 MMCHSReset
, // Reset
1419 MMCHSReadBlocks
, // ReadBlocks
1420 MMCHSWriteBlocks
, // WriteBlocks
1421 MMCHSFlushBlocks
// FlushBlocks
1427 Timer callback to convert card present hardware into a boolean that indicates
1429 a media change event has happened. If you just check the GPIO you could see
1431 card 1 and then check again after card 1 was removed and card 2 was inserted
1433 and you would still see media present. Thus you need the timer tick to catch
1439 @param Event Event whose notification function is being invoked.
1441 @param Context The pointer to the notification function's context,
1443 which is implementation-dependent. Not used.
1457 Present
= CardPresent ();
1458 if (gMMCHSMedia
.MediaPresent
) {
1459 if (!Present
&& !gMediaChange
) {
1460 gMediaChange
= TRUE
;
1463 if (Present
&& !gMediaChange
) {
1464 gMediaChange
= TRUE
;
1473 IN EFI_HANDLE ImageHandle
,
1474 IN EFI_SYSTEM_TABLE
*SystemTable
1479 Status
= gBS
->LocateProtocol (&gEmbeddedExternalDeviceProtocolGuid
, NULL
, (VOID
**)&gTPS65950
);
1480 ASSERT_EFI_ERROR(Status
);
1482 ZeroMem (&gCardInfo
, sizeof (CARD_INFO
));
1484 Status
= gBS
->CreateEvent (EVT_TIMER
| EVT_NOTIFY_SIGNAL
, TPL_CALLBACK
, TimerCallback
, NULL
, &gTimerEvent
);
1485 ASSERT_EFI_ERROR (Status
);
1487 Status
= gBS
->SetTimer (gTimerEvent
, TimerPeriodic
, FixedPcdGet32 (PcdMmchsTimerFreq100NanoSeconds
));
1488 ASSERT_EFI_ERROR (Status
);
1491 Status
= gBS
->InstallMultipleProtocolInterfaces (
1493 &gEfiBlockIoProtocolGuid
, &gBlockIo
,
1494 &gEfiDevicePathProtocolGuid
, &gMmcHsDevicePath
,