3 The SD host controller driver model and HC protocol routines.
5 Copyright (c) 2013-2016 Intel Corporation.
7 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include "SDController.h"
16 EFI_DRIVER_BINDING_PROTOCOL gSDControllerDriverBinding
= {
17 SDControllerSupported
,
26 EFI_SD_HOST_IO_PROTOCOL mSDHostIo
= {
27 EFI_SD_HOST_IO_PROTOCOL_REVISION_01
,
29 0, // HighSpeedSupport
38 (512 * 1024) //BoundarySize
46 DetectCardAndInitHost
,
53 Find sdclk_freq_sel and upr_sdclk_freq_sel bits
54 for Clock Control Register (CLK_CTL)Offset 2Ch when using 8bit or 10bit
57 @param BaseClockFreg Base Clock Frequency in Hz For SD Clock in the
58 Capabilities register.
59 @param TargetFreq Target Frequency in Hz to reach.
60 @param Is8BitMode True if 8-bit Divided Clock Mode else 10bit mode.
61 @param Bits sdclk_freq_sel and upr_sdclk_freq_sel bits for
64 @return EFI_SUCCESS // Bits setup.
65 @return EFI_UNSUPPORTED // Cannot divide base clock to reach target clock.
68 DividedClockModeBits (
69 IN CONST UINTN BaseClockFreg
,
70 IN CONST UINTN TargetFreq
,
71 IN CONST BOOLEAN Is8BitMode
,
79 CurrFreq
= BaseClockFreg
;
82 // N == 0 same for 8bit & 10bit mode i.e. BaseClockFreg of controller.
84 if (TargetFreq
< CurrFreq
) {
89 // N values for 8bit mode when N > 0.
90 // Bit[15:8] SDCLK Frequency Select at offset 2Ch
91 // 80h - base clock divided by 256
92 // 40h - base clock divided by 128
93 // 20h - base clock divided by 64
94 // 10h - base clock divided by 32
95 // 08h - base clock divided by 16
96 // 04h - base clock divided by 8
97 // 02h - base clock divided by 4
98 // 01h - base clock divided by 2
100 CurrFreq
= BaseClockFreg
/ (2 * N
);
101 if (TargetFreq
>= CurrFreq
) {
105 if (N
> V_MMIO_CLKCTL_MAX_8BIT_FREQ_SEL
) {
106 return EFI_UNSUPPORTED
;
111 CurrFreq
= BaseClockFreg
/ (2 * N
);
113 // (try N = 0 or 1 first since don't want divide by 0).
115 if (TargetFreq
< CurrFreq
) {
117 // If still no match then calculate it for 10bit.
118 // N values for 10bit mode.
119 // N 1/2N Divided Clock (Duty 50%).
120 // from Spec "The length of divider is extended to 10 bits and all
121 // divider values shall be supported.
123 N
= (BaseClockFreg
/ TargetFreq
) / 2;
126 // Can only be N or N+1;
128 CurrFreq
= BaseClockFreg
/ (2 * N
);
129 if (TargetFreq
< CurrFreq
) {
131 CurrFreq
= BaseClockFreg
/ (2 * N
);
134 if (N
> V_MMIO_CLKCTL_MAX_10BIT_FREQ_SEL
) {
135 return EFI_UNSUPPORTED
;
139 // Set upper bits of SDCLK Frequency Select (bits 7:6 of reg 0x2c).
141 *Bits
|= ((UINT16
) ((N
>> 2) & B_MMIO_CLKCTL_UPR_SDCLK_FREQ_SEL_MASK
));
147 // Set lower bits of SDCLK Frequency Select (bits 15:8 of reg 0x2c).
149 *Bits
|= ((UINT16
) ((UINT8
) N
) << 8);
152 "SDIO:DividedClockModeBits: %dbit mode Want %dHz Got %dHz bits = %04x\r\n",
153 (Is8BitMode
) ? 8 : 10,
163 Print type of error and command index
165 @param CommandIndex Command index to set the command index field of command register.
166 @param ErrorCode Error interrupt status read from host controller
168 @return EFI_DEVICE_ERROR
170 @return EFI_CRC_ERROR
175 IN UINT16 CommandIndex
,
181 Status
= EFI_DEVICE_ERROR
;
183 DEBUG((EFI_D_ERROR
, "[%2d] -- ", CommandIndex
));
185 if (ErrorCode
& BIT0
) {
186 Status
= EFI_TIMEOUT
;
187 DEBUG((EFI_D_ERROR
, "Command Timeout Erro"));
190 if (ErrorCode
& BIT1
) {
191 Status
= EFI_CRC_ERROR
;
192 DEBUG((EFI_D_ERROR
, "Command CRC Error"));
195 if (ErrorCode
& BIT2
) {
196 DEBUG((EFI_D_ERROR
, "Command End Bit Error"));
199 if (ErrorCode
& BIT3
) {
200 DEBUG((EFI_D_ERROR
, "Command Index Error"));
202 if (ErrorCode
& BIT4
) {
203 Status
= EFI_TIMEOUT
;
204 DEBUG((EFI_D_ERROR
, "Data Timeout Error"));
207 if (ErrorCode
& BIT5
) {
208 Status
= EFI_CRC_ERROR
;
209 DEBUG((EFI_D_ERROR
, "Data CRC Error"));
212 if (ErrorCode
& BIT6
) {
213 DEBUG((EFI_D_ERROR
, "Data End Bit Error"));
216 if (ErrorCode
& BIT7
) {
217 DEBUG((EFI_D_ERROR
, "Current Limit Error"));
220 if (ErrorCode
& BIT8
) {
221 DEBUG((EFI_D_ERROR
, "Auto CMD12 Error"));
224 if (ErrorCode
& BIT9
) {
225 DEBUG((EFI_D_ERROR
, "ADMA Error"));
228 DEBUG((EFI_D_ERROR
, "\n"));
233 Enable/Disable High Speed transfer mode
235 @param This A pointer to the EFI_SD_HOST_IO_PROTOCOL instance.
236 @param Enable TRUE to Enable, FALSE to Disable
243 IN EFI_SD_HOST_IO_PROTOCOL
*This
,
248 SDHOST_DATA
*SDHostData
;
249 EFI_PCI_IO_PROTOCOL
*PciIo
;
251 SDHostData
= SDHOST_DATA_FROM_THIS (This
);
252 PciIo
= SDHostData
->PciIo
;
258 (UINT64
)MMIO_HOSTCTL
,
264 if (PcdGetBool(PcdSdHciQuirkNoHiSpd
)) {
265 DEBUG ((EFI_D_INFO
, "SDIO: Quirk never set High Speed Enable bit\r\n"));
268 DEBUG ((EFI_D_INFO
, "Enable High Speed transfer mode ... \r\n"));
277 (UINT64
)MMIO_HOSTCTL
,
286 IN EFI_SD_HOST_IO_PROTOCOL
*This
,
291 SDHOST_DATA
*SDHostData
;
292 EFI_PCI_IO_PROTOCOL
*PciIo
;
293 SDHostData
= SDHOST_DATA_FROM_THIS (This
);
294 PciIo
= SDHostData
->PciIo
;
299 (UINT64
)MMIO_HOSTCTL2
,
305 Data
|= 0x0004; // Enable DDR50 by default, later should enable other mode like HS200/400
306 Data
|= BIT3
; // Enable 1.8V Signaling
312 (UINT64
)MMIO_HOSTCTL2
,
319 Power on/off the LED associated with the slot
321 @param This A pointer to the EFI_SD_HOST_IO_PROTOCOL instance.
322 @param Enable TRUE to set LED on, FALSE to set LED off
328 IN EFI_SD_HOST_IO_PROTOCOL
*This
,
332 SDHOST_DATA
*SDHostData
;
333 EFI_PCI_IO_PROTOCOL
*PciIo
;
336 SDHostData
= SDHOST_DATA_FROM_THIS (This
);
337 PciIo
= SDHostData
->PciIo
;
343 (UINT64
)MMIO_HOSTCTL
,
364 (UINT64
)MMIO_HOSTCTL
,
374 The main function used to send the command to the card inserted into the SD host slot.
375 It will assemble the arguments to set the command register and wait for the command
376 and transfer completed until timeout. Then it will read the response register to fill
379 @param This A pointer to the EFI_SD_HOST_IO_PROTOCOL instance.
380 @param CommandIndex The command index to set the command index field of command register.
381 @param Argument Command argument to set the argument field of command register.
382 @param DataType TRANSFER_TYPE, indicates no data, data in or data out.
383 @param Buffer Contains the data read from / write to the device.
384 @param BufferSize The size of the buffer.
385 @param ResponseType RESPONSE_TYPE.
386 @param TimeOut Time out value in 1 ms unit.
387 @param ResponseData Depending on the ResponseType, such as CSD or card status.
390 @retval EFI_INVALID_PARAMETER
391 @retval EFI_OUT_OF_RESOURCES
393 @retval EFI_DEVICE_ERROR
400 IN EFI_SD_HOST_IO_PROTOCOL
*This
,
401 IN UINT16 CommandIndex
,
403 IN TRANSFER_TYPE DataType
,
404 IN UINT8
*Buffer
, OPTIONAL
405 IN UINT32 BufferSize
,
406 IN RESPONSE_TYPE ResponseType
,
408 OUT UINT32
*ResponseData OPTIONAL
413 The main function used to send the command to the card inserted into the SD host
415 It will assemble the arguments to set the command register and wait for the command
416 and transfer completed until timeout. Then it will read the response register to fill
420 This - Pointer to EFI_SD_HOST_IO_PROTOCOL
421 CommandIndex - The command index to set the command index field of command register
422 Argument - Command argument to set the argument field of command register
423 DataType - TRANSFER_TYPE, indicates no data, data in or data out
424 Buffer - Contains the data read from / write to the device
425 BufferSize - The size of the buffer
426 ResponseType - RESPONSE_TYPE
427 TimeOut - Time out value in 1 ms unit
428 ResponseData - Depending on the ResponseType, such as CSD or card status
432 EFI_INVALID_PARAMETER
440 SDHOST_DATA
*SDHostData
;
441 EFI_PCI_IO_PROTOCOL
*PciIo
;
442 UINT32 ResponseDataCount
;
447 BOOLEAN AutoCMD12Enable
= FALSE
;
450 Status
= EFI_SUCCESS
;
451 ResponseDataCount
= 1;
452 SDHostData
= SDHOST_DATA_FROM_THIS (This
);
453 PciIo
= SDHostData
->PciIo
;
454 AutoCMD12Enable
= (CommandIndex
& AUTO_CMD12_ENABLE
) ? TRUE
: FALSE
;
455 CommandIndex
= CommandIndex
& CMD_INDEX_MASK
;
457 if (Buffer
!= NULL
&& DataType
== NoData
) {
458 Status
= EFI_INVALID_PARAMETER
;
459 DEBUG ((EFI_D_ERROR
, "SendCommand: invalid parameter \r\n"));
463 if (((UINTN
)Buffer
& (This
->HostCapability
.BoundarySize
- 1)) != (UINTN
)NULL
) {
464 Status
= EFI_INVALID_PARAMETER
;
465 DEBUG ((EFI_D_ERROR
, "SendCommand: invalid parameter \r\n"));
469 DEBUG ((EFI_D_INFO
, "SendCommand: Command Index = %d \r\n", CommandIndex
));
471 TimeOut2
= 1000; // 10 ms
482 }while ((TimeOut2
-- > 0) && (Data
& BIT0
));
483 TimeOut2
= 1000; // 10 ms
494 }while ((TimeOut2
-- > 0) && (Data
& BIT1
));
502 (UINT64
)MMIO_NINTSTS
,
512 (UINT64
)MMIO_ERINTSTS
,
518 if (Buffer
!= NULL
) {
537 if (BufferSize
<= SDHostData
->BlockLength
) {
538 Data
|= (BufferSize
| 0x7000);
540 Data
|= (SDHostData
->BlockLength
| 0x7000);
552 if (BufferSize
<= SDHostData
->BlockLength
) {
555 Data
= BufferSize
/ SDHostData
->BlockLength
;
604 (UINT64
)MMIO_XFRMODE
,
610 DEBUG ((EFI_D_INFO
, "Transfer mode read = 0x%x \r\n", (Data
& 0xFFFF)));
615 if (DataType
== InData
) {
617 } else if (DataType
== OutData
){
621 Data
&= ~(BIT4
| BIT0
);
624 if (BufferSize
<= SDHostData
->BlockLength
) {
625 Data
&= ~ (BIT5
| BIT1
| BIT2
);
626 Data
|= BIT1
; // Enable block count always
628 if (SDHostData
->IsAutoStopCmd
&& AutoCMD12Enable
) {
629 Data
|= (BIT5
| BIT1
| BIT2
);
631 Data
|= (BIT5
| BIT1
);
635 DEBUG ((EFI_D_INFO
, "Transfer mode write = 0x%x \r\n", (Data
& 0xffff)));
640 (UINT64
)MMIO_XFRMODE
,
647 //ResponseTypeSelect IndexCheck CRCCheck ResponseType
651 // 10 1 1 R1, R5, R6, R7
654 switch (ResponseType
) {
656 Data
= (CommandIndex
<< 8);
657 ResponseDataCount
= 0;
664 Data
= (CommandIndex
<< 8) | BIT1
| BIT4
| BIT3
;
665 ResponseDataCount
= 1;
670 Data
= (CommandIndex
<< 8) | BIT0
| BIT1
| BIT4
| BIT3
;
671 ResponseDataCount
= 1;
675 Data
= (CommandIndex
<< 8) | BIT0
| BIT3
;
676 ResponseDataCount
= 4;
681 Data
= (CommandIndex
<< 8) | BIT1
;
682 ResponseDataCount
= 1;
687 Status
= EFI_INVALID_PARAMETER
;
688 DEBUG ((EFI_D_ERROR
, "SendCommand: invalid parameter \r\n"));
692 if (DataType
!= NoData
) {
696 HostLEDEnable (This
, TRUE
);
715 (UINT64
)MMIO_ERINTSTS
,
720 if ((Data
& 0x07FF) != 0) {
721 Status
= GetErrorReason (CommandIndex
, (UINT16
)Data
);
722 DEBUG ((EFI_D_ERROR
, "SendCommand: Error happens \r\n"));
730 (UINT64
)MMIO_NINTSTS
,
735 if ((Data
& BIT0
) == BIT0
) {
737 //Command completed, can read response
739 if (DataType
== NoData
) {
745 if ((Data
& BIT1
) == BIT1
) {
751 gBS
->Stall (1 * 1000);
755 } while (TimeOut
> 0);
758 Status
= EFI_TIMEOUT
;
759 DEBUG ((EFI_D_ERROR
, "SendCommand: Time out \r\n"));
763 if (ResponseData
!= NULL
) {
772 if (ResponseType
== ResponseR2
) {
774 // Adjustment for R2 response
777 for (Index
= 0; Index
< ResponseDataCount
; Index
++) {
778 Data64
= LShiftU64(*ResponseData
, 8);
779 *ResponseData
= (UINT32
)((Data64
& 0xFFFFFFFF) | Data
);
780 Data
= (UINT32
)RShiftU64 (Data64
, 32);
787 HostLEDEnable (This
, FALSE
);
792 Set max clock frequency of the host, the actual frequency may not be the same as MaxFrequency.
793 It depends on the max frequency the host can support, divider, and host speed mode.
795 @param This A pointer to the EFI_SD_HOST_IO_PROTOCOL instance.
796 @param MaxFrequency Max frequency in HZ.
805 IN EFI_SD_HOST_IO_PROTOCOL
*This
,
806 IN UINT32 MaxFrequency
812 SDHOST_DATA
*SDHostData
;
813 EFI_PCI_IO_PROTOCOL
*PciIo
;
817 SDHostData
= SDHOST_DATA_FROM_THIS (This
);
818 PciIo
= SDHostData
->PciIo
;
833 (UINT64
)MMIO_CTRLRVER
,
837 Revision
&= 0x000000FF;
839 Status
= DividedClockModeBits (
840 SDHostData
->BaseClockInMHz
* 1000 * 1000,
842 (Revision
< SDHCI_SPEC_300
),
846 if (EFI_ERROR (Status
)) {
848 // Cannot reach MaxFrequency with SDHostData->BaseClockInMHz.
850 ASSERT_EFI_ERROR (Status
);
857 //Enable internal clock and Stop Clock Enable
869 TimeOutCount
= TIME_OUT_1S
;
879 gBS
->Stall (1 * 1000);
881 if (TimeOutCount
== 0) {
882 DEBUG ((EFI_D_ERROR
, "SetClockFrequency: Time out \r\n"));
885 } while ((Data
& BIT1
) != BIT1
);
887 DEBUG ((EFI_D_INFO
, "Base Clock In MHz: %d\r\n", SDHostData
->BaseClockInMHz
));
889 Data
= (BIT0
| ((UINT32
) FreqSelBits
));
890 DEBUG ((EFI_D_INFO
, "Data write to MMIO_CLKCTL: 0x%04x \r\n", Data
));
900 TimeOutCount
= TIME_OUT_1S
;
910 gBS
->Stall (1 * 1000);
912 if (TimeOutCount
== 0) {
913 DEBUG ((EFI_D_ERROR
, "SetClockFrequency: Time out \r\n"));
916 } while ((Data
& BIT1
) != BIT1
);
917 gBS
->Stall (20 * 1000);
932 Set bus width of the host controller
934 @param This A pointer to the EFI_SD_HOST_IO_PROTOCOL instance.
935 @param BusWidth Bus width in 1, 4, 8 bits.
938 @retval EFI_INVALID_PARAMETER
944 IN EFI_SD_HOST_IO_PROTOCOL
*This
,
948 SDHOST_DATA
*SDHostData
;
949 EFI_PCI_IO_PROTOCOL
*PciIo
;
952 SDHostData
= SDHOST_DATA_FROM_THIS (This
);
955 if ((BusWidth
!= 1) && (BusWidth
!= 4) && (BusWidth
!= 8)) {
956 DEBUG ((EFI_D_ERROR
, "SetBusWidth: Invalid parameter \r\n"));
957 return EFI_INVALID_PARAMETER
;
960 if ((SDHostData
->SDHostIo
.HostCapability
.BusWidth8
== FALSE
) && (BusWidth
== 8)) {
961 DEBUG ((EFI_D_ERROR
, "SetBusWidth: Invalid parameter \r\n"));
962 return EFI_INVALID_PARAMETER
;
965 PciIo
= SDHostData
->PciIo
;
971 (UINT64
)MMIO_HOSTCTL
,
976 // BIT5 8-bit MMC Support (MMC8):
977 // If set, IOH supports 8-bit MMC. When cleared, IOH does not support this feature
980 DEBUG ((EFI_D_INFO
, "Bus Width is 8-bit ... \r\n"));
982 } else if (BusWidth
== 4) {
983 DEBUG ((EFI_D_INFO
, "Bus Width is 4-bit ... \r\n"));
987 DEBUG ((EFI_D_INFO
, "Bus Width is 1-bit ... \r\n"));
996 (UINT64
)MMIO_HOSTCTL
,
1006 Set voltage which could supported by the host controller.
1007 Support 0(Power off the host), 1.8V, 3.0V, 3.3V
1009 @param This A pointer to the EFI_SD_HOST_IO_PROTOCOL instance.
1010 @param Voltage Units in 0.1 V.
1013 @retval EFI_INVALID_PARAMETER
1019 IN EFI_SD_HOST_IO_PROTOCOL
*This
,
1023 SDHOST_DATA
*SDHostData
;
1024 EFI_PCI_IO_PROTOCOL
*PciIo
;
1028 SDHostData
= SDHOST_DATA_FROM_THIS (This
);
1029 PciIo
= SDHostData
->PciIo
;
1030 Status
= EFI_SUCCESS
;
1036 (UINT64
)MMIO_PWRCTL
,
1043 //Power Off the host
1046 } else if (Voltage
<= 18 && This
->HostCapability
.V18Support
) {
1050 Data
|= (BIT1
| BIT3
| BIT0
);
1051 } else if (Voltage
> 18 && Voltage
<= 30 && This
->HostCapability
.V30Support
) {
1055 Data
|= (BIT2
| BIT3
| BIT0
);
1056 } else if (Voltage
> 30 && Voltage
<= 33 && This
->HostCapability
.V33Support
) {
1060 Data
|= (BIT1
| BIT2
| BIT3
| BIT0
);
1062 Status
= EFI_UNSUPPORTED
;
1070 (UINT64
)MMIO_PWRCTL
,
1074 gBS
->Stall (10 * 1000);
1083 Reset the host controller.
1085 @param This A pointer to the EFI_SD_HOST_IO_PROTOCOL instance.
1086 @param ResetAll TRUE to reset all.
1095 IN EFI_SD_HOST_IO_PROTOCOL
*This
,
1096 IN RESET_TYPE ResetType
1099 SDHOST_DATA
*SDHostData
;
1100 EFI_PCI_IO_PROTOCOL
*PciIo
;
1104 UINT32 TimeOutCount
;
1108 SDHostData
= SDHOST_DATA_FROM_THIS (This
);
1109 PciIo
= SDHostData
->PciIo
;
1113 if (ResetType
== Reset_Auto
) {
1116 EfiPciIoWidthUint16
,
1118 (UINT64
)MMIO_ERINTSTS
,
1122 if ((ErrStatus
& 0xF) != 0) {
1128 if ((ErrStatus
& 0x70) != 0) {
1137 if (ResetType
== Reset_DAT
|| ResetType
== Reset_DAT_CMD
) {
1140 if (ResetType
== Reset_CMD
|| ResetType
== Reset_DAT_CMD
) {
1143 if (ResetType
== Reset_All
) {
1152 // To improve SD stability, we zero the MMIO_CLKCTL register and
1153 // stall for 50 microseconds before resetting the controller. We
1154 // restore the register setting following the reset operation.
1158 EfiPciIoWidthUint16
,
1160 (UINT64
)MMIO_CLKCTL
,
1165 ZeroClkCtl
= (UINT16
) 0;
1168 EfiPciIoWidthUint16
,
1170 (UINT64
)MMIO_CLKCTL
,
1178 // Reset the SD host controller
1190 TimeOutCount
= TIME_OUT_1S
;
1193 gBS
->Stall (1 * 1000);
1205 if ((Data
& Mask
) == 0) {
1208 } while (TimeOutCount
> 0);
1211 // We now restore the MMIO_CLKCTL register which we set to 0 above.
1215 EfiPciIoWidthUint16
,
1217 (UINT64
)MMIO_CLKCTL
,
1222 if (TimeOutCount
== 0) {
1223 DEBUG ((EFI_D_ERROR
, "ResetSDHost: Time out \r\n"));
1232 Enable auto stop on the host controller.
1234 @param This A pointer to the EFI_SD_HOST_IO_PROTOCOL instance.
1235 @param Enable TRUE to enable, FALSE to disable.
1244 IN EFI_SD_HOST_IO_PROTOCOL
*This
,
1248 SDHOST_DATA
*SDHostData
;
1250 SDHostData
= SDHOST_DATA_FROM_THIS (This
);
1252 SDHostData
->IsAutoStopCmd
= Enable
;
1258 Set the Block length on the host controller.
1260 @param This A pointer to the EFI_SD_HOST_IO_PROTOCOL instance.
1261 @param BlockLength card supportes block length.
1270 IN EFI_SD_HOST_IO_PROTOCOL
*This
,
1271 IN UINT32 BlockLength
1274 SDHOST_DATA
*SDHostData
;
1276 SDHostData
= SDHOST_DATA_FROM_THIS (This
);
1278 DEBUG ((EFI_D_INFO
, "Block length on the host controller: %d \r\n", BlockLength
));
1279 SDHostData
->BlockLength
= BlockLength
;
1286 Find whether these is a card inserted into the slot. If so init the host.
1287 If not, return EFI_NOT_FOUND.
1289 @param This A pointer to the EFI_SD_HOST_IO_PROTOCOL instance.
1292 @retval EFI_NOT_FOUND
1297 DetectCardAndInitHost (
1298 IN EFI_SD_HOST_IO_PROTOCOL
*This
1301 SDHOST_DATA
*SDHostData
;
1302 EFI_PCI_IO_PROTOCOL
*PciIo
;
1305 UINT8 Voltages
[] = { 33, 30, 18 };
1308 SDHostData
= SDHOST_DATA_FROM_THIS (This
);
1309 PciIo
= SDHostData
->PciIo
;
1310 Status
= EFI_NOT_FOUND
;
1315 EfiPciIoWidthUint32
,
1317 (UINT64
)MMIO_PSTATE
,
1322 if ((Data
& (BIT16
| BIT17
| BIT18
)) != (BIT16
| BIT17
| BIT18
)) {
1324 // Has no card inserted
1326 DEBUG ((EFI_D_INFO
, "DetectCardAndInitHost: No Cards \r\n"));
1327 Status
= EFI_NOT_FOUND
;
1330 DEBUG ((EFI_D_INFO
, "DetectCardAndInitHost: Find Cards \r\n"));
1332 Status
= EFI_NOT_FOUND
;
1333 for (Loop
= 0; Loop
< sizeof (Voltages
); Loop
++) {
1336 "DetectCardAndInitHost: SetHostVoltage %d.%dV \r\n",
1337 Voltages
[Loop
] / 10,
1340 Status
= SetHostVoltage (This
, Voltages
[Loop
]);
1341 if (EFI_ERROR (Status
)) {
1342 DEBUG ((EFI_D_INFO
, "DetectCardAndInitHost set voltages: [failed]\n"));
1344 DEBUG ((EFI_D_INFO
, "DetectCardAndInitHost set voltages: [success]\n"));
1348 if (EFI_ERROR (Status
)) {
1349 DEBUG ((EFI_D_ERROR
, "DetectCardAndInitHost: Fail to set voltage \r\n"));
1353 Status
= SetClockFrequency (This
, FREQUENCY_OD
);
1354 if (EFI_ERROR (Status
)) {
1355 DEBUG ((EFI_D_ERROR
, "DetectCardAndInitHost: Fail to set frequency \r\n"));
1358 SetBusWidth (This
, 1);
1361 //Enable normal status change
1364 Data
= (BIT0
| BIT1
);
1368 EfiPciIoWidthUint16
,
1370 (UINT64
)MMIO_NINTEN
,
1376 //Enable error status change
1380 EfiPciIoWidthUint16
,
1382 (UINT64
)MMIO_ERINTEN
,
1387 Data
|= (BIT0
| BIT1
| BIT2
| BIT3
| BIT4
| BIT5
| BIT6
| BIT7
| BIT8
);
1391 EfiPciIoWidthUint16
,
1393 (UINT64
)MMIO_ERINTEN
,
1399 //Data transfer Timeout control
1412 //Set Default Bus width as 1 bit
1421 Entry point for EFI drivers.
1423 @param ImageHandle EFI_HANDLE.
1424 @param SystemTable EFI_SYSTEM_TABLE.
1426 @retval EFI_SUCCESS Driver is successfully loaded.
1427 @return Others Failed.
1432 InitializeSDController (
1433 IN EFI_HANDLE ImageHandle
,
1434 IN EFI_SYSTEM_TABLE
*SystemTable
1437 return EfiLibInstallDriverBindingComponentName2 (
1440 &gSDControllerDriverBinding
,
1449 Test to see if this driver supports ControllerHandle. Any
1450 ControllerHandle that has SDHostIoProtocol installed will be supported.
1452 @param This Protocol instance pointer.
1453 @param Controller Handle of device to test.
1454 @param RemainingDevicePath Not used.
1456 @return EFI_SUCCESS This driver supports this device.
1457 @return EFI_UNSUPPORTED This driver does not support this device.
1462 SDControllerSupported (
1463 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1464 IN EFI_HANDLE Controller
,
1465 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1468 EFI_STATUS OpenStatus
;
1470 EFI_PCI_IO_PROTOCOL
*PciIo
;
1471 PCI_CLASSC PciClass
;
1472 EFI_SD_HOST_IO_PROTOCOL
*SdHostIo
;
1473 Status
= gBS
->OpenProtocol (
1475 &gEfiSDHostIoProtocolGuid
,
1477 This
->DriverBindingHandle
,
1479 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1481 if (!EFI_ERROR (Status
)) {
1482 DEBUG (( DEBUG_INFO
, "SdHost controller is already started\n"));
1483 return EFI_ALREADY_STARTED
;
1487 // Test whether there is PCI IO Protocol attached on the controller handle.
1489 OpenStatus
= gBS
->OpenProtocol (
1491 &gEfiPciIoProtocolGuid
,
1493 This
->DriverBindingHandle
,
1495 EFI_OPEN_PROTOCOL_BY_DRIVER
1498 if (EFI_ERROR (OpenStatus
)) {
1502 Status
= PciIo
->Pci
.Read (
1505 PCI_CLASSCODE_OFFSET
,
1506 sizeof (PCI_CLASSC
) / sizeof (UINT8
),
1510 if (EFI_ERROR (Status
)) {
1511 Status
= EFI_UNSUPPORTED
;
1516 // Test whether the controller belongs to SD type
1518 if ((PciClass
.BaseCode
!= PCI_CLASS_SYSTEM_PERIPHERAL
) ||
1519 (PciClass
.SubClassCode
!= PCI_SUBCLASS_SD_HOST_CONTROLLER
) ||
1520 ((PciClass
.PI
!= PCI_IF_STANDARD_HOST_NO_DMA
) && (PciClass
.PI
!= PCI_IF_STANDARD_HOST_SUPPORT_DMA
))
1523 Status
= EFI_UNSUPPORTED
;
1527 gBS
->CloseProtocol (
1529 &gEfiPciIoProtocolGuid
,
1530 This
->DriverBindingHandle
,
1537 Starting the SD Host Controller Driver.
1539 @param This Protocol instance pointer.
1540 @param Controller Handle of device to test.
1541 @param RemainingDevicePath Not used.
1543 @retval EFI_SUCCESS This driver supports this device.
1544 @retval EFI_UNSUPPORTED This driver does not support this device.
1545 @retval EFI_DEVICE_ERROR This driver cannot be started due to device Error.
1546 EFI_OUT_OF_RESOURCES- Failed due to resource shortage.
1552 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1553 IN EFI_HANDLE Controller
,
1554 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1558 EFI_PCI_IO_PROTOCOL
*PciIo
;
1559 SDHOST_DATA
*SDHostData
;
1567 // Open PCI I/O Protocol and save pointer to open protocol
1568 // in private data area.
1570 Status
= gBS
->OpenProtocol (
1572 &gEfiPciIoProtocolGuid
,
1574 This
->DriverBindingHandle
,
1576 EFI_OPEN_PROTOCOL_BY_DRIVER
1579 if (EFI_ERROR (Status
)) {
1584 // Enable the SD Host Controller MMIO space
1586 Status
= PciIo
->Attributes (
1588 EfiPciIoAttributeOperationEnable
,
1589 EFI_PCI_DEVICE_ENABLE
,
1592 if (EFI_ERROR (Status
)) {
1593 Status
= EFI_OUT_OF_RESOURCES
;
1598 SDHostData
= (SDHOST_DATA
*)AllocateZeroPool(sizeof (SDHOST_DATA
));
1599 if (SDHostData
== NULL
) {
1600 Status
= EFI_OUT_OF_RESOURCES
;
1604 SDHostData
->Signature
= SDHOST_DATA_SIGNATURE
;
1605 SDHostData
->PciIo
= PciIo
;
1607 CopyMem (&SDHostData
->SDHostIo
, &mSDHostIo
, sizeof (EFI_SD_HOST_IO_PROTOCOL
));
1609 ResetSDHost (&SDHostData
->SDHostIo
, Reset_All
);
1613 EfiPciIoWidthUint16
,
1615 (UINT64
)MMIO_CTRLRVER
,
1619 SDHostData
->SDHostIo
.HostCapability
.HostVersion
= Data
& 0xFF;
1620 DEBUG ((EFI_D_INFO
, "SdHostDriverBindingStart: HostVersion 0x%x \r\n", SDHostData
->SDHostIo
.HostCapability
.HostVersion
));
1624 EfiPciIoWidthUint32
,
1630 DEBUG ((EFI_D_INFO
, "SdHostDriverBindingStart: MMIO_CAP 0x%x \r\n", Data
));
1631 if ((Data
& BIT18
) != 0) {
1632 SDHostData
->SDHostIo
.HostCapability
.BusWidth8
= TRUE
;
1635 if ((Data
& BIT21
) != 0) {
1636 SDHostData
->SDHostIo
.HostCapability
.HighSpeedSupport
= TRUE
;
1639 if ((Data
& BIT24
) != 0) {
1640 SDHostData
->SDHostIo
.HostCapability
.V33Support
= TRUE
;
1643 if ((Data
& BIT25
) != 0) {
1644 SDHostData
->SDHostIo
.HostCapability
.V30Support
= TRUE
;
1647 if ((Data
& BIT26
) != 0) {
1648 SDHostData
->SDHostIo
.HostCapability
.V18Support
= TRUE
;
1651 SDHostData
->SDHostIo
.HostCapability
.BusWidth4
= TRUE
;
1653 if(SDHostData
->SDHostIo
.HostCapability
.HostVersion
< SDHCI_SPEC_300
) {
1657 SDHostData
->BaseClockInMHz
= (Data
>> 8) & 0x3F;
1660 SDHostData
->BaseClockInMHz
= (Data
>> 8) & 0xFF;
1664 SDHostData
->BlockLength
= 512 << ((Data
>> 16) & 0x03);
1665 DEBUG ((EFI_D_INFO
, "SdHostDriverBindingStart: BlockLength 0x%x \r\n", SDHostData
->BlockLength
));
1666 SDHostData
->IsAutoStopCmd
= TRUE
;
1668 Status
= gBS
->InstallProtocolInterface (
1670 &gEfiSDHostIoProtocolGuid
,
1671 EFI_NATIVE_INTERFACE
,
1672 &SDHostData
->SDHostIo
1674 if (EFI_ERROR (Status
)) {
1679 // Install the component name protocol
1681 SDHostData
->ControllerNameTable
= NULL
;
1685 gSDControllerName
.SupportedLanguages
,
1686 &SDHostData
->ControllerNameTable
,
1687 L
"SD Host Controller",
1692 gSDControllerName2
.SupportedLanguages
,
1693 &SDHostData
->ControllerNameTable
,
1694 L
"SD Host Controller",
1699 if (EFI_ERROR (Status
)) {
1700 if (SDHostData
!= NULL
) {
1701 FreePool (SDHostData
);
1710 Stop this driver on ControllerHandle. Support stopping any child handles
1711 created by this driver.
1713 @param This Protocol instance pointer.
1714 @param Controller Handle of device to stop driver on.
1715 @param NumberOfChildren Number of Children in the ChildHandleBuffer.
1716 @param ChildHandleBuffer List of handles for the children we need to stop.
1725 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1726 IN EFI_HANDLE Controller
,
1727 IN UINTN NumberOfChildren
,
1728 IN EFI_HANDLE
*ChildHandleBuffer
1732 EFI_SD_HOST_IO_PROTOCOL
*SDHostIo
;
1733 SDHOST_DATA
*SDHostData
;
1735 Status
= gBS
->OpenProtocol (
1737 &gEfiSDHostIoProtocolGuid
,
1738 (VOID
**) &SDHostIo
,
1739 This
->DriverBindingHandle
,
1741 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1745 // Test whether the Controller handler passed in is a valid
1746 // Usb controller handle that should be supported, if not,
1747 // return the error status directly
1749 if (EFI_ERROR (Status
)) {
1753 SetHostVoltage (SDHostIo
, 0);
1755 SDHostData
= SDHOST_DATA_FROM_THIS(SDHostIo
);
1758 // Uninstall Block I/O protocol from the device handle
1760 Status
= gBS
->UninstallProtocolInterface (
1762 &gEfiSDHostIoProtocolGuid
,
1765 if (EFI_ERROR (Status
)) {
1769 FreeUnicodeStringTable (SDHostData
->ControllerNameTable
);
1771 FreePool (SDHostData
);
1773 gBS
->CloseProtocol (
1775 &gEfiPciIoProtocolGuid
,
1776 This
->DriverBindingHandle
,