3 The definition for SD media device driver model and blkio protocol routines.
5 Copyright (c) 2013-2015 Intel Corporation.
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include "SDMediaDevice.h"
21 EFI_DRIVER_BINDING_PROTOCOL gSDMediaDeviceDriverBinding
= {
22 SDMediaDeviceSupported
,
31 Entry point for EFI drivers.
33 @param ImageHandle EFI_HANDLE.
34 @param SystemTable EFI_SYSTEM_TABLE.
36 @retval EFI_SUCCESS Driver is successfully loaded.
37 @return Others Failed.
42 InitializeSDMediaDevice (
43 IN EFI_HANDLE ImageHandle
,
44 IN EFI_SYSTEM_TABLE
*SystemTable
47 return EfiLibInstallDriverBindingComponentName2 (
50 &gSDMediaDeviceDriverBinding
,
59 Test to see if this driver supports ControllerHandle. Any
60 ControllerHandle that has BlockIoProtocol installed will be supported.
62 @param This Protocol instance pointer.
63 @param Controller Handle of device to test.
64 @param RemainingDevicePath Not used.
66 @return EFI_SUCCESS This driver supports this device.
67 @return EFI_UNSUPPORTED This driver does not support this device.
72 SDMediaDeviceSupported (
73 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
74 IN EFI_HANDLE Controller
,
75 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
79 EFI_SD_HOST_IO_PROTOCOL
*SDHostIo
;
82 // Test whether there is PCI IO Protocol attached on the controller handle.
84 Status
= gBS
->OpenProtocol (
86 &gEfiSDHostIoProtocolGuid
,
88 This
->DriverBindingHandle
,
90 EFI_OPEN_PROTOCOL_BY_DRIVER
92 if (EFI_ERROR (Status
)) {
98 &gEfiSDHostIoProtocolGuid
,
99 This
->DriverBindingHandle
,
108 Starting the SD Media Device Driver.
110 @param This Protocol instance pointer.
111 @param Controller Handle of device to test.
112 @param RemainingDevicePath Not used.
114 @retval EFI_SUCCESS This driver supports this device.
115 @retval EFI_UNSUPPORTED This driver does not support this device.
116 @retval EFI_DEVICE_ERROR This driver cannot be started due to device Error.
117 EFI_OUT_OF_RESOURCES- Failed due to resource shortage.
123 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
124 IN EFI_HANDLE Controller
,
125 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
129 EFI_SD_HOST_IO_PROTOCOL
*SDHostIo
;
135 // Open PCI I/O Protocol and save pointer to open protocol
136 // in private data area.
138 Status
= gBS
->OpenProtocol (
140 &gEfiSDHostIoProtocolGuid
,
142 This
->DriverBindingHandle
,
144 EFI_OPEN_PROTOCOL_BY_DRIVER
146 if (EFI_ERROR (Status
)) {
147 DEBUG ((EFI_D_ERROR
, "SDMediaDeviceStart: Fail to open gEfiSDHostIoProtocolGuid \r\n"));
151 Status
= SDHostIo
->DetectCardAndInitHost (SDHostIo
);
152 if (EFI_ERROR (Status
)) {
153 DEBUG ((EFI_D_INFO
, "SDMediaDeviceStart: Fail to DetectCardAndInitHost \r\n"));
157 CardData
= (CARD_DATA
*)AllocateZeroPool(sizeof (CARD_DATA
));
158 if (CardData
== NULL
) {
159 Status
= EFI_OUT_OF_RESOURCES
;
160 DEBUG ((EFI_D_ERROR
, "SDMediaDeviceStart: Fail to AllocateZeroPool(CARD_DATA) \r\n"));
164 ASSERT (SDHostIo
->HostCapability
.BoundarySize
>= 4 * 1024);
165 CardData
->RawBufferPointer
= (UINT8
*)((UINTN
)DMA_MEMORY_TOP
);
166 Status
= gBS
->AllocatePages (
169 EFI_SIZE_TO_PAGES (2 * SDHostIo
->HostCapability
.BoundarySize
),
170 (EFI_PHYSICAL_ADDRESS
*)(&CardData
->RawBufferPointer
)
173 if (CardData
->RawBufferPointer
== NULL
) {
174 DEBUG ((EFI_D_ERROR
, "SDMediaDeviceStart: Fail to AllocateZeroPool(2*x) \r\n"));
175 Status
= EFI_OUT_OF_RESOURCES
;
178 CardData
->AlignedBuffer
= CardData
->RawBufferPointer
- ((UINTN
)(CardData
->RawBufferPointer
) & (SDHostIo
->HostCapability
.BoundarySize
- 1)) + SDHostIo
->HostCapability
.BoundarySize
;
180 CardData
->Signature
= CARD_DATA_SIGNATURE
;
181 CardData
->SDHostIo
= SDHostIo
;
183 Status
= MMCSDCardInit (CardData
);
184 if (EFI_ERROR (Status
)) {
185 DEBUG ((EFI_D_ERROR
, "SDMediaDeviceStart: Fail to MMCSDCardInit \r\n"));
188 DEBUG ((EFI_D_INFO
, "SDMediaDeviceStart: MMCSDCardInit SuccessFul\n"));
190 if (CardData
->CardType
== CEATACard
) {
191 Status
= CEATABlockIoInit (CardData
);
193 Status
= MMCSDBlockIoInit (CardData
);
196 if (EFI_ERROR (Status
)) {
197 DEBUG ((EFI_D_ERROR
, "SDMediaDeviceStart: Fail to BlockIoInit \r\n"));
200 DEBUG ((EFI_D_INFO
, "SDMediaDeviceStart: BlockIo is successfully installed\n"));
203 Status
= gBS
->InstallProtocolInterface (
205 &gEfiBlockIoProtocolGuid
,
206 EFI_NATIVE_INTERFACE
,
209 if (EFI_ERROR (Status
)) {
210 DEBUG ((EFI_D_ERROR
, "SDMediaDeviceStart: Fail to install gEfiBlockIoProtocolGuid \r\n"));
215 // Install the component name protocol
217 CardData
->ControllerNameTable
= NULL
;
221 gSDMediaDeviceName
.SupportedLanguages
,
222 &CardData
->ControllerNameTable
,
223 L
"MMC/SD Media Device",
228 gSDMediaDeviceName2
.SupportedLanguages
,
229 &CardData
->ControllerNameTable
,
230 L
"MMC/SD Media Device",
235 if (EFI_ERROR (Status
)) {
236 DEBUG ((EFI_D_INFO
, "SDMediaDeviceStart: End with failure\r\n"));
237 if (CardData
!= NULL
) {
238 if (CardData
->RawBufferPointer
!= NULL
) {
239 gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) CardData
->RawBufferPointer
, EFI_SIZE_TO_PAGES (2 * SDHostIo
->HostCapability
.BoundarySize
));
250 Stop this driver on ControllerHandle. Support stoping any child handles
251 created by this driver.
253 @param This Protocol instance pointer.
254 @param Controller Handle of device to stop driver on.
255 @param NumberOfChildren Number of Children in the ChildHandleBuffer.
256 @param ChildHandleBuffer List of handles for the children we need to stop.
265 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
266 IN EFI_HANDLE Controller
,
267 IN UINTN NumberOfChildren
,
268 IN EFI_HANDLE
*ChildHandleBuffer
273 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
276 // First find BlockIo Protocol
278 Status
= gBS
->OpenProtocol (
280 &gEfiBlockIoProtocolGuid
,
282 This
->DriverBindingHandle
,
284 EFI_OPEN_PROTOCOL_GET_PROTOCOL
286 if (EFI_ERROR (Status
)) {
290 CardData
= CARD_DATA_FROM_THIS(BlockIo
);
293 // Uninstall Block I/O protocol from the device handle
295 Status
= gBS
->UninstallProtocolInterface (
297 &gEfiBlockIoProtocolGuid
,
300 if (EFI_ERROR (Status
)) {
304 if (CardData
!= NULL
) {
305 if (CardData
->RawBufferPointer
!= NULL
) {
306 gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) CardData
->RawBufferPointer
, EFI_SIZE_TO_PAGES (2 * CardData
->SDHostIo
->HostCapability
.BoundarySize
));
308 FreeUnicodeStringTable (CardData
->ControllerNameTable
);
314 &gEfiSDHostIoProtocolGuid
,
315 This
->DriverBindingHandle
,