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 plus the 64-bit
4 System Addressing support in SD Host Controller Simplified Specification version
7 It would expose EFI_SD_MMC_PASS_THRU_PROTOCOL for upper layer use.
9 Copyright (c) 2018-2019, NVIDIA CORPORATION. All rights reserved.
10 Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.<BR>
11 SPDX-License-Identifier: BSD-2-Clause-Patent
15 #include "SdMmcPciHcDxe.h"
17 EDKII_SD_MMC_OVERRIDE
*mOverride
;
20 // Driver Global Variables
22 EFI_DRIVER_BINDING_PROTOCOL gSdMmcPciHcDriverBinding
= {
23 SdMmcPciHcDriverBindingSupported
,
24 SdMmcPciHcDriverBindingStart
,
25 SdMmcPciHcDriverBindingStop
,
32 // Template for SD/MMC host controller private data.
34 SD_MMC_HC_PRIVATE_DATA gSdMmcPciHcTemplate
= {
35 SD_MMC_HC_PRIVATE_SIGNATURE
, // Signature
36 NULL
, // ControllerHandle
40 SdMmcPassThruPassThru
,
41 SdMmcPassThruGetNextSlot
,
42 SdMmcPassThruBuildDevicePath
,
43 SdMmcPassThruGetSlotNumber
,
44 SdMmcPassThruResetDevice
51 INITIALIZE_LIST_HEAD_VARIABLE (gSdMmcPciHcTemplate
.Queue
),
53 {0, UnknownSlot
, 0, 0, 0}, {0, UnknownSlot
, 0, 0, 0}, {0, UnknownSlot
, 0, 0, 0},
54 {0, UnknownSlot
, 0, 0, 0}, {0, UnknownSlot
, 0, 0, 0}, {0, UnknownSlot
, 0, 0, 0}
63 0 // ControllerVersion
67 SD_DEVICE_PATH mSdDpTemplate
= {
69 MESSAGING_DEVICE_PATH
,
72 (UINT8
) (sizeof (SD_DEVICE_PATH
)),
73 (UINT8
) ((sizeof (SD_DEVICE_PATH
)) >> 8)
79 EMMC_DEVICE_PATH mEmmcDpTemplate
= {
81 MESSAGING_DEVICE_PATH
,
84 (UINT8
) (sizeof (EMMC_DEVICE_PATH
)),
85 (UINT8
) ((sizeof (EMMC_DEVICE_PATH
)) >> 8)
92 // Prioritized function list to detect card type.
93 // User could add other card detection logic here.
95 CARD_TYPE_DETECT_ROUTINE mCardTypeDetectRoutineTable
[] = {
102 The entry point for SD host controller driver, used to install this driver on the ImageHandle.
104 @param[in] ImageHandle The firmware allocated handle for this driver image.
105 @param[in] SystemTable Pointer to the EFI system table.
107 @retval EFI_SUCCESS Driver loaded.
108 @retval other Driver not loaded.
113 InitializeSdMmcPciHcDxe (
114 IN EFI_HANDLE ImageHandle
,
115 IN EFI_SYSTEM_TABLE
*SystemTable
120 Status
= EfiLibInstallDriverBindingComponentName2 (
123 &gSdMmcPciHcDriverBinding
,
125 &gSdMmcPciHcComponentName
,
126 &gSdMmcPciHcComponentName2
128 ASSERT_EFI_ERROR (Status
);
134 Call back function when the timer event is signaled.
136 @param[in] Event The Event this notify function registered to.
137 @param[in] Context Pointer to the context data registered to the
143 ProcessAsyncTaskList (
148 SD_MMC_HC_PRIVATE_DATA
*Private
;
152 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
153 BOOLEAN InfiniteWait
;
156 Private
= (SD_MMC_HC_PRIVATE_DATA
*)Context
;
159 // Check if the first entry in the async I/O queue is done or not.
161 Status
= EFI_SUCCESS
;
163 Link
= GetFirstNode (&Private
->Queue
);
164 if (!IsNull (&Private
->Queue
, Link
)) {
165 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
166 if (!Private
->Slot
[Trb
->Slot
].MediaPresent
) {
167 Status
= EFI_NO_MEDIA
;
172 // Check whether the cmd/data line is ready for transfer.
174 Status
= SdMmcCheckTrbEnv (Private
, Trb
);
175 if (!EFI_ERROR (Status
)) {
177 Status
= SdMmcExecTrb (Private
, Trb
);
178 if (EFI_ERROR (Status
)) {
185 Status
= SdMmcCheckTrbResult (Private
, Trb
);
189 if ((Trb
!= NULL
) && (Status
== EFI_NOT_READY
)) {
190 Packet
= Trb
->Packet
;
191 if (Packet
->Timeout
== 0) {
194 InfiniteWait
= FALSE
;
196 if ((!InfiniteWait
) && (Trb
->Timeout
-- == 0)) {
197 RemoveEntryList (Link
);
198 Trb
->Packet
->TransactionStatus
= EFI_TIMEOUT
;
199 TrbEvent
= Trb
->Event
;
201 DEBUG ((DEBUG_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p EFI_TIMEOUT\n", TrbEvent
));
202 gBS
->SignalEvent (TrbEvent
);
206 if ((Trb
!= NULL
) && (Status
!= EFI_NOT_READY
)) {
207 RemoveEntryList (Link
);
208 Trb
->Packet
->TransactionStatus
= Status
;
209 TrbEvent
= Trb
->Event
;
211 DEBUG ((DEBUG_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p with %r\n", TrbEvent
, Status
));
212 gBS
->SignalEvent (TrbEvent
);
218 Sd removable device enumeration callback function when the timer event is signaled.
220 @param[in] Event The Event this notify function registered to.
221 @param[in] Context Pointer to the context data registered to the
227 SdMmcPciHcEnumerateDevice (
232 SD_MMC_HC_PRIVATE_DATA
*Private
;
235 BOOLEAN MediaPresent
;
237 CARD_TYPE_DETECT_ROUTINE
*Routine
;
240 LIST_ENTRY
*NextLink
;
244 Private
= (SD_MMC_HC_PRIVATE_DATA
*)Context
;
246 for (Slot
= 0; Slot
< SD_MMC_HC_MAX_SLOT
; Slot
++) {
247 if ((Private
->Slot
[Slot
].Enable
) && (Private
->Slot
[Slot
].SlotType
== RemovableSlot
)) {
248 Status
= SdMmcHcCardDetect (Private
->PciIo
, Slot
, &MediaPresent
);
249 if ((Status
== EFI_MEDIA_CHANGED
) && !MediaPresent
) {
250 DEBUG ((DEBUG_INFO
, "SdMmcPciHcEnumerateDevice: device disconnected at slot %d of pci %p\n", Slot
, Private
->PciIo
));
251 Private
->Slot
[Slot
].MediaPresent
= FALSE
;
252 Private
->Slot
[Slot
].Initialized
= FALSE
;
254 // Signal all async task events at the slot with EFI_NO_MEDIA status.
256 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
257 for (Link
= GetFirstNode (&Private
->Queue
);
258 !IsNull (&Private
->Queue
, Link
);
260 NextLink
= GetNextNode (&Private
->Queue
, Link
);
261 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
262 if (Trb
->Slot
== Slot
) {
263 RemoveEntryList (Link
);
264 Trb
->Packet
->TransactionStatus
= EFI_NO_MEDIA
;
265 gBS
->SignalEvent (Trb
->Event
);
269 gBS
->RestoreTPL (OldTpl
);
271 // Notify the upper layer the connect state change through ReinstallProtocolInterface.
273 gBS
->ReinstallProtocolInterface (
274 Private
->ControllerHandle
,
275 &gEfiSdMmcPassThruProtocolGuid
,
280 if ((Status
== EFI_MEDIA_CHANGED
) && MediaPresent
) {
281 DEBUG ((DEBUG_INFO
, "SdMmcPciHcEnumerateDevice: device connected at slot %d of pci %p\n", Slot
, Private
->PciIo
));
283 // Reset the specified slot of the SD/MMC Pci Host Controller
285 Status
= SdMmcHcReset (Private
, Slot
);
286 if (EFI_ERROR (Status
)) {
290 // Reinitialize slot and restart identification process for the new attached device
292 Status
= SdMmcHcInitHost (Private
, Slot
);
293 if (EFI_ERROR (Status
)) {
297 Private
->Slot
[Slot
].MediaPresent
= TRUE
;
298 Private
->Slot
[Slot
].Initialized
= TRUE
;
299 RoutineNum
= sizeof (mCardTypeDetectRoutineTable
) / sizeof (CARD_TYPE_DETECT_ROUTINE
);
300 for (Index
= 0; Index
< RoutineNum
; Index
++) {
301 Routine
= &mCardTypeDetectRoutineTable
[Index
];
302 if (*Routine
!= NULL
) {
303 Status
= (*Routine
) (Private
, Slot
);
304 if (!EFI_ERROR (Status
)) {
310 // This card doesn't get initialized correctly.
312 if (Index
== RoutineNum
) {
313 Private
->Slot
[Slot
].Initialized
= FALSE
;
317 // Notify the upper layer the connect state change through ReinstallProtocolInterface.
319 gBS
->ReinstallProtocolInterface (
320 Private
->ControllerHandle
,
321 &gEfiSdMmcPassThruProtocolGuid
,
332 Tests to see if this driver supports a given controller. If a child device is provided,
333 it further tests to see if this driver supports creating a handle for the specified child device.
335 This function checks to see if the driver specified by This supports the device specified by
336 ControllerHandle. Drivers will typically use the device path attached to
337 ControllerHandle and/or the services from the bus I/O abstraction attached to
338 ControllerHandle to determine if the driver supports ControllerHandle. This function
339 may be called many times during platform initialization. In order to reduce boot times, the tests
340 performed by this function must be very small, and take as little time as possible to execute. This
341 function must not change the state of any hardware devices, and this function must be aware that the
342 device specified by ControllerHandle may already be managed by the same driver or a
343 different driver. This function must match its calls to AllocatePages() with FreePages(),
344 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
345 Since ControllerHandle may have been previously started by the same driver, if a protocol is
346 already in the opened state, then it must not be closed with CloseProtocol(). This is required
347 to guarantee the state of ControllerHandle is not modified by this function.
349 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
350 @param[in] ControllerHandle The handle of the controller to test. This handle
351 must support a protocol interface that supplies
352 an I/O abstraction to the driver.
353 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
354 parameter is ignored by device drivers, and is optional for bus
355 drivers. For bus drivers, if this parameter is not NULL, then
356 the bus driver must determine if the bus controller specified
357 by ControllerHandle and the child controller specified
358 by RemainingDevicePath are both supported by this
361 @retval EFI_SUCCESS The device specified by ControllerHandle and
362 RemainingDevicePath is supported by the driver specified by This.
363 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
364 RemainingDevicePath is already being managed by the driver
366 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
367 RemainingDevicePath is already being managed by a different
368 driver or an application that requires exclusive access.
369 Currently not implemented.
370 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
371 RemainingDevicePath is not supported by the driver specified by This.
375 SdMmcPciHcDriverBindingSupported (
376 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
377 IN EFI_HANDLE Controller
,
378 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
382 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
383 EFI_PCI_IO_PROTOCOL
*PciIo
;
387 ParentDevicePath
= NULL
;
390 // SdPciHcDxe is a device driver, and should ingore the
391 // "RemainingDevicePath" according to EFI spec.
393 Status
= gBS
->OpenProtocol (
395 &gEfiDevicePathProtocolGuid
,
396 (VOID
*) &ParentDevicePath
,
397 This
->DriverBindingHandle
,
399 EFI_OPEN_PROTOCOL_BY_DRIVER
401 if (EFI_ERROR (Status
)) {
403 // EFI_ALREADY_STARTED is also an error.
408 // Close the protocol because we don't use it here.
412 &gEfiDevicePathProtocolGuid
,
413 This
->DriverBindingHandle
,
418 // Now test the EfiPciIoProtocol.
420 Status
= gBS
->OpenProtocol (
422 &gEfiPciIoProtocolGuid
,
424 This
->DriverBindingHandle
,
426 EFI_OPEN_PROTOCOL_BY_DRIVER
428 if (EFI_ERROR (Status
)) {
433 // Now further check the PCI header: Base class (offset 0x08) and
434 // Sub Class (offset 0x05). This controller should be an SD/MMC PCI
437 Status
= PciIo
->Pci
.Read (
444 if (EFI_ERROR (Status
)) {
447 &gEfiPciIoProtocolGuid
,
448 This
->DriverBindingHandle
,
451 return EFI_UNSUPPORTED
;
454 // Since we already got the PciData, we can close protocol to avoid to carry it
455 // on for multiple exit points.
459 &gEfiPciIoProtocolGuid
,
460 This
->DriverBindingHandle
,
465 // Examine SD PCI Host Controller PCI Configuration table fields.
467 if ((PciData
.Hdr
.ClassCode
[2] == PCI_CLASS_SYSTEM_PERIPHERAL
) &&
468 (PciData
.Hdr
.ClassCode
[1] == PCI_SUBCLASS_SD_HOST_CONTROLLER
) &&
469 ((PciData
.Hdr
.ClassCode
[0] == 0x00) || (PciData
.Hdr
.ClassCode
[0] == 0x01))) {
473 return EFI_UNSUPPORTED
;
477 Starts a device controller or a bus controller.
479 The Start() function is designed to be invoked from the EFI boot service ConnectController().
480 As a result, much of the error checking on the parameters to Start() has been moved into this
481 common boot service. It is legal to call Start() from other locations,
482 but the following calling restrictions must be followed or the system behavior will not be deterministic.
483 1. ControllerHandle must be a valid EFI_HANDLE.
484 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
485 EFI_DEVICE_PATH_PROTOCOL.
486 3. Prior to calling Start(), the Supported() function for the driver specified by This must
487 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
489 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
490 @param[in] ControllerHandle The handle of the controller to start. This handle
491 must support a protocol interface that supplies
492 an I/O abstraction to the driver.
493 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
494 parameter is ignored by device drivers, and is optional for bus
495 drivers. For a bus driver, if this parameter is NULL, then handles
496 for all the children of Controller are created by this driver.
497 If this parameter is not NULL and the first Device Path Node is
498 not the End of Device Path Node, then only the handle for the
499 child device specified by the first Device Path Node of
500 RemainingDevicePath is created by this driver.
501 If the first Device Path Node of RemainingDevicePath is
502 the End of Device Path Node, no child handle is created by this
505 @retval EFI_SUCCESS The device was started.
506 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
507 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
508 @retval Others The driver failded to start the device.
513 SdMmcPciHcDriverBindingStart (
514 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
515 IN EFI_HANDLE Controller
,
516 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
520 SD_MMC_HC_PRIVATE_DATA
*Private
;
521 EFI_PCI_IO_PROTOCOL
*PciIo
;
523 UINT64 PciAttributes
;
528 CARD_TYPE_DETECT_ROUTINE
*Routine
;
530 BOOLEAN MediaPresent
;
531 BOOLEAN Support64BitDma
;
533 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStart: Start\n"));
536 // Open PCI I/O Protocol and save pointer to open protocol
537 // in private data area.
540 Status
= gBS
->OpenProtocol (
542 &gEfiPciIoProtocolGuid
,
544 This
->DriverBindingHandle
,
546 EFI_OPEN_PROTOCOL_BY_DRIVER
548 if (EFI_ERROR (Status
)) {
553 // Enable the SD Host Controller MMIO space
556 Status
= PciIo
->Attributes (
558 EfiPciIoAttributeOperationGet
,
563 if (EFI_ERROR (Status
)) {
567 Status
= PciIo
->Attributes (
569 EfiPciIoAttributeOperationSupported
,
574 if (!EFI_ERROR (Status
)) {
575 Supports
&= (UINT64
)EFI_PCI_DEVICE_ENABLE
;
576 Status
= PciIo
->Attributes (
578 EfiPciIoAttributeOperationEnable
,
586 Private
= AllocateCopyPool (sizeof (SD_MMC_HC_PRIVATE_DATA
), &gSdMmcPciHcTemplate
);
587 if (Private
== NULL
) {
588 Status
= EFI_OUT_OF_RESOURCES
;
592 Private
->ControllerHandle
= Controller
;
593 Private
->PciIo
= PciIo
;
594 Private
->PciAttributes
= PciAttributes
;
595 InitializeListHead (&Private
->Queue
);
598 // Get SD/MMC Pci Host Controller Slot info
600 Status
= SdMmcHcGetSlotInfo (PciIo
, &FirstBar
, &SlotNum
);
601 if (EFI_ERROR (Status
)) {
606 // Attempt to locate the singleton instance of the SD/MMC override protocol,
607 // which implements platform specific workarounds for non-standard SDHCI
610 if (mOverride
== NULL
) {
611 Status
= gBS
->LocateProtocol (&gEdkiiSdMmcOverrideProtocolGuid
, NULL
,
612 (VOID
**)&mOverride
);
613 if (!EFI_ERROR (Status
)) {
614 DEBUG ((DEBUG_INFO
, "%a: found SD/MMC override protocol\n",
619 Support64BitDma
= TRUE
;
620 for (Slot
= FirstBar
; Slot
< (FirstBar
+ SlotNum
); Slot
++) {
621 Private
->Slot
[Slot
].Enable
= TRUE
;
624 // Get SD/MMC Pci Host Controller Version
626 Status
= SdMmcHcGetControllerVersion (PciIo
, Slot
, &Private
->ControllerVersion
[Slot
]);
627 if (EFI_ERROR (Status
)) {
631 Status
= SdMmcHcGetCapability (PciIo
, Slot
, &Private
->Capability
[Slot
]);
632 if (EFI_ERROR (Status
)) {
636 Private
->BaseClkFreq
[Slot
] = Private
->Capability
[Slot
].BaseClkFreq
;
638 if (mOverride
!= NULL
&& mOverride
->Capability
!= NULL
) {
639 Status
= mOverride
->Capability (
642 &Private
->Capability
[Slot
],
643 &Private
->BaseClkFreq
[Slot
]
645 if (EFI_ERROR (Status
)) {
646 DEBUG ((DEBUG_WARN
, "%a: Failed to override capability - %r\n",
647 __FUNCTION__
, Status
));
651 DumpCapabilityReg (Slot
, &Private
->Capability
[Slot
]);
654 "Slot[%d] Base Clock Frequency: %dMHz\n",
656 Private
->BaseClkFreq
[Slot
]
660 // If any of the slots does not support 64b system bus
661 // do not enable 64b DMA in the PCI layer.
663 if ((Private
->ControllerVersion
[Slot
] == SD_MMC_HC_CTRL_VER_300
&&
664 Private
->Capability
[Slot
].SysBus64V3
== 0) ||
665 (Private
->ControllerVersion
[Slot
] == SD_MMC_HC_CTRL_VER_400
&&
666 Private
->Capability
[Slot
].SysBus64V3
== 0) ||
667 (Private
->ControllerVersion
[Slot
] >= SD_MMC_HC_CTRL_VER_410
&&
668 Private
->Capability
[Slot
].SysBus64V4
== 0)) {
669 Support64BitDma
= FALSE
;
672 Status
= SdMmcHcGetMaxCurrent (PciIo
, Slot
, &Private
->MaxCurrent
[Slot
]);
673 if (EFI_ERROR (Status
)) {
677 Private
->Slot
[Slot
].SlotType
= Private
->Capability
[Slot
].SlotType
;
678 if ((Private
->Slot
[Slot
].SlotType
!= RemovableSlot
) && (Private
->Slot
[Slot
].SlotType
!= EmbeddedSlot
)) {
679 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDxe doesn't support the slot type [%d]!!!\n", Private
->Slot
[Slot
].SlotType
));
684 // Reset the specified slot of the SD/MMC Pci Host Controller
686 Status
= SdMmcHcReset (Private
, Slot
);
687 if (EFI_ERROR (Status
)) {
691 // Check whether there is a SD/MMC card attached
693 if (Private
->Slot
[Slot
].SlotType
== RemovableSlot
) {
694 Status
= SdMmcHcCardDetect (PciIo
, Slot
, &MediaPresent
);
695 if (EFI_ERROR (Status
) && (Status
!= EFI_MEDIA_CHANGED
)) {
697 } else if (!MediaPresent
) {
700 "SdMmcHcCardDetect: No device attached in Slot[%d]!!!\n",
707 Status
= SdMmcHcInitHost (Private
, Slot
);
708 if (EFI_ERROR (Status
)) {
712 Private
->Slot
[Slot
].MediaPresent
= TRUE
;
713 Private
->Slot
[Slot
].Initialized
= TRUE
;
714 RoutineNum
= sizeof (mCardTypeDetectRoutineTable
) / sizeof (CARD_TYPE_DETECT_ROUTINE
);
715 for (Index
= 0; Index
< RoutineNum
; Index
++) {
716 Routine
= &mCardTypeDetectRoutineTable
[Index
];
717 if (*Routine
!= NULL
) {
718 Status
= (*Routine
) (Private
, Slot
);
719 if (!EFI_ERROR (Status
)) {
725 // This card doesn't get initialized correctly.
727 if (Index
== RoutineNum
) {
728 Private
->Slot
[Slot
].Initialized
= FALSE
;
733 // Enable 64-bit DMA support in the PCI layer if this controller
736 if (Support64BitDma
) {
737 Status
= PciIo
->Attributes (
739 EfiPciIoAttributeOperationEnable
,
740 EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE
,
743 if (EFI_ERROR (Status
)) {
744 DEBUG ((DEBUG_WARN
, "SdMmcPciHcDriverBindingStart: failed to enable 64-bit DMA (%r)\n", Status
));
749 // Start the asynchronous I/O monitor
751 Status
= gBS
->CreateEvent (
752 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
754 ProcessAsyncTaskList
,
758 if (EFI_ERROR (Status
)) {
762 Status
= gBS
->SetTimer (Private
->TimerEvent
, TimerPeriodic
, SD_MMC_HC_ASYNC_TIMER
);
763 if (EFI_ERROR (Status
)) {
768 // Start the Sd removable device connection enumeration
770 Status
= gBS
->CreateEvent (
771 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
773 SdMmcPciHcEnumerateDevice
,
775 &Private
->ConnectEvent
777 if (EFI_ERROR (Status
)) {
781 Status
= gBS
->SetTimer (Private
->ConnectEvent
, TimerPeriodic
, SD_MMC_HC_ENUM_TIMER
);
782 if (EFI_ERROR (Status
)) {
786 Status
= gBS
->InstallMultipleProtocolInterfaces (
788 &gEfiSdMmcPassThruProtocolGuid
,
789 &(Private
->PassThru
),
793 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStart: %r End on %x\n", Status
, Controller
));
796 if (EFI_ERROR (Status
)) {
797 if ((Private
!= NULL
) && (Private
->PciAttributes
!= 0)) {
799 // Restore original PCI attributes
803 EfiPciIoAttributeOperationSet
,
804 Private
->PciAttributes
,
810 &gEfiPciIoProtocolGuid
,
811 This
->DriverBindingHandle
,
815 if ((Private
!= NULL
) && (Private
->TimerEvent
!= NULL
)) {
816 gBS
->CloseEvent (Private
->TimerEvent
);
819 if ((Private
!= NULL
) && (Private
->ConnectEvent
!= NULL
)) {
820 gBS
->CloseEvent (Private
->ConnectEvent
);
823 if (Private
!= NULL
) {
832 Stops a device controller or a bus controller.
834 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
835 As a result, much of the error checking on the parameters to Stop() has been moved
836 into this common boot service. It is legal to call Stop() from other locations,
837 but the following calling restrictions must be followed or the system behavior will not be deterministic.
838 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
839 same driver's Start() function.
840 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
841 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
842 Start() function, and the Start() function must have called OpenProtocol() on
843 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
845 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
846 @param[in] ControllerHandle A handle to the device being stopped. The handle must
847 support a bus specific I/O protocol for the driver
848 to use to stop the device.
849 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
850 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
851 if NumberOfChildren is 0.
853 @retval EFI_SUCCESS The device was stopped.
854 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
859 SdMmcPciHcDriverBindingStop (
860 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
861 IN EFI_HANDLE Controller
,
862 IN UINTN NumberOfChildren
,
863 IN EFI_HANDLE
*ChildHandleBuffer
867 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
868 SD_MMC_HC_PRIVATE_DATA
*Private
;
869 EFI_PCI_IO_PROTOCOL
*PciIo
;
871 LIST_ENTRY
*NextLink
;
874 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStop: Start\n"));
876 Status
= gBS
->OpenProtocol (
878 &gEfiSdMmcPassThruProtocolGuid
,
880 This
->DriverBindingHandle
,
882 EFI_OPEN_PROTOCOL_GET_PROTOCOL
884 if (EFI_ERROR (Status
)) {
888 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (PassThru
);
890 // Close Non-Blocking timer and free Task list.
892 if (Private
->TimerEvent
!= NULL
) {
893 gBS
->CloseEvent (Private
->TimerEvent
);
894 Private
->TimerEvent
= NULL
;
896 if (Private
->ConnectEvent
!= NULL
) {
897 gBS
->CloseEvent (Private
->ConnectEvent
);
898 Private
->ConnectEvent
= NULL
;
901 // As the timer is closed, there is no needs to use TPL lock to
902 // protect the critical region "queue".
904 for (Link
= GetFirstNode (&Private
->Queue
);
905 !IsNull (&Private
->Queue
, Link
);
907 NextLink
= GetNextNode (&Private
->Queue
, Link
);
908 RemoveEntryList (Link
);
909 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
910 Trb
->Packet
->TransactionStatus
= EFI_ABORTED
;
911 gBS
->SignalEvent (Trb
->Event
);
916 // Uninstall Block I/O protocol from the device handle
918 Status
= gBS
->UninstallProtocolInterface (
920 &gEfiSdMmcPassThruProtocolGuid
,
924 if (EFI_ERROR (Status
)) {
930 &gEfiPciIoProtocolGuid
,
931 This
->DriverBindingHandle
,
935 // Restore original PCI attributes
937 PciIo
= Private
->PciIo
;
938 Status
= PciIo
->Attributes (
940 EfiPciIoAttributeOperationSet
,
941 Private
->PciAttributes
,
944 ASSERT_EFI_ERROR (Status
);
948 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStop: End with %r\n", Status
));
954 Sends SD command to an SD card that is attached to the SD controller.
956 The PassThru() function sends the SD command specified by Packet to the SD card
959 If Packet is successfully sent to the SD card, then EFI_SUCCESS is returned.
961 If a device error occurs while sending the Packet, then EFI_DEVICE_ERROR is returned.
963 If Slot is not in a valid range for the SD controller, then EFI_INVALID_PARAMETER
966 If Packet defines a data command but both InDataBuffer and OutDataBuffer are NULL,
967 EFI_INVALID_PARAMETER is returned.
969 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
970 @param[in] Slot The slot number of the SD card to send the command to.
971 @param[in,out] Packet A pointer to the SD command data structure.
972 @param[in] Event If Event is NULL, blocking I/O is performed. If Event is
973 not NULL, then nonblocking I/O is performed, and Event
974 will be signaled when the Packet completes.
976 @retval EFI_SUCCESS The SD Command Packet was sent by the host.
977 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SD
979 @retval EFI_INVALID_PARAMETER Packet, Slot, or the contents of the Packet is invalid.
980 @retval EFI_INVALID_PARAMETER Packet defines a data command but both InDataBuffer and
981 OutDataBuffer are NULL.
982 @retval EFI_NO_MEDIA SD Device not present in the Slot.
983 @retval EFI_UNSUPPORTED The command described by the SD Command Packet is not
984 supported by the host controller.
985 @retval EFI_BAD_BUFFER_SIZE The InTransferLength or OutTransferLength exceeds the
986 limit supported by SD card ( i.e. if the number of bytes
987 exceed the Last LBA).
992 SdMmcPassThruPassThru (
993 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
995 IN OUT EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
,
996 IN EFI_EVENT Event OPTIONAL
1000 SD_MMC_HC_PRIVATE_DATA
*Private
;
1004 if ((This
== NULL
) || (Packet
== NULL
)) {
1005 return EFI_INVALID_PARAMETER
;
1008 if ((Packet
->SdMmcCmdBlk
== NULL
) || (Packet
->SdMmcStatusBlk
== NULL
)) {
1009 return EFI_INVALID_PARAMETER
;
1012 if ((Packet
->OutDataBuffer
== NULL
) && (Packet
->OutTransferLength
!= 0)) {
1013 return EFI_INVALID_PARAMETER
;
1016 if ((Packet
->InDataBuffer
== NULL
) && (Packet
->InTransferLength
!= 0)) {
1017 return EFI_INVALID_PARAMETER
;
1020 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1022 if (!Private
->Slot
[Slot
].Enable
) {
1023 return EFI_INVALID_PARAMETER
;
1026 if (!Private
->Slot
[Slot
].MediaPresent
) {
1027 return EFI_NO_MEDIA
;
1030 if (!Private
->Slot
[Slot
].Initialized
) {
1031 return EFI_DEVICE_ERROR
;
1034 Trb
= SdMmcCreateTrb (Private
, Slot
, Packet
, Event
);
1036 return EFI_OUT_OF_RESOURCES
;
1039 // Immediately return for async I/O.
1041 if (Event
!= NULL
) {
1046 // Wait async I/O list is empty before execute sync I/O operation.
1049 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1050 if (IsListEmpty (&Private
->Queue
)) {
1051 gBS
->RestoreTPL (OldTpl
);
1054 gBS
->RestoreTPL (OldTpl
);
1057 Status
= SdMmcWaitTrbEnv (Private
, Trb
);
1058 if (EFI_ERROR (Status
)) {
1062 Status
= SdMmcExecTrb (Private
, Trb
);
1063 if (EFI_ERROR (Status
)) {
1067 Status
= SdMmcWaitTrbResult (Private
, Trb
);
1068 if (EFI_ERROR (Status
)) {
1079 Used to retrieve next slot numbers supported by the SD controller. The function
1080 returns information about all available slots (populated or not-populated).
1082 The GetNextSlot() function retrieves the next slot number on an SD controller.
1083 If on input Slot is 0xFF, then the slot number of the first slot on the SD controller
1086 If Slot is a slot number that was returned on a previous call to GetNextSlot(), then
1087 the slot number of the next slot on the SD controller is returned.
1089 If Slot is not 0xFF and Slot was not returned on a previous call to GetNextSlot(),
1090 EFI_INVALID_PARAMETER is returned.
1092 If Slot is the slot number of the last slot on the SD controller, then EFI_NOT_FOUND
1095 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
1096 @param[in,out] Slot On input, a pointer to a slot number on the SD controller.
1097 On output, a pointer to the next slot number on the SD controller.
1098 An input value of 0xFF retrieves the first slot number on the SD
1101 @retval EFI_SUCCESS The next slot number on the SD controller was returned in Slot.
1102 @retval EFI_NOT_FOUND There are no more slots on this SD controller.
1103 @retval EFI_INVALID_PARAMETER Slot is not 0xFF and Slot was not returned on a previous call
1109 SdMmcPassThruGetNextSlot (
1110 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1114 SD_MMC_HC_PRIVATE_DATA
*Private
;
1117 if ((This
== NULL
) || (Slot
== NULL
)) {
1118 return EFI_INVALID_PARAMETER
;
1121 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1123 if (*Slot
== 0xFF) {
1124 for (Index
= 0; Index
< SD_MMC_HC_MAX_SLOT
; Index
++) {
1125 if (Private
->Slot
[Index
].Enable
) {
1127 Private
->PreviousSlot
= Index
;
1131 return EFI_NOT_FOUND
;
1132 } else if (*Slot
== Private
->PreviousSlot
) {
1133 for (Index
= *Slot
+ 1; Index
< SD_MMC_HC_MAX_SLOT
; Index
++) {
1134 if (Private
->Slot
[Index
].Enable
) {
1136 Private
->PreviousSlot
= Index
;
1140 return EFI_NOT_FOUND
;
1142 return EFI_INVALID_PARAMETER
;
1147 Used to allocate and build a device path node for an SD card on the SD controller.
1149 The BuildDevicePath() function allocates and builds a single device node for the SD
1150 card specified by Slot.
1152 If the SD card specified by Slot is not present on the SD controller, then EFI_NOT_FOUND
1155 If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned.
1157 If there are not enough resources to allocate the device path node, then EFI_OUT_OF_RESOURCES
1160 Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of
1161 DevicePath are initialized to describe the SD card specified by Slot, and EFI_SUCCESS is
1164 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
1165 @param[in] Slot Specifies the slot number of the SD card for which a device
1166 path node is to be allocated and built.
1167 @param[in,out] DevicePath A pointer to a single device path node that describes the SD
1168 card specified by Slot. This function is responsible for
1169 allocating the buffer DevicePath with the boot service
1170 AllocatePool(). It is the caller's responsibility to free
1171 DevicePath when the caller is finished with DevicePath.
1173 @retval EFI_SUCCESS The device path node that describes the SD card specified by
1174 Slot was allocated and returned in DevicePath.
1175 @retval EFI_NOT_FOUND The SD card specified by Slot does not exist on the SD controller.
1176 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1177 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
1182 SdMmcPassThruBuildDevicePath (
1183 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1185 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
1188 SD_MMC_HC_PRIVATE_DATA
*Private
;
1189 SD_DEVICE_PATH
*SdNode
;
1190 EMMC_DEVICE_PATH
*EmmcNode
;
1192 if ((This
== NULL
) || (DevicePath
== NULL
) || (Slot
>= SD_MMC_HC_MAX_SLOT
)) {
1193 return EFI_INVALID_PARAMETER
;
1196 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1198 if ((!Private
->Slot
[Slot
].Enable
) || (!Private
->Slot
[Slot
].MediaPresent
)) {
1199 return EFI_NOT_FOUND
;
1202 if (Private
->Slot
[Slot
].CardType
== SdCardType
) {
1203 SdNode
= AllocateCopyPool (sizeof (SD_DEVICE_PATH
), &mSdDpTemplate
);
1204 if (SdNode
== NULL
) {
1205 return EFI_OUT_OF_RESOURCES
;
1207 SdNode
->SlotNumber
= Slot
;
1209 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) SdNode
;
1210 } else if (Private
->Slot
[Slot
].CardType
== EmmcCardType
) {
1211 EmmcNode
= AllocateCopyPool (sizeof (EMMC_DEVICE_PATH
), &mEmmcDpTemplate
);
1212 if (EmmcNode
== NULL
) {
1213 return EFI_OUT_OF_RESOURCES
;
1215 EmmcNode
->SlotNumber
= Slot
;
1217 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) EmmcNode
;
1220 // Currently we only support SD and EMMC two device nodes.
1222 return EFI_NOT_FOUND
;
1229 This function retrieves an SD card slot number based on the input device path.
1231 The GetSlotNumber() function retrieves slot number for the SD card specified by
1232 the DevicePath node. If DevicePath is NULL, EFI_INVALID_PARAMETER is returned.
1234 If DevicePath is not a device path node type that the SD Pass Thru driver supports,
1235 EFI_UNSUPPORTED is returned.
1237 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1238 @param[in] DevicePath A pointer to the device path node that describes a SD
1239 card on the SD controller.
1240 @param[out] Slot On return, points to the slot number of an SD card on
1243 @retval EFI_SUCCESS SD card slot number is returned in Slot.
1244 @retval EFI_INVALID_PARAMETER Slot or DevicePath is NULL.
1245 @retval EFI_UNSUPPORTED DevicePath is not a device path node type that the SD
1246 Pass Thru driver supports.
1251 SdMmcPassThruGetSlotNumber (
1252 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1253 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1257 SD_MMC_HC_PRIVATE_DATA
*Private
;
1258 SD_DEVICE_PATH
*SdNode
;
1259 EMMC_DEVICE_PATH
*EmmcNode
;
1262 if ((This
== NULL
) || (DevicePath
== NULL
) || (Slot
== NULL
)) {
1263 return EFI_INVALID_PARAMETER
;
1266 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1269 // Check whether the DevicePath belongs to SD_DEVICE_PATH or EMMC_DEVICE_PATH
1271 if ((DevicePath
->Type
!= MESSAGING_DEVICE_PATH
) ||
1272 ((DevicePath
->SubType
!= MSG_SD_DP
) &&
1273 (DevicePath
->SubType
!= MSG_EMMC_DP
)) ||
1274 (DevicePathNodeLength(DevicePath
) != sizeof(SD_DEVICE_PATH
)) ||
1275 (DevicePathNodeLength(DevicePath
) != sizeof(EMMC_DEVICE_PATH
))) {
1276 return EFI_UNSUPPORTED
;
1279 if (DevicePath
->SubType
== MSG_SD_DP
) {
1280 SdNode
= (SD_DEVICE_PATH
*) DevicePath
;
1281 SlotNumber
= SdNode
->SlotNumber
;
1283 EmmcNode
= (EMMC_DEVICE_PATH
*) DevicePath
;
1284 SlotNumber
= EmmcNode
->SlotNumber
;
1287 if (SlotNumber
>= SD_MMC_HC_MAX_SLOT
) {
1288 return EFI_NOT_FOUND
;
1291 if (Private
->Slot
[SlotNumber
].Enable
) {
1295 return EFI_NOT_FOUND
;
1300 Resets an SD card that is connected to the SD controller.
1302 The ResetDevice() function resets the SD card specified by Slot.
1304 If this SD controller does not support a device reset operation, EFI_UNSUPPORTED is
1307 If Slot is not in a valid slot number for this SD controller, EFI_INVALID_PARAMETER
1310 If the device reset operation is completed, EFI_SUCCESS is returned.
1312 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1313 @param[in] Slot Specifies the slot number of the SD card to be reset.
1315 @retval EFI_SUCCESS The SD card specified by Slot was reset.
1316 @retval EFI_UNSUPPORTED The SD controller does not support a device reset operation.
1317 @retval EFI_INVALID_PARAMETER Slot number is invalid.
1318 @retval EFI_NO_MEDIA SD Device not present in the Slot.
1319 @retval EFI_DEVICE_ERROR The reset command failed due to a device error
1324 SdMmcPassThruResetDevice (
1325 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1329 SD_MMC_HC_PRIVATE_DATA
*Private
;
1331 LIST_ENTRY
*NextLink
;
1336 return EFI_INVALID_PARAMETER
;
1339 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1341 if (!Private
->Slot
[Slot
].Enable
) {
1342 return EFI_INVALID_PARAMETER
;
1345 if (!Private
->Slot
[Slot
].MediaPresent
) {
1346 return EFI_NO_MEDIA
;
1349 if (!Private
->Slot
[Slot
].Initialized
) {
1350 return EFI_DEVICE_ERROR
;
1353 // Free all async I/O requests in the queue
1355 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1357 for (Link
= GetFirstNode (&Private
->Queue
);
1358 !IsNull (&Private
->Queue
, Link
);
1360 NextLink
= GetNextNode (&Private
->Queue
, Link
);
1361 RemoveEntryList (Link
);
1362 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
1363 Trb
->Packet
->TransactionStatus
= EFI_ABORTED
;
1364 gBS
->SignalEvent (Trb
->Event
);
1368 gBS
->RestoreTPL (OldTpl
);