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, \
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
);
215 if ((Trb
!= NULL
) && (Status
!= EFI_NOT_READY
)) {
216 RemoveEntryList (Link
);
217 Trb
->Packet
->TransactionStatus
= Status
;
218 TrbEvent
= Trb
->Event
;
220 DEBUG ((DEBUG_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p with %r\n", TrbEvent
, Status
));
221 gBS
->SignalEvent (TrbEvent
);
227 Sd removable device enumeration callback function when the timer event is signaled.
229 @param[in] Event The Event this notify function registered to.
230 @param[in] Context Pointer to the context data registered to the
236 SdMmcPciHcEnumerateDevice (
241 SD_MMC_HC_PRIVATE_DATA
*Private
;
244 BOOLEAN MediaPresent
;
246 CARD_TYPE_DETECT_ROUTINE
*Routine
;
249 LIST_ENTRY
*NextLink
;
253 Private
= (SD_MMC_HC_PRIVATE_DATA
*)Context
;
255 for (Slot
= 0; Slot
< SD_MMC_HC_MAX_SLOT
; Slot
++) {
256 if ((Private
->Slot
[Slot
].Enable
) && (Private
->Slot
[Slot
].SlotType
== RemovableSlot
)) {
257 Status
= SdMmcHcCardDetect (Private
->PciIo
, Slot
, &MediaPresent
);
258 if ((Status
== EFI_MEDIA_CHANGED
) && !MediaPresent
) {
259 DEBUG ((DEBUG_INFO
, "SdMmcPciHcEnumerateDevice: device disconnected at slot %d of pci %p\n", Slot
, Private
->PciIo
));
260 Private
->Slot
[Slot
].MediaPresent
= FALSE
;
261 Private
->Slot
[Slot
].Initialized
= FALSE
;
263 // Signal all async task events at the slot with EFI_NO_MEDIA status.
265 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
266 for (Link
= GetFirstNode (&Private
->Queue
);
267 !IsNull (&Private
->Queue
, Link
);
269 NextLink
= GetNextNode (&Private
->Queue
, Link
);
270 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
271 if (Trb
->Slot
== Slot
) {
272 RemoveEntryList (Link
);
273 Trb
->Packet
->TransactionStatus
= EFI_NO_MEDIA
;
274 gBS
->SignalEvent (Trb
->Event
);
278 gBS
->RestoreTPL (OldTpl
);
280 // Notify the upper layer the connect state change through ReinstallProtocolInterface.
282 gBS
->ReinstallProtocolInterface (
283 Private
->ControllerHandle
,
284 &gEfiSdMmcPassThruProtocolGuid
,
289 if ((Status
== EFI_MEDIA_CHANGED
) && MediaPresent
) {
290 DEBUG ((DEBUG_INFO
, "SdMmcPciHcEnumerateDevice: device connected at slot %d of pci %p\n", Slot
, Private
->PciIo
));
292 // Reset the specified slot of the SD/MMC Pci Host Controller
294 Status
= SdMmcHcReset (Private
, Slot
);
295 if (EFI_ERROR (Status
)) {
299 // Reinitialize slot and restart identification process for the new attached device
301 Status
= SdMmcHcInitHost (Private
, Slot
);
302 if (EFI_ERROR (Status
)) {
306 Private
->Slot
[Slot
].MediaPresent
= TRUE
;
307 Private
->Slot
[Slot
].Initialized
= TRUE
;
308 RoutineNum
= sizeof (mCardTypeDetectRoutineTable
) / sizeof (CARD_TYPE_DETECT_ROUTINE
);
309 for (Index
= 0; Index
< RoutineNum
; Index
++) {
310 Routine
= &mCardTypeDetectRoutineTable
[Index
];
311 if (*Routine
!= NULL
) {
312 Status
= (*Routine
) (Private
, Slot
);
313 if (!EFI_ERROR (Status
)) {
319 // This card doesn't get initialized correctly.
321 if (Index
== RoutineNum
) {
322 Private
->Slot
[Slot
].Initialized
= FALSE
;
326 // Notify the upper layer the connect state change through ReinstallProtocolInterface.
328 gBS
->ReinstallProtocolInterface (
329 Private
->ControllerHandle
,
330 &gEfiSdMmcPassThruProtocolGuid
,
342 Tests to see if this driver supports a given controller. If a child device is provided,
343 it further tests to see if this driver supports creating a handle for the specified child device.
345 This function checks to see if the driver specified by This supports the device specified by
346 ControllerHandle. Drivers will typically use the device path attached to
347 ControllerHandle and/or the services from the bus I/O abstraction attached to
348 ControllerHandle to determine if the driver supports ControllerHandle. This function
349 may be called many times during platform initialization. In order to reduce boot times, the tests
350 performed by this function must be very small, and take as little time as possible to execute. This
351 function must not change the state of any hardware devices, and this function must be aware that the
352 device specified by ControllerHandle may already be managed by the same driver or a
353 different driver. This function must match its calls to AllocatePages() with FreePages(),
354 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
355 Since ControllerHandle may have been previously started by the same driver, if a protocol is
356 already in the opened state, then it must not be closed with CloseProtocol(). This is required
357 to guarantee the state of ControllerHandle is not modified by this function.
359 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
360 @param[in] ControllerHandle The handle of the controller to test. This handle
361 must support a protocol interface that supplies
362 an I/O abstraction to the driver.
363 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
364 parameter is ignored by device drivers, and is optional for bus
365 drivers. For bus drivers, if this parameter is not NULL, then
366 the bus driver must determine if the bus controller specified
367 by ControllerHandle and the child controller specified
368 by RemainingDevicePath are both supported by this
371 @retval EFI_SUCCESS The device specified by ControllerHandle and
372 RemainingDevicePath is supported by the driver specified by This.
373 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
374 RemainingDevicePath is already being managed by the driver
376 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
377 RemainingDevicePath is already being managed by a different
378 driver or an application that requires exclusive access.
379 Currently not implemented.
380 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
381 RemainingDevicePath is not supported by the driver specified by This.
385 SdMmcPciHcDriverBindingSupported (
386 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
387 IN EFI_HANDLE Controller
,
388 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
392 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
393 EFI_PCI_IO_PROTOCOL
*PciIo
;
397 ParentDevicePath
= NULL
;
400 // SdPciHcDxe is a device driver, and should ingore the
401 // "RemainingDevicePath" according to EFI spec.
403 Status
= gBS
->OpenProtocol (
405 &gEfiDevicePathProtocolGuid
,
406 (VOID
*) &ParentDevicePath
,
407 This
->DriverBindingHandle
,
409 EFI_OPEN_PROTOCOL_BY_DRIVER
411 if (EFI_ERROR (Status
)) {
413 // EFI_ALREADY_STARTED is also an error.
418 // Close the protocol because we don't use it here.
422 &gEfiDevicePathProtocolGuid
,
423 This
->DriverBindingHandle
,
428 // Now test the EfiPciIoProtocol.
430 Status
= gBS
->OpenProtocol (
432 &gEfiPciIoProtocolGuid
,
434 This
->DriverBindingHandle
,
436 EFI_OPEN_PROTOCOL_BY_DRIVER
438 if (EFI_ERROR (Status
)) {
443 // Now further check the PCI header: Base class (offset 0x08) and
444 // Sub Class (offset 0x05). This controller should be an SD/MMC PCI
447 Status
= PciIo
->Pci
.Read (
454 if (EFI_ERROR (Status
)) {
457 &gEfiPciIoProtocolGuid
,
458 This
->DriverBindingHandle
,
461 return EFI_UNSUPPORTED
;
464 // Since we already got the PciData, we can close protocol to avoid to carry it
465 // on for multiple exit points.
469 &gEfiPciIoProtocolGuid
,
470 This
->DriverBindingHandle
,
475 // Examine SD PCI Host Controller PCI Configuration table fields.
477 if ((PciData
.Hdr
.ClassCode
[2] == PCI_CLASS_SYSTEM_PERIPHERAL
) &&
478 (PciData
.Hdr
.ClassCode
[1] == PCI_SUBCLASS_SD_HOST_CONTROLLER
) &&
479 ((PciData
.Hdr
.ClassCode
[0] == 0x00) || (PciData
.Hdr
.ClassCode
[0] == 0x01))) {
483 return EFI_UNSUPPORTED
;
487 Starts a device controller or a bus controller.
489 The Start() function is designed to be invoked from the EFI boot service ConnectController().
490 As a result, much of the error checking on the parameters to Start() has been moved into this
491 common boot service. It is legal to call Start() from other locations,
492 but the following calling restrictions must be followed or the system behavior will not be deterministic.
493 1. ControllerHandle must be a valid EFI_HANDLE.
494 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
495 EFI_DEVICE_PATH_PROTOCOL.
496 3. Prior to calling Start(), the Supported() function for the driver specified by This must
497 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
499 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
500 @param[in] ControllerHandle The handle of the controller to start. This handle
501 must support a protocol interface that supplies
502 an I/O abstraction to the driver.
503 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
504 parameter is ignored by device drivers, and is optional for bus
505 drivers. For a bus driver, if this parameter is NULL, then handles
506 for all the children of Controller are created by this driver.
507 If this parameter is not NULL and the first Device Path Node is
508 not the End of Device Path Node, then only the handle for the
509 child device specified by the first Device Path Node of
510 RemainingDevicePath is created by this driver.
511 If the first Device Path Node of RemainingDevicePath is
512 the End of Device Path Node, no child handle is created by this
515 @retval EFI_SUCCESS The device was started.
516 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
517 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
518 @retval Others The driver failded to start the device.
523 SdMmcPciHcDriverBindingStart (
524 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
525 IN EFI_HANDLE Controller
,
526 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
530 SD_MMC_HC_PRIVATE_DATA
*Private
;
531 EFI_PCI_IO_PROTOCOL
*PciIo
;
533 UINT64 PciAttributes
;
538 CARD_TYPE_DETECT_ROUTINE
*Routine
;
540 BOOLEAN MediaPresent
;
541 BOOLEAN Support64BitDma
;
543 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStart: Start\n"));
546 // Open PCI I/O Protocol and save pointer to open protocol
547 // in private data area.
550 Status
= gBS
->OpenProtocol (
552 &gEfiPciIoProtocolGuid
,
554 This
->DriverBindingHandle
,
556 EFI_OPEN_PROTOCOL_BY_DRIVER
558 if (EFI_ERROR (Status
)) {
563 // Enable the SD Host Controller MMIO space
566 Status
= PciIo
->Attributes (
568 EfiPciIoAttributeOperationGet
,
573 if (EFI_ERROR (Status
)) {
577 Status
= PciIo
->Attributes (
579 EfiPciIoAttributeOperationSupported
,
584 if (!EFI_ERROR (Status
)) {
585 Supports
&= (UINT64
)EFI_PCI_DEVICE_ENABLE
;
586 Status
= PciIo
->Attributes (
588 EfiPciIoAttributeOperationEnable
,
596 Private
= AllocateCopyPool (sizeof (SD_MMC_HC_PRIVATE_DATA
), &gSdMmcPciHcTemplate
);
597 if (Private
== NULL
) {
598 Status
= EFI_OUT_OF_RESOURCES
;
602 Private
->ControllerHandle
= Controller
;
603 Private
->PciIo
= PciIo
;
604 Private
->PciAttributes
= PciAttributes
;
605 InitializeListHead (&Private
->Queue
);
608 // Get SD/MMC Pci Host Controller Slot info
610 Status
= SdMmcHcGetSlotInfo (PciIo
, &FirstBar
, &SlotNum
);
611 if (EFI_ERROR (Status
)) {
616 // Attempt to locate the singleton instance of the SD/MMC override protocol,
617 // which implements platform specific workarounds for non-standard SDHCI
620 if (mOverride
== NULL
) {
621 Status
= gBS
->LocateProtocol (&gEdkiiSdMmcOverrideProtocolGuid
, NULL
,
622 (VOID
**)&mOverride
);
623 if (!EFI_ERROR (Status
)) {
624 DEBUG ((DEBUG_INFO
, "%a: found SD/MMC override protocol\n",
629 Support64BitDma
= TRUE
;
630 for (Slot
= FirstBar
; Slot
< (FirstBar
+ SlotNum
); Slot
++) {
631 Private
->Slot
[Slot
].Enable
= TRUE
;
633 // Get SD/MMC Pci Host Controller Version
635 Status
= SdMmcHcGetControllerVersion (PciIo
, Slot
, &Private
->ControllerVersion
[Slot
]);
636 if (EFI_ERROR (Status
)) {
640 Status
= SdMmcHcGetCapability (PciIo
, Slot
, &Private
->Capability
[Slot
]);
641 if (EFI_ERROR (Status
)) {
645 Private
->BaseClkFreq
[Slot
] = Private
->Capability
[Slot
].BaseClkFreq
;
647 if (mOverride
!= NULL
) {
648 if (mOverride
->Capability
!= NULL
) {
649 Status
= mOverride
->Capability (
652 &Private
->Capability
[Slot
],
653 &Private
->BaseClkFreq
[Slot
]
655 if (EFI_ERROR (Status
)) {
656 DEBUG ((DEBUG_WARN
, "%a: Failed to override capability - %r\n",
657 __FUNCTION__
, Status
));
662 if (mOverride
->NotifyPhase
!= NULL
) {
663 Status
= mOverride
->NotifyPhase (
666 EdkiiSdMmcGetOperatingParam
,
667 (VOID
*)&Private
->Slot
[Slot
].OperatingParameters
669 if (EFI_ERROR (Status
)) {
670 DEBUG ((DEBUG_WARN
, "%a: Failed to get operating parameters, using defaults\n", __FUNCTION__
));
675 DumpCapabilityReg (Slot
, &Private
->Capability
[Slot
]);
678 "Slot[%d] Base Clock Frequency: %dMHz\n",
680 Private
->BaseClkFreq
[Slot
]
684 // If any of the slots does not support 64b system bus
685 // do not enable 64b DMA in the PCI layer.
687 if ((Private
->ControllerVersion
[Slot
] == SD_MMC_HC_CTRL_VER_300
&&
688 Private
->Capability
[Slot
].SysBus64V3
== 0) ||
689 (Private
->ControllerVersion
[Slot
] == SD_MMC_HC_CTRL_VER_400
&&
690 Private
->Capability
[Slot
].SysBus64V3
== 0) ||
691 (Private
->ControllerVersion
[Slot
] >= SD_MMC_HC_CTRL_VER_410
&&
692 Private
->Capability
[Slot
].SysBus64V4
== 0)) {
693 Support64BitDma
= FALSE
;
696 Status
= SdMmcHcGetMaxCurrent (PciIo
, Slot
, &Private
->MaxCurrent
[Slot
]);
697 if (EFI_ERROR (Status
)) {
701 Private
->Slot
[Slot
].SlotType
= Private
->Capability
[Slot
].SlotType
;
702 if ((Private
->Slot
[Slot
].SlotType
!= RemovableSlot
) && (Private
->Slot
[Slot
].SlotType
!= EmbeddedSlot
)) {
703 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDxe doesn't support the slot type [%d]!!!\n", Private
->Slot
[Slot
].SlotType
));
708 // Reset the specified slot of the SD/MMC Pci Host Controller
710 Status
= SdMmcHcReset (Private
, Slot
);
711 if (EFI_ERROR (Status
)) {
715 // Check whether there is a SD/MMC card attached
717 if (Private
->Slot
[Slot
].SlotType
== RemovableSlot
) {
718 Status
= SdMmcHcCardDetect (PciIo
, Slot
, &MediaPresent
);
719 if (EFI_ERROR (Status
) && (Status
!= EFI_MEDIA_CHANGED
)) {
721 } else if (!MediaPresent
) {
724 "SdMmcHcCardDetect: No device attached in Slot[%d]!!!\n",
731 Status
= SdMmcHcInitHost (Private
, Slot
);
732 if (EFI_ERROR (Status
)) {
736 Private
->Slot
[Slot
].MediaPresent
= TRUE
;
737 Private
->Slot
[Slot
].Initialized
= TRUE
;
738 RoutineNum
= sizeof (mCardTypeDetectRoutineTable
) / sizeof (CARD_TYPE_DETECT_ROUTINE
);
739 for (Index
= 0; Index
< RoutineNum
; Index
++) {
740 Routine
= &mCardTypeDetectRoutineTable
[Index
];
741 if (*Routine
!= NULL
) {
742 Status
= (*Routine
) (Private
, Slot
);
743 if (!EFI_ERROR (Status
)) {
749 // This card doesn't get initialized correctly.
751 if (Index
== RoutineNum
) {
752 Private
->Slot
[Slot
].Initialized
= FALSE
;
757 // Enable 64-bit DMA support in the PCI layer if this controller
760 if (Support64BitDma
) {
761 Status
= PciIo
->Attributes (
763 EfiPciIoAttributeOperationEnable
,
764 EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE
,
767 if (EFI_ERROR (Status
)) {
768 DEBUG ((DEBUG_WARN
, "SdMmcPciHcDriverBindingStart: failed to enable 64-bit DMA (%r)\n", Status
));
773 // Start the asynchronous I/O monitor
775 Status
= gBS
->CreateEvent (
776 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
778 ProcessAsyncTaskList
,
782 if (EFI_ERROR (Status
)) {
786 Status
= gBS
->SetTimer (Private
->TimerEvent
, TimerPeriodic
, SD_MMC_HC_ASYNC_TIMER
);
787 if (EFI_ERROR (Status
)) {
792 // Start the Sd removable device connection enumeration
794 Status
= gBS
->CreateEvent (
795 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
797 SdMmcPciHcEnumerateDevice
,
799 &Private
->ConnectEvent
801 if (EFI_ERROR (Status
)) {
805 Status
= gBS
->SetTimer (Private
->ConnectEvent
, TimerPeriodic
, SD_MMC_HC_ENUM_TIMER
);
806 if (EFI_ERROR (Status
)) {
810 Status
= gBS
->InstallMultipleProtocolInterfaces (
812 &gEfiSdMmcPassThruProtocolGuid
,
813 &(Private
->PassThru
),
817 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStart: %r End on %x\n", Status
, Controller
));
820 if (EFI_ERROR (Status
)) {
821 if ((Private
!= NULL
) && (Private
->PciAttributes
!= 0)) {
823 // Restore original PCI attributes
827 EfiPciIoAttributeOperationSet
,
828 Private
->PciAttributes
,
834 &gEfiPciIoProtocolGuid
,
835 This
->DriverBindingHandle
,
839 if ((Private
!= NULL
) && (Private
->TimerEvent
!= NULL
)) {
840 gBS
->CloseEvent (Private
->TimerEvent
);
843 if ((Private
!= NULL
) && (Private
->ConnectEvent
!= NULL
)) {
844 gBS
->CloseEvent (Private
->ConnectEvent
);
847 if (Private
!= NULL
) {
856 Stops a device controller or a bus controller.
858 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
859 As a result, much of the error checking on the parameters to Stop() has been moved
860 into this common boot service. It is legal to call Stop() from other locations,
861 but the following calling restrictions must be followed or the system behavior will not be deterministic.
862 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
863 same driver's Start() function.
864 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
865 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
866 Start() function, and the Start() function must have called OpenProtocol() on
867 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
869 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
870 @param[in] ControllerHandle A handle to the device being stopped. The handle must
871 support a bus specific I/O protocol for the driver
872 to use to stop the device.
873 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
874 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
875 if NumberOfChildren is 0.
877 @retval EFI_SUCCESS The device was stopped.
878 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
883 SdMmcPciHcDriverBindingStop (
884 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
885 IN EFI_HANDLE Controller
,
886 IN UINTN NumberOfChildren
,
887 IN EFI_HANDLE
*ChildHandleBuffer
891 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
892 SD_MMC_HC_PRIVATE_DATA
*Private
;
893 EFI_PCI_IO_PROTOCOL
*PciIo
;
895 LIST_ENTRY
*NextLink
;
898 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStop: Start\n"));
900 Status
= gBS
->OpenProtocol (
902 &gEfiSdMmcPassThruProtocolGuid
,
904 This
->DriverBindingHandle
,
906 EFI_OPEN_PROTOCOL_GET_PROTOCOL
908 if (EFI_ERROR (Status
)) {
912 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (PassThru
);
914 // Close Non-Blocking timer and free Task list.
916 if (Private
->TimerEvent
!= NULL
) {
917 gBS
->CloseEvent (Private
->TimerEvent
);
918 Private
->TimerEvent
= NULL
;
920 if (Private
->ConnectEvent
!= NULL
) {
921 gBS
->CloseEvent (Private
->ConnectEvent
);
922 Private
->ConnectEvent
= NULL
;
925 // As the timer is closed, there is no needs to use TPL lock to
926 // protect the critical region "queue".
928 for (Link
= GetFirstNode (&Private
->Queue
);
929 !IsNull (&Private
->Queue
, Link
);
931 NextLink
= GetNextNode (&Private
->Queue
, Link
);
932 RemoveEntryList (Link
);
933 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
934 Trb
->Packet
->TransactionStatus
= EFI_ABORTED
;
935 gBS
->SignalEvent (Trb
->Event
);
940 // Uninstall Block I/O protocol from the device handle
942 Status
= gBS
->UninstallProtocolInterface (
944 &gEfiSdMmcPassThruProtocolGuid
,
948 if (EFI_ERROR (Status
)) {
954 &gEfiPciIoProtocolGuid
,
955 This
->DriverBindingHandle
,
959 // Restore original PCI attributes
961 PciIo
= Private
->PciIo
;
962 Status
= PciIo
->Attributes (
964 EfiPciIoAttributeOperationSet
,
965 Private
->PciAttributes
,
968 ASSERT_EFI_ERROR (Status
);
972 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStop: End with %r\n", Status
));
978 Execute TRB synchronously.
980 @param[in] Private Pointer to driver private data.
981 @param[in] Trb Pointer to TRB to execute.
983 @retval EFI_SUCCESS TRB executed successfully.
984 @retval Other TRB failed.
987 SdMmcPassThruExecSyncTrb (
988 IN SD_MMC_HC_PRIVATE_DATA
*Private
,
989 IN SD_MMC_HC_TRB
*Trb
996 // Wait async I/O list is empty before execute sync I/O operation.
999 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1000 if (IsListEmpty (&Private
->Queue
)) {
1001 gBS
->RestoreTPL (OldTpl
);
1004 gBS
->RestoreTPL (OldTpl
);
1007 while (Trb
->Retries
) {
1008 Status
= SdMmcWaitTrbEnv (Private
, Trb
);
1009 if (EFI_ERROR (Status
)) {
1013 Status
= SdMmcExecTrb (Private
, Trb
);
1014 if (EFI_ERROR (Status
)) {
1018 Status
= SdMmcWaitTrbResult (Private
, Trb
);
1019 if (Status
== EFI_CRC_ERROR
) {
1030 Sends SD command to an SD card that is attached to the SD controller.
1032 The PassThru() function sends the SD command specified by Packet to the SD card
1035 If Packet is successfully sent to the SD card, then EFI_SUCCESS is returned.
1037 If a device error occurs while sending the Packet, then EFI_DEVICE_ERROR is returned.
1039 If Slot is not in a valid range for the SD controller, then EFI_INVALID_PARAMETER
1042 If Packet defines a data command but both InDataBuffer and OutDataBuffer are NULL,
1043 EFI_INVALID_PARAMETER is returned.
1045 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1046 @param[in] Slot The slot number of the SD card to send the command to.
1047 @param[in,out] Packet A pointer to the SD command data structure.
1048 @param[in] Event If Event is NULL, blocking I/O is performed. If Event is
1049 not NULL, then nonblocking I/O is performed, and Event
1050 will be signaled when the Packet completes.
1052 @retval EFI_SUCCESS The SD Command Packet was sent by the host.
1053 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SD
1055 @retval EFI_INVALID_PARAMETER Packet, Slot, or the contents of the Packet is invalid.
1056 @retval EFI_INVALID_PARAMETER Packet defines a data command but both InDataBuffer and
1057 OutDataBuffer are NULL.
1058 @retval EFI_NO_MEDIA SD Device not present in the Slot.
1059 @retval EFI_UNSUPPORTED The command described by the SD Command Packet is not
1060 supported by the host controller.
1061 @retval EFI_BAD_BUFFER_SIZE The InTransferLength or OutTransferLength exceeds the
1062 limit supported by SD card ( i.e. if the number of bytes
1063 exceed the Last LBA).
1068 SdMmcPassThruPassThru (
1069 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1071 IN OUT EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
,
1072 IN EFI_EVENT Event OPTIONAL
1076 SD_MMC_HC_PRIVATE_DATA
*Private
;
1079 if ((This
== NULL
) || (Packet
== NULL
)) {
1080 return EFI_INVALID_PARAMETER
;
1083 if ((Packet
->SdMmcCmdBlk
== NULL
) || (Packet
->SdMmcStatusBlk
== NULL
)) {
1084 return EFI_INVALID_PARAMETER
;
1087 if ((Packet
->OutDataBuffer
== NULL
) && (Packet
->OutTransferLength
!= 0)) {
1088 return EFI_INVALID_PARAMETER
;
1091 if ((Packet
->InDataBuffer
== NULL
) && (Packet
->InTransferLength
!= 0)) {
1092 return EFI_INVALID_PARAMETER
;
1095 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1097 if (!Private
->Slot
[Slot
].Enable
) {
1098 return EFI_INVALID_PARAMETER
;
1101 if (!Private
->Slot
[Slot
].MediaPresent
) {
1102 return EFI_NO_MEDIA
;
1105 if (!Private
->Slot
[Slot
].Initialized
) {
1106 return EFI_DEVICE_ERROR
;
1109 Trb
= SdMmcCreateTrb (Private
, Slot
, Packet
, Event
);
1111 return EFI_OUT_OF_RESOURCES
;
1114 // Immediately return for async I/O.
1116 if (Event
!= NULL
) {
1120 Status
= SdMmcPassThruExecSyncTrb (Private
, Trb
);
1128 Used to retrieve next slot numbers supported by the SD controller. The function
1129 returns information about all available slots (populated or not-populated).
1131 The GetNextSlot() function retrieves the next slot number on an SD controller.
1132 If on input Slot is 0xFF, then the slot number of the first slot on the SD controller
1135 If Slot is a slot number that was returned on a previous call to GetNextSlot(), then
1136 the slot number of the next slot on the SD controller is returned.
1138 If Slot is not 0xFF and Slot was not returned on a previous call to GetNextSlot(),
1139 EFI_INVALID_PARAMETER is returned.
1141 If Slot is the slot number of the last slot on the SD controller, then EFI_NOT_FOUND
1144 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
1145 @param[in,out] Slot On input, a pointer to a slot number on the SD controller.
1146 On output, a pointer to the next slot number on the SD controller.
1147 An input value of 0xFF retrieves the first slot number on the SD
1150 @retval EFI_SUCCESS The next slot number on the SD controller was returned in Slot.
1151 @retval EFI_NOT_FOUND There are no more slots on this SD controller.
1152 @retval EFI_INVALID_PARAMETER Slot is not 0xFF and Slot was not returned on a previous call
1158 SdMmcPassThruGetNextSlot (
1159 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1163 SD_MMC_HC_PRIVATE_DATA
*Private
;
1166 if ((This
== NULL
) || (Slot
== NULL
)) {
1167 return EFI_INVALID_PARAMETER
;
1170 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1172 if (*Slot
== 0xFF) {
1173 for (Index
= 0; Index
< SD_MMC_HC_MAX_SLOT
; Index
++) {
1174 if (Private
->Slot
[Index
].Enable
) {
1176 Private
->PreviousSlot
= Index
;
1180 return EFI_NOT_FOUND
;
1181 } else if (*Slot
== Private
->PreviousSlot
) {
1182 for (Index
= *Slot
+ 1; Index
< SD_MMC_HC_MAX_SLOT
; Index
++) {
1183 if (Private
->Slot
[Index
].Enable
) {
1185 Private
->PreviousSlot
= Index
;
1189 return EFI_NOT_FOUND
;
1191 return EFI_INVALID_PARAMETER
;
1196 Used to allocate and build a device path node for an SD card on the SD controller.
1198 The BuildDevicePath() function allocates and builds a single device node for the SD
1199 card specified by Slot.
1201 If the SD card specified by Slot is not present on the SD controller, then EFI_NOT_FOUND
1204 If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned.
1206 If there are not enough resources to allocate the device path node, then EFI_OUT_OF_RESOURCES
1209 Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of
1210 DevicePath are initialized to describe the SD card specified by Slot, and EFI_SUCCESS is
1213 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
1214 @param[in] Slot Specifies the slot number of the SD card for which a device
1215 path node is to be allocated and built.
1216 @param[in,out] DevicePath A pointer to a single device path node that describes the SD
1217 card specified by Slot. This function is responsible for
1218 allocating the buffer DevicePath with the boot service
1219 AllocatePool(). It is the caller's responsibility to free
1220 DevicePath when the caller is finished with DevicePath.
1222 @retval EFI_SUCCESS The device path node that describes the SD card specified by
1223 Slot was allocated and returned in DevicePath.
1224 @retval EFI_NOT_FOUND The SD card specified by Slot does not exist on the SD controller.
1225 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1226 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
1231 SdMmcPassThruBuildDevicePath (
1232 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1234 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
1237 SD_MMC_HC_PRIVATE_DATA
*Private
;
1238 SD_DEVICE_PATH
*SdNode
;
1239 EMMC_DEVICE_PATH
*EmmcNode
;
1241 if ((This
== NULL
) || (DevicePath
== NULL
) || (Slot
>= SD_MMC_HC_MAX_SLOT
)) {
1242 return EFI_INVALID_PARAMETER
;
1245 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1247 if ((!Private
->Slot
[Slot
].Enable
) || (!Private
->Slot
[Slot
].MediaPresent
)) {
1248 return EFI_NOT_FOUND
;
1251 if (Private
->Slot
[Slot
].CardType
== SdCardType
) {
1252 SdNode
= AllocateCopyPool (sizeof (SD_DEVICE_PATH
), &mSdDpTemplate
);
1253 if (SdNode
== NULL
) {
1254 return EFI_OUT_OF_RESOURCES
;
1256 SdNode
->SlotNumber
= Slot
;
1258 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) SdNode
;
1259 } else if (Private
->Slot
[Slot
].CardType
== EmmcCardType
) {
1260 EmmcNode
= AllocateCopyPool (sizeof (EMMC_DEVICE_PATH
), &mEmmcDpTemplate
);
1261 if (EmmcNode
== NULL
) {
1262 return EFI_OUT_OF_RESOURCES
;
1264 EmmcNode
->SlotNumber
= Slot
;
1266 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) EmmcNode
;
1269 // Currently we only support SD and EMMC two device nodes.
1271 return EFI_NOT_FOUND
;
1278 This function retrieves an SD card slot number based on the input device path.
1280 The GetSlotNumber() function retrieves slot number for the SD card specified by
1281 the DevicePath node. If DevicePath is NULL, EFI_INVALID_PARAMETER is returned.
1283 If DevicePath is not a device path node type that the SD Pass Thru driver supports,
1284 EFI_UNSUPPORTED is returned.
1286 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1287 @param[in] DevicePath A pointer to the device path node that describes a SD
1288 card on the SD controller.
1289 @param[out] Slot On return, points to the slot number of an SD card on
1292 @retval EFI_SUCCESS SD card slot number is returned in Slot.
1293 @retval EFI_INVALID_PARAMETER Slot or DevicePath is NULL.
1294 @retval EFI_UNSUPPORTED DevicePath is not a device path node type that the SD
1295 Pass Thru driver supports.
1300 SdMmcPassThruGetSlotNumber (
1301 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1302 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1306 SD_MMC_HC_PRIVATE_DATA
*Private
;
1307 SD_DEVICE_PATH
*SdNode
;
1308 EMMC_DEVICE_PATH
*EmmcNode
;
1311 if ((This
== NULL
) || (DevicePath
== NULL
) || (Slot
== NULL
)) {
1312 return EFI_INVALID_PARAMETER
;
1315 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1318 // Check whether the DevicePath belongs to SD_DEVICE_PATH or EMMC_DEVICE_PATH
1320 if ((DevicePath
->Type
!= MESSAGING_DEVICE_PATH
) ||
1321 ((DevicePath
->SubType
!= MSG_SD_DP
) &&
1322 (DevicePath
->SubType
!= MSG_EMMC_DP
)) ||
1323 (DevicePathNodeLength(DevicePath
) != sizeof(SD_DEVICE_PATH
)) ||
1324 (DevicePathNodeLength(DevicePath
) != sizeof(EMMC_DEVICE_PATH
))) {
1325 return EFI_UNSUPPORTED
;
1328 if (DevicePath
->SubType
== MSG_SD_DP
) {
1329 SdNode
= (SD_DEVICE_PATH
*) DevicePath
;
1330 SlotNumber
= SdNode
->SlotNumber
;
1332 EmmcNode
= (EMMC_DEVICE_PATH
*) DevicePath
;
1333 SlotNumber
= EmmcNode
->SlotNumber
;
1336 if (SlotNumber
>= SD_MMC_HC_MAX_SLOT
) {
1337 return EFI_NOT_FOUND
;
1340 if (Private
->Slot
[SlotNumber
].Enable
) {
1344 return EFI_NOT_FOUND
;
1349 Resets an SD card that is connected to the SD controller.
1351 The ResetDevice() function resets the SD card specified by Slot.
1353 If this SD controller does not support a device reset operation, EFI_UNSUPPORTED is
1356 If Slot is not in a valid slot number for this SD controller, EFI_INVALID_PARAMETER
1359 If the device reset operation is completed, EFI_SUCCESS is returned.
1361 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1362 @param[in] Slot Specifies the slot number of the SD card to be reset.
1364 @retval EFI_SUCCESS The SD card specified by Slot was reset.
1365 @retval EFI_UNSUPPORTED The SD controller does not support a device reset operation.
1366 @retval EFI_INVALID_PARAMETER Slot number is invalid.
1367 @retval EFI_NO_MEDIA SD Device not present in the Slot.
1368 @retval EFI_DEVICE_ERROR The reset command failed due to a device error
1373 SdMmcPassThruResetDevice (
1374 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1378 SD_MMC_HC_PRIVATE_DATA
*Private
;
1380 LIST_ENTRY
*NextLink
;
1385 return EFI_INVALID_PARAMETER
;
1388 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1390 if (!Private
->Slot
[Slot
].Enable
) {
1391 return EFI_INVALID_PARAMETER
;
1394 if (!Private
->Slot
[Slot
].MediaPresent
) {
1395 return EFI_NO_MEDIA
;
1398 if (!Private
->Slot
[Slot
].Initialized
) {
1399 return EFI_DEVICE_ERROR
;
1402 // Free all async I/O requests in the queue
1404 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1406 for (Link
= GetFirstNode (&Private
->Queue
);
1407 !IsNull (&Private
->Queue
, Link
);
1409 NextLink
= GetNextNode (&Private
->Queue
, Link
);
1410 RemoveEntryList (Link
);
1411 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
1412 Trb
->Packet
->TransactionStatus
= EFI_ABORTED
;
1413 gBS
->SignalEvent (Trb
->Event
);
1417 gBS
->RestoreTPL (OldTpl
);