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