]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Pci/SdMmcPciHcDxe/SdMmcPciHcDxe.c
MdeModulePkg/SdMmc: Fix build failure caused by last check-in
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / SdMmcPciHcDxe / SdMmcPciHcDxe.c
CommitLineData
48555339
FT
1/** @file\r
2 This driver is used to manage SD/MMC PCI host controllers which are compliance\r
3 with SD Host Controller Simplified Specification version 3.00.\r
4\r
5 It would expose EFI_SD_MMC_PASS_THRU_PROTOCOL for upper layer use.\r
6\r
2e9107b8 7 Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>\r
48555339
FT
8 This program and the accompanying materials\r
9 are licensed and made available under the terms and conditions of the BSD License\r
10 which accompanies this distribution. The full text of the license may be found at\r
11 http://opensource.org/licenses/bsd-license.php\r
12\r
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17\r
18#include "SdMmcPciHcDxe.h"\r
19\r
20//\r
21// Driver Global Variables\r
22//\r
23EFI_DRIVER_BINDING_PROTOCOL gSdMmcPciHcDriverBinding = {\r
24 SdMmcPciHcDriverBindingSupported,\r
25 SdMmcPciHcDriverBindingStart,\r
26 SdMmcPciHcDriverBindingStop,\r
27 0x10,\r
28 NULL,\r
29 NULL\r
30};\r
31\r
32//\r
33// Template for SD/MMC host controller private data.\r
34//\r
35SD_MMC_HC_PRIVATE_DATA gSdMmcPciHcTemplate = {\r
36 SD_MMC_HC_PRIVATE_SIGNATURE, // Signature\r
37 NULL, // ControllerHandle\r
38 NULL, // PciIo\r
39 { // PassThru\r
40 sizeof (UINT32),\r
41 SdMmcPassThruPassThru,\r
42 SdMmcPassThruGetNextSlot,\r
43 SdMmcPassThruBuildDevicePath,\r
44 SdMmcPassThruGetSlotNumber,\r
45 SdMmcPassThruResetDevice\r
46 },\r
47 0, // PciAttributes\r
48 0, // PreviousSlot\r
49 NULL, // TimerEvent\r
50 NULL, // ConnectEvent\r
51 // Queue\r
52 INITIALIZE_LIST_HEAD_VARIABLE (gSdMmcPciHcTemplate.Queue),\r
53 { // Slot\r
c25ddd01
FT
54 {0, UnknownSlot, 0, 0, 0}, {0, UnknownSlot, 0, 0, 0}, {0, UnknownSlot, 0, 0, 0},\r
55 {0, UnknownSlot, 0, 0, 0}, {0, UnknownSlot, 0, 0, 0}, {0, UnknownSlot, 0, 0, 0}\r
48555339
FT
56 },\r
57 { // Capability\r
58 {0},\r
59 },\r
60 { // MaxCurrent\r
61 0,\r
62 },\r
63 0 // ControllerVersion\r
64};\r
65\r
66SD_DEVICE_PATH mSdDpTemplate = {\r
67 {\r
68 MESSAGING_DEVICE_PATH,\r
69 MSG_SD_DP,\r
70 {\r
71 (UINT8) (sizeof (SD_DEVICE_PATH)),\r
72 (UINT8) ((sizeof (SD_DEVICE_PATH)) >> 8)\r
73 }\r
74 },\r
75 0\r
76};\r
77\r
78EMMC_DEVICE_PATH mEmmcDpTemplate = {\r
79 {\r
80 MESSAGING_DEVICE_PATH,\r
81 MSG_EMMC_DP,\r
82 {\r
83 (UINT8) (sizeof (EMMC_DEVICE_PATH)),\r
84 (UINT8) ((sizeof (EMMC_DEVICE_PATH)) >> 8)\r
85 }\r
86 },\r
87 0\r
88};\r
89\r
90//\r
91// Prioritized function list to detect card type.\r
92// User could add other card detection logic here.\r
93//\r
94CARD_TYPE_DETECT_ROUTINE mCardTypeDetectRoutineTable[] = {\r
95 EmmcIdentification,\r
96 SdCardIdentification,\r
97 NULL\r
98};\r
99\r
100/**\r
101 The entry point for SD host controller driver, used to install this driver on the ImageHandle.\r
102\r
103 @param[in] ImageHandle The firmware allocated handle for this driver image.\r
104 @param[in] SystemTable Pointer to the EFI system table.\r
105\r
106 @retval EFI_SUCCESS Driver loaded.\r
107 @retval other Driver not loaded.\r
108\r
109**/\r
110EFI_STATUS\r
111EFIAPI\r
112InitializeSdMmcPciHcDxe (\r
113 IN EFI_HANDLE ImageHandle,\r
114 IN EFI_SYSTEM_TABLE *SystemTable\r
115 )\r
116{\r
117 EFI_STATUS Status;\r
118\r
119 Status = EfiLibInstallDriverBindingComponentName2 (\r
120 ImageHandle,\r
121 SystemTable,\r
122 &gSdMmcPciHcDriverBinding,\r
123 ImageHandle,\r
124 &gSdMmcPciHcComponentName,\r
125 &gSdMmcPciHcComponentName2\r
126 );\r
127 ASSERT_EFI_ERROR (Status);\r
128\r
129 return Status;\r
130}\r
131\r
132/**\r
133 Call back function when the timer event is signaled.\r
134\r
135 @param[in] Event The Event this notify function registered to.\r
136 @param[in] Context Pointer to the context data registered to the\r
137 Event.\r
138\r
139**/\r
140VOID\r
141EFIAPI\r
142ProcessAsyncTaskList (\r
143 IN EFI_EVENT Event,\r
144 IN VOID* Context\r
145 )\r
146{\r
147 SD_MMC_HC_PRIVATE_DATA *Private;\r
148 LIST_ENTRY *Link;\r
149 SD_MMC_HC_TRB *Trb;\r
150 EFI_STATUS Status;\r
151 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET *Packet;\r
152 BOOLEAN InfiniteWait;\r
153 EFI_EVENT TrbEvent;\r
154\r
155 Private = (SD_MMC_HC_PRIVATE_DATA*)Context;\r
156\r
157 //\r
158 // Check if the first entry in the async I/O queue is done or not.\r
159 //\r
160 Status = EFI_SUCCESS;\r
161 Trb = NULL;\r
162 Link = GetFirstNode (&Private->Queue);\r
163 if (!IsNull (&Private->Queue, Link)) {\r
164 Trb = SD_MMC_HC_TRB_FROM_THIS (Link);\r
e26575f7 165 if (!Private->Slot[Trb->Slot].MediaPresent) {\r
48555339
FT
166 Status = EFI_NO_MEDIA;\r
167 goto Done;\r
168 }\r
169 if (!Trb->Started) {\r
170 //\r
171 // Check whether the cmd/data line is ready for transfer.\r
172 //\r
173 Status = SdMmcCheckTrbEnv (Private, Trb);\r
174 if (!EFI_ERROR (Status)) {\r
175 Trb->Started = TRUE;\r
176 Status = SdMmcExecTrb (Private, Trb);\r
177 if (EFI_ERROR (Status)) {\r
178 goto Done;\r
179 }\r
180 } else {\r
181 goto Done;\r
182 }\r
183 }\r
184 Status = SdMmcCheckTrbResult (Private, Trb);\r
185 }\r
186\r
187Done:\r
188 if ((Trb != NULL) && (Status == EFI_NOT_READY)) {\r
189 Packet = Trb->Packet;\r
190 if (Packet->Timeout == 0) {\r
191 InfiniteWait = TRUE;\r
192 } else {\r
193 InfiniteWait = FALSE;\r
194 }\r
195 if ((!InfiniteWait) && (Trb->Timeout-- == 0)) {\r
196 RemoveEntryList (Link);\r
197 Trb->Packet->TransactionStatus = EFI_TIMEOUT;\r
198 TrbEvent = Trb->Event;\r
199 SdMmcFreeTrb (Trb);\r
e27ccaba 200 DEBUG ((DEBUG_VERBOSE, "ProcessAsyncTaskList(): Signal Event %p EFI_TIMEOUT\n", TrbEvent));\r
48555339
FT
201 gBS->SignalEvent (TrbEvent);\r
202 return;\r
203 }\r
204 }\r
205 if ((Trb != NULL) && (Status != EFI_NOT_READY)) {\r
206 RemoveEntryList (Link);\r
207 Trb->Packet->TransactionStatus = Status;\r
208 TrbEvent = Trb->Event;\r
209 SdMmcFreeTrb (Trb);\r
e27ccaba 210 DEBUG ((DEBUG_VERBOSE, "ProcessAsyncTaskList(): Signal Event %p with %r\n", TrbEvent, Status));\r
48555339
FT
211 gBS->SignalEvent (TrbEvent);\r
212 }\r
213 return;\r
214}\r
215\r
216/**\r
217 Sd removable device enumeration callback function when the timer event is signaled.\r
218\r
219 @param[in] Event The Event this notify function registered to.\r
220 @param[in] Context Pointer to the context data registered to the\r
221 Event.\r
222\r
223**/\r
224VOID\r
225EFIAPI\r
226SdMmcPciHcEnumerateDevice (\r
227 IN EFI_EVENT Event,\r
228 IN VOID* Context\r
229 )\r
230{\r
231 SD_MMC_HC_PRIVATE_DATA *Private;\r
232 EFI_STATUS Status;\r
233 UINT8 Slot;\r
234 BOOLEAN MediaPresent;\r
235 UINT32 RoutineNum;\r
236 CARD_TYPE_DETECT_ROUTINE *Routine;\r
237 UINTN Index;\r
238 LIST_ENTRY *Link;\r
239 LIST_ENTRY *NextLink;\r
240 SD_MMC_HC_TRB *Trb;\r
3b1d8241 241 EFI_TPL OldTpl;\r
48555339
FT
242\r
243 Private = (SD_MMC_HC_PRIVATE_DATA*)Context;\r
244\r
245 for (Slot = 0; Slot < SD_MMC_HC_MAX_SLOT; Slot++) {\r
246 if ((Private->Slot[Slot].Enable) && (Private->Slot[Slot].SlotType == RemovableSlot)) {\r
247 Status = SdMmcHcCardDetect (Private->PciIo, Slot, &MediaPresent);\r
e26575f7 248 if ((Status == EFI_MEDIA_CHANGED) && !MediaPresent) {\r
e27ccaba 249 DEBUG ((DEBUG_INFO, "SdMmcPciHcEnumerateDevice: device disconnected at slot %d of pci %p\n", Slot, Private->PciIo));\r
48555339 250 Private->Slot[Slot].MediaPresent = FALSE;\r
c25ddd01 251 Private->Slot[Slot].Initialized = FALSE;\r
48555339
FT
252 //\r
253 // Signal all async task events at the slot with EFI_NO_MEDIA status.\r
254 //\r
3b1d8241 255 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
48555339
FT
256 for (Link = GetFirstNode (&Private->Queue);\r
257 !IsNull (&Private->Queue, Link);\r
258 Link = NextLink) {\r
259 NextLink = GetNextNode (&Private->Queue, Link);\r
260 Trb = SD_MMC_HC_TRB_FROM_THIS (Link);\r
261 if (Trb->Slot == Slot) {\r
262 RemoveEntryList (Link);\r
263 Trb->Packet->TransactionStatus = EFI_NO_MEDIA;\r
264 gBS->SignalEvent (Trb->Event);\r
265 SdMmcFreeTrb (Trb);\r
266 }\r
267 }\r
3b1d8241 268 gBS->RestoreTPL (OldTpl);\r
48555339
FT
269 //\r
270 // Notify the upper layer the connect state change through ReinstallProtocolInterface.\r
271 //\r
272 gBS->ReinstallProtocolInterface (\r
273 Private->ControllerHandle,\r
274 &gEfiSdMmcPassThruProtocolGuid,\r
275 &Private->PassThru,\r
276 &Private->PassThru\r
277 );\r
278 }\r
e26575f7 279 if ((Status == EFI_MEDIA_CHANGED) && MediaPresent) {\r
e27ccaba 280 DEBUG ((DEBUG_INFO, "SdMmcPciHcEnumerateDevice: device connected at slot %d of pci %p\n", Slot, Private->PciIo));\r
48555339 281 //\r
83ceccab
FT
282 // Reset the specified slot of the SD/MMC Pci Host Controller\r
283 //\r
284 Status = SdMmcHcReset (Private->PciIo, Slot);\r
285 if (EFI_ERROR (Status)) {\r
286 continue;\r
287 }\r
288 //\r
48555339
FT
289 // Reinitialize slot and restart identification process for the new attached device\r
290 //\r
291 Status = SdMmcHcInitHost (Private->PciIo, Slot, Private->Capability[Slot]);\r
292 if (EFI_ERROR (Status)) {\r
293 continue;\r
294 }\r
295\r
296 Private->Slot[Slot].MediaPresent = TRUE;\r
c25ddd01 297 Private->Slot[Slot].Initialized = TRUE;\r
48555339
FT
298 RoutineNum = sizeof (mCardTypeDetectRoutineTable) / sizeof (CARD_TYPE_DETECT_ROUTINE);\r
299 for (Index = 0; Index < RoutineNum; Index++) {\r
300 Routine = &mCardTypeDetectRoutineTable[Index];\r
301 if (*Routine != NULL) {\r
302 Status = (*Routine) (Private, Slot);\r
303 if (!EFI_ERROR (Status)) {\r
304 break;\r
305 }\r
306 }\r
307 }\r
c25ddd01
FT
308 //\r
309 // This card doesn't get initialized correctly.\r
310 //\r
311 if (Index == RoutineNum) {\r
312 Private->Slot[Slot].Initialized = FALSE;\r
313 }\r
48555339
FT
314\r
315 //\r
316 // Notify the upper layer the connect state change through ReinstallProtocolInterface.\r
317 //\r
318 gBS->ReinstallProtocolInterface (\r
319 Private->ControllerHandle,\r
320 &gEfiSdMmcPassThruProtocolGuid,\r
321 &Private->PassThru,\r
322 &Private->PassThru\r
323 );\r
324 }\r
325 }\r
326 }\r
327\r
328 return;\r
329}\r
330/**\r
331 Tests to see if this driver supports a given controller. If a child device is provided,\r
332 it further tests to see if this driver supports creating a handle for the specified child device.\r
333\r
334 This function checks to see if the driver specified by This supports the device specified by\r
335 ControllerHandle. Drivers will typically use the device path attached to\r
336 ControllerHandle and/or the services from the bus I/O abstraction attached to\r
337 ControllerHandle to determine if the driver supports ControllerHandle. This function\r
338 may be called many times during platform initialization. In order to reduce boot times, the tests\r
339 performed by this function must be very small, and take as little time as possible to execute. This\r
340 function must not change the state of any hardware devices, and this function must be aware that the\r
341 device specified by ControllerHandle may already be managed by the same driver or a\r
342 different driver. This function must match its calls to AllocatePages() with FreePages(),\r
343 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().\r
344 Since ControllerHandle may have been previously started by the same driver, if a protocol is\r
345 already in the opened state, then it must not be closed with CloseProtocol(). This is required\r
346 to guarantee the state of ControllerHandle is not modified by this function.\r
347\r
348 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
349 @param[in] ControllerHandle The handle of the controller to test. This handle\r
350 must support a protocol interface that supplies\r
351 an I/O abstraction to the driver.\r
352 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
353 parameter is ignored by device drivers, and is optional for bus\r
354 drivers. For bus drivers, if this parameter is not NULL, then\r
355 the bus driver must determine if the bus controller specified\r
356 by ControllerHandle and the child controller specified\r
357 by RemainingDevicePath are both supported by this\r
358 bus driver.\r
359\r
360 @retval EFI_SUCCESS The device specified by ControllerHandle and\r
361 RemainingDevicePath is supported by the driver specified by This.\r
362 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and\r
363 RemainingDevicePath is already being managed by the driver\r
364 specified by This.\r
365 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and\r
366 RemainingDevicePath is already being managed by a different\r
367 driver or an application that requires exclusive access.\r
368 Currently not implemented.\r
369 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and\r
370 RemainingDevicePath is not supported by the driver specified by This.\r
371**/\r
372EFI_STATUS\r
373EFIAPI\r
374SdMmcPciHcDriverBindingSupported (\r
375 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
376 IN EFI_HANDLE Controller,\r
377 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
378 )\r
379{\r
380 EFI_STATUS Status;\r
381 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;\r
382 EFI_PCI_IO_PROTOCOL *PciIo;\r
383 PCI_TYPE00 PciData;\r
384\r
385 PciIo = NULL;\r
386 ParentDevicePath = NULL;\r
387\r
388 //\r
389 // SdPciHcDxe is a device driver, and should ingore the\r
390 // "RemainingDevicePath" according to EFI spec.\r
391 //\r
392 Status = gBS->OpenProtocol (\r
393 Controller,\r
394 &gEfiDevicePathProtocolGuid,\r
395 (VOID *) &ParentDevicePath,\r
396 This->DriverBindingHandle,\r
397 Controller,\r
398 EFI_OPEN_PROTOCOL_BY_DRIVER\r
399 );\r
400 if (EFI_ERROR (Status)) {\r
401 //\r
402 // EFI_ALREADY_STARTED is also an error.\r
403 //\r
404 return Status;\r
405 }\r
406 //\r
407 // Close the protocol because we don't use it here.\r
408 //\r
409 gBS->CloseProtocol (\r
410 Controller,\r
411 &gEfiDevicePathProtocolGuid,\r
412 This->DriverBindingHandle,\r
413 Controller\r
414 );\r
415\r
416 //\r
417 // Now test the EfiPciIoProtocol.\r
418 //\r
419 Status = gBS->OpenProtocol (\r
420 Controller,\r
421 &gEfiPciIoProtocolGuid,\r
422 (VOID **) &PciIo,\r
423 This->DriverBindingHandle,\r
424 Controller,\r
425 EFI_OPEN_PROTOCOL_BY_DRIVER\r
426 );\r
427 if (EFI_ERROR (Status)) {\r
428 return Status;\r
429 }\r
430\r
431 //\r
432 // Now further check the PCI header: Base class (offset 0x08) and\r
433 // Sub Class (offset 0x05). This controller should be an SD/MMC PCI\r
434 // Host Controller.\r
435 //\r
436 Status = PciIo->Pci.Read (\r
437 PciIo,\r
438 EfiPciIoWidthUint8,\r
439 0,\r
440 sizeof (PciData),\r
441 &PciData\r
442 );\r
443 if (EFI_ERROR (Status)) {\r
444 gBS->CloseProtocol (\r
445 Controller,\r
446 &gEfiPciIoProtocolGuid,\r
447 This->DriverBindingHandle,\r
448 Controller\r
449 );\r
450 return EFI_UNSUPPORTED;\r
451 }\r
452 //\r
453 // Since we already got the PciData, we can close protocol to avoid to carry it\r
454 // on for multiple exit points.\r
455 //\r
456 gBS->CloseProtocol (\r
457 Controller,\r
458 &gEfiPciIoProtocolGuid,\r
459 This->DriverBindingHandle,\r
460 Controller\r
461 );\r
462\r
463 //\r
464 // Examine SD PCI Host Controller PCI Configuration table fields.\r
465 //\r
466 if ((PciData.Hdr.ClassCode[2] == PCI_CLASS_SYSTEM_PERIPHERAL) &&\r
467 (PciData.Hdr.ClassCode[1] == PCI_SUBCLASS_SD_HOST_CONTROLLER) &&\r
468 ((PciData.Hdr.ClassCode[0] == 0x00) || (PciData.Hdr.ClassCode[0] == 0x01))) {\r
469 return EFI_SUCCESS;\r
470 }\r
471\r
472 return EFI_UNSUPPORTED;\r
473}\r
474\r
475/**\r
476 Starts a device controller or a bus controller.\r
477\r
478 The Start() function is designed to be invoked from the EFI boot service ConnectController().\r
479 As a result, much of the error checking on the parameters to Start() has been moved into this\r
480 common boot service. It is legal to call Start() from other locations,\r
481 but the following calling restrictions must be followed or the system behavior will not be deterministic.\r
482 1. ControllerHandle must be a valid EFI_HANDLE.\r
483 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned\r
484 EFI_DEVICE_PATH_PROTOCOL.\r
485 3. Prior to calling Start(), the Supported() function for the driver specified by This must\r
486 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.\r
487\r
488 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
489 @param[in] ControllerHandle The handle of the controller to start. This handle\r
490 must support a protocol interface that supplies\r
491 an I/O abstraction to the driver.\r
492 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
493 parameter is ignored by device drivers, and is optional for bus\r
494 drivers. For a bus driver, if this parameter is NULL, then handles\r
495 for all the children of Controller are created by this driver.\r
496 If this parameter is not NULL and the first Device Path Node is\r
497 not the End of Device Path Node, then only the handle for the\r
498 child device specified by the first Device Path Node of\r
499 RemainingDevicePath is created by this driver.\r
500 If the first Device Path Node of RemainingDevicePath is\r
501 the End of Device Path Node, no child handle is created by this\r
502 driver.\r
503\r
504 @retval EFI_SUCCESS The device was started.\r
505 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.\r
506 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
507 @retval Others The driver failded to start the device.\r
508\r
509**/\r
510EFI_STATUS\r
511EFIAPI\r
512SdMmcPciHcDriverBindingStart (\r
513 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
514 IN EFI_HANDLE Controller,\r
515 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
516 )\r
517{\r
518 EFI_STATUS Status;\r
519 SD_MMC_HC_PRIVATE_DATA *Private;\r
520 EFI_PCI_IO_PROTOCOL *PciIo;\r
521 UINT64 Supports;\r
522 UINT64 PciAttributes;\r
523 UINT8 SlotNum;\r
524 UINT8 FirstBar;\r
525 UINT8 Slot;\r
526 UINT8 Index;\r
527 CARD_TYPE_DETECT_ROUTINE *Routine;\r
528 UINT32 RoutineNum;\r
529 BOOLEAN MediaPresent;\r
df0a0e4b 530 BOOLEAN Support64BitDma;\r
48555339 531\r
e27ccaba 532 DEBUG ((DEBUG_INFO, "SdMmcPciHcDriverBindingStart: Start\n"));\r
48555339
FT
533\r
534 //\r
535 // Open PCI I/O Protocol and save pointer to open protocol\r
536 // in private data area.\r
537 //\r
538 PciIo = NULL;\r
539 Status = gBS->OpenProtocol (\r
540 Controller,\r
541 &gEfiPciIoProtocolGuid,\r
542 (VOID **) &PciIo,\r
543 This->DriverBindingHandle,\r
544 Controller,\r
545 EFI_OPEN_PROTOCOL_BY_DRIVER\r
546 );\r
547 if (EFI_ERROR (Status)) {\r
548 return Status;\r
549 }\r
550\r
551 //\r
552 // Enable the SD Host Controller MMIO space\r
553 //\r
554 Private = NULL;\r
555 Status = PciIo->Attributes (\r
556 PciIo,\r
557 EfiPciIoAttributeOperationGet,\r
558 0,\r
559 &PciAttributes\r
560 );\r
561\r
562 if (EFI_ERROR (Status)) {\r
563 goto Done;\r
564 }\r
565\r
566 Status = PciIo->Attributes (\r
567 PciIo,\r
568 EfiPciIoAttributeOperationSupported,\r
569 0,\r
570 &Supports\r
571 );\r
572\r
573 if (!EFI_ERROR (Status)) {\r
574 Supports &= (UINT64)EFI_PCI_DEVICE_ENABLE;\r
575 Status = PciIo->Attributes (\r
576 PciIo,\r
577 EfiPciIoAttributeOperationEnable,\r
578 Supports,\r
579 NULL\r
580 );\r
581 } else {\r
582 goto Done;\r
583 }\r
584\r
585 Private = AllocateCopyPool (sizeof (SD_MMC_HC_PRIVATE_DATA), &gSdMmcPciHcTemplate);\r
586 if (Private == NULL) {\r
587 Status = EFI_OUT_OF_RESOURCES;\r
588 goto Done;\r
589 }\r
590\r
591 Private->ControllerHandle = Controller;\r
592 Private->PciIo = PciIo;\r
593 Private->PciAttributes = PciAttributes;\r
594 InitializeListHead (&Private->Queue);\r
595\r
596 //\r
597 // Get SD/MMC Pci Host Controller Slot info\r
598 //\r
599 Status = SdMmcHcGetSlotInfo (PciIo, &FirstBar, &SlotNum);\r
600 if (EFI_ERROR (Status)) {\r
601 goto Done;\r
602 }\r
603\r
df0a0e4b 604 Support64BitDma = TRUE;\r
48555339
FT
605 for (Slot = FirstBar; Slot < (FirstBar + SlotNum); Slot++) {\r
606 Private->Slot[Slot].Enable = TRUE;\r
607\r
608 Status = SdMmcHcGetCapability (PciIo, Slot, &Private->Capability[Slot]);\r
609 if (EFI_ERROR (Status)) {\r
610 continue;\r
611 }\r
612 DumpCapabilityReg (Slot, &Private->Capability[Slot]);\r
613\r
df0a0e4b
AB
614 Support64BitDma &= Private->Capability[Slot].SysBus64;\r
615\r
48555339
FT
616 Status = SdMmcHcGetMaxCurrent (PciIo, Slot, &Private->MaxCurrent[Slot]);\r
617 if (EFI_ERROR (Status)) {\r
618 continue;\r
619 }\r
620\r
621 Private->Slot[Slot].SlotType = Private->Capability[Slot].SlotType;\r
622 if ((Private->Slot[Slot].SlotType != RemovableSlot) && (Private->Slot[Slot].SlotType != EmbeddedSlot)) {\r
e27ccaba 623 DEBUG ((DEBUG_INFO, "SdMmcPciHcDxe doesn't support the slot type [%d]!!!\n", Private->Slot[Slot].SlotType));\r
48555339
FT
624 continue;\r
625 }\r
626\r
627 //\r
628 // Reset the specified slot of the SD/MMC Pci Host Controller\r
629 //\r
630 Status = SdMmcHcReset (PciIo, Slot);\r
631 if (EFI_ERROR (Status)) {\r
632 continue;\r
633 }\r
634 //\r
635 // Check whether there is a SD/MMC card attached\r
636 //\r
637 Status = SdMmcHcCardDetect (PciIo, Slot, &MediaPresent);\r
2e9107b8
FT
638 if (EFI_ERROR (Status) && (Status != EFI_MEDIA_CHANGED)) {\r
639 continue;\r
e26575f7 640 } else if (!MediaPresent) {\r
12547a99 641 DEBUG ((DEBUG_INFO, "SdMmcHcCardDetect: No device attached in Slot[%d]!!!\n", Slot));\r
48555339
FT
642 continue;\r
643 }\r
644\r
645 Status = SdMmcHcInitHost (PciIo, Slot, Private->Capability[Slot]);\r
646 if (EFI_ERROR (Status)) {\r
647 continue;\r
648 }\r
649\r
650 Private->Slot[Slot].MediaPresent = TRUE;\r
c25ddd01 651 Private->Slot[Slot].Initialized = TRUE;\r
48555339
FT
652 RoutineNum = sizeof (mCardTypeDetectRoutineTable) / sizeof (CARD_TYPE_DETECT_ROUTINE);\r
653 for (Index = 0; Index < RoutineNum; Index++) {\r
654 Routine = &mCardTypeDetectRoutineTable[Index];\r
655 if (*Routine != NULL) {\r
656 Status = (*Routine) (Private, Slot);\r
657 if (!EFI_ERROR (Status)) {\r
658 break;\r
659 }\r
660 }\r
661 }\r
c25ddd01
FT
662 //\r
663 // This card doesn't get initialized correctly.\r
664 //\r
665 if (Index == RoutineNum) {\r
666 Private->Slot[Slot].Initialized = FALSE;\r
667 }\r
48555339
FT
668 }\r
669\r
df0a0e4b
AB
670 //\r
671 // Enable 64-bit DMA support in the PCI layer if this controller\r
672 // supports it.\r
673 //\r
674 if (Support64BitDma) {\r
675 Status = PciIo->Attributes (\r
676 PciIo,\r
677 EfiPciIoAttributeOperationEnable,\r
678 EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE,\r
679 NULL\r
680 );\r
681 if (EFI_ERROR (Status)) {\r
e27ccaba 682 DEBUG ((DEBUG_WARN, "SdMmcPciHcDriverBindingStart: failed to enable 64-bit DMA (%r)\n", Status));\r
df0a0e4b
AB
683 }\r
684 }\r
685\r
48555339
FT
686 //\r
687 // Start the asynchronous I/O monitor\r
688 //\r
689 Status = gBS->CreateEvent (\r
690 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
3b1d8241 691 TPL_NOTIFY,\r
48555339
FT
692 ProcessAsyncTaskList,\r
693 Private,\r
694 &Private->TimerEvent\r
695 );\r
696 if (EFI_ERROR (Status)) {\r
697 goto Done;\r
698 }\r
699\r
700 Status = gBS->SetTimer (Private->TimerEvent, TimerPeriodic, SD_MMC_HC_ASYNC_TIMER);\r
701 if (EFI_ERROR (Status)) {\r
702 goto Done;\r
703 }\r
704\r
705 //\r
706 // Start the Sd removable device connection enumeration\r
707 //\r
708 Status = gBS->CreateEvent (\r
709 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
710 TPL_CALLBACK,\r
711 SdMmcPciHcEnumerateDevice,\r
712 Private,\r
713 &Private->ConnectEvent\r
714 );\r
715 if (EFI_ERROR (Status)) {\r
716 goto Done;\r
717 }\r
718\r
719 Status = gBS->SetTimer (Private->ConnectEvent, TimerPeriodic, SD_MMC_HC_ENUM_TIMER);\r
720 if (EFI_ERROR (Status)) {\r
721 goto Done;\r
722 }\r
723\r
724 Status = gBS->InstallMultipleProtocolInterfaces (\r
725 &Controller,\r
726 &gEfiSdMmcPassThruProtocolGuid,\r
727 &(Private->PassThru),\r
728 NULL\r
729 );\r
730\r
e27ccaba 731 DEBUG ((DEBUG_INFO, "SdMmcPciHcDriverBindingStart: %r End on %x\n", Status, Controller));\r
48555339
FT
732\r
733Done:\r
734 if (EFI_ERROR (Status)) {\r
735 if ((Private != NULL) && (Private->PciAttributes != 0)) {\r
736 //\r
737 // Restore original PCI attributes\r
738 //\r
739 PciIo->Attributes (\r
740 PciIo,\r
741 EfiPciIoAttributeOperationSet,\r
742 Private->PciAttributes,\r
743 NULL\r
744 );\r
745 }\r
746 gBS->CloseProtocol (\r
747 Controller,\r
748 &gEfiPciIoProtocolGuid,\r
749 This->DriverBindingHandle,\r
750 Controller\r
751 );\r
752\r
753 if ((Private != NULL) && (Private->TimerEvent != NULL)) {\r
754 gBS->CloseEvent (Private->TimerEvent);\r
755 }\r
756\r
757 if ((Private != NULL) && (Private->ConnectEvent != NULL)) {\r
758 gBS->CloseEvent (Private->ConnectEvent);\r
759 }\r
760\r
761 if (Private != NULL) {\r
762 FreePool (Private);\r
763 }\r
764 }\r
765\r
766 return Status;\r
767}\r
768\r
769/**\r
770 Stops a device controller or a bus controller.\r
771\r
772 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().\r
773 As a result, much of the error checking on the parameters to Stop() has been moved\r
774 into this common boot service. It is legal to call Stop() from other locations,\r
775 but the following calling restrictions must be followed or the system behavior will not be deterministic.\r
776 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this\r
777 same driver's Start() function.\r
778 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid\r
779 EFI_HANDLE. In addition, all of these handles must have been created in this driver's\r
780 Start() function, and the Start() function must have called OpenProtocol() on\r
781 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.\r
782\r
783 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
784 @param[in] ControllerHandle A handle to the device being stopped. The handle must\r
785 support a bus specific I/O protocol for the driver\r
786 to use to stop the device.\r
787 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.\r
788 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL\r
789 if NumberOfChildren is 0.\r
790\r
791 @retval EFI_SUCCESS The device was stopped.\r
792 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.\r
793\r
794**/\r
795EFI_STATUS\r
796EFIAPI\r
797SdMmcPciHcDriverBindingStop (\r
798 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
799 IN EFI_HANDLE Controller,\r
800 IN UINTN NumberOfChildren,\r
801 IN EFI_HANDLE *ChildHandleBuffer\r
802 )\r
803{\r
804 EFI_STATUS Status;\r
805 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;\r
806 SD_MMC_HC_PRIVATE_DATA *Private;\r
807 EFI_PCI_IO_PROTOCOL *PciIo;\r
808 LIST_ENTRY *Link;\r
809 LIST_ENTRY *NextLink;\r
810 SD_MMC_HC_TRB *Trb;\r
811\r
e27ccaba 812 DEBUG ((DEBUG_INFO, "SdMmcPciHcDriverBindingStop: Start\n"));\r
48555339
FT
813\r
814 Status = gBS->OpenProtocol (\r
815 Controller,\r
816 &gEfiSdMmcPassThruProtocolGuid,\r
817 (VOID**) &PassThru,\r
818 This->DriverBindingHandle,\r
819 Controller,\r
820 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
821 );\r
822 if (EFI_ERROR (Status)) {\r
823 return Status;\r
824 }\r
825\r
826 Private = SD_MMC_HC_PRIVATE_FROM_THIS (PassThru);\r
827 //\r
828 // Close Non-Blocking timer and free Task list.\r
829 //\r
830 if (Private->TimerEvent != NULL) {\r
831 gBS->CloseEvent (Private->TimerEvent);\r
832 Private->TimerEvent = NULL;\r
833 }\r
834 if (Private->ConnectEvent != NULL) {\r
835 gBS->CloseEvent (Private->ConnectEvent);\r
836 Private->ConnectEvent = NULL;\r
837 }\r
838 //\r
839 // As the timer is closed, there is no needs to use TPL lock to\r
840 // protect the critical region "queue".\r
841 //\r
842 for (Link = GetFirstNode (&Private->Queue);\r
843 !IsNull (&Private->Queue, Link);\r
844 Link = NextLink) {\r
845 NextLink = GetNextNode (&Private->Queue, Link);\r
846 RemoveEntryList (Link);\r
847 Trb = SD_MMC_HC_TRB_FROM_THIS (Link);\r
848 Trb->Packet->TransactionStatus = EFI_ABORTED;\r
849 gBS->SignalEvent (Trb->Event);\r
850 SdMmcFreeTrb (Trb);\r
851 }\r
852\r
853 //\r
854 // Uninstall Block I/O protocol from the device handle\r
855 //\r
856 Status = gBS->UninstallProtocolInterface (\r
857 Controller,\r
858 &gEfiSdMmcPassThruProtocolGuid,\r
859 &(Private->PassThru)\r
860 );\r
861\r
862 if (EFI_ERROR (Status)) {\r
863 return Status;\r
864 }\r
865\r
866 gBS->CloseProtocol (\r
867 Controller,\r
868 &gEfiPciIoProtocolGuid,\r
869 This->DriverBindingHandle,\r
870 Controller\r
871 );\r
872 //\r
873 // Restore original PCI attributes\r
874 //\r
875 PciIo = Private->PciIo;\r
876 Status = PciIo->Attributes (\r
877 PciIo,\r
878 EfiPciIoAttributeOperationSet,\r
879 Private->PciAttributes,\r
880 NULL\r
881 );\r
882 ASSERT_EFI_ERROR (Status);\r
883\r
884 FreePool (Private);\r
885\r
e27ccaba 886 DEBUG ((DEBUG_INFO, "SdMmcPciHcDriverBindingStop: End with %r\n", Status));\r
48555339
FT
887\r
888 return Status;\r
889}\r
890\r
891/**\r
892 Sends SD command to an SD card that is attached to the SD controller.\r
893\r
894 The PassThru() function sends the SD command specified by Packet to the SD card\r
895 specified by Slot.\r
896\r
897 If Packet is successfully sent to the SD card, then EFI_SUCCESS is returned.\r
898\r
899 If a device error occurs while sending the Packet, then EFI_DEVICE_ERROR is returned.\r
900\r
901 If Slot is not in a valid range for the SD controller, then EFI_INVALID_PARAMETER\r
902 is returned.\r
903\r
904 If Packet defines a data command but both InDataBuffer and OutDataBuffer are NULL,\r
905 EFI_INVALID_PARAMETER is returned.\r
906\r
907 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.\r
908 @param[in] Slot The slot number of the SD card to send the command to.\r
909 @param[in,out] Packet A pointer to the SD command data structure.\r
910 @param[in] Event If Event is NULL, blocking I/O is performed. If Event is\r
911 not NULL, then nonblocking I/O is performed, and Event\r
912 will be signaled when the Packet completes.\r
913\r
914 @retval EFI_SUCCESS The SD Command Packet was sent by the host.\r
915 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SD\r
916 command Packet.\r
917 @retval EFI_INVALID_PARAMETER Packet, Slot, or the contents of the Packet is invalid.\r
918 @retval EFI_INVALID_PARAMETER Packet defines a data command but both InDataBuffer and\r
919 OutDataBuffer are NULL.\r
920 @retval EFI_NO_MEDIA SD Device not present in the Slot.\r
921 @retval EFI_UNSUPPORTED The command described by the SD Command Packet is not\r
922 supported by the host controller.\r
923 @retval EFI_BAD_BUFFER_SIZE The InTransferLength or OutTransferLength exceeds the\r
924 limit supported by SD card ( i.e. if the number of bytes\r
925 exceed the Last LBA).\r
926\r
927**/\r
928EFI_STATUS\r
929EFIAPI\r
930SdMmcPassThruPassThru (\r
931 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *This,\r
932 IN UINT8 Slot,\r
933 IN OUT EFI_SD_MMC_PASS_THRU_COMMAND_PACKET *Packet,\r
934 IN EFI_EVENT Event OPTIONAL\r
935 )\r
936{\r
937 EFI_STATUS Status;\r
938 SD_MMC_HC_PRIVATE_DATA *Private;\r
939 SD_MMC_HC_TRB *Trb;\r
940 EFI_TPL OldTpl;\r
941\r
942 if ((This == NULL) || (Packet == NULL)) {\r
943 return EFI_INVALID_PARAMETER;\r
944 }\r
945\r
946 if ((Packet->SdMmcCmdBlk == NULL) || (Packet->SdMmcStatusBlk == NULL)) {\r
947 return EFI_INVALID_PARAMETER;\r
948 }\r
949\r
950 if ((Packet->OutDataBuffer == NULL) && (Packet->OutTransferLength != 0)) {\r
951 return EFI_INVALID_PARAMETER;\r
952 }\r
953\r
954 if ((Packet->InDataBuffer == NULL) && (Packet->InTransferLength != 0)) {\r
955 return EFI_INVALID_PARAMETER;\r
956 }\r
957\r
958 Private = SD_MMC_HC_PRIVATE_FROM_THIS (This);\r
959\r
960 if (!Private->Slot[Slot].Enable) {\r
961 return EFI_INVALID_PARAMETER;\r
962 }\r
963\r
964 if (!Private->Slot[Slot].MediaPresent) {\r
965 return EFI_NO_MEDIA;\r
966 }\r
967\r
c25ddd01
FT
968 if (!Private->Slot[Slot].Initialized) {\r
969 return EFI_DEVICE_ERROR;\r
970 }\r
971\r
48555339
FT
972 Trb = SdMmcCreateTrb (Private, Slot, Packet, Event);\r
973 if (Trb == NULL) {\r
974 return EFI_OUT_OF_RESOURCES;\r
975 }\r
976 //\r
977 // Immediately return for async I/O.\r
978 //\r
979 if (Event != NULL) {\r
980 return EFI_SUCCESS;\r
981 }\r
982\r
983 //\r
984 // Wait async I/O list is empty before execute sync I/O operation.\r
985 //\r
986 while (TRUE) {\r
3b1d8241 987 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
48555339
FT
988 if (IsListEmpty (&Private->Queue)) {\r
989 gBS->RestoreTPL (OldTpl);\r
990 break;\r
991 }\r
992 gBS->RestoreTPL (OldTpl);\r
993 }\r
994\r
995 Status = SdMmcWaitTrbEnv (Private, Trb);\r
996 if (EFI_ERROR (Status)) {\r
997 goto Done;\r
998 }\r
999\r
1000 Status = SdMmcExecTrb (Private, Trb);\r
1001 if (EFI_ERROR (Status)) {\r
1002 goto Done;\r
1003 }\r
1004\r
1005 Status = SdMmcWaitTrbResult (Private, Trb);\r
1006 if (EFI_ERROR (Status)) {\r
1007 goto Done;\r
1008 }\r
1009\r
1010Done:\r
1011 if ((Trb != NULL) && (Trb->AdmaDesc != NULL)) {\r
1012 FreePages (Trb->AdmaDesc, Trb->AdmaPages);\r
1013 }\r
1014\r
1015 if (Trb != NULL) {\r
1016 FreePool (Trb);\r
1017 }\r
1018\r
1019 return Status;\r
1020}\r
1021\r
1022/**\r
1023 Used to retrieve next slot numbers supported by the SD controller. The function\r
1024 returns information about all available slots (populated or not-populated).\r
1025\r
1026 The GetNextSlot() function retrieves the next slot number on an SD controller.\r
1027 If on input Slot is 0xFF, then the slot number of the first slot on the SD controller\r
1028 is returned.\r
1029\r
1030 If Slot is a slot number that was returned on a previous call to GetNextSlot(), then\r
1031 the slot number of the next slot on the SD controller is returned.\r
1032\r
1033 If Slot is not 0xFF and Slot was not returned on a previous call to GetNextSlot(),\r
1034 EFI_INVALID_PARAMETER is returned.\r
1035\r
1036 If Slot is the slot number of the last slot on the SD controller, then EFI_NOT_FOUND\r
1037 is returned.\r
1038\r
1039 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.\r
1040 @param[in,out] Slot On input, a pointer to a slot number on the SD controller.\r
1041 On output, a pointer to the next slot number on the SD controller.\r
1042 An input value of 0xFF retrieves the first slot number on the SD\r
1043 controller.\r
1044\r
1045 @retval EFI_SUCCESS The next slot number on the SD controller was returned in Slot.\r
1046 @retval EFI_NOT_FOUND There are no more slots on this SD controller.\r
1047 @retval EFI_INVALID_PARAMETER Slot is not 0xFF and Slot was not returned on a previous call\r
1048 to GetNextSlot().\r
1049\r
1050**/\r
1051EFI_STATUS\r
1052EFIAPI\r
1053SdMmcPassThruGetNextSlot (\r
1054 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *This,\r
1055 IN OUT UINT8 *Slot\r
1056 )\r
1057{\r
1058 SD_MMC_HC_PRIVATE_DATA *Private;\r
1059 UINT8 Index;\r
1060\r
1061 if ((This == NULL) || (Slot == NULL)) {\r
1062 return EFI_INVALID_PARAMETER;\r
1063 }\r
1064\r
1065 Private = SD_MMC_HC_PRIVATE_FROM_THIS (This);\r
1066\r
1067 if (*Slot == 0xFF) {\r
1068 for (Index = 0; Index < SD_MMC_HC_MAX_SLOT; Index++) {\r
1069 if (Private->Slot[Index].Enable) {\r
1070 *Slot = Index;\r
1071 Private->PreviousSlot = Index;\r
1072 return EFI_SUCCESS;\r
1073 }\r
1074 }\r
1075 return EFI_NOT_FOUND;\r
1076 } else if (*Slot == Private->PreviousSlot) {\r
1077 for (Index = *Slot + 1; Index < SD_MMC_HC_MAX_SLOT; Index++) {\r
1078 if (Private->Slot[Index].Enable) {\r
1079 *Slot = Index;\r
1080 Private->PreviousSlot = Index;\r
1081 return EFI_SUCCESS;\r
1082 }\r
1083 }\r
1084 return EFI_NOT_FOUND;\r
1085 } else {\r
1086 return EFI_INVALID_PARAMETER;\r
1087 }\r
1088}\r
1089\r
1090/**\r
1091 Used to allocate and build a device path node for an SD card on the SD controller.\r
1092\r
1093 The BuildDevicePath() function allocates and builds a single device node for the SD\r
1094 card specified by Slot.\r
1095\r
1096 If the SD card specified by Slot is not present on the SD controller, then EFI_NOT_FOUND\r
1097 is returned.\r
1098\r
1099 If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned.\r
1100\r
1101 If there are not enough resources to allocate the device path node, then EFI_OUT_OF_RESOURCES\r
1102 is returned.\r
1103\r
1104 Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of\r
1105 DevicePath are initialized to describe the SD card specified by Slot, and EFI_SUCCESS is\r
1106 returned.\r
1107\r
1108 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.\r
1109 @param[in] Slot Specifies the slot number of the SD card for which a device\r
1110 path node is to be allocated and built.\r
1111 @param[in,out] DevicePath A pointer to a single device path node that describes the SD\r
1112 card specified by Slot. This function is responsible for\r
1113 allocating the buffer DevicePath with the boot service\r
1114 AllocatePool(). It is the caller's responsibility to free\r
1115 DevicePath when the caller is finished with DevicePath.\r
1116\r
1117 @retval EFI_SUCCESS The device path node that describes the SD card specified by\r
1118 Slot was allocated and returned in DevicePath.\r
1119 @retval EFI_NOT_FOUND The SD card specified by Slot does not exist on the SD controller.\r
1120 @retval EFI_INVALID_PARAMETER DevicePath is NULL.\r
1121 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.\r
1122\r
1123**/\r
1124EFI_STATUS\r
1125EFIAPI\r
1126SdMmcPassThruBuildDevicePath (\r
1127 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *This,\r
1128 IN UINT8 Slot,\r
1129 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath\r
1130 )\r
1131{\r
1132 SD_MMC_HC_PRIVATE_DATA *Private;\r
1133 SD_DEVICE_PATH *SdNode;\r
1134 EMMC_DEVICE_PATH *EmmcNode;\r
1135\r
1136 if ((This == NULL) || (DevicePath == NULL) || (Slot >= SD_MMC_HC_MAX_SLOT)) {\r
1137 return EFI_INVALID_PARAMETER;\r
1138 }\r
1139\r
1140 Private = SD_MMC_HC_PRIVATE_FROM_THIS (This);\r
1141\r
1142 if ((!Private->Slot[Slot].Enable) || (!Private->Slot[Slot].MediaPresent)) {\r
1143 return EFI_NOT_FOUND;\r
1144 }\r
1145\r
1146 if (Private->Slot[Slot].CardType == SdCardType) {\r
1147 SdNode = AllocateCopyPool (sizeof (SD_DEVICE_PATH), &mSdDpTemplate);\r
1148 if (SdNode == NULL) {\r
1149 return EFI_OUT_OF_RESOURCES;\r
1150 }\r
1151 SdNode->SlotNumber = Slot;\r
1152\r
1153 *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) SdNode;\r
1154 } else if (Private->Slot[Slot].CardType == EmmcCardType) {\r
1155 EmmcNode = AllocateCopyPool (sizeof (EMMC_DEVICE_PATH), &mEmmcDpTemplate);\r
1156 if (EmmcNode == NULL) {\r
1157 return EFI_OUT_OF_RESOURCES;\r
1158 }\r
1159 EmmcNode->SlotNumber = Slot;\r
1160\r
1161 *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) EmmcNode;\r
1162 } else {\r
1163 //\r
1164 // Currently we only support SD and EMMC two device nodes.\r
1165 //\r
1166 return EFI_NOT_FOUND;\r
1167 }\r
1168\r
1169 return EFI_SUCCESS;\r
1170}\r
1171\r
1172/**\r
1173 This function retrieves an SD card slot number based on the input device path.\r
1174\r
1175 The GetSlotNumber() function retrieves slot number for the SD card specified by\r
1176 the DevicePath node. If DevicePath is NULL, EFI_INVALID_PARAMETER is returned.\r
1177\r
1178 If DevicePath is not a device path node type that the SD Pass Thru driver supports,\r
1179 EFI_UNSUPPORTED is returned.\r
1180\r
1181 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.\r
1182 @param[in] DevicePath A pointer to the device path node that describes a SD\r
1183 card on the SD controller.\r
1184 @param[out] Slot On return, points to the slot number of an SD card on\r
1185 the SD controller.\r
1186\r
1187 @retval EFI_SUCCESS SD card slot number is returned in Slot.\r
1188 @retval EFI_INVALID_PARAMETER Slot or DevicePath is NULL.\r
1189 @retval EFI_UNSUPPORTED DevicePath is not a device path node type that the SD\r
1190 Pass Thru driver supports.\r
1191\r
1192**/\r
1193EFI_STATUS\r
1194EFIAPI\r
1195SdMmcPassThruGetSlotNumber (\r
1196 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *This,\r
1197 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
1198 OUT UINT8 *Slot\r
1199 )\r
1200{\r
1201 SD_MMC_HC_PRIVATE_DATA *Private;\r
1202 SD_DEVICE_PATH *SdNode;\r
1203 EMMC_DEVICE_PATH *EmmcNode;\r
1204 UINT8 SlotNumber;\r
1205\r
1206 if ((This == NULL) || (DevicePath == NULL) || (Slot == NULL)) {\r
1207 return EFI_INVALID_PARAMETER;\r
1208 }\r
1209\r
1210 Private = SD_MMC_HC_PRIVATE_FROM_THIS (This);\r
1211\r
1212 //\r
1213 // Check whether the DevicePath belongs to SD_DEVICE_PATH or EMMC_DEVICE_PATH\r
1214 //\r
1215 if ((DevicePath->Type != MESSAGING_DEVICE_PATH) ||\r
1216 ((DevicePath->SubType != MSG_SD_DP) &&\r
1217 (DevicePath->SubType != MSG_EMMC_DP)) ||\r
1218 (DevicePathNodeLength(DevicePath) != sizeof(SD_DEVICE_PATH)) ||\r
1219 (DevicePathNodeLength(DevicePath) != sizeof(EMMC_DEVICE_PATH))) {\r
1220 return EFI_UNSUPPORTED;\r
1221 }\r
1222\r
1223 if (DevicePath->SubType == MSG_SD_DP) {\r
1224 SdNode = (SD_DEVICE_PATH *) DevicePath;\r
1225 SlotNumber = SdNode->SlotNumber;\r
1226 } else {\r
1227 EmmcNode = (EMMC_DEVICE_PATH *) DevicePath;\r
1228 SlotNumber = EmmcNode->SlotNumber;\r
1229 }\r
1230\r
1231 if (SlotNumber >= SD_MMC_HC_MAX_SLOT) {\r
1232 return EFI_NOT_FOUND;\r
1233 }\r
1234\r
1235 if (Private->Slot[SlotNumber].Enable) {\r
1236 *Slot = SlotNumber;\r
1237 return EFI_SUCCESS;\r
1238 } else {\r
1239 return EFI_NOT_FOUND;\r
1240 }\r
1241}\r
1242\r
1243/**\r
1244 Resets an SD card that is connected to the SD controller.\r
1245\r
1246 The ResetDevice() function resets the SD card specified by Slot.\r
1247\r
1248 If this SD controller does not support a device reset operation, EFI_UNSUPPORTED is\r
1249 returned.\r
1250\r
1251 If Slot is not in a valid slot number for this SD controller, EFI_INVALID_PARAMETER\r
1252 is returned.\r
1253\r
1254 If the device reset operation is completed, EFI_SUCCESS is returned.\r
1255\r
1256 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.\r
1257 @param[in] Slot Specifies the slot number of the SD card to be reset.\r
1258\r
1259 @retval EFI_SUCCESS The SD card specified by Slot was reset.\r
1260 @retval EFI_UNSUPPORTED The SD controller does not support a device reset operation.\r
1261 @retval EFI_INVALID_PARAMETER Slot number is invalid.\r
1262 @retval EFI_NO_MEDIA SD Device not present in the Slot.\r
1263 @retval EFI_DEVICE_ERROR The reset command failed due to a device error\r
1264\r
1265**/\r
1266EFI_STATUS\r
1267EFIAPI\r
1268SdMmcPassThruResetDevice (\r
1269 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *This,\r
1270 IN UINT8 Slot\r
1271 )\r
1272{\r
1273 SD_MMC_HC_PRIVATE_DATA *Private;\r
1274 LIST_ENTRY *Link;\r
1275 LIST_ENTRY *NextLink;\r
1276 SD_MMC_HC_TRB *Trb;\r
1277 EFI_TPL OldTpl;\r
1278\r
1279 if (This == NULL) {\r
1280 return EFI_INVALID_PARAMETER;\r
1281 }\r
1282\r
1283 Private = SD_MMC_HC_PRIVATE_FROM_THIS (This);\r
1284\r
1285 if (!Private->Slot[Slot].Enable) {\r
1286 return EFI_INVALID_PARAMETER;\r
1287 }\r
1288\r
c25ddd01
FT
1289 if (!Private->Slot[Slot].MediaPresent) {\r
1290 return EFI_NO_MEDIA;\r
1291 }\r
1292\r
1293 if (!Private->Slot[Slot].Initialized) {\r
1294 return EFI_DEVICE_ERROR;\r
1295 }\r
48555339
FT
1296 //\r
1297 // Free all async I/O requests in the queue\r
1298 //\r
3b1d8241 1299 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
48555339
FT
1300\r
1301 for (Link = GetFirstNode (&Private->Queue);\r
1302 !IsNull (&Private->Queue, Link);\r
1303 Link = NextLink) {\r
1304 NextLink = GetNextNode (&Private->Queue, Link);\r
1305 RemoveEntryList (Link);\r
1306 Trb = SD_MMC_HC_TRB_FROM_THIS (Link);\r
1307 Trb->Packet->TransactionStatus = EFI_ABORTED;\r
1308 gBS->SignalEvent (Trb->Event);\r
1309 SdMmcFreeTrb (Trb);\r
1310 }\r
1311\r
1312 gBS->RestoreTPL (OldTpl);\r
1313\r
1314 return EFI_SUCCESS;\r
1315}\r
1316\r