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 - 2020, 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
,
31 #define SLOT_INIT_TEMPLATE {0, UnknownSlot, 0, 0, 0, 0, \
32 {EDKII_SD_MMC_BUS_WIDTH_IGNORE,\
33 EDKII_SD_MMC_CLOCK_FREQ_IGNORE,\
34 {EDKII_SD_MMC_DRIVER_STRENGTH_IGNORE}}}
37 // Template for SD/MMC host controller private data.
39 SD_MMC_HC_PRIVATE_DATA gSdMmcPciHcTemplate
= {
40 SD_MMC_HC_PRIVATE_SIGNATURE
, // Signature
41 NULL
, // ControllerHandle
45 SdMmcPassThruPassThru
,
46 SdMmcPassThruGetNextSlot
,
47 SdMmcPassThruBuildDevicePath
,
48 SdMmcPassThruGetSlotNumber
,
49 SdMmcPassThruResetDevice
56 INITIALIZE_LIST_HEAD_VARIABLE (gSdMmcPciHcTemplate
.Queue
),
72 0 // ControllerVersion
76 SD_DEVICE_PATH mSdDpTemplate
= {
78 MESSAGING_DEVICE_PATH
,
81 (UINT8
) (sizeof (SD_DEVICE_PATH
)),
82 (UINT8
) ((sizeof (SD_DEVICE_PATH
)) >> 8)
88 EMMC_DEVICE_PATH mEmmcDpTemplate
= {
90 MESSAGING_DEVICE_PATH
,
93 (UINT8
) (sizeof (EMMC_DEVICE_PATH
)),
94 (UINT8
) ((sizeof (EMMC_DEVICE_PATH
)) >> 8)
101 // Prioritized function list to detect card type.
102 // User could add other card detection logic here.
104 CARD_TYPE_DETECT_ROUTINE mCardTypeDetectRoutineTable
[] = {
106 SdCardIdentification
,
111 The entry point for SD host controller driver, used to install this driver on the ImageHandle.
113 @param[in] ImageHandle The firmware allocated handle for this driver image.
114 @param[in] SystemTable Pointer to the EFI system table.
116 @retval EFI_SUCCESS Driver loaded.
117 @retval other Driver not loaded.
122 InitializeSdMmcPciHcDxe (
123 IN EFI_HANDLE ImageHandle
,
124 IN EFI_SYSTEM_TABLE
*SystemTable
129 Status
= EfiLibInstallDriverBindingComponentName2 (
132 &gSdMmcPciHcDriverBinding
,
134 &gSdMmcPciHcComponentName
,
135 &gSdMmcPciHcComponentName2
137 ASSERT_EFI_ERROR (Status
);
143 Call back function when the timer event is signaled.
145 @param[in] Event The Event this notify function registered to.
146 @param[in] Context Pointer to the context data registered to the
152 ProcessAsyncTaskList (
157 SD_MMC_HC_PRIVATE_DATA
*Private
;
161 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
162 BOOLEAN InfiniteWait
;
165 Private
= (SD_MMC_HC_PRIVATE_DATA
*)Context
;
168 // Check if the first entry in the async I/O queue is done or not.
170 Status
= EFI_SUCCESS
;
172 Link
= GetFirstNode (&Private
->Queue
);
173 if (!IsNull (&Private
->Queue
, Link
)) {
174 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
175 if (!Private
->Slot
[Trb
->Slot
].MediaPresent
) {
176 Status
= EFI_NO_MEDIA
;
181 // Check whether the cmd/data line is ready for transfer.
183 Status
= SdMmcCheckTrbEnv (Private
, Trb
);
184 if (!EFI_ERROR (Status
)) {
186 Status
= SdMmcExecTrb (Private
, Trb
);
187 if (EFI_ERROR (Status
)) {
194 Status
= SdMmcCheckTrbResult (Private
, Trb
);
198 if ((Trb
!= NULL
) && (Status
== EFI_NOT_READY
)) {
199 Packet
= Trb
->Packet
;
200 if (Packet
->Timeout
== 0) {
203 InfiniteWait
= FALSE
;
205 if ((!InfiniteWait
) && (Trb
->Timeout
-- == 0)) {
206 RemoveEntryList (Link
);
207 Trb
->Packet
->TransactionStatus
= EFI_TIMEOUT
;
208 TrbEvent
= Trb
->Event
;
210 DEBUG ((DEBUG_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p EFI_TIMEOUT\n", TrbEvent
));
211 gBS
->SignalEvent (TrbEvent
);
214 } else if ((Trb
!= NULL
) && (Status
== EFI_CRC_ERROR
) && (Trb
->Retries
> 0)) {
216 Trb
->Started
= FALSE
;
217 } else if ((Trb
!= NULL
)) {
218 RemoveEntryList (Link
);
219 Trb
->Packet
->TransactionStatus
= Status
;
220 TrbEvent
= Trb
->Event
;
222 DEBUG ((DEBUG_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p with %r\n", TrbEvent
, Status
));
223 gBS
->SignalEvent (TrbEvent
);
229 Sd removable device enumeration callback function when the timer event is signaled.
231 @param[in] Event The Event this notify function registered to.
232 @param[in] Context Pointer to the context data registered to the
238 SdMmcPciHcEnumerateDevice (
243 SD_MMC_HC_PRIVATE_DATA
*Private
;
246 BOOLEAN MediaPresent
;
248 CARD_TYPE_DETECT_ROUTINE
*Routine
;
251 LIST_ENTRY
*NextLink
;
255 Private
= (SD_MMC_HC_PRIVATE_DATA
*)Context
;
257 for (Slot
= 0; Slot
< SD_MMC_HC_MAX_SLOT
; Slot
++) {
258 if ((Private
->Slot
[Slot
].Enable
) && (Private
->Slot
[Slot
].SlotType
== RemovableSlot
)) {
259 Status
= SdMmcHcCardDetect (Private
->PciIo
, Slot
, &MediaPresent
);
260 if ((Status
== EFI_MEDIA_CHANGED
) && !MediaPresent
) {
261 DEBUG ((DEBUG_INFO
, "SdMmcPciHcEnumerateDevice: device disconnected at slot %d of pci %p\n", Slot
, Private
->PciIo
));
262 Private
->Slot
[Slot
].MediaPresent
= FALSE
;
263 Private
->Slot
[Slot
].Initialized
= FALSE
;
265 // Signal all async task events at the slot with EFI_NO_MEDIA status.
267 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
268 for (Link
= GetFirstNode (&Private
->Queue
);
269 !IsNull (&Private
->Queue
, Link
);
271 NextLink
= GetNextNode (&Private
->Queue
, Link
);
272 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
273 if (Trb
->Slot
== Slot
) {
274 RemoveEntryList (Link
);
275 Trb
->Packet
->TransactionStatus
= EFI_NO_MEDIA
;
276 gBS
->SignalEvent (Trb
->Event
);
280 gBS
->RestoreTPL (OldTpl
);
282 // Notify the upper layer the connect state change through ReinstallProtocolInterface.
284 gBS
->ReinstallProtocolInterface (
285 Private
->ControllerHandle
,
286 &gEfiSdMmcPassThruProtocolGuid
,
291 if ((Status
== EFI_MEDIA_CHANGED
) && MediaPresent
) {
292 DEBUG ((DEBUG_INFO
, "SdMmcPciHcEnumerateDevice: device connected at slot %d of pci %p\n", Slot
, Private
->PciIo
));
294 // Reset the specified slot of the SD/MMC Pci Host Controller
296 Status
= SdMmcHcReset (Private
, Slot
);
297 if (EFI_ERROR (Status
)) {
301 // Reinitialize slot and restart identification process for the new attached device
303 Status
= SdMmcHcInitHost (Private
, Slot
);
304 if (EFI_ERROR (Status
)) {
308 Private
->Slot
[Slot
].MediaPresent
= TRUE
;
309 Private
->Slot
[Slot
].Initialized
= TRUE
;
310 RoutineNum
= sizeof (mCardTypeDetectRoutineTable
) / sizeof (CARD_TYPE_DETECT_ROUTINE
);
311 for (Index
= 0; Index
< RoutineNum
; Index
++) {
312 Routine
= &mCardTypeDetectRoutineTable
[Index
];
313 if (*Routine
!= NULL
) {
314 Status
= (*Routine
) (Private
, Slot
);
315 if (!EFI_ERROR (Status
)) {
321 // This card doesn't get initialized correctly.
323 if (Index
== RoutineNum
) {
324 Private
->Slot
[Slot
].Initialized
= FALSE
;
328 // Notify the upper layer the connect state change through ReinstallProtocolInterface.
330 gBS
->ReinstallProtocolInterface (
331 Private
->ControllerHandle
,
332 &gEfiSdMmcPassThruProtocolGuid
,
344 Tests to see if this driver supports a given controller. If a child device is provided,
345 it further tests to see if this driver supports creating a handle for the specified child device.
347 This function checks to see if the driver specified by This supports the device specified by
348 ControllerHandle. Drivers will typically use the device path attached to
349 ControllerHandle and/or the services from the bus I/O abstraction attached to
350 ControllerHandle to determine if the driver supports ControllerHandle. This function
351 may be called many times during platform initialization. In order to reduce boot times, the tests
352 performed by this function must be very small, and take as little time as possible to execute. This
353 function must not change the state of any hardware devices, and this function must be aware that the
354 device specified by ControllerHandle may already be managed by the same driver or a
355 different driver. This function must match its calls to AllocatePages() with FreePages(),
356 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
357 Since ControllerHandle may have been previously started by the same driver, if a protocol is
358 already in the opened state, then it must not be closed with CloseProtocol(). This is required
359 to guarantee the state of ControllerHandle is not modified by this function.
361 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
362 @param[in] ControllerHandle The handle of the controller to test. This handle
363 must support a protocol interface that supplies
364 an I/O abstraction to the driver.
365 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
366 parameter is ignored by device drivers, and is optional for bus
367 drivers. For bus drivers, if this parameter is not NULL, then
368 the bus driver must determine if the bus controller specified
369 by ControllerHandle and the child controller specified
370 by RemainingDevicePath are both supported by this
373 @retval EFI_SUCCESS The device specified by ControllerHandle and
374 RemainingDevicePath is supported by the driver specified by This.
375 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
376 RemainingDevicePath is already being managed by the driver
378 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
379 RemainingDevicePath is already being managed by a different
380 driver or an application that requires exclusive access.
381 Currently not implemented.
382 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
383 RemainingDevicePath is not supported by the driver specified by This.
387 SdMmcPciHcDriverBindingSupported (
388 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
389 IN EFI_HANDLE Controller
,
390 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
394 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
395 EFI_PCI_IO_PROTOCOL
*PciIo
;
399 ParentDevicePath
= NULL
;
402 // SdPciHcDxe is a device driver, and should ingore the
403 // "RemainingDevicePath" according to EFI spec.
405 Status
= gBS
->OpenProtocol (
407 &gEfiDevicePathProtocolGuid
,
408 (VOID
*) &ParentDevicePath
,
409 This
->DriverBindingHandle
,
411 EFI_OPEN_PROTOCOL_BY_DRIVER
413 if (EFI_ERROR (Status
)) {
415 // EFI_ALREADY_STARTED is also an error.
420 // Close the protocol because we don't use it here.
424 &gEfiDevicePathProtocolGuid
,
425 This
->DriverBindingHandle
,
430 // Now test the EfiPciIoProtocol.
432 Status
= gBS
->OpenProtocol (
434 &gEfiPciIoProtocolGuid
,
436 This
->DriverBindingHandle
,
438 EFI_OPEN_PROTOCOL_BY_DRIVER
440 if (EFI_ERROR (Status
)) {
445 // Now further check the PCI header: Base class (offset 0x08) and
446 // Sub Class (offset 0x05). This controller should be an SD/MMC PCI
449 Status
= PciIo
->Pci
.Read (
456 if (EFI_ERROR (Status
)) {
459 &gEfiPciIoProtocolGuid
,
460 This
->DriverBindingHandle
,
463 return EFI_UNSUPPORTED
;
466 // Since we already got the PciData, we can close protocol to avoid to carry it
467 // on for multiple exit points.
471 &gEfiPciIoProtocolGuid
,
472 This
->DriverBindingHandle
,
477 // Examine SD PCI Host Controller PCI Configuration table fields.
479 if ((PciData
.Hdr
.ClassCode
[2] == PCI_CLASS_SYSTEM_PERIPHERAL
) &&
480 (PciData
.Hdr
.ClassCode
[1] == PCI_SUBCLASS_SD_HOST_CONTROLLER
) &&
481 ((PciData
.Hdr
.ClassCode
[0] == 0x00) || (PciData
.Hdr
.ClassCode
[0] == 0x01))) {
485 return EFI_UNSUPPORTED
;
489 Starts a device controller or a bus controller.
491 The Start() function is designed to be invoked from the EFI boot service ConnectController().
492 As a result, much of the error checking on the parameters to Start() has been moved into this
493 common boot service. It is legal to call Start() from other locations,
494 but the following calling restrictions must be followed or the system behavior will not be deterministic.
495 1. ControllerHandle must be a valid EFI_HANDLE.
496 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
497 EFI_DEVICE_PATH_PROTOCOL.
498 3. Prior to calling Start(), the Supported() function for the driver specified by This must
499 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
501 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
502 @param[in] ControllerHandle The handle of the controller to start. This handle
503 must support a protocol interface that supplies
504 an I/O abstraction to the driver.
505 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
506 parameter is ignored by device drivers, and is optional for bus
507 drivers. For a bus driver, if this parameter is NULL, then handles
508 for all the children of Controller are created by this driver.
509 If this parameter is not NULL and the first Device Path Node is
510 not the End of Device Path Node, then only the handle for the
511 child device specified by the first Device Path Node of
512 RemainingDevicePath is created by this driver.
513 If the first Device Path Node of RemainingDevicePath is
514 the End of Device Path Node, no child handle is created by this
517 @retval EFI_SUCCESS The device was started.
518 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
519 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
520 @retval Others The driver failded to start the device.
525 SdMmcPciHcDriverBindingStart (
526 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
527 IN EFI_HANDLE Controller
,
528 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
532 SD_MMC_HC_PRIVATE_DATA
*Private
;
533 EFI_PCI_IO_PROTOCOL
*PciIo
;
535 UINT64 PciAttributes
;
540 CARD_TYPE_DETECT_ROUTINE
*Routine
;
542 BOOLEAN MediaPresent
;
543 BOOLEAN Support64BitDma
;
545 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStart: Start\n"));
548 // Open PCI I/O Protocol and save pointer to open protocol
549 // in private data area.
552 Status
= gBS
->OpenProtocol (
554 &gEfiPciIoProtocolGuid
,
556 This
->DriverBindingHandle
,
558 EFI_OPEN_PROTOCOL_BY_DRIVER
560 if (EFI_ERROR (Status
)) {
565 // Enable the SD Host Controller MMIO space
568 Status
= PciIo
->Attributes (
570 EfiPciIoAttributeOperationGet
,
575 if (EFI_ERROR (Status
)) {
579 Status
= PciIo
->Attributes (
581 EfiPciIoAttributeOperationSupported
,
586 if (!EFI_ERROR (Status
)) {
587 Supports
&= (UINT64
)EFI_PCI_DEVICE_ENABLE
;
588 Status
= PciIo
->Attributes (
590 EfiPciIoAttributeOperationEnable
,
598 Private
= AllocateCopyPool (sizeof (SD_MMC_HC_PRIVATE_DATA
), &gSdMmcPciHcTemplate
);
599 if (Private
== NULL
) {
600 Status
= EFI_OUT_OF_RESOURCES
;
604 Private
->ControllerHandle
= Controller
;
605 Private
->PciIo
= PciIo
;
606 Private
->PciAttributes
= PciAttributes
;
607 InitializeListHead (&Private
->Queue
);
610 // Get SD/MMC Pci Host Controller Slot info
612 Status
= SdMmcHcGetSlotInfo (PciIo
, &FirstBar
, &SlotNum
);
613 if (EFI_ERROR (Status
)) {
618 // Attempt to locate the singleton instance of the SD/MMC override protocol,
619 // which implements platform specific workarounds for non-standard SDHCI
622 if (mOverride
== NULL
) {
623 Status
= gBS
->LocateProtocol (&gEdkiiSdMmcOverrideProtocolGuid
, NULL
,
624 (VOID
**)&mOverride
);
625 if (!EFI_ERROR (Status
)) {
626 DEBUG ((DEBUG_INFO
, "%a: found SD/MMC override protocol\n",
631 Support64BitDma
= TRUE
;
632 for (Slot
= FirstBar
; Slot
< (FirstBar
+ SlotNum
); Slot
++) {
633 Private
->Slot
[Slot
].Enable
= TRUE
;
635 // Get SD/MMC Pci Host Controller Version
637 Status
= SdMmcHcGetControllerVersion (PciIo
, Slot
, &Private
->ControllerVersion
[Slot
]);
638 if (EFI_ERROR (Status
)) {
642 Status
= SdMmcHcGetCapability (PciIo
, Slot
, &Private
->Capability
[Slot
]);
643 if (EFI_ERROR (Status
)) {
647 Private
->BaseClkFreq
[Slot
] = Private
->Capability
[Slot
].BaseClkFreq
;
649 if (mOverride
!= NULL
) {
650 if (mOverride
->Capability
!= NULL
) {
651 Status
= mOverride
->Capability (
654 &Private
->Capability
[Slot
],
655 &Private
->BaseClkFreq
[Slot
]
657 if (EFI_ERROR (Status
)) {
658 DEBUG ((DEBUG_WARN
, "%a: Failed to override capability - %r\n",
659 __FUNCTION__
, Status
));
664 if (mOverride
->NotifyPhase
!= NULL
) {
665 Status
= mOverride
->NotifyPhase (
668 EdkiiSdMmcGetOperatingParam
,
669 (VOID
*)&Private
->Slot
[Slot
].OperatingParameters
671 if (EFI_ERROR (Status
)) {
672 DEBUG ((DEBUG_WARN
, "%a: Failed to get operating parameters, using defaults\n", __FUNCTION__
));
677 DumpCapabilityReg (Slot
, &Private
->Capability
[Slot
]);
680 "Slot[%d] Base Clock Frequency: %dMHz\n",
682 Private
->BaseClkFreq
[Slot
]
686 // If any of the slots does not support 64b system bus
687 // do not enable 64b DMA in the PCI layer.
689 if ((Private
->ControllerVersion
[Slot
] == SD_MMC_HC_CTRL_VER_300
&&
690 Private
->Capability
[Slot
].SysBus64V3
== 0) ||
691 (Private
->ControllerVersion
[Slot
] == SD_MMC_HC_CTRL_VER_400
&&
692 Private
->Capability
[Slot
].SysBus64V3
== 0) ||
693 (Private
->ControllerVersion
[Slot
] >= SD_MMC_HC_CTRL_VER_410
&&
694 Private
->Capability
[Slot
].SysBus64V4
== 0)) {
695 Support64BitDma
= FALSE
;
698 Status
= SdMmcHcGetMaxCurrent (PciIo
, Slot
, &Private
->MaxCurrent
[Slot
]);
699 if (EFI_ERROR (Status
)) {
703 Private
->Slot
[Slot
].SlotType
= Private
->Capability
[Slot
].SlotType
;
704 if ((Private
->Slot
[Slot
].SlotType
!= RemovableSlot
) && (Private
->Slot
[Slot
].SlotType
!= EmbeddedSlot
)) {
705 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDxe doesn't support the slot type [%d]!!!\n", Private
->Slot
[Slot
].SlotType
));
710 // Reset the specified slot of the SD/MMC Pci Host Controller
712 Status
= SdMmcHcReset (Private
, Slot
);
713 if (EFI_ERROR (Status
)) {
717 // Check whether there is a SD/MMC card attached
719 if (Private
->Slot
[Slot
].SlotType
== RemovableSlot
) {
720 Status
= SdMmcHcCardDetect (PciIo
, Slot
, &MediaPresent
);
721 if (EFI_ERROR (Status
) && (Status
!= EFI_MEDIA_CHANGED
)) {
723 } else if (!MediaPresent
) {
726 "SdMmcHcCardDetect: No device attached in Slot[%d]!!!\n",
733 Status
= SdMmcHcInitHost (Private
, Slot
);
734 if (EFI_ERROR (Status
)) {
738 Private
->Slot
[Slot
].MediaPresent
= TRUE
;
739 Private
->Slot
[Slot
].Initialized
= TRUE
;
740 RoutineNum
= sizeof (mCardTypeDetectRoutineTable
) / sizeof (CARD_TYPE_DETECT_ROUTINE
);
741 for (Index
= 0; Index
< RoutineNum
; Index
++) {
742 Routine
= &mCardTypeDetectRoutineTable
[Index
];
743 if (*Routine
!= NULL
) {
744 Status
= (*Routine
) (Private
, Slot
);
745 if (!EFI_ERROR (Status
)) {
751 // This card doesn't get initialized correctly.
753 if (Index
== RoutineNum
) {
754 Private
->Slot
[Slot
].Initialized
= FALSE
;
759 // Enable 64-bit DMA support in the PCI layer if this controller
762 if (Support64BitDma
) {
763 Status
= PciIo
->Attributes (
765 EfiPciIoAttributeOperationEnable
,
766 EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE
,
769 if (EFI_ERROR (Status
)) {
770 DEBUG ((DEBUG_WARN
, "SdMmcPciHcDriverBindingStart: failed to enable 64-bit DMA (%r)\n", Status
));
775 // Start the asynchronous I/O monitor
777 Status
= gBS
->CreateEvent (
778 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
780 ProcessAsyncTaskList
,
784 if (EFI_ERROR (Status
)) {
788 Status
= gBS
->SetTimer (Private
->TimerEvent
, TimerPeriodic
, SD_MMC_HC_ASYNC_TIMER
);
789 if (EFI_ERROR (Status
)) {
794 // Start the Sd removable device connection enumeration
796 Status
= gBS
->CreateEvent (
797 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
799 SdMmcPciHcEnumerateDevice
,
801 &Private
->ConnectEvent
803 if (EFI_ERROR (Status
)) {
807 Status
= gBS
->SetTimer (Private
->ConnectEvent
, TimerPeriodic
, SD_MMC_HC_ENUM_TIMER
);
808 if (EFI_ERROR (Status
)) {
812 Status
= gBS
->InstallMultipleProtocolInterfaces (
814 &gEfiSdMmcPassThruProtocolGuid
,
815 &(Private
->PassThru
),
819 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStart: %r End on %x\n", Status
, Controller
));
822 if (EFI_ERROR (Status
)) {
823 if ((Private
!= NULL
) && (Private
->PciAttributes
!= 0)) {
825 // Restore original PCI attributes
829 EfiPciIoAttributeOperationSet
,
830 Private
->PciAttributes
,
836 &gEfiPciIoProtocolGuid
,
837 This
->DriverBindingHandle
,
841 if ((Private
!= NULL
) && (Private
->TimerEvent
!= NULL
)) {
842 gBS
->CloseEvent (Private
->TimerEvent
);
845 if ((Private
!= NULL
) && (Private
->ConnectEvent
!= NULL
)) {
846 gBS
->CloseEvent (Private
->ConnectEvent
);
849 if (Private
!= NULL
) {
858 Stops a device controller or a bus controller.
860 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
861 As a result, much of the error checking on the parameters to Stop() has been moved
862 into this common boot service. It is legal to call Stop() from other locations,
863 but the following calling restrictions must be followed or the system behavior will not be deterministic.
864 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
865 same driver's Start() function.
866 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
867 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
868 Start() function, and the Start() function must have called OpenProtocol() on
869 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
871 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
872 @param[in] ControllerHandle A handle to the device being stopped. The handle must
873 support a bus specific I/O protocol for the driver
874 to use to stop the device.
875 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
876 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
877 if NumberOfChildren is 0.
879 @retval EFI_SUCCESS The device was stopped.
880 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
885 SdMmcPciHcDriverBindingStop (
886 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
887 IN EFI_HANDLE Controller
,
888 IN UINTN NumberOfChildren
,
889 IN EFI_HANDLE
*ChildHandleBuffer
893 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
894 SD_MMC_HC_PRIVATE_DATA
*Private
;
895 EFI_PCI_IO_PROTOCOL
*PciIo
;
897 LIST_ENTRY
*NextLink
;
900 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStop: Start\n"));
902 Status
= gBS
->OpenProtocol (
904 &gEfiSdMmcPassThruProtocolGuid
,
906 This
->DriverBindingHandle
,
908 EFI_OPEN_PROTOCOL_GET_PROTOCOL
910 if (EFI_ERROR (Status
)) {
914 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (PassThru
);
916 // Close Non-Blocking timer and free Task list.
918 if (Private
->TimerEvent
!= NULL
) {
919 gBS
->CloseEvent (Private
->TimerEvent
);
920 Private
->TimerEvent
= NULL
;
922 if (Private
->ConnectEvent
!= NULL
) {
923 gBS
->CloseEvent (Private
->ConnectEvent
);
924 Private
->ConnectEvent
= NULL
;
927 // As the timer is closed, there is no needs to use TPL lock to
928 // protect the critical region "queue".
930 for (Link
= GetFirstNode (&Private
->Queue
);
931 !IsNull (&Private
->Queue
, Link
);
933 NextLink
= GetNextNode (&Private
->Queue
, Link
);
934 RemoveEntryList (Link
);
935 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
936 Trb
->Packet
->TransactionStatus
= EFI_ABORTED
;
937 gBS
->SignalEvent (Trb
->Event
);
942 // Uninstall Block I/O protocol from the device handle
944 Status
= gBS
->UninstallProtocolInterface (
946 &gEfiSdMmcPassThruProtocolGuid
,
950 if (EFI_ERROR (Status
)) {
956 &gEfiPciIoProtocolGuid
,
957 This
->DriverBindingHandle
,
961 // Restore original PCI attributes
963 PciIo
= Private
->PciIo
;
964 Status
= PciIo
->Attributes (
966 EfiPciIoAttributeOperationSet
,
967 Private
->PciAttributes
,
970 ASSERT_EFI_ERROR (Status
);
974 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStop: End with %r\n", Status
));
980 Execute TRB synchronously.
982 @param[in] Private Pointer to driver private data.
983 @param[in] Trb Pointer to TRB to execute.
985 @retval EFI_SUCCESS TRB executed successfully.
986 @retval Other TRB failed.
989 SdMmcPassThruExecSyncTrb (
990 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
991 IN SD_MMC_HC_TRB
*Trb
998 // Wait async I/O list is empty before execute sync I/O operation.
1001 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1002 if (IsListEmpty (&Private
->Queue
)) {
1003 gBS
->RestoreTPL (OldTpl
);
1006 gBS
->RestoreTPL (OldTpl
);
1009 while (Trb
->Retries
) {
1010 Status
= SdMmcWaitTrbEnv (Private
, Trb
);
1011 if (EFI_ERROR (Status
)) {
1015 Status
= SdMmcExecTrb (Private
, Trb
);
1016 if (EFI_ERROR (Status
)) {
1020 Status
= SdMmcWaitTrbResult (Private
, Trb
);
1021 if (Status
== EFI_CRC_ERROR
) {
1032 Sends SD command to an SD card that is attached to the SD controller.
1034 The PassThru() function sends the SD command specified by Packet to the SD card
1037 If Packet is successfully sent to the SD card, then EFI_SUCCESS is returned.
1039 If a device error occurs while sending the Packet, then EFI_DEVICE_ERROR is returned.
1041 If Slot is not in a valid range for the SD controller, then EFI_INVALID_PARAMETER
1044 If Packet defines a data command but both InDataBuffer and OutDataBuffer are NULL,
1045 EFI_INVALID_PARAMETER is returned.
1047 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1048 @param[in] Slot The slot number of the SD card to send the command to.
1049 @param[in,out] Packet A pointer to the SD command data structure.
1050 @param[in] Event If Event is NULL, blocking I/O is performed. If Event is
1051 not NULL, then nonblocking I/O is performed, and Event
1052 will be signaled when the Packet completes.
1054 @retval EFI_SUCCESS The SD Command Packet was sent by the host.
1055 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SD
1057 @retval EFI_INVALID_PARAMETER Packet, Slot, or the contents of the Packet is invalid.
1058 @retval EFI_INVALID_PARAMETER Packet defines a data command but both InDataBuffer and
1059 OutDataBuffer are NULL.
1060 @retval EFI_NO_MEDIA SD Device not present in the Slot.
1061 @retval EFI_UNSUPPORTED The command described by the SD Command Packet is not
1062 supported by the host controller.
1063 @retval EFI_BAD_BUFFER_SIZE The InTransferLength or OutTransferLength exceeds the
1064 limit supported by SD card ( i.e. if the number of bytes
1065 exceed the Last LBA).
1070 SdMmcPassThruPassThru (
1071 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1073 IN OUT EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
,
1074 IN EFI_EVENT Event OPTIONAL
1078 SD_MMC_HC_PRIVATE_DATA
*Private
;
1081 if ((This
== NULL
) || (Packet
== NULL
)) {
1082 return EFI_INVALID_PARAMETER
;
1085 if ((Packet
->SdMmcCmdBlk
== NULL
) || (Packet
->SdMmcStatusBlk
== NULL
)) {
1086 return EFI_INVALID_PARAMETER
;
1089 if ((Packet
->OutDataBuffer
== NULL
) && (Packet
->OutTransferLength
!= 0)) {
1090 return EFI_INVALID_PARAMETER
;
1093 if ((Packet
->InDataBuffer
== NULL
) && (Packet
->InTransferLength
!= 0)) {
1094 return EFI_INVALID_PARAMETER
;
1097 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1099 if (!Private
->Slot
[Slot
].Enable
) {
1100 return EFI_INVALID_PARAMETER
;
1103 if (!Private
->Slot
[Slot
].MediaPresent
) {
1104 return EFI_NO_MEDIA
;
1107 if (!Private
->Slot
[Slot
].Initialized
) {
1108 return EFI_DEVICE_ERROR
;
1111 Trb
= SdMmcCreateTrb (Private
, Slot
, Packet
, Event
);
1113 return EFI_OUT_OF_RESOURCES
;
1116 // Immediately return for async I/O.
1118 if (Event
!= NULL
) {
1122 Status
= SdMmcPassThruExecSyncTrb (Private
, Trb
);
1130 Used to retrieve next slot numbers supported by the SD controller. The function
1131 returns information about all available slots (populated or not-populated).
1133 The GetNextSlot() function retrieves the next slot number on an SD controller.
1134 If on input Slot is 0xFF, then the slot number of the first slot on the SD controller
1137 If Slot is a slot number that was returned on a previous call to GetNextSlot(), then
1138 the slot number of the next slot on the SD controller is returned.
1140 If Slot is not 0xFF and Slot was not returned on a previous call to GetNextSlot(),
1141 EFI_INVALID_PARAMETER is returned.
1143 If Slot is the slot number of the last slot on the SD controller, then EFI_NOT_FOUND
1146 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
1147 @param[in,out] Slot On input, a pointer to a slot number on the SD controller.
1148 On output, a pointer to the next slot number on the SD controller.
1149 An input value of 0xFF retrieves the first slot number on the SD
1152 @retval EFI_SUCCESS The next slot number on the SD controller was returned in Slot.
1153 @retval EFI_NOT_FOUND There are no more slots on this SD controller.
1154 @retval EFI_INVALID_PARAMETER Slot is not 0xFF and Slot was not returned on a previous call
1160 SdMmcPassThruGetNextSlot (
1161 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1165 SD_MMC_HC_PRIVATE_DATA
*Private
;
1168 if ((This
== NULL
) || (Slot
== NULL
)) {
1169 return EFI_INVALID_PARAMETER
;
1172 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1174 if (*Slot
== 0xFF) {
1175 for (Index
= 0; Index
< SD_MMC_HC_MAX_SLOT
; Index
++) {
1176 if (Private
->Slot
[Index
].Enable
) {
1178 Private
->PreviousSlot
= Index
;
1182 return EFI_NOT_FOUND
;
1183 } else if (*Slot
== Private
->PreviousSlot
) {
1184 for (Index
= *Slot
+ 1; Index
< SD_MMC_HC_MAX_SLOT
; Index
++) {
1185 if (Private
->Slot
[Index
].Enable
) {
1187 Private
->PreviousSlot
= Index
;
1191 return EFI_NOT_FOUND
;
1193 return EFI_INVALID_PARAMETER
;
1198 Used to allocate and build a device path node for an SD card on the SD controller.
1200 The BuildDevicePath() function allocates and builds a single device node for the SD
1201 card specified by Slot.
1203 If the SD card specified by Slot is not present on the SD controller, then EFI_NOT_FOUND
1206 If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned.
1208 If there are not enough resources to allocate the device path node, then EFI_OUT_OF_RESOURCES
1211 Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of
1212 DevicePath are initialized to describe the SD card specified by Slot, and EFI_SUCCESS is
1215 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
1216 @param[in] Slot Specifies the slot number of the SD card for which a device
1217 path node is to be allocated and built.
1218 @param[in,out] DevicePath A pointer to a single device path node that describes the SD
1219 card specified by Slot. This function is responsible for
1220 allocating the buffer DevicePath with the boot service
1221 AllocatePool(). It is the caller's responsibility to free
1222 DevicePath when the caller is finished with DevicePath.
1224 @retval EFI_SUCCESS The device path node that describes the SD card specified by
1225 Slot was allocated and returned in DevicePath.
1226 @retval EFI_NOT_FOUND The SD card specified by Slot does not exist on the SD controller.
1227 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1228 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
1233 SdMmcPassThruBuildDevicePath (
1234 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1236 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
1239 SD_MMC_HC_PRIVATE_DATA
*Private
;
1240 SD_DEVICE_PATH
*SdNode
;
1241 EMMC_DEVICE_PATH
*EmmcNode
;
1243 if ((This
== NULL
) || (DevicePath
== NULL
) || (Slot
>= SD_MMC_HC_MAX_SLOT
)) {
1244 return EFI_INVALID_PARAMETER
;
1247 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1249 if ((!Private
->Slot
[Slot
].Enable
) || (!Private
->Slot
[Slot
].MediaPresent
)) {
1250 return EFI_NOT_FOUND
;
1253 if (Private
->Slot
[Slot
].CardType
== SdCardType
) {
1254 SdNode
= AllocateCopyPool (sizeof (SD_DEVICE_PATH
), &mSdDpTemplate
);
1255 if (SdNode
== NULL
) {
1256 return EFI_OUT_OF_RESOURCES
;
1258 SdNode
->SlotNumber
= Slot
;
1260 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) SdNode
;
1261 } else if (Private
->Slot
[Slot
].CardType
== EmmcCardType
) {
1262 EmmcNode
= AllocateCopyPool (sizeof (EMMC_DEVICE_PATH
), &mEmmcDpTemplate
);
1263 if (EmmcNode
== NULL
) {
1264 return EFI_OUT_OF_RESOURCES
;
1266 EmmcNode
->SlotNumber
= Slot
;
1268 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) EmmcNode
;
1271 // Currently we only support SD and EMMC two device nodes.
1273 return EFI_NOT_FOUND
;
1280 This function retrieves an SD card slot number based on the input device path.
1282 The GetSlotNumber() function retrieves slot number for the SD card specified by
1283 the DevicePath node. If DevicePath is NULL, EFI_INVALID_PARAMETER is returned.
1285 If DevicePath is not a device path node type that the SD Pass Thru driver supports,
1286 EFI_UNSUPPORTED is returned.
1288 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1289 @param[in] DevicePath A pointer to the device path node that describes a SD
1290 card on the SD controller.
1291 @param[out] Slot On return, points to the slot number of an SD card on
1294 @retval EFI_SUCCESS SD card slot number is returned in Slot.
1295 @retval EFI_INVALID_PARAMETER Slot or DevicePath is NULL.
1296 @retval EFI_UNSUPPORTED DevicePath is not a device path node type that the SD
1297 Pass Thru driver supports.
1302 SdMmcPassThruGetSlotNumber (
1303 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1304 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1308 SD_MMC_HC_PRIVATE_DATA
*Private
;
1309 SD_DEVICE_PATH
*SdNode
;
1310 EMMC_DEVICE_PATH
*EmmcNode
;
1313 if ((This
== NULL
) || (DevicePath
== NULL
) || (Slot
== NULL
)) {
1314 return EFI_INVALID_PARAMETER
;
1317 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1320 // Check whether the DevicePath belongs to SD_DEVICE_PATH or EMMC_DEVICE_PATH
1322 if ((DevicePath
->Type
!= MESSAGING_DEVICE_PATH
) ||
1323 ((DevicePath
->SubType
!= MSG_SD_DP
) &&
1324 (DevicePath
->SubType
!= MSG_EMMC_DP
)) ||
1325 (DevicePathNodeLength(DevicePath
) != sizeof(SD_DEVICE_PATH
)) ||
1326 (DevicePathNodeLength(DevicePath
) != sizeof(EMMC_DEVICE_PATH
))) {
1327 return EFI_UNSUPPORTED
;
1330 if (DevicePath
->SubType
== MSG_SD_DP
) {
1331 SdNode
= (SD_DEVICE_PATH
*) DevicePath
;
1332 SlotNumber
= SdNode
->SlotNumber
;
1334 EmmcNode
= (EMMC_DEVICE_PATH
*) DevicePath
;
1335 SlotNumber
= EmmcNode
->SlotNumber
;
1338 if (SlotNumber
>= SD_MMC_HC_MAX_SLOT
) {
1339 return EFI_NOT_FOUND
;
1342 if (Private
->Slot
[SlotNumber
].Enable
) {
1346 return EFI_NOT_FOUND
;
1351 Resets an SD card that is connected to the SD controller.
1353 The ResetDevice() function resets the SD card specified by Slot.
1355 If this SD controller does not support a device reset operation, EFI_UNSUPPORTED is
1358 If Slot is not in a valid slot number for this SD controller, EFI_INVALID_PARAMETER
1361 If the device reset operation is completed, EFI_SUCCESS is returned.
1363 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1364 @param[in] Slot Specifies the slot number of the SD card to be reset.
1366 @retval EFI_SUCCESS The SD card specified by Slot was reset.
1367 @retval EFI_UNSUPPORTED The SD controller does not support a device reset operation.
1368 @retval EFI_INVALID_PARAMETER Slot number is invalid.
1369 @retval EFI_NO_MEDIA SD Device not present in the Slot.
1370 @retval EFI_DEVICE_ERROR The reset command failed due to a device error
1375 SdMmcPassThruResetDevice (
1376 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1380 SD_MMC_HC_PRIVATE_DATA
*Private
;
1382 LIST_ENTRY
*NextLink
;
1387 return EFI_INVALID_PARAMETER
;
1390 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1392 if (!Private
->Slot
[Slot
].Enable
) {
1393 return EFI_INVALID_PARAMETER
;
1396 if (!Private
->Slot
[Slot
].MediaPresent
) {
1397 return EFI_NO_MEDIA
;
1400 if (!Private
->Slot
[Slot
].Initialized
) {
1401 return EFI_DEVICE_ERROR
;
1404 // Free all async I/O requests in the queue
1406 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1408 for (Link
= GetFirstNode (&Private
->Queue
);
1409 !IsNull (&Private
->Queue
, Link
);
1411 NextLink
= GetNextNode (&Private
->Queue
, Link
);
1412 RemoveEntryList (Link
);
1413 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
1414 Trb
->Packet
->TransactionStatus
= EFI_ABORTED
;
1415 gBS
->SignalEvent (Trb
->Event
);
1419 gBS
->RestoreTPL (OldTpl
);