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.
5 It would expose EFI_SD_MMC_PASS_THRU_PROTOCOL for upper layer use.
7 Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include "SdMmcPciHcDxe.h"
21 // Driver Global Variables
23 EFI_DRIVER_BINDING_PROTOCOL gSdMmcPciHcDriverBinding
= {
24 SdMmcPciHcDriverBindingSupported
,
25 SdMmcPciHcDriverBindingStart
,
26 SdMmcPciHcDriverBindingStop
,
33 // Template for SD/MMC host controller private data.
35 SD_MMC_HC_PRIVATE_DATA gSdMmcPciHcTemplate
= {
36 SD_MMC_HC_PRIVATE_SIGNATURE
, // Signature
37 NULL
, // ControllerHandle
41 SdMmcPassThruPassThru
,
42 SdMmcPassThruGetNextSlot
,
43 SdMmcPassThruBuildDevicePath
,
44 SdMmcPassThruGetSlotNumber
,
45 SdMmcPassThruResetDevice
52 INITIALIZE_LIST_HEAD_VARIABLE (gSdMmcPciHcTemplate
.Queue
),
54 {0, UnknownSlot
, 0, 0}, {0, UnknownSlot
, 0, 0}, {0, UnknownSlot
, 0, 0},
55 {0, UnknownSlot
, 0, 0}, {0, UnknownSlot
, 0, 0}, {0, UnknownSlot
, 0, 0}
63 0 // ControllerVersion
66 SD_DEVICE_PATH mSdDpTemplate
= {
68 MESSAGING_DEVICE_PATH
,
71 (UINT8
) (sizeof (SD_DEVICE_PATH
)),
72 (UINT8
) ((sizeof (SD_DEVICE_PATH
)) >> 8)
78 EMMC_DEVICE_PATH mEmmcDpTemplate
= {
80 MESSAGING_DEVICE_PATH
,
83 (UINT8
) (sizeof (EMMC_DEVICE_PATH
)),
84 (UINT8
) ((sizeof (EMMC_DEVICE_PATH
)) >> 8)
91 // Prioritized function list to detect card type.
92 // User could add other card detection logic here.
94 CARD_TYPE_DETECT_ROUTINE mCardTypeDetectRoutineTable
[] = {
101 The entry point for SD host controller driver, used to install this driver on the ImageHandle.
103 @param[in] ImageHandle The firmware allocated handle for this driver image.
104 @param[in] SystemTable Pointer to the EFI system table.
106 @retval EFI_SUCCESS Driver loaded.
107 @retval other Driver not loaded.
112 InitializeSdMmcPciHcDxe (
113 IN EFI_HANDLE ImageHandle
,
114 IN EFI_SYSTEM_TABLE
*SystemTable
119 Status
= EfiLibInstallDriverBindingComponentName2 (
122 &gSdMmcPciHcDriverBinding
,
124 &gSdMmcPciHcComponentName
,
125 &gSdMmcPciHcComponentName2
127 ASSERT_EFI_ERROR (Status
);
133 Call back function when the timer event is signaled.
135 @param[in] Event The Event this notify function registered to.
136 @param[in] Context Pointer to the context data registered to the
142 ProcessAsyncTaskList (
147 SD_MMC_HC_PRIVATE_DATA
*Private
;
151 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
152 BOOLEAN InfiniteWait
;
155 Private
= (SD_MMC_HC_PRIVATE_DATA
*)Context
;
158 // Check if the first entry in the async I/O queue is done or not.
160 Status
= EFI_SUCCESS
;
162 Link
= GetFirstNode (&Private
->Queue
);
163 if (!IsNull (&Private
->Queue
, Link
)) {
164 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
165 if (Private
->Slot
[Trb
->Slot
].MediaPresent
== FALSE
) {
166 Status
= EFI_NO_MEDIA
;
171 // Check whether the cmd/data line is ready for transfer.
173 Status
= SdMmcCheckTrbEnv (Private
, Trb
);
174 if (!EFI_ERROR (Status
)) {
176 Status
= SdMmcExecTrb (Private
, Trb
);
177 if (EFI_ERROR (Status
)) {
184 Status
= SdMmcCheckTrbResult (Private
, Trb
);
188 if ((Trb
!= NULL
) && (Status
== EFI_NOT_READY
)) {
189 Packet
= Trb
->Packet
;
190 if (Packet
->Timeout
== 0) {
193 InfiniteWait
= FALSE
;
195 if ((!InfiniteWait
) && (Trb
->Timeout
-- == 0)) {
196 RemoveEntryList (Link
);
197 Trb
->Packet
->TransactionStatus
= EFI_TIMEOUT
;
198 TrbEvent
= Trb
->Event
;
200 DEBUG ((EFI_D_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p EFI_TIMEOUT\n", TrbEvent
));
201 gBS
->SignalEvent (TrbEvent
);
205 if ((Trb
!= NULL
) && (Status
!= EFI_NOT_READY
)) {
206 RemoveEntryList (Link
);
207 Trb
->Packet
->TransactionStatus
= Status
;
208 TrbEvent
= Trb
->Event
;
210 DEBUG ((EFI_D_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p with %r\n", TrbEvent
, Status
));
211 gBS
->SignalEvent (TrbEvent
);
217 Sd removable device enumeration callback function when the timer event is signaled.
219 @param[in] Event The Event this notify function registered to.
220 @param[in] Context Pointer to the context data registered to the
226 SdMmcPciHcEnumerateDevice (
231 SD_MMC_HC_PRIVATE_DATA
*Private
;
234 BOOLEAN MediaPresent
;
236 CARD_TYPE_DETECT_ROUTINE
*Routine
;
239 LIST_ENTRY
*NextLink
;
242 Private
= (SD_MMC_HC_PRIVATE_DATA
*)Context
;
244 for (Slot
= 0; Slot
< SD_MMC_HC_MAX_SLOT
; Slot
++) {
245 if ((Private
->Slot
[Slot
].Enable
) && (Private
->Slot
[Slot
].SlotType
== RemovableSlot
)) {
246 Status
= SdMmcHcCardDetect (Private
->PciIo
, Slot
, &MediaPresent
);
247 if ((Status
== EFI_MEDIA_CHANGED
) && (MediaPresent
== FALSE
)) {
248 DEBUG ((EFI_D_INFO
, "SdMmcPciHcEnumerateDevice: device disconnected at slot %d of pci %p\n", Slot
, Private
->PciIo
));
249 Private
->Slot
[Slot
].MediaPresent
= FALSE
;
251 // Signal all async task events at the slot with EFI_NO_MEDIA status.
253 for (Link
= GetFirstNode (&Private
->Queue
);
254 !IsNull (&Private
->Queue
, Link
);
256 NextLink
= GetNextNode (&Private
->Queue
, Link
);
257 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
258 if (Trb
->Slot
== Slot
) {
259 RemoveEntryList (Link
);
260 Trb
->Packet
->TransactionStatus
= EFI_NO_MEDIA
;
261 gBS
->SignalEvent (Trb
->Event
);
266 // Notify the upper layer the connect state change through ReinstallProtocolInterface.
268 gBS
->ReinstallProtocolInterface (
269 Private
->ControllerHandle
,
270 &gEfiSdMmcPassThruProtocolGuid
,
275 if ((Status
== EFI_MEDIA_CHANGED
) && (MediaPresent
== TRUE
)) {
276 DEBUG ((EFI_D_INFO
, "SdMmcPciHcEnumerateDevice: device connected at slot %d of pci %p\n", Slot
, Private
->PciIo
));
278 // Reinitialize slot and restart identification process for the new attached device
280 Status
= SdMmcHcInitHost (Private
->PciIo
, Slot
, Private
->Capability
[Slot
]);
281 if (EFI_ERROR (Status
)) {
285 Private
->Slot
[Slot
].MediaPresent
= TRUE
;
286 RoutineNum
= sizeof (mCardTypeDetectRoutineTable
) / sizeof (CARD_TYPE_DETECT_ROUTINE
);
287 for (Index
= 0; Index
< RoutineNum
; Index
++) {
288 Routine
= &mCardTypeDetectRoutineTable
[Index
];
289 if (*Routine
!= NULL
) {
290 Status
= (*Routine
) (Private
, Slot
);
291 if (!EFI_ERROR (Status
)) {
298 // Notify the upper layer the connect state change through ReinstallProtocolInterface.
300 gBS
->ReinstallProtocolInterface (
301 Private
->ControllerHandle
,
302 &gEfiSdMmcPassThruProtocolGuid
,
313 Tests to see if this driver supports a given controller. If a child device is provided,
314 it further tests to see if this driver supports creating a handle for the specified child device.
316 This function checks to see if the driver specified by This supports the device specified by
317 ControllerHandle. Drivers will typically use the device path attached to
318 ControllerHandle and/or the services from the bus I/O abstraction attached to
319 ControllerHandle to determine if the driver supports ControllerHandle. This function
320 may be called many times during platform initialization. In order to reduce boot times, the tests
321 performed by this function must be very small, and take as little time as possible to execute. This
322 function must not change the state of any hardware devices, and this function must be aware that the
323 device specified by ControllerHandle may already be managed by the same driver or a
324 different driver. This function must match its calls to AllocatePages() with FreePages(),
325 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
326 Since ControllerHandle may have been previously started by the same driver, if a protocol is
327 already in the opened state, then it must not be closed with CloseProtocol(). This is required
328 to guarantee the state of ControllerHandle is not modified by this function.
330 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
331 @param[in] ControllerHandle The handle of the controller to test. This handle
332 must support a protocol interface that supplies
333 an I/O abstraction to the driver.
334 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
335 parameter is ignored by device drivers, and is optional for bus
336 drivers. For bus drivers, if this parameter is not NULL, then
337 the bus driver must determine if the bus controller specified
338 by ControllerHandle and the child controller specified
339 by RemainingDevicePath are both supported by this
342 @retval EFI_SUCCESS The device specified by ControllerHandle and
343 RemainingDevicePath is supported by the driver specified by This.
344 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
345 RemainingDevicePath is already being managed by the driver
347 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
348 RemainingDevicePath is already being managed by a different
349 driver or an application that requires exclusive access.
350 Currently not implemented.
351 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
352 RemainingDevicePath is not supported by the driver specified by This.
356 SdMmcPciHcDriverBindingSupported (
357 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
358 IN EFI_HANDLE Controller
,
359 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
363 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
364 EFI_PCI_IO_PROTOCOL
*PciIo
;
368 ParentDevicePath
= NULL
;
371 // SdPciHcDxe is a device driver, and should ingore the
372 // "RemainingDevicePath" according to EFI spec.
374 Status
= gBS
->OpenProtocol (
376 &gEfiDevicePathProtocolGuid
,
377 (VOID
*) &ParentDevicePath
,
378 This
->DriverBindingHandle
,
380 EFI_OPEN_PROTOCOL_BY_DRIVER
382 if (EFI_ERROR (Status
)) {
384 // EFI_ALREADY_STARTED is also an error.
389 // Close the protocol because we don't use it here.
393 &gEfiDevicePathProtocolGuid
,
394 This
->DriverBindingHandle
,
399 // Now test the EfiPciIoProtocol.
401 Status
= gBS
->OpenProtocol (
403 &gEfiPciIoProtocolGuid
,
405 This
->DriverBindingHandle
,
407 EFI_OPEN_PROTOCOL_BY_DRIVER
409 if (EFI_ERROR (Status
)) {
414 // Now further check the PCI header: Base class (offset 0x08) and
415 // Sub Class (offset 0x05). This controller should be an SD/MMC PCI
418 Status
= PciIo
->Pci
.Read (
425 if (EFI_ERROR (Status
)) {
428 &gEfiPciIoProtocolGuid
,
429 This
->DriverBindingHandle
,
432 return EFI_UNSUPPORTED
;
435 // Since we already got the PciData, we can close protocol to avoid to carry it
436 // on for multiple exit points.
440 &gEfiPciIoProtocolGuid
,
441 This
->DriverBindingHandle
,
446 // Examine SD PCI Host Controller PCI Configuration table fields.
448 if ((PciData
.Hdr
.ClassCode
[2] == PCI_CLASS_SYSTEM_PERIPHERAL
) &&
449 (PciData
.Hdr
.ClassCode
[1] == PCI_SUBCLASS_SD_HOST_CONTROLLER
) &&
450 ((PciData
.Hdr
.ClassCode
[0] == 0x00) || (PciData
.Hdr
.ClassCode
[0] == 0x01))) {
454 return EFI_UNSUPPORTED
;
458 Starts a device controller or a bus controller.
460 The Start() function is designed to be invoked from the EFI boot service ConnectController().
461 As a result, much of the error checking on the parameters to Start() has been moved into this
462 common boot service. It is legal to call Start() from other locations,
463 but the following calling restrictions must be followed or the system behavior will not be deterministic.
464 1. ControllerHandle must be a valid EFI_HANDLE.
465 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
466 EFI_DEVICE_PATH_PROTOCOL.
467 3. Prior to calling Start(), the Supported() function for the driver specified by This must
468 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
470 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
471 @param[in] ControllerHandle The handle of the controller to start. This handle
472 must support a protocol interface that supplies
473 an I/O abstraction to the driver.
474 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
475 parameter is ignored by device drivers, and is optional for bus
476 drivers. For a bus driver, if this parameter is NULL, then handles
477 for all the children of Controller are created by this driver.
478 If this parameter is not NULL and the first Device Path Node is
479 not the End of Device Path Node, then only the handle for the
480 child device specified by the first Device Path Node of
481 RemainingDevicePath is created by this driver.
482 If the first Device Path Node of RemainingDevicePath is
483 the End of Device Path Node, no child handle is created by this
486 @retval EFI_SUCCESS The device was started.
487 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
488 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
489 @retval Others The driver failded to start the device.
494 SdMmcPciHcDriverBindingStart (
495 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
496 IN EFI_HANDLE Controller
,
497 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
501 SD_MMC_HC_PRIVATE_DATA
*Private
;
502 EFI_PCI_IO_PROTOCOL
*PciIo
;
504 UINT64 PciAttributes
;
509 CARD_TYPE_DETECT_ROUTINE
*Routine
;
511 BOOLEAN MediaPresent
;
513 DEBUG ((EFI_D_INFO
, "SdMmcPciHcDriverBindingStart: Start\n"));
516 // Open PCI I/O Protocol and save pointer to open protocol
517 // in private data area.
520 Status
= gBS
->OpenProtocol (
522 &gEfiPciIoProtocolGuid
,
524 This
->DriverBindingHandle
,
526 EFI_OPEN_PROTOCOL_BY_DRIVER
528 if (EFI_ERROR (Status
)) {
533 // Enable the SD Host Controller MMIO space
536 Status
= PciIo
->Attributes (
538 EfiPciIoAttributeOperationGet
,
543 if (EFI_ERROR (Status
)) {
547 Status
= PciIo
->Attributes (
549 EfiPciIoAttributeOperationSupported
,
554 if (!EFI_ERROR (Status
)) {
555 Supports
&= (UINT64
)EFI_PCI_DEVICE_ENABLE
;
556 Status
= PciIo
->Attributes (
558 EfiPciIoAttributeOperationEnable
,
566 Private
= AllocateCopyPool (sizeof (SD_MMC_HC_PRIVATE_DATA
), &gSdMmcPciHcTemplate
);
567 if (Private
== NULL
) {
568 Status
= EFI_OUT_OF_RESOURCES
;
572 Private
->ControllerHandle
= Controller
;
573 Private
->PciIo
= PciIo
;
574 Private
->PciAttributes
= PciAttributes
;
575 InitializeListHead (&Private
->Queue
);
578 // Get SD/MMC Pci Host Controller Slot info
580 Status
= SdMmcHcGetSlotInfo (PciIo
, &FirstBar
, &SlotNum
);
581 if (EFI_ERROR (Status
)) {
585 for (Slot
= FirstBar
; Slot
< (FirstBar
+ SlotNum
); Slot
++) {
586 Private
->Slot
[Slot
].Enable
= TRUE
;
588 Status
= SdMmcHcGetCapability (PciIo
, Slot
, &Private
->Capability
[Slot
]);
589 if (EFI_ERROR (Status
)) {
592 DumpCapabilityReg (Slot
, &Private
->Capability
[Slot
]);
594 Status
= SdMmcHcGetMaxCurrent (PciIo
, Slot
, &Private
->MaxCurrent
[Slot
]);
595 if (EFI_ERROR (Status
)) {
599 Private
->Slot
[Slot
].SlotType
= Private
->Capability
[Slot
].SlotType
;
600 if ((Private
->Slot
[Slot
].SlotType
!= RemovableSlot
) && (Private
->Slot
[Slot
].SlotType
!= EmbeddedSlot
)) {
601 DEBUG ((EFI_D_INFO
, "SdMmcPciHcDxe doesn't support the slot type [%d]!!!\n", Private
->Slot
[Slot
].SlotType
));
606 // Reset the specified slot of the SD/MMC Pci Host Controller
608 Status
= SdMmcHcReset (PciIo
, Slot
);
609 if (EFI_ERROR (Status
)) {
613 // Check whether there is a SD/MMC card attached
615 Status
= SdMmcHcCardDetect (PciIo
, Slot
, &MediaPresent
);
616 if (EFI_ERROR (Status
) && (Status
!= EFI_MEDIA_CHANGED
)) {
618 } else if (MediaPresent
== FALSE
) {
619 DEBUG ((EFI_D_ERROR
, "SdMmcHcCardDetect: No device attached in Slot[%d]!!!\n", Slot
));
623 Status
= SdMmcHcInitHost (PciIo
, Slot
, Private
->Capability
[Slot
]);
624 if (EFI_ERROR (Status
)) {
628 Private
->Slot
[Slot
].MediaPresent
= TRUE
;
629 RoutineNum
= sizeof (mCardTypeDetectRoutineTable
) / sizeof (CARD_TYPE_DETECT_ROUTINE
);
630 for (Index
= 0; Index
< RoutineNum
; Index
++) {
631 Routine
= &mCardTypeDetectRoutineTable
[Index
];
632 if (*Routine
!= NULL
) {
633 Status
= (*Routine
) (Private
, Slot
);
634 if (!EFI_ERROR (Status
)) {
642 // Start the asynchronous I/O monitor
644 Status
= gBS
->CreateEvent (
645 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
647 ProcessAsyncTaskList
,
651 if (EFI_ERROR (Status
)) {
655 Status
= gBS
->SetTimer (Private
->TimerEvent
, TimerPeriodic
, SD_MMC_HC_ASYNC_TIMER
);
656 if (EFI_ERROR (Status
)) {
661 // Start the Sd removable device connection enumeration
663 Status
= gBS
->CreateEvent (
664 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
666 SdMmcPciHcEnumerateDevice
,
668 &Private
->ConnectEvent
670 if (EFI_ERROR (Status
)) {
674 Status
= gBS
->SetTimer (Private
->ConnectEvent
, TimerPeriodic
, SD_MMC_HC_ENUM_TIMER
);
675 if (EFI_ERROR (Status
)) {
679 Status
= gBS
->InstallMultipleProtocolInterfaces (
681 &gEfiSdMmcPassThruProtocolGuid
,
682 &(Private
->PassThru
),
686 DEBUG ((EFI_D_INFO
, "SdMmcPciHcDriverBindingStart: %r End on %x\n", Status
, Controller
));
689 if (EFI_ERROR (Status
)) {
690 if ((Private
!= NULL
) && (Private
->PciAttributes
!= 0)) {
692 // Restore original PCI attributes
696 EfiPciIoAttributeOperationSet
,
697 Private
->PciAttributes
,
703 &gEfiPciIoProtocolGuid
,
704 This
->DriverBindingHandle
,
708 if ((Private
!= NULL
) && (Private
->TimerEvent
!= NULL
)) {
709 gBS
->CloseEvent (Private
->TimerEvent
);
712 if ((Private
!= NULL
) && (Private
->ConnectEvent
!= NULL
)) {
713 gBS
->CloseEvent (Private
->ConnectEvent
);
716 if (Private
!= NULL
) {
725 Stops a device controller or a bus controller.
727 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
728 As a result, much of the error checking on the parameters to Stop() has been moved
729 into this common boot service. It is legal to call Stop() from other locations,
730 but the following calling restrictions must be followed or the system behavior will not be deterministic.
731 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
732 same driver's Start() function.
733 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
734 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
735 Start() function, and the Start() function must have called OpenProtocol() on
736 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
738 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
739 @param[in] ControllerHandle A handle to the device being stopped. The handle must
740 support a bus specific I/O protocol for the driver
741 to use to stop the device.
742 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
743 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
744 if NumberOfChildren is 0.
746 @retval EFI_SUCCESS The device was stopped.
747 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
752 SdMmcPciHcDriverBindingStop (
753 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
754 IN EFI_HANDLE Controller
,
755 IN UINTN NumberOfChildren
,
756 IN EFI_HANDLE
*ChildHandleBuffer
760 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
761 SD_MMC_HC_PRIVATE_DATA
*Private
;
762 EFI_PCI_IO_PROTOCOL
*PciIo
;
764 LIST_ENTRY
*NextLink
;
767 DEBUG ((EFI_D_INFO
, "SdMmcPciHcDriverBindingStop: Start\n"));
769 Status
= gBS
->OpenProtocol (
771 &gEfiSdMmcPassThruProtocolGuid
,
773 This
->DriverBindingHandle
,
775 EFI_OPEN_PROTOCOL_GET_PROTOCOL
777 if (EFI_ERROR (Status
)) {
781 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (PassThru
);
783 // Close Non-Blocking timer and free Task list.
785 if (Private
->TimerEvent
!= NULL
) {
786 gBS
->CloseEvent (Private
->TimerEvent
);
787 Private
->TimerEvent
= NULL
;
789 if (Private
->ConnectEvent
!= NULL
) {
790 gBS
->CloseEvent (Private
->ConnectEvent
);
791 Private
->ConnectEvent
= NULL
;
794 // As the timer is closed, there is no needs to use TPL lock to
795 // protect the critical region "queue".
797 for (Link
= GetFirstNode (&Private
->Queue
);
798 !IsNull (&Private
->Queue
, Link
);
800 NextLink
= GetNextNode (&Private
->Queue
, Link
);
801 RemoveEntryList (Link
);
802 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
803 Trb
->Packet
->TransactionStatus
= EFI_ABORTED
;
804 gBS
->SignalEvent (Trb
->Event
);
809 // Uninstall Block I/O protocol from the device handle
811 Status
= gBS
->UninstallProtocolInterface (
813 &gEfiSdMmcPassThruProtocolGuid
,
817 if (EFI_ERROR (Status
)) {
823 &gEfiPciIoProtocolGuid
,
824 This
->DriverBindingHandle
,
828 // Restore original PCI attributes
830 PciIo
= Private
->PciIo
;
831 Status
= PciIo
->Attributes (
833 EfiPciIoAttributeOperationSet
,
834 Private
->PciAttributes
,
837 ASSERT_EFI_ERROR (Status
);
841 DEBUG ((EFI_D_INFO
, "SdMmcPciHcDriverBindingStop: End with %r\n", Status
));
847 Sends SD command to an SD card that is attached to the SD controller.
849 The PassThru() function sends the SD command specified by Packet to the SD card
852 If Packet is successfully sent to the SD card, then EFI_SUCCESS is returned.
854 If a device error occurs while sending the Packet, then EFI_DEVICE_ERROR is returned.
856 If Slot is not in a valid range for the SD controller, then EFI_INVALID_PARAMETER
859 If Packet defines a data command but both InDataBuffer and OutDataBuffer are NULL,
860 EFI_INVALID_PARAMETER is returned.
862 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
863 @param[in] Slot The slot number of the SD card to send the command to.
864 @param[in,out] Packet A pointer to the SD command data structure.
865 @param[in] Event If Event is NULL, blocking I/O is performed. If Event is
866 not NULL, then nonblocking I/O is performed, and Event
867 will be signaled when the Packet completes.
869 @retval EFI_SUCCESS The SD Command Packet was sent by the host.
870 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SD
872 @retval EFI_INVALID_PARAMETER Packet, Slot, or the contents of the Packet is invalid.
873 @retval EFI_INVALID_PARAMETER Packet defines a data command but both InDataBuffer and
874 OutDataBuffer are NULL.
875 @retval EFI_NO_MEDIA SD Device not present in the Slot.
876 @retval EFI_UNSUPPORTED The command described by the SD Command Packet is not
877 supported by the host controller.
878 @retval EFI_BAD_BUFFER_SIZE The InTransferLength or OutTransferLength exceeds the
879 limit supported by SD card ( i.e. if the number of bytes
880 exceed the Last LBA).
885 SdMmcPassThruPassThru (
886 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
888 IN OUT EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
,
889 IN EFI_EVENT Event OPTIONAL
893 SD_MMC_HC_PRIVATE_DATA
*Private
;
897 if ((This
== NULL
) || (Packet
== NULL
)) {
898 return EFI_INVALID_PARAMETER
;
901 if ((Packet
->SdMmcCmdBlk
== NULL
) || (Packet
->SdMmcStatusBlk
== NULL
)) {
902 return EFI_INVALID_PARAMETER
;
905 if ((Packet
->OutDataBuffer
== NULL
) && (Packet
->OutTransferLength
!= 0)) {
906 return EFI_INVALID_PARAMETER
;
909 if ((Packet
->InDataBuffer
== NULL
) && (Packet
->InTransferLength
!= 0)) {
910 return EFI_INVALID_PARAMETER
;
913 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
915 if (!Private
->Slot
[Slot
].Enable
) {
916 return EFI_INVALID_PARAMETER
;
919 if (!Private
->Slot
[Slot
].MediaPresent
) {
923 Trb
= SdMmcCreateTrb (Private
, Slot
, Packet
, Event
);
925 return EFI_OUT_OF_RESOURCES
;
928 // Immediately return for async I/O.
935 // Wait async I/O list is empty before execute sync I/O operation.
938 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
939 if (IsListEmpty (&Private
->Queue
)) {
940 gBS
->RestoreTPL (OldTpl
);
943 gBS
->RestoreTPL (OldTpl
);
946 Status
= SdMmcWaitTrbEnv (Private
, Trb
);
947 if (EFI_ERROR (Status
)) {
951 Status
= SdMmcExecTrb (Private
, Trb
);
952 if (EFI_ERROR (Status
)) {
956 Status
= SdMmcWaitTrbResult (Private
, Trb
);
957 if (EFI_ERROR (Status
)) {
962 if ((Trb
!= NULL
) && (Trb
->AdmaDesc
!= NULL
)) {
963 FreePages (Trb
->AdmaDesc
, Trb
->AdmaPages
);
974 Used to retrieve next slot numbers supported by the SD controller. The function
975 returns information about all available slots (populated or not-populated).
977 The GetNextSlot() function retrieves the next slot number on an SD controller.
978 If on input Slot is 0xFF, then the slot number of the first slot on the SD controller
981 If Slot is a slot number that was returned on a previous call to GetNextSlot(), then
982 the slot number of the next slot on the SD controller is returned.
984 If Slot is not 0xFF and Slot was not returned on a previous call to GetNextSlot(),
985 EFI_INVALID_PARAMETER is returned.
987 If Slot is the slot number of the last slot on the SD controller, then EFI_NOT_FOUND
990 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
991 @param[in,out] Slot On input, a pointer to a slot number on the SD controller.
992 On output, a pointer to the next slot number on the SD controller.
993 An input value of 0xFF retrieves the first slot number on the SD
996 @retval EFI_SUCCESS The next slot number on the SD controller was returned in Slot.
997 @retval EFI_NOT_FOUND There are no more slots on this SD controller.
998 @retval EFI_INVALID_PARAMETER Slot is not 0xFF and Slot was not returned on a previous call
1004 SdMmcPassThruGetNextSlot (
1005 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1009 SD_MMC_HC_PRIVATE_DATA
*Private
;
1012 if ((This
== NULL
) || (Slot
== NULL
)) {
1013 return EFI_INVALID_PARAMETER
;
1016 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1018 if (*Slot
== 0xFF) {
1019 for (Index
= 0; Index
< SD_MMC_HC_MAX_SLOT
; Index
++) {
1020 if (Private
->Slot
[Index
].Enable
) {
1022 Private
->PreviousSlot
= Index
;
1026 return EFI_NOT_FOUND
;
1027 } else if (*Slot
== Private
->PreviousSlot
) {
1028 for (Index
= *Slot
+ 1; Index
< SD_MMC_HC_MAX_SLOT
; Index
++) {
1029 if (Private
->Slot
[Index
].Enable
) {
1031 Private
->PreviousSlot
= Index
;
1035 return EFI_NOT_FOUND
;
1037 return EFI_INVALID_PARAMETER
;
1042 Used to allocate and build a device path node for an SD card on the SD controller.
1044 The BuildDevicePath() function allocates and builds a single device node for the SD
1045 card specified by Slot.
1047 If the SD card specified by Slot is not present on the SD controller, then EFI_NOT_FOUND
1050 If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned.
1052 If there are not enough resources to allocate the device path node, then EFI_OUT_OF_RESOURCES
1055 Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of
1056 DevicePath are initialized to describe the SD card specified by Slot, and EFI_SUCCESS is
1059 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
1060 @param[in] Slot Specifies the slot number of the SD card for which a device
1061 path node is to be allocated and built.
1062 @param[in,out] DevicePath A pointer to a single device path node that describes the SD
1063 card specified by Slot. This function is responsible for
1064 allocating the buffer DevicePath with the boot service
1065 AllocatePool(). It is the caller's responsibility to free
1066 DevicePath when the caller is finished with DevicePath.
1068 @retval EFI_SUCCESS The device path node that describes the SD card specified by
1069 Slot was allocated and returned in DevicePath.
1070 @retval EFI_NOT_FOUND The SD card specified by Slot does not exist on the SD controller.
1071 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1072 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
1077 SdMmcPassThruBuildDevicePath (
1078 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1080 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
1083 SD_MMC_HC_PRIVATE_DATA
*Private
;
1084 SD_DEVICE_PATH
*SdNode
;
1085 EMMC_DEVICE_PATH
*EmmcNode
;
1087 if ((This
== NULL
) || (DevicePath
== NULL
) || (Slot
>= SD_MMC_HC_MAX_SLOT
)) {
1088 return EFI_INVALID_PARAMETER
;
1091 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1093 if ((!Private
->Slot
[Slot
].Enable
) || (!Private
->Slot
[Slot
].MediaPresent
)) {
1094 return EFI_NOT_FOUND
;
1097 if (Private
->Slot
[Slot
].CardType
== SdCardType
) {
1098 SdNode
= AllocateCopyPool (sizeof (SD_DEVICE_PATH
), &mSdDpTemplate
);
1099 if (SdNode
== NULL
) {
1100 return EFI_OUT_OF_RESOURCES
;
1102 SdNode
->SlotNumber
= Slot
;
1104 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) SdNode
;
1105 } else if (Private
->Slot
[Slot
].CardType
== EmmcCardType
) {
1106 EmmcNode
= AllocateCopyPool (sizeof (EMMC_DEVICE_PATH
), &mEmmcDpTemplate
);
1107 if (EmmcNode
== NULL
) {
1108 return EFI_OUT_OF_RESOURCES
;
1110 EmmcNode
->SlotNumber
= Slot
;
1112 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) EmmcNode
;
1115 // Currently we only support SD and EMMC two device nodes.
1117 return EFI_NOT_FOUND
;
1124 This function retrieves an SD card slot number based on the input device path.
1126 The GetSlotNumber() function retrieves slot number for the SD card specified by
1127 the DevicePath node. If DevicePath is NULL, EFI_INVALID_PARAMETER is returned.
1129 If DevicePath is not a device path node type that the SD Pass Thru driver supports,
1130 EFI_UNSUPPORTED is returned.
1132 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1133 @param[in] DevicePath A pointer to the device path node that describes a SD
1134 card on the SD controller.
1135 @param[out] Slot On return, points to the slot number of an SD card on
1138 @retval EFI_SUCCESS SD card slot number is returned in Slot.
1139 @retval EFI_INVALID_PARAMETER Slot or DevicePath is NULL.
1140 @retval EFI_UNSUPPORTED DevicePath is not a device path node type that the SD
1141 Pass Thru driver supports.
1146 SdMmcPassThruGetSlotNumber (
1147 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1148 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1152 SD_MMC_HC_PRIVATE_DATA
*Private
;
1153 SD_DEVICE_PATH
*SdNode
;
1154 EMMC_DEVICE_PATH
*EmmcNode
;
1157 if ((This
== NULL
) || (DevicePath
== NULL
) || (Slot
== NULL
)) {
1158 return EFI_INVALID_PARAMETER
;
1161 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1164 // Check whether the DevicePath belongs to SD_DEVICE_PATH or EMMC_DEVICE_PATH
1166 if ((DevicePath
->Type
!= MESSAGING_DEVICE_PATH
) ||
1167 ((DevicePath
->SubType
!= MSG_SD_DP
) &&
1168 (DevicePath
->SubType
!= MSG_EMMC_DP
)) ||
1169 (DevicePathNodeLength(DevicePath
) != sizeof(SD_DEVICE_PATH
)) ||
1170 (DevicePathNodeLength(DevicePath
) != sizeof(EMMC_DEVICE_PATH
))) {
1171 return EFI_UNSUPPORTED
;
1174 if (DevicePath
->SubType
== MSG_SD_DP
) {
1175 SdNode
= (SD_DEVICE_PATH
*) DevicePath
;
1176 SlotNumber
= SdNode
->SlotNumber
;
1178 EmmcNode
= (EMMC_DEVICE_PATH
*) DevicePath
;
1179 SlotNumber
= EmmcNode
->SlotNumber
;
1182 if (SlotNumber
>= SD_MMC_HC_MAX_SLOT
) {
1183 return EFI_NOT_FOUND
;
1186 if (Private
->Slot
[SlotNumber
].Enable
) {
1190 return EFI_NOT_FOUND
;
1195 Resets an SD card that is connected to the SD controller.
1197 The ResetDevice() function resets the SD card specified by Slot.
1199 If this SD controller does not support a device reset operation, EFI_UNSUPPORTED is
1202 If Slot is not in a valid slot number for this SD controller, EFI_INVALID_PARAMETER
1205 If the device reset operation is completed, EFI_SUCCESS is returned.
1207 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1208 @param[in] Slot Specifies the slot number of the SD card to be reset.
1210 @retval EFI_SUCCESS The SD card specified by Slot was reset.
1211 @retval EFI_UNSUPPORTED The SD controller does not support a device reset operation.
1212 @retval EFI_INVALID_PARAMETER Slot number is invalid.
1213 @retval EFI_NO_MEDIA SD Device not present in the Slot.
1214 @retval EFI_DEVICE_ERROR The reset command failed due to a device error
1219 SdMmcPassThruResetDevice (
1220 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1224 SD_MMC_HC_PRIVATE_DATA
*Private
;
1226 LIST_ENTRY
*NextLink
;
1231 return EFI_INVALID_PARAMETER
;
1234 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1236 if (!Private
->Slot
[Slot
].Enable
) {
1237 return EFI_INVALID_PARAMETER
;
1240 if (!Private
->Slot
[Slot
].MediaPresent
) {
1241 return EFI_NO_MEDIA
;
1244 // Free all async I/O requests in the queue
1246 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1248 for (Link
= GetFirstNode (&Private
->Queue
);
1249 !IsNull (&Private
->Queue
, Link
);
1251 NextLink
= GetNextNode (&Private
->Queue
, Link
);
1252 RemoveEntryList (Link
);
1253 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
1254 Trb
->Packet
->TransactionStatus
= EFI_ABORTED
;
1255 gBS
->SignalEvent (Trb
->Event
);
1259 gBS
->RestoreTPL (OldTpl
);