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
),
61 EMBEDDED_EXTERNAL_DEVICE
*gTPS65950
;
62 EFI_EVENT gTimerEvent
;
63 BOOLEAN gMediaChange
= FALSE
;
78 gCardInfo
.CIDData
.MDT
= ((Response0
>> 8) & 0xFFF);
79 gCardInfo
.CIDData
.PSN
= (((Response0
>> 24) & 0xFF) | ((Response1
& 0xFFFFFF) << 8));
80 gCardInfo
.CIDData
.PRV
= ((Response1
>> 24) & 0xFF);
81 gCardInfo
.CIDData
.PNM
[4] = ((Response2
) & 0xFF);
82 gCardInfo
.CIDData
.PNM
[3] = ((Response2
>> 8) & 0xFF);
83 gCardInfo
.CIDData
.PNM
[2] = ((Response2
>> 16) & 0xFF);
84 gCardInfo
.CIDData
.PNM
[1] = ((Response2
>> 24) & 0xFF);
85 gCardInfo
.CIDData
.PNM
[0] = ((Response3
) & 0xFF);
86 gCardInfo
.CIDData
.OID
= ((Response3
>> 8) & 0xFFFF);
87 gCardInfo
.CIDData
.MID
= ((Response3
>> 24) & 0xFF);
92 UpdateMMCHSClkFrequency (
96 //Set Clock enable to 0x0 to not provide the clock to the card
97 MmioAnd32 (MMCHS_SYSCTL
, ~CEN
);
99 //Set new clock frequency.
100 MmioAndThenOr32 (MMCHS_SYSCTL
, ~CLKD_MASK
, NewCLKD
<< 6);
102 //Poll till Internal Clock Stable
103 while ((MmioRead32 (MMCHS_SYSCTL
) & ICS_MASK
) != ICS
);
105 //Set Clock enable to 0x1 to provide the clock to the card
106 MmioOr32 (MMCHS_SYSCTL
, CEN
);
113 UINTN CmdInterruptEnableVal
,
118 UINTN RetryCount
= 0;
120 //Check if command line is in use or not. Poll till command line is available.
121 while ((MmioRead32 (MMCHS_PSTATE
) & DATI_MASK
) == DATI_NOT_ALLOWED
);
123 //Provide the block size.
124 MmioWrite32 (MMCHS_BLK
, BLEN_512BYTES
);
126 //Setting Data timeout counter value to max value.
127 MmioAndThenOr32 (MMCHS_SYSCTL
, ~DTO_MASK
, DTO_VAL
);
129 //Clear Status register.
130 MmioWrite32 (MMCHS_STAT
, 0xFFFFFFFF);
132 //Set command argument register
133 MmioWrite32 (MMCHS_ARG
, CmdArgument
);
135 //Enable interrupt enable events to occur
136 MmioWrite32 (MMCHS_IE
, CmdInterruptEnableVal
);
139 MmioWrite32 (MMCHS_CMD
, Cmd
);
141 //Check for the command status.
142 while (RetryCount
< MAX_RETRY_COUNT
) {
144 MmcStatus
= MmioRead32 (MMCHS_STAT
);
145 } while (MmcStatus
== 0);
147 //Read status of command response
148 if ((MmcStatus
& ERRI
) != 0) {
150 //Perform soft-reset for mmci_cmd line.
151 MmioOr32 (MMCHS_SYSCTL
, SRC
);
152 while ((MmioRead32 (MMCHS_SYSCTL
) & SRC
));
154 DEBUG ((EFI_D_INFO
, "MmcStatus: %x\n", MmcStatus
));
155 return EFI_DEVICE_ERROR
;
158 //Check if command is completed.
159 if ((MmcStatus
& CC
) == CC
) {
160 MmioWrite32 (MMCHS_STAT
, CC
);
167 if (RetryCount
== MAX_RETRY_COUNT
) {
176 GetBlockInformation (
181 CSD_SDV2
*CsdSDV2Data
;
184 if (gCardInfo
.CardType
== SD_CARD_2_HIGH
) {
185 CsdSDV2Data
= (CSD_SDV2
*)&gCardInfo
.CSDData
;
187 //Populate BlockSize.
188 *BlockSize
= (0x1UL
<< CsdSDV2Data
->READ_BL_LEN
);
190 //Calculate Total number of blocks.
191 CardSize
= CsdSDV2Data
->C_SIZELow16
| (CsdSDV2Data
->C_SIZEHigh6
<< 2);
192 *NumBlocks
= ((CardSize
+ 1) * 1024);
194 //Populate BlockSize.
195 *BlockSize
= (0x1UL
<< gCardInfo
.CSDData
.READ_BL_LEN
);
197 //Calculate Total number of blocks.
198 CardSize
= gCardInfo
.CSDData
.C_SIZELow2
| (gCardInfo
.CSDData
.C_SIZEHigh10
<< 2);
199 *NumBlocks
= (CardSize
+ 1) * (1 << (gCardInfo
.CSDData
.C_SIZE_MULT
+ 2));
202 //For >=2G card, BlockSize may be 1K, but the transfer size is 512 bytes.
203 if (*BlockSize
> 512) {
204 *NumBlocks
= MultU64x32(*NumBlocks
, *BlockSize
/2);
208 DEBUG ((EFI_D_INFO
, "Card type: %x, BlockSize: %x, NumBlocks: %x\n", gCardInfo
.CardType
, *BlockSize
, *NumBlocks
));
214 UINTN
*ClockFrequencySelect
217 UINT8 MaxDataTransferRate
;
218 UINTN TransferRateValue
= 0;
219 UINTN TimeValue
= 0 ;
222 MaxDataTransferRate
= gCardInfo
.CSDData
.TRAN_SPEED
;
224 // For SD Cards we would need to send CMD6 to set
225 // speeds abouve 25MHz. High Speed mode 50 MHz and up
227 //Calculate Transfer rate unit (Bits 2:0 of TRAN_SPEED)
228 switch (MaxDataTransferRate
& 0x7) {
230 TransferRateValue
= 100 * 1000;
234 TransferRateValue
= 1 * 1000 * 1000;
238 TransferRateValue
= 10 * 1000 * 1000;
242 TransferRateValue
= 100 * 1000 * 1000;
246 DEBUG((EFI_D_ERROR
, "Invalid parameter.\n"));
250 //Calculate Time value (Bits 6:3 of TRAN_SPEED)
251 switch ((MaxDataTransferRate
>> 3) & 0xF) {
313 DEBUG((EFI_D_ERROR
, "Invalid parameter.\n"));
317 Frequency
= TransferRateValue
* TimeValue
/10;
319 //Calculate Clock divider value to program in MMCHS_SYSCTL[CLKD] field.
320 *ClockFrequencySelect
= ((MMC_REFERENCE_CLK
/Frequency
) + 1);
322 DEBUG ((EFI_D_INFO
, "MaxDataTransferRate: 0x%x, Frequency: %d KHz, ClockFrequencySelect: %x\n", MaxDataTransferRate
, Frequency
/1000, *ClockFrequencySelect
));
327 GetCardConfigurationData (
333 UINTN ClockFrequencySelect
;
335 //Calculate BlockSize and Total number of blocks in the detected card.
336 GetBlockInformation(&BlockSize
, &NumBlocks
);
337 gCardInfo
.BlockSize
= BlockSize
;
338 gCardInfo
.NumBlocks
= NumBlocks
;
340 //Calculate Card clock divider value.
341 CalculateCardCLKD(&ClockFrequencySelect
);
342 gCardInfo
.ClockFrequencySelect
= ClockFrequencySelect
;
354 //Select Device group to belong to P1 device group in Power IC.
356 Status
= gTPS65950
->Write (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4
, VMMC1_DEV_GRP
), 1, &Data
);
357 ASSERT_EFI_ERROR(Status
);
359 //Configure voltage regulator for MMC1 in Power IC to output 3.0 voltage.
361 Status
= gTPS65950
->Write (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4
, VMMC1_DEDICATED_REG
), 1, &Data
);
362 ASSERT_EFI_ERROR(Status
);
364 //After ramping up voltage, set VDDS stable bit to indicate that voltage level is stable.
365 MmioOr32 (CONTROL_PBIAS_LITE
, (PBIASLITEVMODE0
| PBIASLITEPWRDNZ0
| PBIASSPEEDCTRL0
| PBIASLITEVMODE1
| PBIASLITEWRDNZ1
));
368 MmioAndThenOr32 (GPIO1_BASE
+ GPIO_OE
, ~BIT23
, BIT23
);
370 // Enable Card Detect
371 Data
= CARD_DETECT_ENABLE
;
372 gTPS65950
->Write (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID2
, TPS65950_GPIO_CTRL
), 1, &Data
);
380 PerformCardIdenfication (
385 UINTN CmdArgument
= 0;
387 UINTN RetryCount
= 0;
388 BOOLEAN SDCmd8Supported
= FALSE
;
391 MmioWrite32 (MMCHS_IE
, (BADA_EN
| CERR_EN
| DEB_EN
| DCRC_EN
| DTO_EN
| CIE_EN
|
392 CEB_EN
| CCRC_EN
| CTO_EN
| BRR_EN
| BWR_EN
| TC_EN
| CC_EN
));
394 //Controller INIT procedure start.
395 MmioOr32 (MMCHS_CON
, INIT
);
396 MmioWrite32 (MMCHS_CMD
, 0x00000000);
397 while (!(MmioRead32 (MMCHS_STAT
) & CC
));
402 //Set CC bit to 0x1 to clear the flag
403 MmioOr32 (MMCHS_STAT
, CC
);
405 //Retry INIT procedure.
406 MmioWrite32 (MMCHS_CMD
, 0x00000000);
407 while (!(MmioRead32 (MMCHS_STAT
) & CC
));
409 //End initialization sequence
410 MmioAnd32 (MMCHS_CON
, ~INIT
);
412 MmioOr32 (MMCHS_HCTL
, (SDVS_3_0_V
| DTW_1_BIT
| SDBP_ON
));
414 //Change clock frequency to 400KHz to fit protocol
415 UpdateMMCHSClkFrequency(CLKD_400KHZ
);
417 MmioOr32 (MMCHS_CON
, OD
);
420 Status
= SendCmd (CMD0
, CMD0_INT_EN
, CmdArgument
);
421 if (EFI_ERROR(Status
)) {
422 DEBUG ((EFI_D_ERROR
, "Cmd0 fails.\n"));
426 DEBUG ((EFI_D_INFO
, "CMD0 response: %x\n", MmioRead32 (MMCHS_RSP10
)));
429 Status
= SendCmd (CMD5
, CMD5_INT_EN
, CmdArgument
);
430 if (Status
== EFI_SUCCESS
) {
431 DEBUG ((EFI_D_ERROR
, "CMD5 Success. SDIO card. Follow SDIO card specification.\n"));
432 DEBUG ((EFI_D_INFO
, "CMD5 response: %x\n", MmioRead32 (MMCHS_RSP10
)));
433 //NOTE: Returning unsupported error for now. Need to implement SDIO specification.
434 return EFI_UNSUPPORTED
;
436 DEBUG ((EFI_D_INFO
, "CMD5 fails. Not an SDIO card.\n"));
439 MmioOr32 (MMCHS_SYSCTL
, SRC
);
441 while ((MmioRead32 (MMCHS_SYSCTL
) & SRC
));
443 //Send CMD8 command. (New v2.00 command for Voltage check)
444 //Only 2.7V - 3.6V is supported for SD2.0, only SD 2.0 card can pass.
445 //MMC & SD1.1 card will fail this command.
446 CmdArgument
= CMD8_ARG
;
447 Status
= SendCmd (CMD8
, CMD8_INT_EN
, CmdArgument
);
448 if (Status
== EFI_SUCCESS
) {
449 Response
= MmioRead32 (MMCHS_RSP10
);
450 DEBUG ((EFI_D_INFO
, "CMD8 success. CMD8 response: %x\n", Response
));
451 if (Response
!= CmdArgument
) {
452 return EFI_DEVICE_ERROR
;
454 DEBUG ((EFI_D_INFO
, "Card is SD2.0\n"));
455 SDCmd8Supported
= TRUE
; //Supports high capacity.
457 DEBUG ((EFI_D_INFO
, "CMD8 fails. Not an SD2.0 card.\n"));
460 MmioOr32 (MMCHS_SYSCTL
, SRC
);
462 while ((MmioRead32 (MMCHS_SYSCTL
) & SRC
));
464 //Poll till card is busy
465 while (RetryCount
< MAX_RETRY_COUNT
) {
466 //Send CMD55 command.
468 Status
= SendCmd (CMD55
, CMD55_INT_EN
, CmdArgument
);
469 if (Status
== EFI_SUCCESS
) {
470 DEBUG ((EFI_D_INFO
, "CMD55 success. CMD55 response: %x\n", MmioRead32 (MMCHS_RSP10
)));
471 gCardInfo
.CardType
= SD_CARD
;
473 DEBUG ((EFI_D_INFO
, "CMD55 fails.\n"));
474 gCardInfo
.CardType
= MMC_CARD
;
477 //Send appropriate command for the card type which got detected.
478 if (gCardInfo
.CardType
== SD_CARD
) {
479 CmdArgument
= ((UINTN
*) &(gCardInfo
.OCRData
))[0];
482 if (SDCmd8Supported
) {
486 Status
= SendCmd (ACMD41
, ACMD41_INT_EN
, CmdArgument
);
487 if (EFI_ERROR(Status
)) {
488 DEBUG ((EFI_D_INFO
, "ACMD41 fails.\n"));
491 ((UINT32
*) &(gCardInfo
.OCRData
))[0] = MmioRead32 (MMCHS_RSP10
);
492 DEBUG ((EFI_D_INFO
, "SD card detected. ACMD41 OCR: %x\n", ((UINT32
*) &(gCardInfo
.OCRData
))[0]));
493 } else if (gCardInfo
.CardType
== MMC_CARD
) {
495 Status
= SendCmd (CMD1
, CMD1_INT_EN
, CmdArgument
);
496 if (EFI_ERROR(Status
)) {
497 DEBUG ((EFI_D_INFO
, "CMD1 fails.\n"));
500 Response
= MmioRead32 (MMCHS_RSP10
);
501 DEBUG ((EFI_D_INFO
, "MMC card detected.. CMD1 response: %x\n", Response
));
503 //NOTE: For now, I am skipping this since I only have an SD card.
504 //Compare card OCR and host OCR (Section 22.6.1.3.2.4)
505 return EFI_UNSUPPORTED
; //For now, MMC is not supported.
508 //Poll the card until it is out of its power-up sequence.
509 if (gCardInfo
.OCRData
.Busy
== 1) {
511 if (SDCmd8Supported
) {
512 gCardInfo
.CardType
= SD_CARD_2
;
515 //Card is ready. Check CCS (Card capacity status) bit (bit#30).
516 //SD 2.0 standard card will response with CCS 0, SD high capacity card will respond with CCS 1.
517 if (gCardInfo
.OCRData
.AccessMode
& BIT1
) {
518 gCardInfo
.CardType
= SD_CARD_2_HIGH
;
519 DEBUG ((EFI_D_INFO
, "High capacity card.\n"));
521 DEBUG ((EFI_D_INFO
, "Standard capacity card.\n"));
531 if (RetryCount
== MAX_RETRY_COUNT
) {
532 DEBUG ((EFI_D_ERROR
, "Timeout error. RetryCount: %d\n", RetryCount
));
538 Status
= SendCmd (CMD2
, CMD2_INT_EN
, CmdArgument
);
539 if (EFI_ERROR(Status
)) {
540 DEBUG ((EFI_D_ERROR
, "CMD2 fails. Status: %x\n", Status
));
544 DEBUG ((EFI_D_INFO
, "CMD2 response: %x %x %x %x\n", MmioRead32 (MMCHS_RSP10
), MmioRead32 (MMCHS_RSP32
), MmioRead32 (MMCHS_RSP54
), MmioRead32 (MMCHS_RSP76
)));
546 //Parse CID register data.
547 ParseCardCIDData(MmioRead32 (MMCHS_RSP10
), MmioRead32 (MMCHS_RSP32
), MmioRead32 (MMCHS_RSP54
), MmioRead32 (MMCHS_RSP76
));
551 Status
= SendCmd (CMD3
, CMD3_INT_EN
, CmdArgument
);
552 if (EFI_ERROR(Status
)) {
553 DEBUG ((EFI_D_ERROR
, "CMD3 fails. Status: %x\n", Status
));
557 //Set RCA for the detected card. RCA is CMD3 response.
558 gCardInfo
.RCA
= (MmioRead32 (MMCHS_RSP10
) >> 16);
559 DEBUG ((EFI_D_INFO
, "CMD3 response: RCA %x\n", gCardInfo
.RCA
));
561 //MMC Bus setting change after card identification.
562 MmioAnd32 (MMCHS_CON
, ~OD
);
563 MmioOr32 (MMCHS_HCTL
, SDVS_3_0_V
);
564 UpdateMMCHSClkFrequency(CLKD_400KHZ
); //Set the clock frequency to 400KHz.
571 GetCardSpecificData (
578 //Send CMD9 to retrieve CSD.
579 CmdArgument
= gCardInfo
.RCA
<< 16;
580 Status
= SendCmd (CMD9
, CMD9_INT_EN
, CmdArgument
);
581 if (EFI_ERROR(Status
)) {
582 DEBUG ((EFI_D_ERROR
, "CMD9 fails. Status: %x\n", Status
));
586 //Populate 128-bit CSD register data.
587 ((UINT32
*)&(gCardInfo
.CSDData
))[0] = MmioRead32 (MMCHS_RSP10
);
588 ((UINT32
*)&(gCardInfo
.CSDData
))[1] = MmioRead32 (MMCHS_RSP32
);
589 ((UINT32
*)&(gCardInfo
.CSDData
))[2] = MmioRead32 (MMCHS_RSP54
);
590 ((UINT32
*)&(gCardInfo
.CSDData
))[3] = MmioRead32 (MMCHS_RSP76
);
592 DEBUG ((EFI_D_INFO
, "CMD9 response: %x %x %x %x\n", MmioRead32 (MMCHS_RSP10
), MmioRead32 (MMCHS_RSP32
), MmioRead32 (MMCHS_RSP54
), MmioRead32 (MMCHS_RSP76
)));
594 //Calculate total number of blocks and max. data transfer rate supported by the detected card.
595 GetCardConfigurationData();
602 PerformCardConfiguration (
606 UINTN CmdArgument
= 0;
610 CmdArgument
= gCardInfo
.RCA
<< 16;
611 Status
= SendCmd (CMD7
, CMD7_INT_EN
, CmdArgument
);
612 if (EFI_ERROR(Status
)) {
613 DEBUG ((EFI_D_ERROR
, "CMD7 fails. Status: %x\n", Status
));
617 if ((gCardInfo
.CardType
!= UNKNOWN_CARD
) && (gCardInfo
.CardType
!= MMC_CARD
)) {
618 // We could read SCR register, but SD Card Phys spec stats any SD Card shall
619 // set SCR.SD_BUS_WIDTHS to support 4-bit mode, so why bother?
621 // Send ACMD6 (application specific commands must be prefixed with CMD55)
622 Status
= SendCmd (CMD55
, CMD55_INT_EN
, CmdArgument
);
623 if (!EFI_ERROR (Status
)) {
624 // set device into 4-bit data bus mode
625 Status
= SendCmd (ACMD6
, ACMD6_INT_EN
, 0x2);
626 if (!EFI_ERROR (Status
)) {
627 // Set host controler into 4-bit mode
628 MmioOr32 (MMCHS_HCTL
, DTW_4_BIT
);
629 DEBUG ((EFI_D_INFO
, "SD Memory Card set to 4-bit mode\n"));
634 //Send CMD16 to set the block length
635 CmdArgument
= gCardInfo
.BlockSize
;
636 Status
= SendCmd (CMD16
, CMD16_INT_EN
, CmdArgument
);
637 if (EFI_ERROR(Status
)) {
638 DEBUG ((EFI_D_ERROR
, "CMD16 fails. Status: %x\n", Status
));
642 //Change MMCHS clock frequency to what detected card can support.
643 UpdateMMCHSClkFrequency(gCardInfo
.ClockFrequencySelect
);
651 IN EFI_BLOCK_IO_PROTOCOL
*This
,
656 UINTN
*DataBuffer
= Buffer
;
657 UINTN DataSize
= This
->Media
->BlockSize
/4;
659 UINTN RetryCount
= 0;
661 //Check controller status to make sure there is no error.
662 while (RetryCount
< MAX_RETRY_COUNT
) {
665 MmcStatus
= MmioRead32 (MMCHS_STAT
);
666 } while(MmcStatus
== 0);
668 //Check if Buffer read ready (BRR) bit is set?
669 if (MmcStatus
& BRR
) {
672 MmioOr32 (MMCHS_STAT
, BRR
);
674 //Read block worth of data.
675 for (Count
= 0; Count
< DataSize
; Count
++) {
676 *DataBuffer
++ = MmioRead32 (MMCHS_DATA
);
683 if (RetryCount
== MAX_RETRY_COUNT
) {
693 IN EFI_BLOCK_IO_PROTOCOL
*This
,
698 UINTN
*DataBuffer
= Buffer
;
699 UINTN DataSize
= This
->Media
->BlockSize
/4;
701 UINTN RetryCount
= 0;
703 //Check controller status to make sure there is no error.
704 while (RetryCount
< MAX_RETRY_COUNT
) {
707 MmcStatus
= MmioRead32 (MMCHS_STAT
);
708 } while(MmcStatus
== 0);
710 //Check if Buffer write ready (BWR) bit is set?
711 if (MmcStatus
& BWR
) {
714 MmioOr32 (MMCHS_STAT
, BWR
);
716 //Write block worth of data.
717 for (Count
= 0; Count
< DataSize
; Count
++) {
718 MmioWrite32 (MMCHS_DATA
, *DataBuffer
++);
726 if (RetryCount
== MAX_RETRY_COUNT
) {
735 IN EFI_BLOCK_IO_PROTOCOL
*This
,
739 IN OPERATION_TYPE OperationType
743 UINTN RetryCount
= 0;
745 UINTN CmdInterruptEnable
;
748 EFI_PHYSICAL_ADDRESS BufferAddress
;
750 DMA_MAP_OPERATION DmaOperation
;
753 //Populate the command information based on the operation type.
754 if (OperationType
== READ
) {
755 Cmd
= CMD18
; //Multiple block read
756 CmdInterruptEnable
= CMD18_INT_EN
;
757 DmaOperation
= MapOperationBusMasterCommonBuffer
;
758 } else if (OperationType
== WRITE
) {
759 Cmd
= CMD25
; //Multiple block write
760 CmdInterruptEnable
= CMD25_INT_EN
;
761 DmaOperation
= MapOperationBusMasterRead
;
763 return EFI_INVALID_PARAMETER
;
766 // Map passed in buffer for DMA xfer
767 RetryCount
= BlockCount
* This
->Media
->BlockSize
;
768 Status
= DmaMap (DmaOperation
, Buffer
, &RetryCount
, &BufferAddress
, &BufferMap
);
769 if (EFI_ERROR (Status
)) {
774 MmioWrite32 (DMA4_CSDP(0), DMA4_CSDP_DATA_TYPE32
| DMA4_CSDP_SRC_BURST_EN64
| DMA4_CSDP_WRITE_MODE_POSTED
);
775 MmioWrite32 (DMA4_CEN(0), 0x4096); // Channel Element number
776 MmioWrite32 (DMA4_CFN(0), 0x1); // Channel Frame number
778 MmioWrite32 (DMA4_CCR(0), X
| DMA4_CCR_FS_PACKET
| DMA4_CCR_RD_ACTIVE
| DMA4_CCR_WR_ACTIVE
| DMA4_CCR_DST_AMODE_POST_INC
| DMA4_CCR_SEL_SRC_DEST_SYNC_SOURCE
);
779 MmioWrite32 (DMA4_CSSA(0), MMCHS_DATA
); // Src is SD Card
780 MmioWrite32 (DMA4_CDSA(0), (UINT32
)BufferAddress
); // Dst memory
782 MmioWrite32 (DMA4_CCR(0), X
| DMA4_CCR_FS_PACKET
| DMA4_CCR_RD_ACTIVE
| DMA4_CCR_WR_ACTIVE
| DMA4_CCR_SRC_AMODE_POST_INC
);
783 MmioWrite32 (DMA4_CSSA(0), (UINT32
)BufferAddress
); // Src memory
784 MmioWrite32 (DMA4_CDSA(0), MMCHS_DATA
); // Dst SD Card
786 MmioWrite32 (DMA4_CSE(0), 1);
787 MmioWrite32 (DMA4_CSF(0), This
->Media
->BlockSize
);
788 MmioWrite32 (DMA4_CDE(0), 1);
790 Dma4
.DataType
= 0; // DMA4_CSDPi[1:0]
791 Dma4
.ReadPortAccessType
=0; // DMA4_CSDPi[8:7]
792 Dma4
.WritePortAccessType
=0; // DMA4_CSDPi[15:14]
793 Dma4
.SourceEndiansim
= 0; // DMA4_CSDPi[21]
794 Dma4
.DestinationEndianism
= 0; // DMA4_CSDPi[19]
795 Dma4
.WriteMode
= 0; // DMA4_CSDPi[17:16]
796 Dma4
.SourcePacked
= 0; // DMA4_CSDPi[6]
797 Dma4
.DestinationPacked
= 0; // DMA4_CSDPi[13]
798 Dma4
.NumberOfElementPerFrame
= 0; // DMA4_CENi
799 Dma4
.NumberOfFramePerTransferBlock
= 0; // DMA4_CFNi
800 Dma4
.SourceStartAddress
= 0; // DMA4_CSSAi
801 Dma4
.DestinationStartAddress
= 0; // DMA4_CDSAi
802 Dma4
.SourceElementIndex
= 0; // DMA4_CSEi
803 Dma4
.SourceFrameIndex
= 0; // DMA4_CSFi
804 Dma4
.DestinationElementIndex
= 0; // DMA4_CDEi
805 Dma4
.DestinationFrameIndex
= 0; // DMA4_CDFi
806 Dma4
.ReadPortAccessMode
= 0; // DMA4_CCRi[13:12]
807 Dma4
.WritePortAccessMode
= 0; // DMA4_CCRi[15:14]
808 Dma4
.ReadPriority
= 0; // DMA4_CCRi[6]
809 Dma4
.WritePriority
= 0; // DMA4_CCRi[23]
810 Dma4
.ReadRequestNumber
= 0; // DMA4_CCRi[4:0]
811 Dma4
.WriteRequestNumber
= 0; // DMA4_CCRi[20:19]
813 EnableDmaChannel (2, &Dma4
);
816 //Set command argument based on the card access mode (Byte mode or Block mode)
817 if (gCardInfo
.OCRData
.AccessMode
& BIT1
) {
820 CmdArgument
= Lba
* This
->Media
->BlockSize
;
824 Status
= SendCmd (Cmd
, CmdInterruptEnable
, CmdArgument
);
825 if (EFI_ERROR (Status
)) {
826 DEBUG ((EFI_D_ERROR
, "CMD fails. Status: %x\n", Status
));
830 DisableDmaChannel (2);
831 Status
= DmaUnmap (BufferMap
);
839 IN EFI_BLOCK_IO_PROTOCOL
*This
,
842 IN OPERATION_TYPE OperationType
847 UINTN RetryCount
= 0;
849 UINTN CmdInterruptEnable
= 0;
850 UINTN CmdArgument
= 0;
853 //Populate the command information based on the operation type.
854 if (OperationType
== READ
) {
855 Cmd
= CMD17
; //Single block read
856 CmdInterruptEnable
= CMD18_INT_EN
;
857 } else if (OperationType
== WRITE
) {
858 Cmd
= CMD24
; //Single block write
859 CmdInterruptEnable
= CMD24_INT_EN
;
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 //Read or Write data.
877 if (OperationType
== READ
) {
878 Status
= ReadBlockData (This
, Buffer
);
879 if (EFI_ERROR(Status
)) {
880 DEBUG((EFI_D_ERROR
, "ReadBlockData fails.\n"));
883 } else if (OperationType
== WRITE
) {
884 Status
= WriteBlockData (This
, Buffer
);
885 if (EFI_ERROR(Status
)) {
886 DEBUG((EFI_D_ERROR
, "WriteBlockData fails.\n"));
891 //Check for the Transfer completion.
892 while (RetryCount
< MAX_RETRY_COUNT
) {
895 MmcStatus
= MmioRead32 (MMCHS_STAT
);
896 } while (MmcStatus
== 0);
898 //Check if Transfer complete (TC) bit is set?
899 if (MmcStatus
& TC
) {
902 DEBUG ((EFI_D_ERROR
, "MmcStatus for TC: %x\n", MmcStatus
));
903 //Check if DEB, DCRC or DTO interrupt occured.
904 if ((MmcStatus
& DEB
) | (MmcStatus
& DCRC
) | (MmcStatus
& DTO
)) {
905 //There was an error during the data transfer.
907 //Set SRD bit to 1 and wait until it return to 0x0.
908 MmioOr32 (MMCHS_SYSCTL
, SRD
);
909 while((MmioRead32 (MMCHS_SYSCTL
) & SRD
) != 0x0);
911 return EFI_DEVICE_ERROR
;
917 if (RetryCount
== MAX_RETRY_COUNT
) {
918 DEBUG ((EFI_D_ERROR
, "TransferBlockData timed out.\n"));
934 // Card detect is a GPIO0 on the TPS65950
936 Status
= gTPS65950
->Read (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID2
, GPIODATAIN1
), 1, &Data
);
937 if (EFI_ERROR (Status
)) {
941 if ((Data
& CARD_DETECT_BIT
) == CARD_DETECT_BIT
) {
956 if (!CardPresent ()) {
960 //Initialize MMC host controller clocks.
961 Status
= InitializeMMCHS ();
962 if (EFI_ERROR(Status
)) {
963 DEBUG ((EFI_D_ERROR
, "Initialize MMC host controller fails. Status: %x\n", Status
));
967 //Software reset of the MMCHS host controller.
968 MmioWrite32 (MMCHS_SYSCONFIG
, SOFTRESET
);
970 while ((MmioRead32 (MMCHS_SYSSTATUS
) & RESETDONE_MASK
) != RESETDONE
);
972 //Soft reset for all.
973 MmioWrite32 (MMCHS_SYSCTL
, SRA
);
975 while ((MmioRead32 (MMCHS_SYSCTL
) & SRA
) != 0x0);
977 //Voltage capabilities initialization. Activate VS18 and VS30.
978 MmioOr32 (MMCHS_CAPA
, (VS30
| VS18
));
980 //Wakeup configuration
981 MmioOr32 (MMCHS_SYSCONFIG
, ENAWAKEUP
);
982 MmioOr32 (MMCHS_HCTL
, IWE
);
984 //MMCHS Controller default initialization
985 MmioOr32 (MMCHS_CON
, (OD
| DW8_1_4_BIT
| CEATA_OFF
));
987 MmioWrite32 (MMCHS_HCTL
, (SDVS_3_0_V
| DTW_1_BIT
| SDBP_OFF
));
989 //Enable internal clock
990 MmioOr32 (MMCHS_SYSCTL
, ICE
);
992 //Set the clock frequency to 80KHz.
993 UpdateMMCHSClkFrequency (CLKD_80KHZ
);
995 //Enable SD bus power.
996 MmioOr32 (MMCHS_HCTL
, (SDBP_ON
));
998 //Poll till SD bus power bit is set.
999 while ((MmioRead32 (MMCHS_HCTL
) & SDBP_MASK
) != SDBP_ON
);
1002 Status
= PerformCardIdenfication ();
1003 if (EFI_ERROR(Status
)) {
1004 DEBUG ((EFI_D_ERROR
, "No MMC/SD card detected.\n"));
1008 //Get CSD (Card specific data) for the detected card.
1009 Status
= GetCardSpecificData();
1010 if (EFI_ERROR(Status
)) {
1014 //Configure the card in data transfer mode.
1015 Status
= PerformCardConfiguration();
1016 if (EFI_ERROR(Status
)) {
1020 //Patch the Media structure.
1021 gMMCHSMedia
.LastBlock
= (gCardInfo
.NumBlocks
- 1);
1022 gMMCHSMedia
.BlockSize
= gCardInfo
.BlockSize
;
1023 gMMCHSMedia
.ReadOnly
= (MmioRead32 (GPIO1_BASE
+ GPIO_DATAIN
) & BIT23
) == BIT23
;
1024 gMMCHSMedia
.MediaPresent
= TRUE
;
1025 gMMCHSMedia
.MediaId
++;
1027 DEBUG ((EFI_D_INFO
, "SD Card Media Change on Handle 0x%08x\n", gImageHandle
));
1035 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1038 IN UINTN BufferSize
,
1039 IN OPERATION_TYPE OperationType
1042 EFI_STATUS Status
= EFI_SUCCESS
;
1043 UINTN RetryCount
= 0;
1045 UINTN BytesToBeTranferedThisPass
;
1046 UINTN BytesRemainingToBeTransfered
;
1055 Status
= DetectCard ();
1056 if (EFI_ERROR (Status
)) {
1057 // We detected a removal
1058 gMMCHSMedia
.MediaPresent
= FALSE
;
1059 gMMCHSMedia
.LastBlock
= 0;
1060 gMMCHSMedia
.BlockSize
= 512; // Should be zero but there is a bug in DiskIo
1061 gMMCHSMedia
.ReadOnly
= FALSE
;
1063 gMediaChange
= FALSE
;
1064 } else if (!gMMCHSMedia
.MediaPresent
) {
1065 Status
= EFI_NO_MEDIA
;
1070 DEBUG ((EFI_D_INFO
, "SD Card ReinstallProtocolInterface ()\n"));
1071 gBS
->ReinstallProtocolInterface (
1073 &gEfiBlockIoProtocolGuid
,
1079 if (EFI_ERROR (Status
)) {
1083 if (Buffer
== NULL
) {
1084 Status
= EFI_INVALID_PARAMETER
;
1088 if (Lba
> This
->Media
->LastBlock
) {
1089 Status
= EFI_INVALID_PARAMETER
;
1093 if ((BufferSize
% This
->Media
->BlockSize
) != 0) {
1094 Status
= EFI_BAD_BUFFER_SIZE
;
1098 //Check if the data lines are not in use.
1099 while ((RetryCount
++ < MAX_RETRY_COUNT
) && ((MmioRead32 (MMCHS_PSTATE
) & DATI_MASK
) != DATI_ALLOWED
));
1100 if (RetryCount
== MAX_RETRY_COUNT
) {
1101 Status
= EFI_TIMEOUT
;
1105 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1107 BytesRemainingToBeTransfered
= BufferSize
;
1108 while (BytesRemainingToBeTransfered
> 0) {
1111 Status
= EFI_NO_MEDIA
;
1112 DEBUG ((EFI_D_INFO
, "SdReadWrite() EFI_NO_MEDIA due to gMediaChange\n"));
1113 goto DoneRestoreTPL
;
1116 //BytesToBeTranferedThisPass = (BytesToBeTranferedThisPass >= MAX_MMCHS_TRANSFER_SIZE) ? MAX_MMCHS_TRANSFER_SIZE : BytesRemainingToBeTransfered;
1117 BytesToBeTranferedThisPass
= This
->Media
->BlockSize
;
1119 BlockCount
= BytesToBeTranferedThisPass
/This
->Media
->BlockSize
;
1121 if (BlockCount
> 1) {
1122 Status
= DmaBlocks (This
, Lba
, Buffer
, BlockCount
, OperationType
);
1124 //Transfer a block worth of data.
1125 Status
= TransferBlock (This
, Lba
, Buffer
, OperationType
);
1128 if (EFI_ERROR(Status
)) {
1129 DEBUG ((EFI_D_ERROR
, "TransferBlockData fails. %x\n", Status
));
1130 goto DoneRestoreTPL
;
1133 BytesRemainingToBeTransfered
-= BytesToBeTranferedThisPass
;
1135 Buffer
= (UINT8
*)Buffer
+ This
->Media
->BlockSize
;
1139 gBS
->RestoreTPL (OldTpl
);
1146 Reset the Block Device.
1148 @param This Indicates a pointer to the calling context.
1149 @param ExtendedVerification Driver may perform diagnostics on reset.
1151 @retval EFI_SUCCESS The device was reset.
1152 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
1159 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1160 IN BOOLEAN ExtendedVerification
1168 Read BufferSize bytes from Lba into Buffer.
1170 @param This Indicates a pointer to the calling context.
1171 @param MediaId Id of the media, changes every time the media is replaced.
1172 @param Lba The starting Logical Block Address to read from
1173 @param BufferSize Size of Buffer, must be a multiple of device block size.
1174 @param Buffer A pointer to the destination buffer for the data. The caller is
1175 responsible for either having implicit or explicit ownership of the buffer.
1177 @retval EFI_SUCCESS The data was read correctly from the device.
1178 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
1179 @retval EFI_NO_MEDIA There is no media in the device.
1180 @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.
1181 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
1182 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
1183 or the buffer is not on proper alignment.
1190 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1193 IN UINTN BufferSize
,
1199 //Perform Read operation.
1200 Status
= SdReadWrite (This
, (UINTN
)Lba
, Buffer
, BufferSize
, READ
);
1207 Write BufferSize bytes from Lba into Buffer.
1209 @param This Indicates a pointer to the calling context.
1210 @param MediaId The media ID that the write request is for.
1211 @param Lba The starting logical block address to be written. The caller is
1212 responsible for writing to only legitimate locations.
1213 @param BufferSize Size of Buffer, must be a multiple of device block size.
1214 @param Buffer A pointer to the source buffer for the data.
1216 @retval EFI_SUCCESS The data was written correctly to the device.
1217 @retval EFI_WRITE_PROTECTED The device can not be written to.
1218 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
1219 @retval EFI_NO_MEDIA There is no media in the device.
1220 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
1221 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
1222 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
1223 or the buffer is not on proper alignment.
1229 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1232 IN UINTN BufferSize
,
1238 //Perform write operation.
1239 Status
= SdReadWrite (This
, (UINTN
)Lba
, Buffer
, BufferSize
, WRITE
);
1246 Flush the Block Device.
1248 @param This Indicates a pointer to the calling context.
1250 @retval EFI_SUCCESS All outstanding data was written to the device
1251 @retval EFI_DEVICE_ERROR The device reported an error while writting back the data
1252 @retval EFI_NO_MEDIA There is no media in the device.
1258 IN EFI_BLOCK_IO_PROTOCOL
*This
1265 EFI_BLOCK_IO_PROTOCOL gBlockIo
= {
1266 EFI_BLOCK_IO_INTERFACE_REVISION
, // Revision
1267 &gMMCHSMedia
, // *Media
1268 MMCHSReset
, // Reset
1269 MMCHSReadBlocks
, // ReadBlocks
1270 MMCHSWriteBlocks
, // WriteBlocks
1271 MMCHSFlushBlocks
// FlushBlocks
1276 Timer callback to convert card present hardware into a boolean that indicates
1277 a media change event has happened. If you just check the GPIO you could see
1278 card 1 and then check again after card 1 was removed and card 2 was inserted
1279 and you would still see media present. Thus you need the timer tick to catch
1282 @param Event Event whose notification function is being invoked.
1283 @param Context The pointer to the notification function's context,
1284 which is implementation-dependent. Not used.
1296 Present
= CardPresent ();
1297 if (gMMCHSMedia
.MediaPresent
) {
1298 if (!Present
&& !gMediaChange
) {
1299 gMediaChange
= TRUE
;
1302 if (Present
&& !gMediaChange
) {
1303 gMediaChange
= TRUE
;
1312 IN EFI_HANDLE ImageHandle
,
1313 IN EFI_SYSTEM_TABLE
*SystemTable
1318 Status
= gBS
->LocateProtocol (&gEmbeddedExternalDeviceProtocolGuid
, NULL
, (VOID
**)&gTPS65950
);
1319 ASSERT_EFI_ERROR(Status
);
1321 ZeroMem (&gCardInfo
, sizeof (CARD_INFO
));
1323 Status
= gBS
->CreateEvent (EVT_TIMER
| EVT_NOTIFY_SIGNAL
, TPL_CALLBACK
, TimerCallback
, NULL
, &gTimerEvent
);
1324 ASSERT_EFI_ERROR (Status
);
1326 Status
= gBS
->SetTimer (gTimerEvent
, TimerPeriodic
, 1000000ULL); // make me a PCD
1327 ASSERT_EFI_ERROR (Status
);
1330 Status
= gBS
->InstallMultipleProtocolInterfaces (
1332 &gEfiBlockIoProtocolGuid
, &gBlockIo
,
1333 &gEfiDevicePathProtocolGuid
, &gMmcHsDevicePath
,