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, NVIDIA CORPORATION. All rights reserved.
10 Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.<BR>
11 This program and the accompanying materials
12 are licensed and made available under the terms and conditions of the BSD License
13 which accompanies this distribution. The full text of the license may be found at
14 http://opensource.org/licenses/bsd-license.php
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 #include "SdMmcPciHcDxe.h"
23 EDKII_SD_MMC_OVERRIDE
*mOverride
;
26 // Driver Global Variables
28 EFI_DRIVER_BINDING_PROTOCOL gSdMmcPciHcDriverBinding
= {
29 SdMmcPciHcDriverBindingSupported
,
30 SdMmcPciHcDriverBindingStart
,
31 SdMmcPciHcDriverBindingStop
,
38 // Template for SD/MMC host controller private data.
40 SD_MMC_HC_PRIVATE_DATA gSdMmcPciHcTemplate
= {
41 SD_MMC_HC_PRIVATE_SIGNATURE
, // Signature
42 NULL
, // ControllerHandle
46 SdMmcPassThruPassThru
,
47 SdMmcPassThruGetNextSlot
,
48 SdMmcPassThruBuildDevicePath
,
49 SdMmcPassThruGetSlotNumber
,
50 SdMmcPassThruResetDevice
57 INITIALIZE_LIST_HEAD_VARIABLE (gSdMmcPciHcTemplate
.Queue
),
59 {0, UnknownSlot
, 0, 0, 0}, {0, UnknownSlot
, 0, 0, 0}, {0, UnknownSlot
, 0, 0, 0},
60 {0, UnknownSlot
, 0, 0, 0}, {0, UnknownSlot
, 0, 0, 0}, {0, UnknownSlot
, 0, 0, 0}
69 0 // ControllerVersion
73 SD_DEVICE_PATH mSdDpTemplate
= {
75 MESSAGING_DEVICE_PATH
,
78 (UINT8
) (sizeof (SD_DEVICE_PATH
)),
79 (UINT8
) ((sizeof (SD_DEVICE_PATH
)) >> 8)
85 EMMC_DEVICE_PATH mEmmcDpTemplate
= {
87 MESSAGING_DEVICE_PATH
,
90 (UINT8
) (sizeof (EMMC_DEVICE_PATH
)),
91 (UINT8
) ((sizeof (EMMC_DEVICE_PATH
)) >> 8)
98 // Prioritized function list to detect card type.
99 // User could add other card detection logic here.
101 CARD_TYPE_DETECT_ROUTINE mCardTypeDetectRoutineTable
[] = {
103 SdCardIdentification
,
108 The entry point for SD host controller driver, used to install this driver on the ImageHandle.
110 @param[in] ImageHandle The firmware allocated handle for this driver image.
111 @param[in] SystemTable Pointer to the EFI system table.
113 @retval EFI_SUCCESS Driver loaded.
114 @retval other Driver not loaded.
119 InitializeSdMmcPciHcDxe (
120 IN EFI_HANDLE ImageHandle
,
121 IN EFI_SYSTEM_TABLE
*SystemTable
126 Status
= EfiLibInstallDriverBindingComponentName2 (
129 &gSdMmcPciHcDriverBinding
,
131 &gSdMmcPciHcComponentName
,
132 &gSdMmcPciHcComponentName2
134 ASSERT_EFI_ERROR (Status
);
140 Call back function when the timer event is signaled.
142 @param[in] Event The Event this notify function registered to.
143 @param[in] Context Pointer to the context data registered to the
149 ProcessAsyncTaskList (
154 SD_MMC_HC_PRIVATE_DATA
*Private
;
158 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
159 BOOLEAN InfiniteWait
;
162 Private
= (SD_MMC_HC_PRIVATE_DATA
*)Context
;
165 // Check if the first entry in the async I/O queue is done or not.
167 Status
= EFI_SUCCESS
;
169 Link
= GetFirstNode (&Private
->Queue
);
170 if (!IsNull (&Private
->Queue
, Link
)) {
171 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
172 if (!Private
->Slot
[Trb
->Slot
].MediaPresent
) {
173 Status
= EFI_NO_MEDIA
;
178 // Check whether the cmd/data line is ready for transfer.
180 Status
= SdMmcCheckTrbEnv (Private
, Trb
);
181 if (!EFI_ERROR (Status
)) {
183 Status
= SdMmcExecTrb (Private
, Trb
);
184 if (EFI_ERROR (Status
)) {
191 Status
= SdMmcCheckTrbResult (Private
, Trb
);
195 if ((Trb
!= NULL
) && (Status
== EFI_NOT_READY
)) {
196 Packet
= Trb
->Packet
;
197 if (Packet
->Timeout
== 0) {
200 InfiniteWait
= FALSE
;
202 if ((!InfiniteWait
) && (Trb
->Timeout
-- == 0)) {
203 RemoveEntryList (Link
);
204 Trb
->Packet
->TransactionStatus
= EFI_TIMEOUT
;
205 TrbEvent
= Trb
->Event
;
207 DEBUG ((DEBUG_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p EFI_TIMEOUT\n", TrbEvent
));
208 gBS
->SignalEvent (TrbEvent
);
212 if ((Trb
!= NULL
) && (Status
!= EFI_NOT_READY
)) {
213 RemoveEntryList (Link
);
214 Trb
->Packet
->TransactionStatus
= Status
;
215 TrbEvent
= Trb
->Event
;
217 DEBUG ((DEBUG_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p with %r\n", TrbEvent
, Status
));
218 gBS
->SignalEvent (TrbEvent
);
224 Sd removable device enumeration callback function when the timer event is signaled.
226 @param[in] Event The Event this notify function registered to.
227 @param[in] Context Pointer to the context data registered to the
233 SdMmcPciHcEnumerateDevice (
238 SD_MMC_HC_PRIVATE_DATA
*Private
;
241 BOOLEAN MediaPresent
;
243 CARD_TYPE_DETECT_ROUTINE
*Routine
;
246 LIST_ENTRY
*NextLink
;
250 Private
= (SD_MMC_HC_PRIVATE_DATA
*)Context
;
252 for (Slot
= 0; Slot
< SD_MMC_HC_MAX_SLOT
; Slot
++) {
253 if ((Private
->Slot
[Slot
].Enable
) && (Private
->Slot
[Slot
].SlotType
== RemovableSlot
)) {
254 Status
= SdMmcHcCardDetect (Private
->PciIo
, Slot
, &MediaPresent
);
255 if ((Status
== EFI_MEDIA_CHANGED
) && !MediaPresent
) {
256 DEBUG ((DEBUG_INFO
, "SdMmcPciHcEnumerateDevice: device disconnected at slot %d of pci %p\n", Slot
, Private
->PciIo
));
257 Private
->Slot
[Slot
].MediaPresent
= FALSE
;
258 Private
->Slot
[Slot
].Initialized
= FALSE
;
260 // Signal all async task events at the slot with EFI_NO_MEDIA status.
262 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
263 for (Link
= GetFirstNode (&Private
->Queue
);
264 !IsNull (&Private
->Queue
, Link
);
266 NextLink
= GetNextNode (&Private
->Queue
, Link
);
267 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
268 if (Trb
->Slot
== Slot
) {
269 RemoveEntryList (Link
);
270 Trb
->Packet
->TransactionStatus
= EFI_NO_MEDIA
;
271 gBS
->SignalEvent (Trb
->Event
);
275 gBS
->RestoreTPL (OldTpl
);
277 // Notify the upper layer the connect state change through ReinstallProtocolInterface.
279 gBS
->ReinstallProtocolInterface (
280 Private
->ControllerHandle
,
281 &gEfiSdMmcPassThruProtocolGuid
,
286 if ((Status
== EFI_MEDIA_CHANGED
) && MediaPresent
) {
287 DEBUG ((DEBUG_INFO
, "SdMmcPciHcEnumerateDevice: device connected at slot %d of pci %p\n", Slot
, Private
->PciIo
));
289 // Reset the specified slot of the SD/MMC Pci Host Controller
291 Status
= SdMmcHcReset (Private
, Slot
);
292 if (EFI_ERROR (Status
)) {
296 // Reinitialize slot and restart identification process for the new attached device
298 Status
= SdMmcHcInitHost (Private
, Slot
);
299 if (EFI_ERROR (Status
)) {
303 Private
->Slot
[Slot
].MediaPresent
= TRUE
;
304 Private
->Slot
[Slot
].Initialized
= TRUE
;
305 RoutineNum
= sizeof (mCardTypeDetectRoutineTable
) / sizeof (CARD_TYPE_DETECT_ROUTINE
);
306 for (Index
= 0; Index
< RoutineNum
; Index
++) {
307 Routine
= &mCardTypeDetectRoutineTable
[Index
];
308 if (*Routine
!= NULL
) {
309 Status
= (*Routine
) (Private
, Slot
);
310 if (!EFI_ERROR (Status
)) {
316 // This card doesn't get initialized correctly.
318 if (Index
== RoutineNum
) {
319 Private
->Slot
[Slot
].Initialized
= FALSE
;
323 // Notify the upper layer the connect state change through ReinstallProtocolInterface.
325 gBS
->ReinstallProtocolInterface (
326 Private
->ControllerHandle
,
327 &gEfiSdMmcPassThruProtocolGuid
,
338 Tests to see if this driver supports a given controller. If a child device is provided,
339 it further tests to see if this driver supports creating a handle for the specified child device.
341 This function checks to see if the driver specified by This supports the device specified by
342 ControllerHandle. Drivers will typically use the device path attached to
343 ControllerHandle and/or the services from the bus I/O abstraction attached to
344 ControllerHandle to determine if the driver supports ControllerHandle. This function
345 may be called many times during platform initialization. In order to reduce boot times, the tests
346 performed by this function must be very small, and take as little time as possible to execute. This
347 function must not change the state of any hardware devices, and this function must be aware that the
348 device specified by ControllerHandle may already be managed by the same driver or a
349 different driver. This function must match its calls to AllocatePages() with FreePages(),
350 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
351 Since ControllerHandle may have been previously started by the same driver, if a protocol is
352 already in the opened state, then it must not be closed with CloseProtocol(). This is required
353 to guarantee the state of ControllerHandle is not modified by this function.
355 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
356 @param[in] ControllerHandle The handle of the controller to test. This handle
357 must support a protocol interface that supplies
358 an I/O abstraction to the driver.
359 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
360 parameter is ignored by device drivers, and is optional for bus
361 drivers. For bus drivers, if this parameter is not NULL, then
362 the bus driver must determine if the bus controller specified
363 by ControllerHandle and the child controller specified
364 by RemainingDevicePath are both supported by this
367 @retval EFI_SUCCESS The device specified by ControllerHandle and
368 RemainingDevicePath is supported by the driver specified by This.
369 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
370 RemainingDevicePath is already being managed by the driver
372 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
373 RemainingDevicePath is already being managed by a different
374 driver or an application that requires exclusive access.
375 Currently not implemented.
376 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
377 RemainingDevicePath is not supported by the driver specified by This.
381 SdMmcPciHcDriverBindingSupported (
382 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
383 IN EFI_HANDLE Controller
,
384 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
388 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
389 EFI_PCI_IO_PROTOCOL
*PciIo
;
393 ParentDevicePath
= NULL
;
396 // SdPciHcDxe is a device driver, and should ingore the
397 // "RemainingDevicePath" according to EFI spec.
399 Status
= gBS
->OpenProtocol (
401 &gEfiDevicePathProtocolGuid
,
402 (VOID
*) &ParentDevicePath
,
403 This
->DriverBindingHandle
,
405 EFI_OPEN_PROTOCOL_BY_DRIVER
407 if (EFI_ERROR (Status
)) {
409 // EFI_ALREADY_STARTED is also an error.
414 // Close the protocol because we don't use it here.
418 &gEfiDevicePathProtocolGuid
,
419 This
->DriverBindingHandle
,
424 // Now test the EfiPciIoProtocol.
426 Status
= gBS
->OpenProtocol (
428 &gEfiPciIoProtocolGuid
,
430 This
->DriverBindingHandle
,
432 EFI_OPEN_PROTOCOL_BY_DRIVER
434 if (EFI_ERROR (Status
)) {
439 // Now further check the PCI header: Base class (offset 0x08) and
440 // Sub Class (offset 0x05). This controller should be an SD/MMC PCI
443 Status
= PciIo
->Pci
.Read (
450 if (EFI_ERROR (Status
)) {
453 &gEfiPciIoProtocolGuid
,
454 This
->DriverBindingHandle
,
457 return EFI_UNSUPPORTED
;
460 // Since we already got the PciData, we can close protocol to avoid to carry it
461 // on for multiple exit points.
465 &gEfiPciIoProtocolGuid
,
466 This
->DriverBindingHandle
,
471 // Examine SD PCI Host Controller PCI Configuration table fields.
473 if ((PciData
.Hdr
.ClassCode
[2] == PCI_CLASS_SYSTEM_PERIPHERAL
) &&
474 (PciData
.Hdr
.ClassCode
[1] == PCI_SUBCLASS_SD_HOST_CONTROLLER
) &&
475 ((PciData
.Hdr
.ClassCode
[0] == 0x00) || (PciData
.Hdr
.ClassCode
[0] == 0x01))) {
479 return EFI_UNSUPPORTED
;
483 Starts a device controller or a bus controller.
485 The Start() function is designed to be invoked from the EFI boot service ConnectController().
486 As a result, much of the error checking on the parameters to Start() has been moved into this
487 common boot service. It is legal to call Start() from other locations,
488 but the following calling restrictions must be followed or the system behavior will not be deterministic.
489 1. ControllerHandle must be a valid EFI_HANDLE.
490 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
491 EFI_DEVICE_PATH_PROTOCOL.
492 3. Prior to calling Start(), the Supported() function for the driver specified by This must
493 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
495 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
496 @param[in] ControllerHandle The handle of the controller to start. This handle
497 must support a protocol interface that supplies
498 an I/O abstraction to the driver.
499 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
500 parameter is ignored by device drivers, and is optional for bus
501 drivers. For a bus driver, if this parameter is NULL, then handles
502 for all the children of Controller are created by this driver.
503 If this parameter is not NULL and the first Device Path Node is
504 not the End of Device Path Node, then only the handle for the
505 child device specified by the first Device Path Node of
506 RemainingDevicePath is created by this driver.
507 If the first Device Path Node of RemainingDevicePath is
508 the End of Device Path Node, no child handle is created by this
511 @retval EFI_SUCCESS The device was started.
512 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
513 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
514 @retval Others The driver failded to start the device.
519 SdMmcPciHcDriverBindingStart (
520 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
521 IN EFI_HANDLE Controller
,
522 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
526 SD_MMC_HC_PRIVATE_DATA
*Private
;
527 EFI_PCI_IO_PROTOCOL
*PciIo
;
529 UINT64 PciAttributes
;
534 CARD_TYPE_DETECT_ROUTINE
*Routine
;
536 BOOLEAN MediaPresent
;
537 BOOLEAN Support64BitDma
;
539 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStart: Start\n"));
542 // Open PCI I/O Protocol and save pointer to open protocol
543 // in private data area.
546 Status
= gBS
->OpenProtocol (
548 &gEfiPciIoProtocolGuid
,
550 This
->DriverBindingHandle
,
552 EFI_OPEN_PROTOCOL_BY_DRIVER
554 if (EFI_ERROR (Status
)) {
559 // Enable the SD Host Controller MMIO space
562 Status
= PciIo
->Attributes (
564 EfiPciIoAttributeOperationGet
,
569 if (EFI_ERROR (Status
)) {
573 Status
= PciIo
->Attributes (
575 EfiPciIoAttributeOperationSupported
,
580 if (!EFI_ERROR (Status
)) {
581 Supports
&= (UINT64
)EFI_PCI_DEVICE_ENABLE
;
582 Status
= PciIo
->Attributes (
584 EfiPciIoAttributeOperationEnable
,
592 Private
= AllocateCopyPool (sizeof (SD_MMC_HC_PRIVATE_DATA
), &gSdMmcPciHcTemplate
);
593 if (Private
== NULL
) {
594 Status
= EFI_OUT_OF_RESOURCES
;
598 Private
->ControllerHandle
= Controller
;
599 Private
->PciIo
= PciIo
;
600 Private
->PciAttributes
= PciAttributes
;
601 InitializeListHead (&Private
->Queue
);
604 // Get SD/MMC Pci Host Controller Slot info
606 Status
= SdMmcHcGetSlotInfo (PciIo
, &FirstBar
, &SlotNum
);
607 if (EFI_ERROR (Status
)) {
612 // Attempt to locate the singleton instance of the SD/MMC override protocol,
613 // which implements platform specific workarounds for non-standard SDHCI
616 if (mOverride
== NULL
) {
617 Status
= gBS
->LocateProtocol (&gEdkiiSdMmcOverrideProtocolGuid
, NULL
,
618 (VOID
**)&mOverride
);
619 if (!EFI_ERROR (Status
)) {
620 DEBUG ((DEBUG_INFO
, "%a: found SD/MMC override protocol\n",
625 Support64BitDma
= TRUE
;
626 for (Slot
= FirstBar
; Slot
< (FirstBar
+ SlotNum
); Slot
++) {
627 Private
->Slot
[Slot
].Enable
= TRUE
;
630 // Get SD/MMC Pci Host Controller Version
632 Status
= SdMmcHcGetControllerVersion (PciIo
, Slot
, &Private
->ControllerVersion
[Slot
]);
633 if (EFI_ERROR (Status
)) {
637 Status
= SdMmcHcGetCapability (PciIo
, Slot
, &Private
->Capability
[Slot
]);
638 if (EFI_ERROR (Status
)) {
642 Private
->BaseClkFreq
[Slot
] = Private
->Capability
[Slot
].BaseClkFreq
;
644 if (mOverride
!= NULL
&& mOverride
->Capability
!= NULL
) {
645 Status
= mOverride
->Capability (
648 &Private
->Capability
[Slot
],
649 &Private
->BaseClkFreq
[Slot
]
651 if (EFI_ERROR (Status
)) {
652 DEBUG ((DEBUG_WARN
, "%a: Failed to override capability - %r\n",
653 __FUNCTION__
, Status
));
657 DumpCapabilityReg (Slot
, &Private
->Capability
[Slot
]);
660 "Slot[%d] Base Clock Frequency: %dMHz\n",
662 Private
->BaseClkFreq
[Slot
]
666 // If any of the slots does not support 64b system bus
667 // do not enable 64b DMA in the PCI layer.
669 if (Private
->Capability
[Slot
].SysBus64V3
== 0 &&
670 Private
->Capability
[Slot
].SysBus64V4
== 0) {
671 Support64BitDma
= FALSE
;
674 Status
= SdMmcHcGetMaxCurrent (PciIo
, Slot
, &Private
->MaxCurrent
[Slot
]);
675 if (EFI_ERROR (Status
)) {
679 Private
->Slot
[Slot
].SlotType
= Private
->Capability
[Slot
].SlotType
;
680 if ((Private
->Slot
[Slot
].SlotType
!= RemovableSlot
) && (Private
->Slot
[Slot
].SlotType
!= EmbeddedSlot
)) {
681 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDxe doesn't support the slot type [%d]!!!\n", Private
->Slot
[Slot
].SlotType
));
686 // Reset the specified slot of the SD/MMC Pci Host Controller
688 Status
= SdMmcHcReset (Private
, Slot
);
689 if (EFI_ERROR (Status
)) {
693 // Check whether there is a SD/MMC card attached
695 if (Private
->Slot
[Slot
].SlotType
== RemovableSlot
) {
696 Status
= SdMmcHcCardDetect (PciIo
, Slot
, &MediaPresent
);
697 if (EFI_ERROR (Status
) && (Status
!= EFI_MEDIA_CHANGED
)) {
699 } else if (!MediaPresent
) {
702 "SdMmcHcCardDetect: No device attached in Slot[%d]!!!\n",
709 Status
= SdMmcHcInitHost (Private
, Slot
);
710 if (EFI_ERROR (Status
)) {
714 Private
->Slot
[Slot
].MediaPresent
= TRUE
;
715 Private
->Slot
[Slot
].Initialized
= TRUE
;
716 RoutineNum
= sizeof (mCardTypeDetectRoutineTable
) / sizeof (CARD_TYPE_DETECT_ROUTINE
);
717 for (Index
= 0; Index
< RoutineNum
; Index
++) {
718 Routine
= &mCardTypeDetectRoutineTable
[Index
];
719 if (*Routine
!= NULL
) {
720 Status
= (*Routine
) (Private
, Slot
);
721 if (!EFI_ERROR (Status
)) {
727 // This card doesn't get initialized correctly.
729 if (Index
== RoutineNum
) {
730 Private
->Slot
[Slot
].Initialized
= FALSE
;
735 // Enable 64-bit DMA support in the PCI layer if this controller
738 if (Support64BitDma
) {
739 Status
= PciIo
->Attributes (
741 EfiPciIoAttributeOperationEnable
,
742 EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE
,
745 if (EFI_ERROR (Status
)) {
746 DEBUG ((DEBUG_WARN
, "SdMmcPciHcDriverBindingStart: failed to enable 64-bit DMA (%r)\n", Status
));
751 // Start the asynchronous I/O monitor
753 Status
= gBS
->CreateEvent (
754 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
756 ProcessAsyncTaskList
,
760 if (EFI_ERROR (Status
)) {
764 Status
= gBS
->SetTimer (Private
->TimerEvent
, TimerPeriodic
, SD_MMC_HC_ASYNC_TIMER
);
765 if (EFI_ERROR (Status
)) {
770 // Start the Sd removable device connection enumeration
772 Status
= gBS
->CreateEvent (
773 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
775 SdMmcPciHcEnumerateDevice
,
777 &Private
->ConnectEvent
779 if (EFI_ERROR (Status
)) {
783 Status
= gBS
->SetTimer (Private
->ConnectEvent
, TimerPeriodic
, SD_MMC_HC_ENUM_TIMER
);
784 if (EFI_ERROR (Status
)) {
788 Status
= gBS
->InstallMultipleProtocolInterfaces (
790 &gEfiSdMmcPassThruProtocolGuid
,
791 &(Private
->PassThru
),
795 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStart: %r End on %x\n", Status
, Controller
));
798 if (EFI_ERROR (Status
)) {
799 if ((Private
!= NULL
) && (Private
->PciAttributes
!= 0)) {
801 // Restore original PCI attributes
805 EfiPciIoAttributeOperationSet
,
806 Private
->PciAttributes
,
812 &gEfiPciIoProtocolGuid
,
813 This
->DriverBindingHandle
,
817 if ((Private
!= NULL
) && (Private
->TimerEvent
!= NULL
)) {
818 gBS
->CloseEvent (Private
->TimerEvent
);
821 if ((Private
!= NULL
) && (Private
->ConnectEvent
!= NULL
)) {
822 gBS
->CloseEvent (Private
->ConnectEvent
);
825 if (Private
!= NULL
) {
834 Stops a device controller or a bus controller.
836 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
837 As a result, much of the error checking on the parameters to Stop() has been moved
838 into this common boot service. It is legal to call Stop() from other locations,
839 but the following calling restrictions must be followed or the system behavior will not be deterministic.
840 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
841 same driver's Start() function.
842 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
843 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
844 Start() function, and the Start() function must have called OpenProtocol() on
845 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
847 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
848 @param[in] ControllerHandle A handle to the device being stopped. The handle must
849 support a bus specific I/O protocol for the driver
850 to use to stop the device.
851 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
852 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
853 if NumberOfChildren is 0.
855 @retval EFI_SUCCESS The device was stopped.
856 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
861 SdMmcPciHcDriverBindingStop (
862 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
863 IN EFI_HANDLE Controller
,
864 IN UINTN NumberOfChildren
,
865 IN EFI_HANDLE
*ChildHandleBuffer
869 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
870 SD_MMC_HC_PRIVATE_DATA
*Private
;
871 EFI_PCI_IO_PROTOCOL
*PciIo
;
873 LIST_ENTRY
*NextLink
;
876 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStop: Start\n"));
878 Status
= gBS
->OpenProtocol (
880 &gEfiSdMmcPassThruProtocolGuid
,
882 This
->DriverBindingHandle
,
884 EFI_OPEN_PROTOCOL_GET_PROTOCOL
886 if (EFI_ERROR (Status
)) {
890 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (PassThru
);
892 // Close Non-Blocking timer and free Task list.
894 if (Private
->TimerEvent
!= NULL
) {
895 gBS
->CloseEvent (Private
->TimerEvent
);
896 Private
->TimerEvent
= NULL
;
898 if (Private
->ConnectEvent
!= NULL
) {
899 gBS
->CloseEvent (Private
->ConnectEvent
);
900 Private
->ConnectEvent
= NULL
;
903 // As the timer is closed, there is no needs to use TPL lock to
904 // protect the critical region "queue".
906 for (Link
= GetFirstNode (&Private
->Queue
);
907 !IsNull (&Private
->Queue
, Link
);
909 NextLink
= GetNextNode (&Private
->Queue
, Link
);
910 RemoveEntryList (Link
);
911 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
912 Trb
->Packet
->TransactionStatus
= EFI_ABORTED
;
913 gBS
->SignalEvent (Trb
->Event
);
918 // Uninstall Block I/O protocol from the device handle
920 Status
= gBS
->UninstallProtocolInterface (
922 &gEfiSdMmcPassThruProtocolGuid
,
926 if (EFI_ERROR (Status
)) {
932 &gEfiPciIoProtocolGuid
,
933 This
->DriverBindingHandle
,
937 // Restore original PCI attributes
939 PciIo
= Private
->PciIo
;
940 Status
= PciIo
->Attributes (
942 EfiPciIoAttributeOperationSet
,
943 Private
->PciAttributes
,
946 ASSERT_EFI_ERROR (Status
);
950 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStop: End with %r\n", Status
));
956 Sends SD command to an SD card that is attached to the SD controller.
958 The PassThru() function sends the SD command specified by Packet to the SD card
961 If Packet is successfully sent to the SD card, then EFI_SUCCESS is returned.
963 If a device error occurs while sending the Packet, then EFI_DEVICE_ERROR is returned.
965 If Slot is not in a valid range for the SD controller, then EFI_INVALID_PARAMETER
968 If Packet defines a data command but both InDataBuffer and OutDataBuffer are NULL,
969 EFI_INVALID_PARAMETER is returned.
971 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
972 @param[in] Slot The slot number of the SD card to send the command to.
973 @param[in,out] Packet A pointer to the SD command data structure.
974 @param[in] Event If Event is NULL, blocking I/O is performed. If Event is
975 not NULL, then nonblocking I/O is performed, and Event
976 will be signaled when the Packet completes.
978 @retval EFI_SUCCESS The SD Command Packet was sent by the host.
979 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SD
981 @retval EFI_INVALID_PARAMETER Packet, Slot, or the contents of the Packet is invalid.
982 @retval EFI_INVALID_PARAMETER Packet defines a data command but both InDataBuffer and
983 OutDataBuffer are NULL.
984 @retval EFI_NO_MEDIA SD Device not present in the Slot.
985 @retval EFI_UNSUPPORTED The command described by the SD Command Packet is not
986 supported by the host controller.
987 @retval EFI_BAD_BUFFER_SIZE The InTransferLength or OutTransferLength exceeds the
988 limit supported by SD card ( i.e. if the number of bytes
989 exceed the Last LBA).
994 SdMmcPassThruPassThru (
995 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
997 IN OUT EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
,
998 IN EFI_EVENT Event OPTIONAL
1002 SD_MMC_HC_PRIVATE_DATA
*Private
;
1006 if ((This
== NULL
) || (Packet
== NULL
)) {
1007 return EFI_INVALID_PARAMETER
;
1010 if ((Packet
->SdMmcCmdBlk
== NULL
) || (Packet
->SdMmcStatusBlk
== NULL
)) {
1011 return EFI_INVALID_PARAMETER
;
1014 if ((Packet
->OutDataBuffer
== NULL
) && (Packet
->OutTransferLength
!= 0)) {
1015 return EFI_INVALID_PARAMETER
;
1018 if ((Packet
->InDataBuffer
== NULL
) && (Packet
->InTransferLength
!= 0)) {
1019 return EFI_INVALID_PARAMETER
;
1022 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1024 if (!Private
->Slot
[Slot
].Enable
) {
1025 return EFI_INVALID_PARAMETER
;
1028 if (!Private
->Slot
[Slot
].MediaPresent
) {
1029 return EFI_NO_MEDIA
;
1032 if (!Private
->Slot
[Slot
].Initialized
) {
1033 return EFI_DEVICE_ERROR
;
1036 Trb
= SdMmcCreateTrb (Private
, Slot
, Packet
, Event
);
1038 return EFI_OUT_OF_RESOURCES
;
1041 // Immediately return for async I/O.
1043 if (Event
!= NULL
) {
1048 // Wait async I/O list is empty before execute sync I/O operation.
1051 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1052 if (IsListEmpty (&Private
->Queue
)) {
1053 gBS
->RestoreTPL (OldTpl
);
1056 gBS
->RestoreTPL (OldTpl
);
1059 Status
= SdMmcWaitTrbEnv (Private
, Trb
);
1060 if (EFI_ERROR (Status
)) {
1064 Status
= SdMmcExecTrb (Private
, Trb
);
1065 if (EFI_ERROR (Status
)) {
1069 Status
= SdMmcWaitTrbResult (Private
, Trb
);
1070 if (EFI_ERROR (Status
)) {
1081 Used to retrieve next slot numbers supported by the SD controller. The function
1082 returns information about all available slots (populated or not-populated).
1084 The GetNextSlot() function retrieves the next slot number on an SD controller.
1085 If on input Slot is 0xFF, then the slot number of the first slot on the SD controller
1088 If Slot is a slot number that was returned on a previous call to GetNextSlot(), then
1089 the slot number of the next slot on the SD controller is returned.
1091 If Slot is not 0xFF and Slot was not returned on a previous call to GetNextSlot(),
1092 EFI_INVALID_PARAMETER is returned.
1094 If Slot is the slot number of the last slot on the SD controller, then EFI_NOT_FOUND
1097 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
1098 @param[in,out] Slot On input, a pointer to a slot number on the SD controller.
1099 On output, a pointer to the next slot number on the SD controller.
1100 An input value of 0xFF retrieves the first slot number on the SD
1103 @retval EFI_SUCCESS The next slot number on the SD controller was returned in Slot.
1104 @retval EFI_NOT_FOUND There are no more slots on this SD controller.
1105 @retval EFI_INVALID_PARAMETER Slot is not 0xFF and Slot was not returned on a previous call
1111 SdMmcPassThruGetNextSlot (
1112 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1116 SD_MMC_HC_PRIVATE_DATA
*Private
;
1119 if ((This
== NULL
) || (Slot
== NULL
)) {
1120 return EFI_INVALID_PARAMETER
;
1123 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1125 if (*Slot
== 0xFF) {
1126 for (Index
= 0; Index
< SD_MMC_HC_MAX_SLOT
; Index
++) {
1127 if (Private
->Slot
[Index
].Enable
) {
1129 Private
->PreviousSlot
= Index
;
1133 return EFI_NOT_FOUND
;
1134 } else if (*Slot
== Private
->PreviousSlot
) {
1135 for (Index
= *Slot
+ 1; Index
< SD_MMC_HC_MAX_SLOT
; Index
++) {
1136 if (Private
->Slot
[Index
].Enable
) {
1138 Private
->PreviousSlot
= Index
;
1142 return EFI_NOT_FOUND
;
1144 return EFI_INVALID_PARAMETER
;
1149 Used to allocate and build a device path node for an SD card on the SD controller.
1151 The BuildDevicePath() function allocates and builds a single device node for the SD
1152 card specified by Slot.
1154 If the SD card specified by Slot is not present on the SD controller, then EFI_NOT_FOUND
1157 If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned.
1159 If there are not enough resources to allocate the device path node, then EFI_OUT_OF_RESOURCES
1162 Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of
1163 DevicePath are initialized to describe the SD card specified by Slot, and EFI_SUCCESS is
1166 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
1167 @param[in] Slot Specifies the slot number of the SD card for which a device
1168 path node is to be allocated and built.
1169 @param[in,out] DevicePath A pointer to a single device path node that describes the SD
1170 card specified by Slot. This function is responsible for
1171 allocating the buffer DevicePath with the boot service
1172 AllocatePool(). It is the caller's responsibility to free
1173 DevicePath when the caller is finished with DevicePath.
1175 @retval EFI_SUCCESS The device path node that describes the SD card specified by
1176 Slot was allocated and returned in DevicePath.
1177 @retval EFI_NOT_FOUND The SD card specified by Slot does not exist on the SD controller.
1178 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1179 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
1184 SdMmcPassThruBuildDevicePath (
1185 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1187 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
1190 SD_MMC_HC_PRIVATE_DATA
*Private
;
1191 SD_DEVICE_PATH
*SdNode
;
1192 EMMC_DEVICE_PATH
*EmmcNode
;
1194 if ((This
== NULL
) || (DevicePath
== NULL
) || (Slot
>= SD_MMC_HC_MAX_SLOT
)) {
1195 return EFI_INVALID_PARAMETER
;
1198 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1200 if ((!Private
->Slot
[Slot
].Enable
) || (!Private
->Slot
[Slot
].MediaPresent
)) {
1201 return EFI_NOT_FOUND
;
1204 if (Private
->Slot
[Slot
].CardType
== SdCardType
) {
1205 SdNode
= AllocateCopyPool (sizeof (SD_DEVICE_PATH
), &mSdDpTemplate
);
1206 if (SdNode
== NULL
) {
1207 return EFI_OUT_OF_RESOURCES
;
1209 SdNode
->SlotNumber
= Slot
;
1211 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) SdNode
;
1212 } else if (Private
->Slot
[Slot
].CardType
== EmmcCardType
) {
1213 EmmcNode
= AllocateCopyPool (sizeof (EMMC_DEVICE_PATH
), &mEmmcDpTemplate
);
1214 if (EmmcNode
== NULL
) {
1215 return EFI_OUT_OF_RESOURCES
;
1217 EmmcNode
->SlotNumber
= Slot
;
1219 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) EmmcNode
;
1222 // Currently we only support SD and EMMC two device nodes.
1224 return EFI_NOT_FOUND
;
1231 This function retrieves an SD card slot number based on the input device path.
1233 The GetSlotNumber() function retrieves slot number for the SD card specified by
1234 the DevicePath node. If DevicePath is NULL, EFI_INVALID_PARAMETER is returned.
1236 If DevicePath is not a device path node type that the SD Pass Thru driver supports,
1237 EFI_UNSUPPORTED is returned.
1239 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1240 @param[in] DevicePath A pointer to the device path node that describes a SD
1241 card on the SD controller.
1242 @param[out] Slot On return, points to the slot number of an SD card on
1245 @retval EFI_SUCCESS SD card slot number is returned in Slot.
1246 @retval EFI_INVALID_PARAMETER Slot or DevicePath is NULL.
1247 @retval EFI_UNSUPPORTED DevicePath is not a device path node type that the SD
1248 Pass Thru driver supports.
1253 SdMmcPassThruGetSlotNumber (
1254 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1255 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1259 SD_MMC_HC_PRIVATE_DATA
*Private
;
1260 SD_DEVICE_PATH
*SdNode
;
1261 EMMC_DEVICE_PATH
*EmmcNode
;
1264 if ((This
== NULL
) || (DevicePath
== NULL
) || (Slot
== NULL
)) {
1265 return EFI_INVALID_PARAMETER
;
1268 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1271 // Check whether the DevicePath belongs to SD_DEVICE_PATH or EMMC_DEVICE_PATH
1273 if ((DevicePath
->Type
!= MESSAGING_DEVICE_PATH
) ||
1274 ((DevicePath
->SubType
!= MSG_SD_DP
) &&
1275 (DevicePath
->SubType
!= MSG_EMMC_DP
)) ||
1276 (DevicePathNodeLength(DevicePath
) != sizeof(SD_DEVICE_PATH
)) ||
1277 (DevicePathNodeLength(DevicePath
) != sizeof(EMMC_DEVICE_PATH
))) {
1278 return EFI_UNSUPPORTED
;
1281 if (DevicePath
->SubType
== MSG_SD_DP
) {
1282 SdNode
= (SD_DEVICE_PATH
*) DevicePath
;
1283 SlotNumber
= SdNode
->SlotNumber
;
1285 EmmcNode
= (EMMC_DEVICE_PATH
*) DevicePath
;
1286 SlotNumber
= EmmcNode
->SlotNumber
;
1289 if (SlotNumber
>= SD_MMC_HC_MAX_SLOT
) {
1290 return EFI_NOT_FOUND
;
1293 if (Private
->Slot
[SlotNumber
].Enable
) {
1297 return EFI_NOT_FOUND
;
1302 Resets an SD card that is connected to the SD controller.
1304 The ResetDevice() function resets the SD card specified by Slot.
1306 If this SD controller does not support a device reset operation, EFI_UNSUPPORTED is
1309 If Slot is not in a valid slot number for this SD controller, EFI_INVALID_PARAMETER
1312 If the device reset operation is completed, EFI_SUCCESS is returned.
1314 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1315 @param[in] Slot Specifies the slot number of the SD card to be reset.
1317 @retval EFI_SUCCESS The SD card specified by Slot was reset.
1318 @retval EFI_UNSUPPORTED The SD controller does not support a device reset operation.
1319 @retval EFI_INVALID_PARAMETER Slot number is invalid.
1320 @retval EFI_NO_MEDIA SD Device not present in the Slot.
1321 @retval EFI_DEVICE_ERROR The reset command failed due to a device error
1326 SdMmcPassThruResetDevice (
1327 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1331 SD_MMC_HC_PRIVATE_DATA
*Private
;
1333 LIST_ENTRY
*NextLink
;
1338 return EFI_INVALID_PARAMETER
;
1341 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1343 if (!Private
->Slot
[Slot
].Enable
) {
1344 return EFI_INVALID_PARAMETER
;
1347 if (!Private
->Slot
[Slot
].MediaPresent
) {
1348 return EFI_NO_MEDIA
;
1351 if (!Private
->Slot
[Slot
].Initialized
) {
1352 return EFI_DEVICE_ERROR
;
1355 // Free all async I/O requests in the queue
1357 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1359 for (Link
= GetFirstNode (&Private
->Queue
);
1360 !IsNull (&Private
->Queue
, Link
);
1362 NextLink
= GetNextNode (&Private
->Queue
, Link
);
1363 RemoveEntryList (Link
);
1364 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
1365 Trb
->Packet
->TransactionStatus
= EFI_ABORTED
;
1366 gBS
->SignalEvent (Trb
->Event
);
1370 gBS
->RestoreTPL (OldTpl
);