]> git.proxmox.com Git - mirror_edk2.git/blob - QuarkSocPkg/QuarkSouthCluster/Sdio/Dxe/SDMediaDeviceDxe/SDMediaDevice.c
QuarkSocPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / QuarkSocPkg / QuarkSouthCluster / Sdio / Dxe / SDMediaDeviceDxe / SDMediaDevice.c
1 /** @file
2
3 The definition for SD media device driver model and blkio protocol routines.
4
5 Copyright (c) 2013-2016 Intel Corporation.
6
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11
12 #include "SDMediaDevice.h"
13
14
15 EFI_DRIVER_BINDING_PROTOCOL gSDMediaDeviceDriverBinding = {
16 SDMediaDeviceSupported,
17 SDMediaDeviceStart,
18 SDMediaDeviceStop,
19 0x20,
20 NULL,
21 NULL
22 };
23
24 /**
25 Entry point for EFI drivers.
26
27 @param ImageHandle EFI_HANDLE.
28 @param SystemTable EFI_SYSTEM_TABLE.
29
30 @retval EFI_SUCCESS Driver is successfully loaded.
31 @return Others Failed.
32
33 **/
34 EFI_STATUS
35 EFIAPI
36 InitializeSDMediaDevice (
37 IN EFI_HANDLE ImageHandle,
38 IN EFI_SYSTEM_TABLE *SystemTable
39 )
40 {
41 return EfiLibInstallDriverBindingComponentName2 (
42 ImageHandle,
43 SystemTable,
44 &gSDMediaDeviceDriverBinding,
45 ImageHandle,
46 &gSDMediaDeviceName,
47 &gSDMediaDeviceName2
48 );
49 }
50
51
52 /**
53 Test to see if this driver supports ControllerHandle. Any
54 ControllerHandle that has BlockIoProtocol installed will be supported.
55
56 @param This Protocol instance pointer.
57 @param Controller Handle of device to test.
58 @param RemainingDevicePath Not used.
59
60 @return EFI_SUCCESS This driver supports this device.
61 @return EFI_UNSUPPORTED This driver does not support this device.
62
63 **/
64 EFI_STATUS
65 EFIAPI
66 SDMediaDeviceSupported (
67 IN EFI_DRIVER_BINDING_PROTOCOL *This,
68 IN EFI_HANDLE Controller,
69 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
70 )
71 {
72 EFI_STATUS Status;
73 EFI_SD_HOST_IO_PROTOCOL *SDHostIo;
74
75 //
76 // Test whether there is PCI IO Protocol attached on the controller handle.
77 //
78 Status = gBS->OpenProtocol (
79 Controller,
80 &gEfiSDHostIoProtocolGuid,
81 (VOID **)&SDHostIo,
82 This->DriverBindingHandle,
83 Controller,
84 EFI_OPEN_PROTOCOL_BY_DRIVER
85 );
86 if (EFI_ERROR (Status)) {
87 goto Exit;
88 }
89
90 gBS->CloseProtocol (
91 Controller,
92 &gEfiSDHostIoProtocolGuid,
93 This->DriverBindingHandle,
94 Controller
95 );
96
97 Exit:
98 return Status;
99 }
100
101 /**
102 Starting the SD Media Device Driver.
103
104 @param This Protocol instance pointer.
105 @param Controller Handle of device to test.
106 @param RemainingDevicePath Not used.
107
108 @retval EFI_SUCCESS This driver supports this device.
109 @retval EFI_UNSUPPORTED This driver does not support this device.
110 @retval EFI_DEVICE_ERROR This driver cannot be started due to device Error.
111 EFI_OUT_OF_RESOURCES- Failed due to resource shortage.
112
113 **/
114 EFI_STATUS
115 EFIAPI
116 SDMediaDeviceStart (
117 IN EFI_DRIVER_BINDING_PROTOCOL *This,
118 IN EFI_HANDLE Controller,
119 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
120 )
121 {
122 EFI_STATUS Status;
123 EFI_SD_HOST_IO_PROTOCOL *SDHostIo;
124 CARD_DATA *CardData;
125
126 CardData = NULL;
127
128 //
129 // Open PCI I/O Protocol and save pointer to open protocol
130 // in private data area.
131 //
132 Status = gBS->OpenProtocol (
133 Controller,
134 &gEfiSDHostIoProtocolGuid,
135 (VOID **) &SDHostIo,
136 This->DriverBindingHandle,
137 Controller,
138 EFI_OPEN_PROTOCOL_BY_DRIVER
139 );
140 if (EFI_ERROR (Status)) {
141 DEBUG ((EFI_D_ERROR, "SDMediaDeviceStart: Fail to open gEfiSDHostIoProtocolGuid \r\n"));
142 goto Exit;
143 }
144
145 Status = SDHostIo->DetectCardAndInitHost (SDHostIo);
146 if (EFI_ERROR (Status)) {
147 DEBUG ((EFI_D_INFO, "SDMediaDeviceStart: Fail to DetectCardAndInitHost \r\n"));
148 goto Exit;
149 }
150
151 CardData = (CARD_DATA*)AllocateZeroPool(sizeof (CARD_DATA));
152 if (CardData == NULL) {
153 Status = EFI_OUT_OF_RESOURCES;
154 DEBUG ((EFI_D_ERROR, "SDMediaDeviceStart: Fail to AllocateZeroPool(CARD_DATA) \r\n"));
155 goto Exit;
156 }
157
158 ASSERT (SDHostIo->HostCapability.BoundarySize >= 4 * 1024);
159 CardData->RawBufferPointer = (UINT8*)((UINTN)DMA_MEMORY_TOP);
160 Status = gBS->AllocatePages (
161 AllocateMaxAddress,
162 EfiBootServicesData,
163 EFI_SIZE_TO_PAGES (2 * SDHostIo->HostCapability.BoundarySize),
164 (EFI_PHYSICAL_ADDRESS *)(&CardData->RawBufferPointer)
165 );
166
167 if (CardData->RawBufferPointer == NULL) {
168 DEBUG ((EFI_D_ERROR, "SDMediaDeviceStart: Fail to AllocateZeroPool(2*x) \r\n"));
169 Status = EFI_OUT_OF_RESOURCES;
170 goto Exit;
171 }
172 CardData->AlignedBuffer = CardData->RawBufferPointer - ((UINTN)(CardData->RawBufferPointer) & (SDHostIo->HostCapability.BoundarySize - 1)) + SDHostIo->HostCapability.BoundarySize;
173
174 CardData->Signature = CARD_DATA_SIGNATURE;
175 CardData->SDHostIo = SDHostIo;
176
177 Status = MMCSDCardInit (CardData);
178 if (EFI_ERROR (Status)) {
179 DEBUG ((EFI_D_ERROR, "SDMediaDeviceStart: Fail to MMCSDCardInit \r\n"));
180 goto Exit;
181 }
182 DEBUG ((EFI_D_INFO, "SDMediaDeviceStart: MMCSDCardInit SuccessFul\n"));
183
184 if (CardData->CardType == CEATACard) {
185 Status = CEATABlockIoInit (CardData);
186 } else {
187 Status = MMCSDBlockIoInit (CardData);
188 }
189
190 if (EFI_ERROR (Status)) {
191 DEBUG ((EFI_D_ERROR, "SDMediaDeviceStart: Fail to BlockIoInit \r\n"));
192 goto Exit;
193 }
194 DEBUG ((EFI_D_INFO, "SDMediaDeviceStart: BlockIo is successfully installed\n"));
195
196
197 Status = gBS->InstallProtocolInterface (
198 &Controller,
199 &gEfiBlockIoProtocolGuid,
200 EFI_NATIVE_INTERFACE,
201 &CardData->BlockIo
202 );
203 if (EFI_ERROR (Status)) {
204 DEBUG ((EFI_D_ERROR, "SDMediaDeviceStart: Fail to install gEfiBlockIoProtocolGuid \r\n"));
205 goto Exit;
206 }
207
208 //
209 // Install the component name protocol
210 //
211 CardData->ControllerNameTable = NULL;
212
213 AddUnicodeString2 (
214 "eng",
215 gSDMediaDeviceName.SupportedLanguages,
216 &CardData->ControllerNameTable,
217 L"MMC/SD Media Device",
218 TRUE
219 );
220 AddUnicodeString2 (
221 "en",
222 gSDMediaDeviceName2.SupportedLanguages,
223 &CardData->ControllerNameTable,
224 L"MMC/SD Media Device",
225 FALSE
226 );
227
228 Exit:
229 if (EFI_ERROR (Status)) {
230 DEBUG ((EFI_D_INFO, "SDMediaDeviceStart: End with failure\r\n"));
231 if (CardData != NULL) {
232 if (CardData->RawBufferPointer != NULL) {
233 gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) CardData->RawBufferPointer, EFI_SIZE_TO_PAGES (2 * SDHostIo->HostCapability.BoundarySize));
234 }
235 FreePool (CardData);
236 }
237 }
238
239 return Status;
240 }
241
242
243 /**
244 Stop this driver on ControllerHandle. Support stopping any child handles
245 created by this driver.
246
247 @param This Protocol instance pointer.
248 @param Controller Handle of device to stop driver on.
249 @param NumberOfChildren Number of Children in the ChildHandleBuffer.
250 @param ChildHandleBuffer List of handles for the children we need to stop.
251
252 @return EFI_SUCCESS
253 @return others
254
255 **/
256 EFI_STATUS
257 EFIAPI
258 SDMediaDeviceStop (
259 IN EFI_DRIVER_BINDING_PROTOCOL *This,
260 IN EFI_HANDLE Controller,
261 IN UINTN NumberOfChildren,
262 IN EFI_HANDLE *ChildHandleBuffer
263 )
264 {
265 EFI_STATUS Status;
266 CARD_DATA *CardData;
267 EFI_BLOCK_IO_PROTOCOL *BlockIo;
268
269 //
270 // First find BlockIo Protocol
271 //
272 Status = gBS->OpenProtocol (
273 Controller,
274 &gEfiBlockIoProtocolGuid,
275 (VOID **)&BlockIo,
276 This->DriverBindingHandle,
277 Controller,
278 EFI_OPEN_PROTOCOL_GET_PROTOCOL
279 );
280 if (EFI_ERROR (Status)) {
281 return Status;
282 }
283
284 CardData = CARD_DATA_FROM_THIS(BlockIo);
285
286 //
287 // Uninstall Block I/O protocol from the device handle
288 //
289 Status = gBS->UninstallProtocolInterface (
290 Controller,
291 &gEfiBlockIoProtocolGuid,
292 BlockIo
293 );
294 if (EFI_ERROR (Status)) {
295 return Status;
296 }
297
298 if (CardData != NULL) {
299 if (CardData->RawBufferPointer != NULL) {
300 gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) CardData->RawBufferPointer, EFI_SIZE_TO_PAGES (2 * CardData->SDHostIo->HostCapability.BoundarySize));
301 }
302 FreeUnicodeStringTable (CardData->ControllerNameTable);
303 FreePool (CardData);
304 }
305
306 gBS->CloseProtocol (
307 Controller,
308 &gEfiSDHostIoProtocolGuid,
309 This->DriverBindingHandle,
310 Controller
311 );
312
313 return EFI_SUCCESS;
314 }
315
316
317