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}, {0, UnknownSlot
, 0, 0, 0}, {0, UnknownSlot
, 0, 0, 0},
55 {0, UnknownSlot
, 0, 0, 0}, {0, UnknownSlot
, 0, 0, 0}, {0, UnknownSlot
, 0, 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
) {
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 ((DEBUG_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 ((DEBUG_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
;
243 Private
= (SD_MMC_HC_PRIVATE_DATA
*)Context
;
245 for (Slot
= 0; Slot
< SD_MMC_HC_MAX_SLOT
; Slot
++) {
246 if ((Private
->Slot
[Slot
].Enable
) && (Private
->Slot
[Slot
].SlotType
== RemovableSlot
)) {
247 Status
= SdMmcHcCardDetect (Private
->PciIo
, Slot
, &MediaPresent
);
248 if ((Status
== EFI_MEDIA_CHANGED
) && !MediaPresent
) {
249 DEBUG ((DEBUG_INFO
, "SdMmcPciHcEnumerateDevice: device disconnected at slot %d of pci %p\n", Slot
, Private
->PciIo
));
250 Private
->Slot
[Slot
].MediaPresent
= FALSE
;
251 Private
->Slot
[Slot
].Initialized
= FALSE
;
253 // Signal all async task events at the slot with EFI_NO_MEDIA status.
255 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
256 for (Link
= GetFirstNode (&Private
->Queue
);
257 !IsNull (&Private
->Queue
, Link
);
259 NextLink
= GetNextNode (&Private
->Queue
, Link
);
260 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
261 if (Trb
->Slot
== Slot
) {
262 RemoveEntryList (Link
);
263 Trb
->Packet
->TransactionStatus
= EFI_NO_MEDIA
;
264 gBS
->SignalEvent (Trb
->Event
);
268 gBS
->RestoreTPL (OldTpl
);
270 // Notify the upper layer the connect state change through ReinstallProtocolInterface.
272 gBS
->ReinstallProtocolInterface (
273 Private
->ControllerHandle
,
274 &gEfiSdMmcPassThruProtocolGuid
,
279 if ((Status
== EFI_MEDIA_CHANGED
) && MediaPresent
) {
280 DEBUG ((DEBUG_INFO
, "SdMmcPciHcEnumerateDevice: device connected at slot %d of pci %p\n", Slot
, Private
->PciIo
));
282 // Reset the specified slot of the SD/MMC Pci Host Controller
284 Status
= SdMmcHcReset (Private
->PciIo
, Slot
);
285 if (EFI_ERROR (Status
)) {
289 // Reinitialize slot and restart identification process for the new attached device
291 Status
= SdMmcHcInitHost (Private
->PciIo
, Slot
, Private
->Capability
[Slot
]);
292 if (EFI_ERROR (Status
)) {
296 Private
->Slot
[Slot
].MediaPresent
= TRUE
;
297 Private
->Slot
[Slot
].Initialized
= TRUE
;
298 RoutineNum
= sizeof (mCardTypeDetectRoutineTable
) / sizeof (CARD_TYPE_DETECT_ROUTINE
);
299 for (Index
= 0; Index
< RoutineNum
; Index
++) {
300 Routine
= &mCardTypeDetectRoutineTable
[Index
];
301 if (*Routine
!= NULL
) {
302 Status
= (*Routine
) (Private
, Slot
);
303 if (!EFI_ERROR (Status
)) {
309 // This card doesn't get initialized correctly.
311 if (Index
== RoutineNum
) {
312 Private
->Slot
[Slot
].Initialized
= FALSE
;
316 // Notify the upper layer the connect state change through ReinstallProtocolInterface.
318 gBS
->ReinstallProtocolInterface (
319 Private
->ControllerHandle
,
320 &gEfiSdMmcPassThruProtocolGuid
,
331 Tests to see if this driver supports a given controller. If a child device is provided,
332 it further tests to see if this driver supports creating a handle for the specified child device.
334 This function checks to see if the driver specified by This supports the device specified by
335 ControllerHandle. Drivers will typically use the device path attached to
336 ControllerHandle and/or the services from the bus I/O abstraction attached to
337 ControllerHandle to determine if the driver supports ControllerHandle. This function
338 may be called many times during platform initialization. In order to reduce boot times, the tests
339 performed by this function must be very small, and take as little time as possible to execute. This
340 function must not change the state of any hardware devices, and this function must be aware that the
341 device specified by ControllerHandle may already be managed by the same driver or a
342 different driver. This function must match its calls to AllocatePages() with FreePages(),
343 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
344 Since ControllerHandle may have been previously started by the same driver, if a protocol is
345 already in the opened state, then it must not be closed with CloseProtocol(). This is required
346 to guarantee the state of ControllerHandle is not modified by this function.
348 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
349 @param[in] ControllerHandle The handle of the controller to test. This handle
350 must support a protocol interface that supplies
351 an I/O abstraction to the driver.
352 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
353 parameter is ignored by device drivers, and is optional for bus
354 drivers. For bus drivers, if this parameter is not NULL, then
355 the bus driver must determine if the bus controller specified
356 by ControllerHandle and the child controller specified
357 by RemainingDevicePath are both supported by this
360 @retval EFI_SUCCESS The device specified by ControllerHandle and
361 RemainingDevicePath is supported by the driver specified by This.
362 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
363 RemainingDevicePath is already being managed by the driver
365 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
366 RemainingDevicePath is already being managed by a different
367 driver or an application that requires exclusive access.
368 Currently not implemented.
369 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
370 RemainingDevicePath is not supported by the driver specified by This.
374 SdMmcPciHcDriverBindingSupported (
375 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
376 IN EFI_HANDLE Controller
,
377 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
381 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
382 EFI_PCI_IO_PROTOCOL
*PciIo
;
386 ParentDevicePath
= NULL
;
389 // SdPciHcDxe is a device driver, and should ingore the
390 // "RemainingDevicePath" according to EFI spec.
392 Status
= gBS
->OpenProtocol (
394 &gEfiDevicePathProtocolGuid
,
395 (VOID
*) &ParentDevicePath
,
396 This
->DriverBindingHandle
,
398 EFI_OPEN_PROTOCOL_BY_DRIVER
400 if (EFI_ERROR (Status
)) {
402 // EFI_ALREADY_STARTED is also an error.
407 // Close the protocol because we don't use it here.
411 &gEfiDevicePathProtocolGuid
,
412 This
->DriverBindingHandle
,
417 // Now test the EfiPciIoProtocol.
419 Status
= gBS
->OpenProtocol (
421 &gEfiPciIoProtocolGuid
,
423 This
->DriverBindingHandle
,
425 EFI_OPEN_PROTOCOL_BY_DRIVER
427 if (EFI_ERROR (Status
)) {
432 // Now further check the PCI header: Base class (offset 0x08) and
433 // Sub Class (offset 0x05). This controller should be an SD/MMC PCI
436 Status
= PciIo
->Pci
.Read (
443 if (EFI_ERROR (Status
)) {
446 &gEfiPciIoProtocolGuid
,
447 This
->DriverBindingHandle
,
450 return EFI_UNSUPPORTED
;
453 // Since we already got the PciData, we can close protocol to avoid to carry it
454 // on for multiple exit points.
458 &gEfiPciIoProtocolGuid
,
459 This
->DriverBindingHandle
,
464 // Examine SD PCI Host Controller PCI Configuration table fields.
466 if ((PciData
.Hdr
.ClassCode
[2] == PCI_CLASS_SYSTEM_PERIPHERAL
) &&
467 (PciData
.Hdr
.ClassCode
[1] == PCI_SUBCLASS_SD_HOST_CONTROLLER
) &&
468 ((PciData
.Hdr
.ClassCode
[0] == 0x00) || (PciData
.Hdr
.ClassCode
[0] == 0x01))) {
472 return EFI_UNSUPPORTED
;
476 Starts a device controller or a bus controller.
478 The Start() function is designed to be invoked from the EFI boot service ConnectController().
479 As a result, much of the error checking on the parameters to Start() has been moved into this
480 common boot service. It is legal to call Start() from other locations,
481 but the following calling restrictions must be followed or the system behavior will not be deterministic.
482 1. ControllerHandle must be a valid EFI_HANDLE.
483 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
484 EFI_DEVICE_PATH_PROTOCOL.
485 3. Prior to calling Start(), the Supported() function for the driver specified by This must
486 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
488 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
489 @param[in] ControllerHandle The handle of the controller to start. This handle
490 must support a protocol interface that supplies
491 an I/O abstraction to the driver.
492 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
493 parameter is ignored by device drivers, and is optional for bus
494 drivers. For a bus driver, if this parameter is NULL, then handles
495 for all the children of Controller are created by this driver.
496 If this parameter is not NULL and the first Device Path Node is
497 not the End of Device Path Node, then only the handle for the
498 child device specified by the first Device Path Node of
499 RemainingDevicePath is created by this driver.
500 If the first Device Path Node of RemainingDevicePath is
501 the End of Device Path Node, no child handle is created by this
504 @retval EFI_SUCCESS The device was started.
505 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
506 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
507 @retval Others The driver failded to start the device.
512 SdMmcPciHcDriverBindingStart (
513 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
514 IN EFI_HANDLE Controller
,
515 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
519 SD_MMC_HC_PRIVATE_DATA
*Private
;
520 EFI_PCI_IO_PROTOCOL
*PciIo
;
522 UINT64 PciAttributes
;
527 CARD_TYPE_DETECT_ROUTINE
*Routine
;
529 BOOLEAN MediaPresent
;
530 BOOLEAN Support64BitDma
;
532 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStart: Start\n"));
535 // Open PCI I/O Protocol and save pointer to open protocol
536 // in private data area.
539 Status
= gBS
->OpenProtocol (
541 &gEfiPciIoProtocolGuid
,
543 This
->DriverBindingHandle
,
545 EFI_OPEN_PROTOCOL_BY_DRIVER
547 if (EFI_ERROR (Status
)) {
552 // Enable the SD Host Controller MMIO space
555 Status
= PciIo
->Attributes (
557 EfiPciIoAttributeOperationGet
,
562 if (EFI_ERROR (Status
)) {
566 Status
= PciIo
->Attributes (
568 EfiPciIoAttributeOperationSupported
,
573 if (!EFI_ERROR (Status
)) {
574 Supports
&= (UINT64
)EFI_PCI_DEVICE_ENABLE
;
575 Status
= PciIo
->Attributes (
577 EfiPciIoAttributeOperationEnable
,
585 Private
= AllocateCopyPool (sizeof (SD_MMC_HC_PRIVATE_DATA
), &gSdMmcPciHcTemplate
);
586 if (Private
== NULL
) {
587 Status
= EFI_OUT_OF_RESOURCES
;
591 Private
->ControllerHandle
= Controller
;
592 Private
->PciIo
= PciIo
;
593 Private
->PciAttributes
= PciAttributes
;
594 InitializeListHead (&Private
->Queue
);
597 // Get SD/MMC Pci Host Controller Slot info
599 Status
= SdMmcHcGetSlotInfo (PciIo
, &FirstBar
, &SlotNum
);
600 if (EFI_ERROR (Status
)) {
604 Support64BitDma
= TRUE
;
605 for (Slot
= FirstBar
; Slot
< (FirstBar
+ SlotNum
); Slot
++) {
606 Private
->Slot
[Slot
].Enable
= TRUE
;
608 Status
= SdMmcHcGetCapability (PciIo
, Slot
, &Private
->Capability
[Slot
]);
609 if (EFI_ERROR (Status
)) {
612 DumpCapabilityReg (Slot
, &Private
->Capability
[Slot
]);
614 Support64BitDma
&= Private
->Capability
[Slot
].SysBus64
;
616 Status
= SdMmcHcGetMaxCurrent (PciIo
, Slot
, &Private
->MaxCurrent
[Slot
]);
617 if (EFI_ERROR (Status
)) {
621 Private
->Slot
[Slot
].SlotType
= Private
->Capability
[Slot
].SlotType
;
622 if ((Private
->Slot
[Slot
].SlotType
!= RemovableSlot
) && (Private
->Slot
[Slot
].SlotType
!= EmbeddedSlot
)) {
623 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDxe doesn't support the slot type [%d]!!!\n", Private
->Slot
[Slot
].SlotType
));
628 // Reset the specified slot of the SD/MMC Pci Host Controller
630 Status
= SdMmcHcReset (PciIo
, Slot
);
631 if (EFI_ERROR (Status
)) {
635 // Check whether there is a SD/MMC card attached
637 Status
= SdMmcHcCardDetect (PciIo
, Slot
, &MediaPresent
);
638 if (EFI_ERROR (Status
) && (Status
!= EFI_MEDIA_CHANGED
)) {
640 } else if (!MediaPresent
) {
641 DEBUG ((EFI_ERROR
, "SdMmcHcCardDetect: No device attached in Slot[%d]!!!\n", Slot
));
645 Status
= SdMmcHcInitHost (PciIo
, Slot
, Private
->Capability
[Slot
]);
646 if (EFI_ERROR (Status
)) {
650 Private
->Slot
[Slot
].MediaPresent
= TRUE
;
651 Private
->Slot
[Slot
].Initialized
= TRUE
;
652 RoutineNum
= sizeof (mCardTypeDetectRoutineTable
) / sizeof (CARD_TYPE_DETECT_ROUTINE
);
653 for (Index
= 0; Index
< RoutineNum
; Index
++) {
654 Routine
= &mCardTypeDetectRoutineTable
[Index
];
655 if (*Routine
!= NULL
) {
656 Status
= (*Routine
) (Private
, Slot
);
657 if (!EFI_ERROR (Status
)) {
663 // This card doesn't get initialized correctly.
665 if (Index
== RoutineNum
) {
666 Private
->Slot
[Slot
].Initialized
= FALSE
;
671 // Enable 64-bit DMA support in the PCI layer if this controller
674 if (Support64BitDma
) {
675 Status
= PciIo
->Attributes (
677 EfiPciIoAttributeOperationEnable
,
678 EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE
,
681 if (EFI_ERROR (Status
)) {
682 DEBUG ((DEBUG_WARN
, "SdMmcPciHcDriverBindingStart: failed to enable 64-bit DMA (%r)\n", Status
));
687 // Start the asynchronous I/O monitor
689 Status
= gBS
->CreateEvent (
690 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
692 ProcessAsyncTaskList
,
696 if (EFI_ERROR (Status
)) {
700 Status
= gBS
->SetTimer (Private
->TimerEvent
, TimerPeriodic
, SD_MMC_HC_ASYNC_TIMER
);
701 if (EFI_ERROR (Status
)) {
706 // Start the Sd removable device connection enumeration
708 Status
= gBS
->CreateEvent (
709 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
711 SdMmcPciHcEnumerateDevice
,
713 &Private
->ConnectEvent
715 if (EFI_ERROR (Status
)) {
719 Status
= gBS
->SetTimer (Private
->ConnectEvent
, TimerPeriodic
, SD_MMC_HC_ENUM_TIMER
);
720 if (EFI_ERROR (Status
)) {
724 Status
= gBS
->InstallMultipleProtocolInterfaces (
726 &gEfiSdMmcPassThruProtocolGuid
,
727 &(Private
->PassThru
),
731 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStart: %r End on %x\n", Status
, Controller
));
734 if (EFI_ERROR (Status
)) {
735 if ((Private
!= NULL
) && (Private
->PciAttributes
!= 0)) {
737 // Restore original PCI attributes
741 EfiPciIoAttributeOperationSet
,
742 Private
->PciAttributes
,
748 &gEfiPciIoProtocolGuid
,
749 This
->DriverBindingHandle
,
753 if ((Private
!= NULL
) && (Private
->TimerEvent
!= NULL
)) {
754 gBS
->CloseEvent (Private
->TimerEvent
);
757 if ((Private
!= NULL
) && (Private
->ConnectEvent
!= NULL
)) {
758 gBS
->CloseEvent (Private
->ConnectEvent
);
761 if (Private
!= NULL
) {
770 Stops a device controller or a bus controller.
772 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
773 As a result, much of the error checking on the parameters to Stop() has been moved
774 into this common boot service. It is legal to call Stop() from other locations,
775 but the following calling restrictions must be followed or the system behavior will not be deterministic.
776 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
777 same driver's Start() function.
778 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
779 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
780 Start() function, and the Start() function must have called OpenProtocol() on
781 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
783 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
784 @param[in] ControllerHandle A handle to the device being stopped. The handle must
785 support a bus specific I/O protocol for the driver
786 to use to stop the device.
787 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
788 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
789 if NumberOfChildren is 0.
791 @retval EFI_SUCCESS The device was stopped.
792 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
797 SdMmcPciHcDriverBindingStop (
798 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
799 IN EFI_HANDLE Controller
,
800 IN UINTN NumberOfChildren
,
801 IN EFI_HANDLE
*ChildHandleBuffer
805 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
806 SD_MMC_HC_PRIVATE_DATA
*Private
;
807 EFI_PCI_IO_PROTOCOL
*PciIo
;
809 LIST_ENTRY
*NextLink
;
812 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStop: Start\n"));
814 Status
= gBS
->OpenProtocol (
816 &gEfiSdMmcPassThruProtocolGuid
,
818 This
->DriverBindingHandle
,
820 EFI_OPEN_PROTOCOL_GET_PROTOCOL
822 if (EFI_ERROR (Status
)) {
826 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (PassThru
);
828 // Close Non-Blocking timer and free Task list.
830 if (Private
->TimerEvent
!= NULL
) {
831 gBS
->CloseEvent (Private
->TimerEvent
);
832 Private
->TimerEvent
= NULL
;
834 if (Private
->ConnectEvent
!= NULL
) {
835 gBS
->CloseEvent (Private
->ConnectEvent
);
836 Private
->ConnectEvent
= NULL
;
839 // As the timer is closed, there is no needs to use TPL lock to
840 // protect the critical region "queue".
842 for (Link
= GetFirstNode (&Private
->Queue
);
843 !IsNull (&Private
->Queue
, Link
);
845 NextLink
= GetNextNode (&Private
->Queue
, Link
);
846 RemoveEntryList (Link
);
847 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
848 Trb
->Packet
->TransactionStatus
= EFI_ABORTED
;
849 gBS
->SignalEvent (Trb
->Event
);
854 // Uninstall Block I/O protocol from the device handle
856 Status
= gBS
->UninstallProtocolInterface (
858 &gEfiSdMmcPassThruProtocolGuid
,
862 if (EFI_ERROR (Status
)) {
868 &gEfiPciIoProtocolGuid
,
869 This
->DriverBindingHandle
,
873 // Restore original PCI attributes
875 PciIo
= Private
->PciIo
;
876 Status
= PciIo
->Attributes (
878 EfiPciIoAttributeOperationSet
,
879 Private
->PciAttributes
,
882 ASSERT_EFI_ERROR (Status
);
886 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStop: End with %r\n", Status
));
892 Sends SD command to an SD card that is attached to the SD controller.
894 The PassThru() function sends the SD command specified by Packet to the SD card
897 If Packet is successfully sent to the SD card, then EFI_SUCCESS is returned.
899 If a device error occurs while sending the Packet, then EFI_DEVICE_ERROR is returned.
901 If Slot is not in a valid range for the SD controller, then EFI_INVALID_PARAMETER
904 If Packet defines a data command but both InDataBuffer and OutDataBuffer are NULL,
905 EFI_INVALID_PARAMETER is returned.
907 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
908 @param[in] Slot The slot number of the SD card to send the command to.
909 @param[in,out] Packet A pointer to the SD command data structure.
910 @param[in] Event If Event is NULL, blocking I/O is performed. If Event is
911 not NULL, then nonblocking I/O is performed, and Event
912 will be signaled when the Packet completes.
914 @retval EFI_SUCCESS The SD Command Packet was sent by the host.
915 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SD
917 @retval EFI_INVALID_PARAMETER Packet, Slot, or the contents of the Packet is invalid.
918 @retval EFI_INVALID_PARAMETER Packet defines a data command but both InDataBuffer and
919 OutDataBuffer are NULL.
920 @retval EFI_NO_MEDIA SD Device not present in the Slot.
921 @retval EFI_UNSUPPORTED The command described by the SD Command Packet is not
922 supported by the host controller.
923 @retval EFI_BAD_BUFFER_SIZE The InTransferLength or OutTransferLength exceeds the
924 limit supported by SD card ( i.e. if the number of bytes
925 exceed the Last LBA).
930 SdMmcPassThruPassThru (
931 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
933 IN OUT EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
,
934 IN EFI_EVENT Event OPTIONAL
938 SD_MMC_HC_PRIVATE_DATA
*Private
;
942 if ((This
== NULL
) || (Packet
== NULL
)) {
943 return EFI_INVALID_PARAMETER
;
946 if ((Packet
->SdMmcCmdBlk
== NULL
) || (Packet
->SdMmcStatusBlk
== NULL
)) {
947 return EFI_INVALID_PARAMETER
;
950 if ((Packet
->OutDataBuffer
== NULL
) && (Packet
->OutTransferLength
!= 0)) {
951 return EFI_INVALID_PARAMETER
;
954 if ((Packet
->InDataBuffer
== NULL
) && (Packet
->InTransferLength
!= 0)) {
955 return EFI_INVALID_PARAMETER
;
958 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
960 if (!Private
->Slot
[Slot
].Enable
) {
961 return EFI_INVALID_PARAMETER
;
964 if (!Private
->Slot
[Slot
].MediaPresent
) {
968 if (!Private
->Slot
[Slot
].Initialized
) {
969 return EFI_DEVICE_ERROR
;
972 Trb
= SdMmcCreateTrb (Private
, Slot
, Packet
, Event
);
974 return EFI_OUT_OF_RESOURCES
;
977 // Immediately return for async I/O.
984 // Wait async I/O list is empty before execute sync I/O operation.
987 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
988 if (IsListEmpty (&Private
->Queue
)) {
989 gBS
->RestoreTPL (OldTpl
);
992 gBS
->RestoreTPL (OldTpl
);
995 Status
= SdMmcWaitTrbEnv (Private
, Trb
);
996 if (EFI_ERROR (Status
)) {
1000 Status
= SdMmcExecTrb (Private
, Trb
);
1001 if (EFI_ERROR (Status
)) {
1005 Status
= SdMmcWaitTrbResult (Private
, Trb
);
1006 if (EFI_ERROR (Status
)) {
1011 if ((Trb
!= NULL
) && (Trb
->AdmaDesc
!= NULL
)) {
1012 FreePages (Trb
->AdmaDesc
, Trb
->AdmaPages
);
1023 Used to retrieve next slot numbers supported by the SD controller. The function
1024 returns information about all available slots (populated or not-populated).
1026 The GetNextSlot() function retrieves the next slot number on an SD controller.
1027 If on input Slot is 0xFF, then the slot number of the first slot on the SD controller
1030 If Slot is a slot number that was returned on a previous call to GetNextSlot(), then
1031 the slot number of the next slot on the SD controller is returned.
1033 If Slot is not 0xFF and Slot was not returned on a previous call to GetNextSlot(),
1034 EFI_INVALID_PARAMETER is returned.
1036 If Slot is the slot number of the last slot on the SD controller, then EFI_NOT_FOUND
1039 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
1040 @param[in,out] Slot On input, a pointer to a slot number on the SD controller.
1041 On output, a pointer to the next slot number on the SD controller.
1042 An input value of 0xFF retrieves the first slot number on the SD
1045 @retval EFI_SUCCESS The next slot number on the SD controller was returned in Slot.
1046 @retval EFI_NOT_FOUND There are no more slots on this SD controller.
1047 @retval EFI_INVALID_PARAMETER Slot is not 0xFF and Slot was not returned on a previous call
1053 SdMmcPassThruGetNextSlot (
1054 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1058 SD_MMC_HC_PRIVATE_DATA
*Private
;
1061 if ((This
== NULL
) || (Slot
== NULL
)) {
1062 return EFI_INVALID_PARAMETER
;
1065 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1067 if (*Slot
== 0xFF) {
1068 for (Index
= 0; Index
< SD_MMC_HC_MAX_SLOT
; Index
++) {
1069 if (Private
->Slot
[Index
].Enable
) {
1071 Private
->PreviousSlot
= Index
;
1075 return EFI_NOT_FOUND
;
1076 } else if (*Slot
== Private
->PreviousSlot
) {
1077 for (Index
= *Slot
+ 1; Index
< SD_MMC_HC_MAX_SLOT
; Index
++) {
1078 if (Private
->Slot
[Index
].Enable
) {
1080 Private
->PreviousSlot
= Index
;
1084 return EFI_NOT_FOUND
;
1086 return EFI_INVALID_PARAMETER
;
1091 Used to allocate and build a device path node for an SD card on the SD controller.
1093 The BuildDevicePath() function allocates and builds a single device node for the SD
1094 card specified by Slot.
1096 If the SD card specified by Slot is not present on the SD controller, then EFI_NOT_FOUND
1099 If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned.
1101 If there are not enough resources to allocate the device path node, then EFI_OUT_OF_RESOURCES
1104 Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of
1105 DevicePath are initialized to describe the SD card specified by Slot, and EFI_SUCCESS is
1108 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
1109 @param[in] Slot Specifies the slot number of the SD card for which a device
1110 path node is to be allocated and built.
1111 @param[in,out] DevicePath A pointer to a single device path node that describes the SD
1112 card specified by Slot. This function is responsible for
1113 allocating the buffer DevicePath with the boot service
1114 AllocatePool(). It is the caller's responsibility to free
1115 DevicePath when the caller is finished with DevicePath.
1117 @retval EFI_SUCCESS The device path node that describes the SD card specified by
1118 Slot was allocated and returned in DevicePath.
1119 @retval EFI_NOT_FOUND The SD card specified by Slot does not exist on the SD controller.
1120 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1121 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
1126 SdMmcPassThruBuildDevicePath (
1127 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1129 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
1132 SD_MMC_HC_PRIVATE_DATA
*Private
;
1133 SD_DEVICE_PATH
*SdNode
;
1134 EMMC_DEVICE_PATH
*EmmcNode
;
1136 if ((This
== NULL
) || (DevicePath
== NULL
) || (Slot
>= SD_MMC_HC_MAX_SLOT
)) {
1137 return EFI_INVALID_PARAMETER
;
1140 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1142 if ((!Private
->Slot
[Slot
].Enable
) || (!Private
->Slot
[Slot
].MediaPresent
)) {
1143 return EFI_NOT_FOUND
;
1146 if (Private
->Slot
[Slot
].CardType
== SdCardType
) {
1147 SdNode
= AllocateCopyPool (sizeof (SD_DEVICE_PATH
), &mSdDpTemplate
);
1148 if (SdNode
== NULL
) {
1149 return EFI_OUT_OF_RESOURCES
;
1151 SdNode
->SlotNumber
= Slot
;
1153 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) SdNode
;
1154 } else if (Private
->Slot
[Slot
].CardType
== EmmcCardType
) {
1155 EmmcNode
= AllocateCopyPool (sizeof (EMMC_DEVICE_PATH
), &mEmmcDpTemplate
);
1156 if (EmmcNode
== NULL
) {
1157 return EFI_OUT_OF_RESOURCES
;
1159 EmmcNode
->SlotNumber
= Slot
;
1161 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) EmmcNode
;
1164 // Currently we only support SD and EMMC two device nodes.
1166 return EFI_NOT_FOUND
;
1173 This function retrieves an SD card slot number based on the input device path.
1175 The GetSlotNumber() function retrieves slot number for the SD card specified by
1176 the DevicePath node. If DevicePath is NULL, EFI_INVALID_PARAMETER is returned.
1178 If DevicePath is not a device path node type that the SD Pass Thru driver supports,
1179 EFI_UNSUPPORTED is returned.
1181 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1182 @param[in] DevicePath A pointer to the device path node that describes a SD
1183 card on the SD controller.
1184 @param[out] Slot On return, points to the slot number of an SD card on
1187 @retval EFI_SUCCESS SD card slot number is returned in Slot.
1188 @retval EFI_INVALID_PARAMETER Slot or DevicePath is NULL.
1189 @retval EFI_UNSUPPORTED DevicePath is not a device path node type that the SD
1190 Pass Thru driver supports.
1195 SdMmcPassThruGetSlotNumber (
1196 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1197 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1201 SD_MMC_HC_PRIVATE_DATA
*Private
;
1202 SD_DEVICE_PATH
*SdNode
;
1203 EMMC_DEVICE_PATH
*EmmcNode
;
1206 if ((This
== NULL
) || (DevicePath
== NULL
) || (Slot
== NULL
)) {
1207 return EFI_INVALID_PARAMETER
;
1210 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1213 // Check whether the DevicePath belongs to SD_DEVICE_PATH or EMMC_DEVICE_PATH
1215 if ((DevicePath
->Type
!= MESSAGING_DEVICE_PATH
) ||
1216 ((DevicePath
->SubType
!= MSG_SD_DP
) &&
1217 (DevicePath
->SubType
!= MSG_EMMC_DP
)) ||
1218 (DevicePathNodeLength(DevicePath
) != sizeof(SD_DEVICE_PATH
)) ||
1219 (DevicePathNodeLength(DevicePath
) != sizeof(EMMC_DEVICE_PATH
))) {
1220 return EFI_UNSUPPORTED
;
1223 if (DevicePath
->SubType
== MSG_SD_DP
) {
1224 SdNode
= (SD_DEVICE_PATH
*) DevicePath
;
1225 SlotNumber
= SdNode
->SlotNumber
;
1227 EmmcNode
= (EMMC_DEVICE_PATH
*) DevicePath
;
1228 SlotNumber
= EmmcNode
->SlotNumber
;
1231 if (SlotNumber
>= SD_MMC_HC_MAX_SLOT
) {
1232 return EFI_NOT_FOUND
;
1235 if (Private
->Slot
[SlotNumber
].Enable
) {
1239 return EFI_NOT_FOUND
;
1244 Resets an SD card that is connected to the SD controller.
1246 The ResetDevice() function resets the SD card specified by Slot.
1248 If this SD controller does not support a device reset operation, EFI_UNSUPPORTED is
1251 If Slot is not in a valid slot number for this SD controller, EFI_INVALID_PARAMETER
1254 If the device reset operation is completed, EFI_SUCCESS is returned.
1256 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1257 @param[in] Slot Specifies the slot number of the SD card to be reset.
1259 @retval EFI_SUCCESS The SD card specified by Slot was reset.
1260 @retval EFI_UNSUPPORTED The SD controller does not support a device reset operation.
1261 @retval EFI_INVALID_PARAMETER Slot number is invalid.
1262 @retval EFI_NO_MEDIA SD Device not present in the Slot.
1263 @retval EFI_DEVICE_ERROR The reset command failed due to a device error
1268 SdMmcPassThruResetDevice (
1269 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1273 SD_MMC_HC_PRIVATE_DATA
*Private
;
1275 LIST_ENTRY
*NextLink
;
1280 return EFI_INVALID_PARAMETER
;
1283 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1285 if (!Private
->Slot
[Slot
].Enable
) {
1286 return EFI_INVALID_PARAMETER
;
1289 if (!Private
->Slot
[Slot
].MediaPresent
) {
1290 return EFI_NO_MEDIA
;
1293 if (!Private
->Slot
[Slot
].Initialized
) {
1294 return EFI_DEVICE_ERROR
;
1297 // Free all async I/O requests in the queue
1299 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1301 for (Link
= GetFirstNode (&Private
->Queue
);
1302 !IsNull (&Private
->Queue
, Link
);
1304 NextLink
= GetNextNode (&Private
->Queue
, Link
);
1305 RemoveEntryList (Link
);
1306 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
1307 Trb
->Packet
->TransactionStatus
= EFI_ABORTED
;
1308 gBS
->SignalEvent (Trb
->Event
);
1312 gBS
->RestoreTPL (OldTpl
);