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 // Reset the specified slot of the SD/MMC Pci Host Controller
280 Status
= SdMmcHcReset (Private
->PciIo
, Slot
);
281 if (EFI_ERROR (Status
)) {
285 // Reinitialize slot and restart identification process for the new attached device
287 Status
= SdMmcHcInitHost (Private
->PciIo
, Slot
, Private
->Capability
[Slot
]);
288 if (EFI_ERROR (Status
)) {
292 Private
->Slot
[Slot
].MediaPresent
= TRUE
;
293 RoutineNum
= sizeof (mCardTypeDetectRoutineTable
) / sizeof (CARD_TYPE_DETECT_ROUTINE
);
294 for (Index
= 0; Index
< RoutineNum
; Index
++) {
295 Routine
= &mCardTypeDetectRoutineTable
[Index
];
296 if (*Routine
!= NULL
) {
297 Status
= (*Routine
) (Private
, Slot
);
298 if (!EFI_ERROR (Status
)) {
305 // Notify the upper layer the connect state change through ReinstallProtocolInterface.
307 gBS
->ReinstallProtocolInterface (
308 Private
->ControllerHandle
,
309 &gEfiSdMmcPassThruProtocolGuid
,
320 Tests to see if this driver supports a given controller. If a child device is provided,
321 it further tests to see if this driver supports creating a handle for the specified child device.
323 This function checks to see if the driver specified by This supports the device specified by
324 ControllerHandle. Drivers will typically use the device path attached to
325 ControllerHandle and/or the services from the bus I/O abstraction attached to
326 ControllerHandle to determine if the driver supports ControllerHandle. This function
327 may be called many times during platform initialization. In order to reduce boot times, the tests
328 performed by this function must be very small, and take as little time as possible to execute. This
329 function must not change the state of any hardware devices, and this function must be aware that the
330 device specified by ControllerHandle may already be managed by the same driver or a
331 different driver. This function must match its calls to AllocatePages() with FreePages(),
332 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
333 Since ControllerHandle may have been previously started by the same driver, if a protocol is
334 already in the opened state, then it must not be closed with CloseProtocol(). This is required
335 to guarantee the state of ControllerHandle is not modified by this function.
337 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
338 @param[in] ControllerHandle The handle of the controller to test. This handle
339 must support a protocol interface that supplies
340 an I/O abstraction to the driver.
341 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
342 parameter is ignored by device drivers, and is optional for bus
343 drivers. For bus drivers, if this parameter is not NULL, then
344 the bus driver must determine if the bus controller specified
345 by ControllerHandle and the child controller specified
346 by RemainingDevicePath are both supported by this
349 @retval EFI_SUCCESS The device specified by ControllerHandle and
350 RemainingDevicePath is supported by the driver specified by This.
351 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
352 RemainingDevicePath is already being managed by the driver
354 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
355 RemainingDevicePath is already being managed by a different
356 driver or an application that requires exclusive access.
357 Currently not implemented.
358 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
359 RemainingDevicePath is not supported by the driver specified by This.
363 SdMmcPciHcDriverBindingSupported (
364 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
365 IN EFI_HANDLE Controller
,
366 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
370 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
371 EFI_PCI_IO_PROTOCOL
*PciIo
;
375 ParentDevicePath
= NULL
;
378 // SdPciHcDxe is a device driver, and should ingore the
379 // "RemainingDevicePath" according to EFI spec.
381 Status
= gBS
->OpenProtocol (
383 &gEfiDevicePathProtocolGuid
,
384 (VOID
*) &ParentDevicePath
,
385 This
->DriverBindingHandle
,
387 EFI_OPEN_PROTOCOL_BY_DRIVER
389 if (EFI_ERROR (Status
)) {
391 // EFI_ALREADY_STARTED is also an error.
396 // Close the protocol because we don't use it here.
400 &gEfiDevicePathProtocolGuid
,
401 This
->DriverBindingHandle
,
406 // Now test the EfiPciIoProtocol.
408 Status
= gBS
->OpenProtocol (
410 &gEfiPciIoProtocolGuid
,
412 This
->DriverBindingHandle
,
414 EFI_OPEN_PROTOCOL_BY_DRIVER
416 if (EFI_ERROR (Status
)) {
421 // Now further check the PCI header: Base class (offset 0x08) and
422 // Sub Class (offset 0x05). This controller should be an SD/MMC PCI
425 Status
= PciIo
->Pci
.Read (
432 if (EFI_ERROR (Status
)) {
435 &gEfiPciIoProtocolGuid
,
436 This
->DriverBindingHandle
,
439 return EFI_UNSUPPORTED
;
442 // Since we already got the PciData, we can close protocol to avoid to carry it
443 // on for multiple exit points.
447 &gEfiPciIoProtocolGuid
,
448 This
->DriverBindingHandle
,
453 // Examine SD PCI Host Controller PCI Configuration table fields.
455 if ((PciData
.Hdr
.ClassCode
[2] == PCI_CLASS_SYSTEM_PERIPHERAL
) &&
456 (PciData
.Hdr
.ClassCode
[1] == PCI_SUBCLASS_SD_HOST_CONTROLLER
) &&
457 ((PciData
.Hdr
.ClassCode
[0] == 0x00) || (PciData
.Hdr
.ClassCode
[0] == 0x01))) {
461 return EFI_UNSUPPORTED
;
465 Starts a device controller or a bus controller.
467 The Start() function is designed to be invoked from the EFI boot service ConnectController().
468 As a result, much of the error checking on the parameters to Start() has been moved into this
469 common boot service. It is legal to call Start() from other locations,
470 but the following calling restrictions must be followed or the system behavior will not be deterministic.
471 1. ControllerHandle must be a valid EFI_HANDLE.
472 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
473 EFI_DEVICE_PATH_PROTOCOL.
474 3. Prior to calling Start(), the Supported() function for the driver specified by This must
475 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
477 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
478 @param[in] ControllerHandle The handle of the controller to start. This handle
479 must support a protocol interface that supplies
480 an I/O abstraction to the driver.
481 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
482 parameter is ignored by device drivers, and is optional for bus
483 drivers. For a bus driver, if this parameter is NULL, then handles
484 for all the children of Controller are created by this driver.
485 If this parameter is not NULL and the first Device Path Node is
486 not the End of Device Path Node, then only the handle for the
487 child device specified by the first Device Path Node of
488 RemainingDevicePath is created by this driver.
489 If the first Device Path Node of RemainingDevicePath is
490 the End of Device Path Node, no child handle is created by this
493 @retval EFI_SUCCESS The device was started.
494 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
495 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
496 @retval Others The driver failded to start the device.
501 SdMmcPciHcDriverBindingStart (
502 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
503 IN EFI_HANDLE Controller
,
504 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
508 SD_MMC_HC_PRIVATE_DATA
*Private
;
509 EFI_PCI_IO_PROTOCOL
*PciIo
;
511 UINT64 PciAttributes
;
516 CARD_TYPE_DETECT_ROUTINE
*Routine
;
518 BOOLEAN MediaPresent
;
520 DEBUG ((EFI_D_INFO
, "SdMmcPciHcDriverBindingStart: Start\n"));
523 // Open PCI I/O Protocol and save pointer to open protocol
524 // in private data area.
527 Status
= gBS
->OpenProtocol (
529 &gEfiPciIoProtocolGuid
,
531 This
->DriverBindingHandle
,
533 EFI_OPEN_PROTOCOL_BY_DRIVER
535 if (EFI_ERROR (Status
)) {
540 // Enable the SD Host Controller MMIO space
543 Status
= PciIo
->Attributes (
545 EfiPciIoAttributeOperationGet
,
550 if (EFI_ERROR (Status
)) {
554 Status
= PciIo
->Attributes (
556 EfiPciIoAttributeOperationSupported
,
561 if (!EFI_ERROR (Status
)) {
562 Supports
&= (UINT64
)EFI_PCI_DEVICE_ENABLE
;
563 Status
= PciIo
->Attributes (
565 EfiPciIoAttributeOperationEnable
,
573 Private
= AllocateCopyPool (sizeof (SD_MMC_HC_PRIVATE_DATA
), &gSdMmcPciHcTemplate
);
574 if (Private
== NULL
) {
575 Status
= EFI_OUT_OF_RESOURCES
;
579 Private
->ControllerHandle
= Controller
;
580 Private
->PciIo
= PciIo
;
581 Private
->PciAttributes
= PciAttributes
;
582 InitializeListHead (&Private
->Queue
);
585 // Get SD/MMC Pci Host Controller Slot info
587 Status
= SdMmcHcGetSlotInfo (PciIo
, &FirstBar
, &SlotNum
);
588 if (EFI_ERROR (Status
)) {
592 for (Slot
= FirstBar
; Slot
< (FirstBar
+ SlotNum
); Slot
++) {
593 Private
->Slot
[Slot
].Enable
= TRUE
;
595 Status
= SdMmcHcGetCapability (PciIo
, Slot
, &Private
->Capability
[Slot
]);
596 if (EFI_ERROR (Status
)) {
599 DumpCapabilityReg (Slot
, &Private
->Capability
[Slot
]);
601 Status
= SdMmcHcGetMaxCurrent (PciIo
, Slot
, &Private
->MaxCurrent
[Slot
]);
602 if (EFI_ERROR (Status
)) {
606 Private
->Slot
[Slot
].SlotType
= Private
->Capability
[Slot
].SlotType
;
607 if ((Private
->Slot
[Slot
].SlotType
!= RemovableSlot
) && (Private
->Slot
[Slot
].SlotType
!= EmbeddedSlot
)) {
608 DEBUG ((EFI_D_INFO
, "SdMmcPciHcDxe doesn't support the slot type [%d]!!!\n", Private
->Slot
[Slot
].SlotType
));
613 // Reset the specified slot of the SD/MMC Pci Host Controller
615 Status
= SdMmcHcReset (PciIo
, Slot
);
616 if (EFI_ERROR (Status
)) {
620 // Check whether there is a SD/MMC card attached
622 Status
= SdMmcHcCardDetect (PciIo
, Slot
, &MediaPresent
);
623 if (EFI_ERROR (Status
) && (Status
!= EFI_MEDIA_CHANGED
)) {
625 } else if (MediaPresent
== FALSE
) {
626 DEBUG ((EFI_D_ERROR
, "SdMmcHcCardDetect: No device attached in Slot[%d]!!!\n", Slot
));
630 Status
= SdMmcHcInitHost (PciIo
, Slot
, Private
->Capability
[Slot
]);
631 if (EFI_ERROR (Status
)) {
635 Private
->Slot
[Slot
].MediaPresent
= TRUE
;
636 RoutineNum
= sizeof (mCardTypeDetectRoutineTable
) / sizeof (CARD_TYPE_DETECT_ROUTINE
);
637 for (Index
= 0; Index
< RoutineNum
; Index
++) {
638 Routine
= &mCardTypeDetectRoutineTable
[Index
];
639 if (*Routine
!= NULL
) {
640 Status
= (*Routine
) (Private
, Slot
);
641 if (!EFI_ERROR (Status
)) {
649 // Start the asynchronous I/O monitor
651 Status
= gBS
->CreateEvent (
652 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
654 ProcessAsyncTaskList
,
658 if (EFI_ERROR (Status
)) {
662 Status
= gBS
->SetTimer (Private
->TimerEvent
, TimerPeriodic
, SD_MMC_HC_ASYNC_TIMER
);
663 if (EFI_ERROR (Status
)) {
668 // Start the Sd removable device connection enumeration
670 Status
= gBS
->CreateEvent (
671 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
673 SdMmcPciHcEnumerateDevice
,
675 &Private
->ConnectEvent
677 if (EFI_ERROR (Status
)) {
681 Status
= gBS
->SetTimer (Private
->ConnectEvent
, TimerPeriodic
, SD_MMC_HC_ENUM_TIMER
);
682 if (EFI_ERROR (Status
)) {
686 Status
= gBS
->InstallMultipleProtocolInterfaces (
688 &gEfiSdMmcPassThruProtocolGuid
,
689 &(Private
->PassThru
),
693 DEBUG ((EFI_D_INFO
, "SdMmcPciHcDriverBindingStart: %r End on %x\n", Status
, Controller
));
696 if (EFI_ERROR (Status
)) {
697 if ((Private
!= NULL
) && (Private
->PciAttributes
!= 0)) {
699 // Restore original PCI attributes
703 EfiPciIoAttributeOperationSet
,
704 Private
->PciAttributes
,
710 &gEfiPciIoProtocolGuid
,
711 This
->DriverBindingHandle
,
715 if ((Private
!= NULL
) && (Private
->TimerEvent
!= NULL
)) {
716 gBS
->CloseEvent (Private
->TimerEvent
);
719 if ((Private
!= NULL
) && (Private
->ConnectEvent
!= NULL
)) {
720 gBS
->CloseEvent (Private
->ConnectEvent
);
723 if (Private
!= NULL
) {
732 Stops a device controller or a bus controller.
734 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
735 As a result, much of the error checking on the parameters to Stop() has been moved
736 into this common boot service. It is legal to call Stop() from other locations,
737 but the following calling restrictions must be followed or the system behavior will not be deterministic.
738 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
739 same driver's Start() function.
740 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
741 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
742 Start() function, and the Start() function must have called OpenProtocol() on
743 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
745 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
746 @param[in] ControllerHandle A handle to the device being stopped. The handle must
747 support a bus specific I/O protocol for the driver
748 to use to stop the device.
749 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
750 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
751 if NumberOfChildren is 0.
753 @retval EFI_SUCCESS The device was stopped.
754 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
759 SdMmcPciHcDriverBindingStop (
760 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
761 IN EFI_HANDLE Controller
,
762 IN UINTN NumberOfChildren
,
763 IN EFI_HANDLE
*ChildHandleBuffer
767 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
768 SD_MMC_HC_PRIVATE_DATA
*Private
;
769 EFI_PCI_IO_PROTOCOL
*PciIo
;
771 LIST_ENTRY
*NextLink
;
774 DEBUG ((EFI_D_INFO
, "SdMmcPciHcDriverBindingStop: Start\n"));
776 Status
= gBS
->OpenProtocol (
778 &gEfiSdMmcPassThruProtocolGuid
,
780 This
->DriverBindingHandle
,
782 EFI_OPEN_PROTOCOL_GET_PROTOCOL
784 if (EFI_ERROR (Status
)) {
788 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (PassThru
);
790 // Close Non-Blocking timer and free Task list.
792 if (Private
->TimerEvent
!= NULL
) {
793 gBS
->CloseEvent (Private
->TimerEvent
);
794 Private
->TimerEvent
= NULL
;
796 if (Private
->ConnectEvent
!= NULL
) {
797 gBS
->CloseEvent (Private
->ConnectEvent
);
798 Private
->ConnectEvent
= NULL
;
801 // As the timer is closed, there is no needs to use TPL lock to
802 // protect the critical region "queue".
804 for (Link
= GetFirstNode (&Private
->Queue
);
805 !IsNull (&Private
->Queue
, Link
);
807 NextLink
= GetNextNode (&Private
->Queue
, Link
);
808 RemoveEntryList (Link
);
809 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
810 Trb
->Packet
->TransactionStatus
= EFI_ABORTED
;
811 gBS
->SignalEvent (Trb
->Event
);
816 // Uninstall Block I/O protocol from the device handle
818 Status
= gBS
->UninstallProtocolInterface (
820 &gEfiSdMmcPassThruProtocolGuid
,
824 if (EFI_ERROR (Status
)) {
830 &gEfiPciIoProtocolGuid
,
831 This
->DriverBindingHandle
,
835 // Restore original PCI attributes
837 PciIo
= Private
->PciIo
;
838 Status
= PciIo
->Attributes (
840 EfiPciIoAttributeOperationSet
,
841 Private
->PciAttributes
,
844 ASSERT_EFI_ERROR (Status
);
848 DEBUG ((EFI_D_INFO
, "SdMmcPciHcDriverBindingStop: End with %r\n", Status
));
854 Sends SD command to an SD card that is attached to the SD controller.
856 The PassThru() function sends the SD command specified by Packet to the SD card
859 If Packet is successfully sent to the SD card, then EFI_SUCCESS is returned.
861 If a device error occurs while sending the Packet, then EFI_DEVICE_ERROR is returned.
863 If Slot is not in a valid range for the SD controller, then EFI_INVALID_PARAMETER
866 If Packet defines a data command but both InDataBuffer and OutDataBuffer are NULL,
867 EFI_INVALID_PARAMETER is returned.
869 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
870 @param[in] Slot The slot number of the SD card to send the command to.
871 @param[in,out] Packet A pointer to the SD command data structure.
872 @param[in] Event If Event is NULL, blocking I/O is performed. If Event is
873 not NULL, then nonblocking I/O is performed, and Event
874 will be signaled when the Packet completes.
876 @retval EFI_SUCCESS The SD Command Packet was sent by the host.
877 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SD
879 @retval EFI_INVALID_PARAMETER Packet, Slot, or the contents of the Packet is invalid.
880 @retval EFI_INVALID_PARAMETER Packet defines a data command but both InDataBuffer and
881 OutDataBuffer are NULL.
882 @retval EFI_NO_MEDIA SD Device not present in the Slot.
883 @retval EFI_UNSUPPORTED The command described by the SD Command Packet is not
884 supported by the host controller.
885 @retval EFI_BAD_BUFFER_SIZE The InTransferLength or OutTransferLength exceeds the
886 limit supported by SD card ( i.e. if the number of bytes
887 exceed the Last LBA).
892 SdMmcPassThruPassThru (
893 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
895 IN OUT EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
,
896 IN EFI_EVENT Event OPTIONAL
900 SD_MMC_HC_PRIVATE_DATA
*Private
;
904 if ((This
== NULL
) || (Packet
== NULL
)) {
905 return EFI_INVALID_PARAMETER
;
908 if ((Packet
->SdMmcCmdBlk
== NULL
) || (Packet
->SdMmcStatusBlk
== NULL
)) {
909 return EFI_INVALID_PARAMETER
;
912 if ((Packet
->OutDataBuffer
== NULL
) && (Packet
->OutTransferLength
!= 0)) {
913 return EFI_INVALID_PARAMETER
;
916 if ((Packet
->InDataBuffer
== NULL
) && (Packet
->InTransferLength
!= 0)) {
917 return EFI_INVALID_PARAMETER
;
920 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
922 if (!Private
->Slot
[Slot
].Enable
) {
923 return EFI_INVALID_PARAMETER
;
926 if (!Private
->Slot
[Slot
].MediaPresent
) {
930 Trb
= SdMmcCreateTrb (Private
, Slot
, Packet
, Event
);
932 return EFI_OUT_OF_RESOURCES
;
935 // Immediately return for async I/O.
942 // Wait async I/O list is empty before execute sync I/O operation.
945 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
946 if (IsListEmpty (&Private
->Queue
)) {
947 gBS
->RestoreTPL (OldTpl
);
950 gBS
->RestoreTPL (OldTpl
);
953 Status
= SdMmcWaitTrbEnv (Private
, Trb
);
954 if (EFI_ERROR (Status
)) {
958 Status
= SdMmcExecTrb (Private
, Trb
);
959 if (EFI_ERROR (Status
)) {
963 Status
= SdMmcWaitTrbResult (Private
, Trb
);
964 if (EFI_ERROR (Status
)) {
969 if ((Trb
!= NULL
) && (Trb
->AdmaDesc
!= NULL
)) {
970 FreePages (Trb
->AdmaDesc
, Trb
->AdmaPages
);
981 Used to retrieve next slot numbers supported by the SD controller. The function
982 returns information about all available slots (populated or not-populated).
984 The GetNextSlot() function retrieves the next slot number on an SD controller.
985 If on input Slot is 0xFF, then the slot number of the first slot on the SD controller
988 If Slot is a slot number that was returned on a previous call to GetNextSlot(), then
989 the slot number of the next slot on the SD controller is returned.
991 If Slot is not 0xFF and Slot was not returned on a previous call to GetNextSlot(),
992 EFI_INVALID_PARAMETER is returned.
994 If Slot is the slot number of the last slot on the SD controller, then EFI_NOT_FOUND
997 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
998 @param[in,out] Slot On input, a pointer to a slot number on the SD controller.
999 On output, a pointer to the next slot number on the SD controller.
1000 An input value of 0xFF retrieves the first slot number on the SD
1003 @retval EFI_SUCCESS The next slot number on the SD controller was returned in Slot.
1004 @retval EFI_NOT_FOUND There are no more slots on this SD controller.
1005 @retval EFI_INVALID_PARAMETER Slot is not 0xFF and Slot was not returned on a previous call
1011 SdMmcPassThruGetNextSlot (
1012 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1016 SD_MMC_HC_PRIVATE_DATA
*Private
;
1019 if ((This
== NULL
) || (Slot
== NULL
)) {
1020 return EFI_INVALID_PARAMETER
;
1023 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1025 if (*Slot
== 0xFF) {
1026 for (Index
= 0; Index
< SD_MMC_HC_MAX_SLOT
; Index
++) {
1027 if (Private
->Slot
[Index
].Enable
) {
1029 Private
->PreviousSlot
= Index
;
1033 return EFI_NOT_FOUND
;
1034 } else if (*Slot
== Private
->PreviousSlot
) {
1035 for (Index
= *Slot
+ 1; Index
< SD_MMC_HC_MAX_SLOT
; Index
++) {
1036 if (Private
->Slot
[Index
].Enable
) {
1038 Private
->PreviousSlot
= Index
;
1042 return EFI_NOT_FOUND
;
1044 return EFI_INVALID_PARAMETER
;
1049 Used to allocate and build a device path node for an SD card on the SD controller.
1051 The BuildDevicePath() function allocates and builds a single device node for the SD
1052 card specified by Slot.
1054 If the SD card specified by Slot is not present on the SD controller, then EFI_NOT_FOUND
1057 If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned.
1059 If there are not enough resources to allocate the device path node, then EFI_OUT_OF_RESOURCES
1062 Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of
1063 DevicePath are initialized to describe the SD card specified by Slot, and EFI_SUCCESS is
1066 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
1067 @param[in] Slot Specifies the slot number of the SD card for which a device
1068 path node is to be allocated and built.
1069 @param[in,out] DevicePath A pointer to a single device path node that describes the SD
1070 card specified by Slot. This function is responsible for
1071 allocating the buffer DevicePath with the boot service
1072 AllocatePool(). It is the caller's responsibility to free
1073 DevicePath when the caller is finished with DevicePath.
1075 @retval EFI_SUCCESS The device path node that describes the SD card specified by
1076 Slot was allocated and returned in DevicePath.
1077 @retval EFI_NOT_FOUND The SD card specified by Slot does not exist on the SD controller.
1078 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1079 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
1084 SdMmcPassThruBuildDevicePath (
1085 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1087 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
1090 SD_MMC_HC_PRIVATE_DATA
*Private
;
1091 SD_DEVICE_PATH
*SdNode
;
1092 EMMC_DEVICE_PATH
*EmmcNode
;
1094 if ((This
== NULL
) || (DevicePath
== NULL
) || (Slot
>= SD_MMC_HC_MAX_SLOT
)) {
1095 return EFI_INVALID_PARAMETER
;
1098 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1100 if ((!Private
->Slot
[Slot
].Enable
) || (!Private
->Slot
[Slot
].MediaPresent
)) {
1101 return EFI_NOT_FOUND
;
1104 if (Private
->Slot
[Slot
].CardType
== SdCardType
) {
1105 SdNode
= AllocateCopyPool (sizeof (SD_DEVICE_PATH
), &mSdDpTemplate
);
1106 if (SdNode
== NULL
) {
1107 return EFI_OUT_OF_RESOURCES
;
1109 SdNode
->SlotNumber
= Slot
;
1111 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) SdNode
;
1112 } else if (Private
->Slot
[Slot
].CardType
== EmmcCardType
) {
1113 EmmcNode
= AllocateCopyPool (sizeof (EMMC_DEVICE_PATH
), &mEmmcDpTemplate
);
1114 if (EmmcNode
== NULL
) {
1115 return EFI_OUT_OF_RESOURCES
;
1117 EmmcNode
->SlotNumber
= Slot
;
1119 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) EmmcNode
;
1122 // Currently we only support SD and EMMC two device nodes.
1124 return EFI_NOT_FOUND
;
1131 This function retrieves an SD card slot number based on the input device path.
1133 The GetSlotNumber() function retrieves slot number for the SD card specified by
1134 the DevicePath node. If DevicePath is NULL, EFI_INVALID_PARAMETER is returned.
1136 If DevicePath is not a device path node type that the SD Pass Thru driver supports,
1137 EFI_UNSUPPORTED is returned.
1139 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1140 @param[in] DevicePath A pointer to the device path node that describes a SD
1141 card on the SD controller.
1142 @param[out] Slot On return, points to the slot number of an SD card on
1145 @retval EFI_SUCCESS SD card slot number is returned in Slot.
1146 @retval EFI_INVALID_PARAMETER Slot or DevicePath is NULL.
1147 @retval EFI_UNSUPPORTED DevicePath is not a device path node type that the SD
1148 Pass Thru driver supports.
1153 SdMmcPassThruGetSlotNumber (
1154 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1155 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1159 SD_MMC_HC_PRIVATE_DATA
*Private
;
1160 SD_DEVICE_PATH
*SdNode
;
1161 EMMC_DEVICE_PATH
*EmmcNode
;
1164 if ((This
== NULL
) || (DevicePath
== NULL
) || (Slot
== NULL
)) {
1165 return EFI_INVALID_PARAMETER
;
1168 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1171 // Check whether the DevicePath belongs to SD_DEVICE_PATH or EMMC_DEVICE_PATH
1173 if ((DevicePath
->Type
!= MESSAGING_DEVICE_PATH
) ||
1174 ((DevicePath
->SubType
!= MSG_SD_DP
) &&
1175 (DevicePath
->SubType
!= MSG_EMMC_DP
)) ||
1176 (DevicePathNodeLength(DevicePath
) != sizeof(SD_DEVICE_PATH
)) ||
1177 (DevicePathNodeLength(DevicePath
) != sizeof(EMMC_DEVICE_PATH
))) {
1178 return EFI_UNSUPPORTED
;
1181 if (DevicePath
->SubType
== MSG_SD_DP
) {
1182 SdNode
= (SD_DEVICE_PATH
*) DevicePath
;
1183 SlotNumber
= SdNode
->SlotNumber
;
1185 EmmcNode
= (EMMC_DEVICE_PATH
*) DevicePath
;
1186 SlotNumber
= EmmcNode
->SlotNumber
;
1189 if (SlotNumber
>= SD_MMC_HC_MAX_SLOT
) {
1190 return EFI_NOT_FOUND
;
1193 if (Private
->Slot
[SlotNumber
].Enable
) {
1197 return EFI_NOT_FOUND
;
1202 Resets an SD card that is connected to the SD controller.
1204 The ResetDevice() function resets the SD card specified by Slot.
1206 If this SD controller does not support a device reset operation, EFI_UNSUPPORTED is
1209 If Slot is not in a valid slot number for this SD controller, EFI_INVALID_PARAMETER
1212 If the device reset operation is completed, EFI_SUCCESS is returned.
1214 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1215 @param[in] Slot Specifies the slot number of the SD card to be reset.
1217 @retval EFI_SUCCESS The SD card specified by Slot was reset.
1218 @retval EFI_UNSUPPORTED The SD controller does not support a device reset operation.
1219 @retval EFI_INVALID_PARAMETER Slot number is invalid.
1220 @retval EFI_NO_MEDIA SD Device not present in the Slot.
1221 @retval EFI_DEVICE_ERROR The reset command failed due to a device error
1226 SdMmcPassThruResetDevice (
1227 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1231 SD_MMC_HC_PRIVATE_DATA
*Private
;
1233 LIST_ENTRY
*NextLink
;
1238 return EFI_INVALID_PARAMETER
;
1241 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1243 if (!Private
->Slot
[Slot
].Enable
) {
1244 return EFI_INVALID_PARAMETER
;
1247 if (!Private
->Slot
[Slot
].MediaPresent
) {
1248 return EFI_NO_MEDIA
;
1251 // Free all async I/O requests in the queue
1253 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1255 for (Link
= GetFirstNode (&Private
->Queue
);
1256 !IsNull (&Private
->Queue
, Link
);
1258 NextLink
= GetNextNode (&Private
->Queue
, Link
);
1259 RemoveEntryList (Link
);
1260 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
1261 Trb
->Packet
->TransactionStatus
= EFI_ABORTED
;
1262 gBS
->SignalEvent (Trb
->Event
);
1266 gBS
->RestoreTPL (OldTpl
);