2 This driver is used to manage SD/MMC PCI host controllers which are compliance
3 with SD Host Controller Simplified Specification version 3.00.
5 It would expose EFI_SD_MMC_PASS_THRU_PROTOCOL for upper layer use.
7 Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
8 Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
9 This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include "SdMmcPciHcDxe.h"
21 EDKII_SD_MMC_OVERRIDE
*mOverride
;
24 // Driver Global Variables
26 EFI_DRIVER_BINDING_PROTOCOL gSdMmcPciHcDriverBinding
= {
27 SdMmcPciHcDriverBindingSupported
,
28 SdMmcPciHcDriverBindingStart
,
29 SdMmcPciHcDriverBindingStop
,
36 // Template for SD/MMC host controller private data.
38 SD_MMC_HC_PRIVATE_DATA gSdMmcPciHcTemplate
= {
39 SD_MMC_HC_PRIVATE_SIGNATURE
, // Signature
40 NULL
, // ControllerHandle
44 SdMmcPassThruPassThru
,
45 SdMmcPassThruGetNextSlot
,
46 SdMmcPassThruBuildDevicePath
,
47 SdMmcPassThruGetSlotNumber
,
48 SdMmcPassThruResetDevice
55 INITIALIZE_LIST_HEAD_VARIABLE (gSdMmcPciHcTemplate
.Queue
),
57 {0, UnknownSlot
, 0, 0, 0}, {0, UnknownSlot
, 0, 0, 0}, {0, UnknownSlot
, 0, 0, 0},
58 {0, UnknownSlot
, 0, 0, 0}, {0, UnknownSlot
, 0, 0, 0}, {0, UnknownSlot
, 0, 0, 0}
67 0 // ControllerVersion
71 SD_DEVICE_PATH mSdDpTemplate
= {
73 MESSAGING_DEVICE_PATH
,
76 (UINT8
) (sizeof (SD_DEVICE_PATH
)),
77 (UINT8
) ((sizeof (SD_DEVICE_PATH
)) >> 8)
83 EMMC_DEVICE_PATH mEmmcDpTemplate
= {
85 MESSAGING_DEVICE_PATH
,
88 (UINT8
) (sizeof (EMMC_DEVICE_PATH
)),
89 (UINT8
) ((sizeof (EMMC_DEVICE_PATH
)) >> 8)
96 // Prioritized function list to detect card type.
97 // User could add other card detection logic here.
99 CARD_TYPE_DETECT_ROUTINE mCardTypeDetectRoutineTable
[] = {
101 SdCardIdentification
,
106 The entry point for SD host controller driver, used to install this driver on the ImageHandle.
108 @param[in] ImageHandle The firmware allocated handle for this driver image.
109 @param[in] SystemTable Pointer to the EFI system table.
111 @retval EFI_SUCCESS Driver loaded.
112 @retval other Driver not loaded.
117 InitializeSdMmcPciHcDxe (
118 IN EFI_HANDLE ImageHandle
,
119 IN EFI_SYSTEM_TABLE
*SystemTable
124 Status
= EfiLibInstallDriverBindingComponentName2 (
127 &gSdMmcPciHcDriverBinding
,
129 &gSdMmcPciHcComponentName
,
130 &gSdMmcPciHcComponentName2
132 ASSERT_EFI_ERROR (Status
);
138 Call back function when the timer event is signaled.
140 @param[in] Event The Event this notify function registered to.
141 @param[in] Context Pointer to the context data registered to the
147 ProcessAsyncTaskList (
152 SD_MMC_HC_PRIVATE_DATA
*Private
;
156 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
157 BOOLEAN InfiniteWait
;
160 Private
= (SD_MMC_HC_PRIVATE_DATA
*)Context
;
163 // Check if the first entry in the async I/O queue is done or not.
165 Status
= EFI_SUCCESS
;
167 Link
= GetFirstNode (&Private
->Queue
);
168 if (!IsNull (&Private
->Queue
, Link
)) {
169 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
170 if (!Private
->Slot
[Trb
->Slot
].MediaPresent
) {
171 Status
= EFI_NO_MEDIA
;
176 // Check whether the cmd/data line is ready for transfer.
178 Status
= SdMmcCheckTrbEnv (Private
, Trb
);
179 if (!EFI_ERROR (Status
)) {
181 Status
= SdMmcExecTrb (Private
, Trb
);
182 if (EFI_ERROR (Status
)) {
189 Status
= SdMmcCheckTrbResult (Private
, Trb
);
193 if ((Trb
!= NULL
) && (Status
== EFI_NOT_READY
)) {
194 Packet
= Trb
->Packet
;
195 if (Packet
->Timeout
== 0) {
198 InfiniteWait
= FALSE
;
200 if ((!InfiniteWait
) && (Trb
->Timeout
-- == 0)) {
201 RemoveEntryList (Link
);
202 Trb
->Packet
->TransactionStatus
= EFI_TIMEOUT
;
203 TrbEvent
= Trb
->Event
;
205 DEBUG ((DEBUG_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p EFI_TIMEOUT\n", TrbEvent
));
206 gBS
->SignalEvent (TrbEvent
);
210 if ((Trb
!= NULL
) && (Status
!= EFI_NOT_READY
)) {
211 RemoveEntryList (Link
);
212 Trb
->Packet
->TransactionStatus
= Status
;
213 TrbEvent
= Trb
->Event
;
215 DEBUG ((DEBUG_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p with %r\n", TrbEvent
, Status
));
216 gBS
->SignalEvent (TrbEvent
);
222 Sd removable device enumeration callback function when the timer event is signaled.
224 @param[in] Event The Event this notify function registered to.
225 @param[in] Context Pointer to the context data registered to the
231 SdMmcPciHcEnumerateDevice (
236 SD_MMC_HC_PRIVATE_DATA
*Private
;
239 BOOLEAN MediaPresent
;
241 CARD_TYPE_DETECT_ROUTINE
*Routine
;
244 LIST_ENTRY
*NextLink
;
248 Private
= (SD_MMC_HC_PRIVATE_DATA
*)Context
;
250 for (Slot
= 0; Slot
< SD_MMC_HC_MAX_SLOT
; Slot
++) {
251 if ((Private
->Slot
[Slot
].Enable
) && (Private
->Slot
[Slot
].SlotType
== RemovableSlot
)) {
252 Status
= SdMmcHcCardDetect (Private
->PciIo
, Slot
, &MediaPresent
);
253 if ((Status
== EFI_MEDIA_CHANGED
) && !MediaPresent
) {
254 DEBUG ((DEBUG_INFO
, "SdMmcPciHcEnumerateDevice: device disconnected at slot %d of pci %p\n", Slot
, Private
->PciIo
));
255 Private
->Slot
[Slot
].MediaPresent
= FALSE
;
256 Private
->Slot
[Slot
].Initialized
= FALSE
;
258 // Signal all async task events at the slot with EFI_NO_MEDIA status.
260 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
261 for (Link
= GetFirstNode (&Private
->Queue
);
262 !IsNull (&Private
->Queue
, Link
);
264 NextLink
= GetNextNode (&Private
->Queue
, Link
);
265 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
266 if (Trb
->Slot
== Slot
) {
267 RemoveEntryList (Link
);
268 Trb
->Packet
->TransactionStatus
= EFI_NO_MEDIA
;
269 gBS
->SignalEvent (Trb
->Event
);
273 gBS
->RestoreTPL (OldTpl
);
275 // Notify the upper layer the connect state change through ReinstallProtocolInterface.
277 gBS
->ReinstallProtocolInterface (
278 Private
->ControllerHandle
,
279 &gEfiSdMmcPassThruProtocolGuid
,
284 if ((Status
== EFI_MEDIA_CHANGED
) && MediaPresent
) {
285 DEBUG ((DEBUG_INFO
, "SdMmcPciHcEnumerateDevice: device connected at slot %d of pci %p\n", Slot
, Private
->PciIo
));
287 // Reset the specified slot of the SD/MMC Pci Host Controller
289 Status
= SdMmcHcReset (Private
, Slot
);
290 if (EFI_ERROR (Status
)) {
294 // Reinitialize slot and restart identification process for the new attached device
296 Status
= SdMmcHcInitHost (Private
, Slot
);
297 if (EFI_ERROR (Status
)) {
301 Private
->Slot
[Slot
].MediaPresent
= TRUE
;
302 Private
->Slot
[Slot
].Initialized
= TRUE
;
303 RoutineNum
= sizeof (mCardTypeDetectRoutineTable
) / sizeof (CARD_TYPE_DETECT_ROUTINE
);
304 for (Index
= 0; Index
< RoutineNum
; Index
++) {
305 Routine
= &mCardTypeDetectRoutineTable
[Index
];
306 if (*Routine
!= NULL
) {
307 Status
= (*Routine
) (Private
, Slot
);
308 if (!EFI_ERROR (Status
)) {
314 // This card doesn't get initialized correctly.
316 if (Index
== RoutineNum
) {
317 Private
->Slot
[Slot
].Initialized
= FALSE
;
321 // Notify the upper layer the connect state change through ReinstallProtocolInterface.
323 gBS
->ReinstallProtocolInterface (
324 Private
->ControllerHandle
,
325 &gEfiSdMmcPassThruProtocolGuid
,
336 Tests to see if this driver supports a given controller. If a child device is provided,
337 it further tests to see if this driver supports creating a handle for the specified child device.
339 This function checks to see if the driver specified by This supports the device specified by
340 ControllerHandle. Drivers will typically use the device path attached to
341 ControllerHandle and/or the services from the bus I/O abstraction attached to
342 ControllerHandle to determine if the driver supports ControllerHandle. This function
343 may be called many times during platform initialization. In order to reduce boot times, the tests
344 performed by this function must be very small, and take as little time as possible to execute. This
345 function must not change the state of any hardware devices, and this function must be aware that the
346 device specified by ControllerHandle may already be managed by the same driver or a
347 different driver. This function must match its calls to AllocatePages() with FreePages(),
348 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
349 Since ControllerHandle may have been previously started by the same driver, if a protocol is
350 already in the opened state, then it must not be closed with CloseProtocol(). This is required
351 to guarantee the state of ControllerHandle is not modified by this function.
353 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
354 @param[in] ControllerHandle The handle of the controller to test. This handle
355 must support a protocol interface that supplies
356 an I/O abstraction to the driver.
357 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
358 parameter is ignored by device drivers, and is optional for bus
359 drivers. For bus drivers, if this parameter is not NULL, then
360 the bus driver must determine if the bus controller specified
361 by ControllerHandle and the child controller specified
362 by RemainingDevicePath are both supported by this
365 @retval EFI_SUCCESS The device specified by ControllerHandle and
366 RemainingDevicePath is supported by the driver specified by This.
367 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
368 RemainingDevicePath is already being managed by the driver
370 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
371 RemainingDevicePath is already being managed by a different
372 driver or an application that requires exclusive access.
373 Currently not implemented.
374 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
375 RemainingDevicePath is not supported by the driver specified by This.
379 SdMmcPciHcDriverBindingSupported (
380 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
381 IN EFI_HANDLE Controller
,
382 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
386 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
387 EFI_PCI_IO_PROTOCOL
*PciIo
;
391 ParentDevicePath
= NULL
;
394 // SdPciHcDxe is a device driver, and should ingore the
395 // "RemainingDevicePath" according to EFI spec.
397 Status
= gBS
->OpenProtocol (
399 &gEfiDevicePathProtocolGuid
,
400 (VOID
*) &ParentDevicePath
,
401 This
->DriverBindingHandle
,
403 EFI_OPEN_PROTOCOL_BY_DRIVER
405 if (EFI_ERROR (Status
)) {
407 // EFI_ALREADY_STARTED is also an error.
412 // Close the protocol because we don't use it here.
416 &gEfiDevicePathProtocolGuid
,
417 This
->DriverBindingHandle
,
422 // Now test the EfiPciIoProtocol.
424 Status
= gBS
->OpenProtocol (
426 &gEfiPciIoProtocolGuid
,
428 This
->DriverBindingHandle
,
430 EFI_OPEN_PROTOCOL_BY_DRIVER
432 if (EFI_ERROR (Status
)) {
437 // Now further check the PCI header: Base class (offset 0x08) and
438 // Sub Class (offset 0x05). This controller should be an SD/MMC PCI
441 Status
= PciIo
->Pci
.Read (
448 if (EFI_ERROR (Status
)) {
451 &gEfiPciIoProtocolGuid
,
452 This
->DriverBindingHandle
,
455 return EFI_UNSUPPORTED
;
458 // Since we already got the PciData, we can close protocol to avoid to carry it
459 // on for multiple exit points.
463 &gEfiPciIoProtocolGuid
,
464 This
->DriverBindingHandle
,
469 // Examine SD PCI Host Controller PCI Configuration table fields.
471 if ((PciData
.Hdr
.ClassCode
[2] == PCI_CLASS_SYSTEM_PERIPHERAL
) &&
472 (PciData
.Hdr
.ClassCode
[1] == PCI_SUBCLASS_SD_HOST_CONTROLLER
) &&
473 ((PciData
.Hdr
.ClassCode
[0] == 0x00) || (PciData
.Hdr
.ClassCode
[0] == 0x01))) {
477 return EFI_UNSUPPORTED
;
481 Starts a device controller or a bus controller.
483 The Start() function is designed to be invoked from the EFI boot service ConnectController().
484 As a result, much of the error checking on the parameters to Start() has been moved into this
485 common boot service. It is legal to call Start() from other locations,
486 but the following calling restrictions must be followed or the system behavior will not be deterministic.
487 1. ControllerHandle must be a valid EFI_HANDLE.
488 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
489 EFI_DEVICE_PATH_PROTOCOL.
490 3. Prior to calling Start(), the Supported() function for the driver specified by This must
491 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
493 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
494 @param[in] ControllerHandle The handle of the controller to start. This handle
495 must support a protocol interface that supplies
496 an I/O abstraction to the driver.
497 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
498 parameter is ignored by device drivers, and is optional for bus
499 drivers. For a bus driver, if this parameter is NULL, then handles
500 for all the children of Controller are created by this driver.
501 If this parameter is not NULL and the first Device Path Node is
502 not the End of Device Path Node, then only the handle for the
503 child device specified by the first Device Path Node of
504 RemainingDevicePath is created by this driver.
505 If the first Device Path Node of RemainingDevicePath is
506 the End of Device Path Node, no child handle is created by this
509 @retval EFI_SUCCESS The device was started.
510 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
511 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
512 @retval Others The driver failded to start the device.
517 SdMmcPciHcDriverBindingStart (
518 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
519 IN EFI_HANDLE Controller
,
520 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
524 SD_MMC_HC_PRIVATE_DATA
*Private
;
525 EFI_PCI_IO_PROTOCOL
*PciIo
;
527 UINT64 PciAttributes
;
532 CARD_TYPE_DETECT_ROUTINE
*Routine
;
534 BOOLEAN MediaPresent
;
535 BOOLEAN Support64BitDma
;
537 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStart: Start\n"));
540 // Open PCI I/O Protocol and save pointer to open protocol
541 // in private data area.
544 Status
= gBS
->OpenProtocol (
546 &gEfiPciIoProtocolGuid
,
548 This
->DriverBindingHandle
,
550 EFI_OPEN_PROTOCOL_BY_DRIVER
552 if (EFI_ERROR (Status
)) {
557 // Enable the SD Host Controller MMIO space
560 Status
= PciIo
->Attributes (
562 EfiPciIoAttributeOperationGet
,
567 if (EFI_ERROR (Status
)) {
571 Status
= PciIo
->Attributes (
573 EfiPciIoAttributeOperationSupported
,
578 if (!EFI_ERROR (Status
)) {
579 Supports
&= (UINT64
)EFI_PCI_DEVICE_ENABLE
;
580 Status
= PciIo
->Attributes (
582 EfiPciIoAttributeOperationEnable
,
590 Private
= AllocateCopyPool (sizeof (SD_MMC_HC_PRIVATE_DATA
), &gSdMmcPciHcTemplate
);
591 if (Private
== NULL
) {
592 Status
= EFI_OUT_OF_RESOURCES
;
596 Private
->ControllerHandle
= Controller
;
597 Private
->PciIo
= PciIo
;
598 Private
->PciAttributes
= PciAttributes
;
599 InitializeListHead (&Private
->Queue
);
602 // Get SD/MMC Pci Host Controller Slot info
604 Status
= SdMmcHcGetSlotInfo (PciIo
, &FirstBar
, &SlotNum
);
605 if (EFI_ERROR (Status
)) {
610 // Attempt to locate the singleton instance of the SD/MMC override protocol,
611 // which implements platform specific workarounds for non-standard SDHCI
614 if (mOverride
== NULL
) {
615 Status
= gBS
->LocateProtocol (&gEdkiiSdMmcOverrideProtocolGuid
, NULL
,
616 (VOID
**)&mOverride
);
617 if (!EFI_ERROR (Status
)) {
618 DEBUG ((DEBUG_INFO
, "%a: found SD/MMC override protocol\n",
623 Support64BitDma
= TRUE
;
624 for (Slot
= FirstBar
; Slot
< (FirstBar
+ SlotNum
); Slot
++) {
625 Private
->Slot
[Slot
].Enable
= TRUE
;
628 // Get SD/MMC Pci Host Controller Version
630 Status
= SdMmcHcGetControllerVersion (PciIo
, Slot
, &Private
->ControllerVersion
[Slot
]);
631 if (EFI_ERROR (Status
)) {
635 Status
= SdMmcHcGetCapability (PciIo
, Slot
, &Private
->Capability
[Slot
]);
636 if (EFI_ERROR (Status
)) {
640 Private
->BaseClkFreq
[Slot
] = Private
->Capability
[Slot
].BaseClkFreq
;
642 if (mOverride
!= NULL
&& mOverride
->Capability
!= NULL
) {
643 Status
= mOverride
->Capability (
646 &Private
->Capability
[Slot
],
647 &Private
->BaseClkFreq
[Slot
]
649 if (EFI_ERROR (Status
)) {
650 DEBUG ((DEBUG_WARN
, "%a: Failed to override capability - %r\n",
651 __FUNCTION__
, Status
));
655 DumpCapabilityReg (Slot
, &Private
->Capability
[Slot
]);
658 "Slot[%d] Base Clock Frequency: %dMHz\n",
660 Private
->BaseClkFreq
[Slot
]
664 // If any of the slots does not support 64b system bus
665 // do not enable 64b DMA in the PCI layer.
667 if (Private
->Capability
[Slot
].SysBus64V3
== 0 &&
668 Private
->Capability
[Slot
].SysBus64V4
== 0) {
669 Support64BitDma
= FALSE
;
672 Status
= SdMmcHcGetMaxCurrent (PciIo
, Slot
, &Private
->MaxCurrent
[Slot
]);
673 if (EFI_ERROR (Status
)) {
677 Private
->Slot
[Slot
].SlotType
= Private
->Capability
[Slot
].SlotType
;
678 if ((Private
->Slot
[Slot
].SlotType
!= RemovableSlot
) && (Private
->Slot
[Slot
].SlotType
!= EmbeddedSlot
)) {
679 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDxe doesn't support the slot type [%d]!!!\n", Private
->Slot
[Slot
].SlotType
));
684 // Reset the specified slot of the SD/MMC Pci Host Controller
686 Status
= SdMmcHcReset (Private
, Slot
);
687 if (EFI_ERROR (Status
)) {
691 // Check whether there is a SD/MMC card attached
693 if (Private
->Slot
[Slot
].SlotType
== RemovableSlot
) {
694 Status
= SdMmcHcCardDetect (PciIo
, Slot
, &MediaPresent
);
695 if (EFI_ERROR (Status
) && (Status
!= EFI_MEDIA_CHANGED
)) {
697 } else if (!MediaPresent
) {
700 "SdMmcHcCardDetect: No device attached in Slot[%d]!!!\n",
707 Status
= SdMmcHcInitHost (Private
, Slot
);
708 if (EFI_ERROR (Status
)) {
712 Private
->Slot
[Slot
].MediaPresent
= TRUE
;
713 Private
->Slot
[Slot
].Initialized
= TRUE
;
714 RoutineNum
= sizeof (mCardTypeDetectRoutineTable
) / sizeof (CARD_TYPE_DETECT_ROUTINE
);
715 for (Index
= 0; Index
< RoutineNum
; Index
++) {
716 Routine
= &mCardTypeDetectRoutineTable
[Index
];
717 if (*Routine
!= NULL
) {
718 Status
= (*Routine
) (Private
, Slot
);
719 if (!EFI_ERROR (Status
)) {
725 // This card doesn't get initialized correctly.
727 if (Index
== RoutineNum
) {
728 Private
->Slot
[Slot
].Initialized
= FALSE
;
733 // Enable 64-bit DMA support in the PCI layer if this controller
736 if (Support64BitDma
) {
737 Status
= PciIo
->Attributes (
739 EfiPciIoAttributeOperationEnable
,
740 EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE
,
743 if (EFI_ERROR (Status
)) {
744 DEBUG ((DEBUG_WARN
, "SdMmcPciHcDriverBindingStart: failed to enable 64-bit DMA (%r)\n", Status
));
749 // Start the asynchronous I/O monitor
751 Status
= gBS
->CreateEvent (
752 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
754 ProcessAsyncTaskList
,
758 if (EFI_ERROR (Status
)) {
762 Status
= gBS
->SetTimer (Private
->TimerEvent
, TimerPeriodic
, SD_MMC_HC_ASYNC_TIMER
);
763 if (EFI_ERROR (Status
)) {
768 // Start the Sd removable device connection enumeration
770 Status
= gBS
->CreateEvent (
771 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
773 SdMmcPciHcEnumerateDevice
,
775 &Private
->ConnectEvent
777 if (EFI_ERROR (Status
)) {
781 Status
= gBS
->SetTimer (Private
->ConnectEvent
, TimerPeriodic
, SD_MMC_HC_ENUM_TIMER
);
782 if (EFI_ERROR (Status
)) {
786 Status
= gBS
->InstallMultipleProtocolInterfaces (
788 &gEfiSdMmcPassThruProtocolGuid
,
789 &(Private
->PassThru
),
793 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStart: %r End on %x\n", Status
, Controller
));
796 if (EFI_ERROR (Status
)) {
797 if ((Private
!= NULL
) && (Private
->PciAttributes
!= 0)) {
799 // Restore original PCI attributes
803 EfiPciIoAttributeOperationSet
,
804 Private
->PciAttributes
,
810 &gEfiPciIoProtocolGuid
,
811 This
->DriverBindingHandle
,
815 if ((Private
!= NULL
) && (Private
->TimerEvent
!= NULL
)) {
816 gBS
->CloseEvent (Private
->TimerEvent
);
819 if ((Private
!= NULL
) && (Private
->ConnectEvent
!= NULL
)) {
820 gBS
->CloseEvent (Private
->ConnectEvent
);
823 if (Private
!= NULL
) {
832 Stops a device controller or a bus controller.
834 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
835 As a result, much of the error checking on the parameters to Stop() has been moved
836 into this common boot service. It is legal to call Stop() from other locations,
837 but the following calling restrictions must be followed or the system behavior will not be deterministic.
838 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
839 same driver's Start() function.
840 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
841 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
842 Start() function, and the Start() function must have called OpenProtocol() on
843 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
845 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
846 @param[in] ControllerHandle A handle to the device being stopped. The handle must
847 support a bus specific I/O protocol for the driver
848 to use to stop the device.
849 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
850 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
851 if NumberOfChildren is 0.
853 @retval EFI_SUCCESS The device was stopped.
854 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
859 SdMmcPciHcDriverBindingStop (
860 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
861 IN EFI_HANDLE Controller
,
862 IN UINTN NumberOfChildren
,
863 IN EFI_HANDLE
*ChildHandleBuffer
867 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
868 SD_MMC_HC_PRIVATE_DATA
*Private
;
869 EFI_PCI_IO_PROTOCOL
*PciIo
;
871 LIST_ENTRY
*NextLink
;
874 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStop: Start\n"));
876 Status
= gBS
->OpenProtocol (
878 &gEfiSdMmcPassThruProtocolGuid
,
880 This
->DriverBindingHandle
,
882 EFI_OPEN_PROTOCOL_GET_PROTOCOL
884 if (EFI_ERROR (Status
)) {
888 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (PassThru
);
890 // Close Non-Blocking timer and free Task list.
892 if (Private
->TimerEvent
!= NULL
) {
893 gBS
->CloseEvent (Private
->TimerEvent
);
894 Private
->TimerEvent
= NULL
;
896 if (Private
->ConnectEvent
!= NULL
) {
897 gBS
->CloseEvent (Private
->ConnectEvent
);
898 Private
->ConnectEvent
= NULL
;
901 // As the timer is closed, there is no needs to use TPL lock to
902 // protect the critical region "queue".
904 for (Link
= GetFirstNode (&Private
->Queue
);
905 !IsNull (&Private
->Queue
, Link
);
907 NextLink
= GetNextNode (&Private
->Queue
, Link
);
908 RemoveEntryList (Link
);
909 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
910 Trb
->Packet
->TransactionStatus
= EFI_ABORTED
;
911 gBS
->SignalEvent (Trb
->Event
);
916 // Uninstall Block I/O protocol from the device handle
918 Status
= gBS
->UninstallProtocolInterface (
920 &gEfiSdMmcPassThruProtocolGuid
,
924 if (EFI_ERROR (Status
)) {
930 &gEfiPciIoProtocolGuid
,
931 This
->DriverBindingHandle
,
935 // Restore original PCI attributes
937 PciIo
= Private
->PciIo
;
938 Status
= PciIo
->Attributes (
940 EfiPciIoAttributeOperationSet
,
941 Private
->PciAttributes
,
944 ASSERT_EFI_ERROR (Status
);
948 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStop: End with %r\n", Status
));
954 Sends SD command to an SD card that is attached to the SD controller.
956 The PassThru() function sends the SD command specified by Packet to the SD card
959 If Packet is successfully sent to the SD card, then EFI_SUCCESS is returned.
961 If a device error occurs while sending the Packet, then EFI_DEVICE_ERROR is returned.
963 If Slot is not in a valid range for the SD controller, then EFI_INVALID_PARAMETER
966 If Packet defines a data command but both InDataBuffer and OutDataBuffer are NULL,
967 EFI_INVALID_PARAMETER is returned.
969 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
970 @param[in] Slot The slot number of the SD card to send the command to.
971 @param[in,out] Packet A pointer to the SD command data structure.
972 @param[in] Event If Event is NULL, blocking I/O is performed. If Event is
973 not NULL, then nonblocking I/O is performed, and Event
974 will be signaled when the Packet completes.
976 @retval EFI_SUCCESS The SD Command Packet was sent by the host.
977 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SD
979 @retval EFI_INVALID_PARAMETER Packet, Slot, or the contents of the Packet is invalid.
980 @retval EFI_INVALID_PARAMETER Packet defines a data command but both InDataBuffer and
981 OutDataBuffer are NULL.
982 @retval EFI_NO_MEDIA SD Device not present in the Slot.
983 @retval EFI_UNSUPPORTED The command described by the SD Command Packet is not
984 supported by the host controller.
985 @retval EFI_BAD_BUFFER_SIZE The InTransferLength or OutTransferLength exceeds the
986 limit supported by SD card ( i.e. if the number of bytes
987 exceed the Last LBA).
992 SdMmcPassThruPassThru (
993 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
995 IN OUT EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
,
996 IN EFI_EVENT Event OPTIONAL
1000 SD_MMC_HC_PRIVATE_DATA
*Private
;
1004 if ((This
== NULL
) || (Packet
== NULL
)) {
1005 return EFI_INVALID_PARAMETER
;
1008 if ((Packet
->SdMmcCmdBlk
== NULL
) || (Packet
->SdMmcStatusBlk
== NULL
)) {
1009 return EFI_INVALID_PARAMETER
;
1012 if ((Packet
->OutDataBuffer
== NULL
) && (Packet
->OutTransferLength
!= 0)) {
1013 return EFI_INVALID_PARAMETER
;
1016 if ((Packet
->InDataBuffer
== NULL
) && (Packet
->InTransferLength
!= 0)) {
1017 return EFI_INVALID_PARAMETER
;
1020 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1022 if (!Private
->Slot
[Slot
].Enable
) {
1023 return EFI_INVALID_PARAMETER
;
1026 if (!Private
->Slot
[Slot
].MediaPresent
) {
1027 return EFI_NO_MEDIA
;
1030 if (!Private
->Slot
[Slot
].Initialized
) {
1031 return EFI_DEVICE_ERROR
;
1034 Trb
= SdMmcCreateTrb (Private
, Slot
, Packet
, Event
);
1036 return EFI_OUT_OF_RESOURCES
;
1039 // Immediately return for async I/O.
1041 if (Event
!= NULL
) {
1046 // Wait async I/O list is empty before execute sync I/O operation.
1049 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1050 if (IsListEmpty (&Private
->Queue
)) {
1051 gBS
->RestoreTPL (OldTpl
);
1054 gBS
->RestoreTPL (OldTpl
);
1057 Status
= SdMmcWaitTrbEnv (Private
, Trb
);
1058 if (EFI_ERROR (Status
)) {
1062 Status
= SdMmcExecTrb (Private
, Trb
);
1063 if (EFI_ERROR (Status
)) {
1067 Status
= SdMmcWaitTrbResult (Private
, Trb
);
1068 if (EFI_ERROR (Status
)) {
1079 Used to retrieve next slot numbers supported by the SD controller. The function
1080 returns information about all available slots (populated or not-populated).
1082 The GetNextSlot() function retrieves the next slot number on an SD controller.
1083 If on input Slot is 0xFF, then the slot number of the first slot on the SD controller
1086 If Slot is a slot number that was returned on a previous call to GetNextSlot(), then
1087 the slot number of the next slot on the SD controller is returned.
1089 If Slot is not 0xFF and Slot was not returned on a previous call to GetNextSlot(),
1090 EFI_INVALID_PARAMETER is returned.
1092 If Slot is the slot number of the last slot on the SD controller, then EFI_NOT_FOUND
1095 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
1096 @param[in,out] Slot On input, a pointer to a slot number on the SD controller.
1097 On output, a pointer to the next slot number on the SD controller.
1098 An input value of 0xFF retrieves the first slot number on the SD
1101 @retval EFI_SUCCESS The next slot number on the SD controller was returned in Slot.
1102 @retval EFI_NOT_FOUND There are no more slots on this SD controller.
1103 @retval EFI_INVALID_PARAMETER Slot is not 0xFF and Slot was not returned on a previous call
1109 SdMmcPassThruGetNextSlot (
1110 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1114 SD_MMC_HC_PRIVATE_DATA
*Private
;
1117 if ((This
== NULL
) || (Slot
== NULL
)) {
1118 return EFI_INVALID_PARAMETER
;
1121 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1123 if (*Slot
== 0xFF) {
1124 for (Index
= 0; Index
< SD_MMC_HC_MAX_SLOT
; Index
++) {
1125 if (Private
->Slot
[Index
].Enable
) {
1127 Private
->PreviousSlot
= Index
;
1131 return EFI_NOT_FOUND
;
1132 } else if (*Slot
== Private
->PreviousSlot
) {
1133 for (Index
= *Slot
+ 1; Index
< SD_MMC_HC_MAX_SLOT
; Index
++) {
1134 if (Private
->Slot
[Index
].Enable
) {
1136 Private
->PreviousSlot
= Index
;
1140 return EFI_NOT_FOUND
;
1142 return EFI_INVALID_PARAMETER
;
1147 Used to allocate and build a device path node for an SD card on the SD controller.
1149 The BuildDevicePath() function allocates and builds a single device node for the SD
1150 card specified by Slot.
1152 If the SD card specified by Slot is not present on the SD controller, then EFI_NOT_FOUND
1155 If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned.
1157 If there are not enough resources to allocate the device path node, then EFI_OUT_OF_RESOURCES
1160 Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of
1161 DevicePath are initialized to describe the SD card specified by Slot, and EFI_SUCCESS is
1164 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
1165 @param[in] Slot Specifies the slot number of the SD card for which a device
1166 path node is to be allocated and built.
1167 @param[in,out] DevicePath A pointer to a single device path node that describes the SD
1168 card specified by Slot. This function is responsible for
1169 allocating the buffer DevicePath with the boot service
1170 AllocatePool(). It is the caller's responsibility to free
1171 DevicePath when the caller is finished with DevicePath.
1173 @retval EFI_SUCCESS The device path node that describes the SD card specified by
1174 Slot was allocated and returned in DevicePath.
1175 @retval EFI_NOT_FOUND The SD card specified by Slot does not exist on the SD controller.
1176 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1177 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
1182 SdMmcPassThruBuildDevicePath (
1183 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1185 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
1188 SD_MMC_HC_PRIVATE_DATA
*Private
;
1189 SD_DEVICE_PATH
*SdNode
;
1190 EMMC_DEVICE_PATH
*EmmcNode
;
1192 if ((This
== NULL
) || (DevicePath
== NULL
) || (Slot
>= SD_MMC_HC_MAX_SLOT
)) {
1193 return EFI_INVALID_PARAMETER
;
1196 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1198 if ((!Private
->Slot
[Slot
].Enable
) || (!Private
->Slot
[Slot
].MediaPresent
)) {
1199 return EFI_NOT_FOUND
;
1202 if (Private
->Slot
[Slot
].CardType
== SdCardType
) {
1203 SdNode
= AllocateCopyPool (sizeof (SD_DEVICE_PATH
), &mSdDpTemplate
);
1204 if (SdNode
== NULL
) {
1205 return EFI_OUT_OF_RESOURCES
;
1207 SdNode
->SlotNumber
= Slot
;
1209 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) SdNode
;
1210 } else if (Private
->Slot
[Slot
].CardType
== EmmcCardType
) {
1211 EmmcNode
= AllocateCopyPool (sizeof (EMMC_DEVICE_PATH
), &mEmmcDpTemplate
);
1212 if (EmmcNode
== NULL
) {
1213 return EFI_OUT_OF_RESOURCES
;
1215 EmmcNode
->SlotNumber
= Slot
;
1217 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) EmmcNode
;
1220 // Currently we only support SD and EMMC two device nodes.
1222 return EFI_NOT_FOUND
;
1229 This function retrieves an SD card slot number based on the input device path.
1231 The GetSlotNumber() function retrieves slot number for the SD card specified by
1232 the DevicePath node. If DevicePath is NULL, EFI_INVALID_PARAMETER is returned.
1234 If DevicePath is not a device path node type that the SD Pass Thru driver supports,
1235 EFI_UNSUPPORTED is returned.
1237 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1238 @param[in] DevicePath A pointer to the device path node that describes a SD
1239 card on the SD controller.
1240 @param[out] Slot On return, points to the slot number of an SD card on
1243 @retval EFI_SUCCESS SD card slot number is returned in Slot.
1244 @retval EFI_INVALID_PARAMETER Slot or DevicePath is NULL.
1245 @retval EFI_UNSUPPORTED DevicePath is not a device path node type that the SD
1246 Pass Thru driver supports.
1251 SdMmcPassThruGetSlotNumber (
1252 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1253 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1257 SD_MMC_HC_PRIVATE_DATA
*Private
;
1258 SD_DEVICE_PATH
*SdNode
;
1259 EMMC_DEVICE_PATH
*EmmcNode
;
1262 if ((This
== NULL
) || (DevicePath
== NULL
) || (Slot
== NULL
)) {
1263 return EFI_INVALID_PARAMETER
;
1266 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1269 // Check whether the DevicePath belongs to SD_DEVICE_PATH or EMMC_DEVICE_PATH
1271 if ((DevicePath
->Type
!= MESSAGING_DEVICE_PATH
) ||
1272 ((DevicePath
->SubType
!= MSG_SD_DP
) &&
1273 (DevicePath
->SubType
!= MSG_EMMC_DP
)) ||
1274 (DevicePathNodeLength(DevicePath
) != sizeof(SD_DEVICE_PATH
)) ||
1275 (DevicePathNodeLength(DevicePath
) != sizeof(EMMC_DEVICE_PATH
))) {
1276 return EFI_UNSUPPORTED
;
1279 if (DevicePath
->SubType
== MSG_SD_DP
) {
1280 SdNode
= (SD_DEVICE_PATH
*) DevicePath
;
1281 SlotNumber
= SdNode
->SlotNumber
;
1283 EmmcNode
= (EMMC_DEVICE_PATH
*) DevicePath
;
1284 SlotNumber
= EmmcNode
->SlotNumber
;
1287 if (SlotNumber
>= SD_MMC_HC_MAX_SLOT
) {
1288 return EFI_NOT_FOUND
;
1291 if (Private
->Slot
[SlotNumber
].Enable
) {
1295 return EFI_NOT_FOUND
;
1300 Resets an SD card that is connected to the SD controller.
1302 The ResetDevice() function resets the SD card specified by Slot.
1304 If this SD controller does not support a device reset operation, EFI_UNSUPPORTED is
1307 If Slot is not in a valid slot number for this SD controller, EFI_INVALID_PARAMETER
1310 If the device reset operation is completed, EFI_SUCCESS is returned.
1312 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1313 @param[in] Slot Specifies the slot number of the SD card to be reset.
1315 @retval EFI_SUCCESS The SD card specified by Slot was reset.
1316 @retval EFI_UNSUPPORTED The SD controller does not support a device reset operation.
1317 @retval EFI_INVALID_PARAMETER Slot number is invalid.
1318 @retval EFI_NO_MEDIA SD Device not present in the Slot.
1319 @retval EFI_DEVICE_ERROR The reset command failed due to a device error
1324 SdMmcPassThruResetDevice (
1325 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1329 SD_MMC_HC_PRIVATE_DATA
*Private
;
1331 LIST_ENTRY
*NextLink
;
1336 return EFI_INVALID_PARAMETER
;
1339 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1341 if (!Private
->Slot
[Slot
].Enable
) {
1342 return EFI_INVALID_PARAMETER
;
1345 if (!Private
->Slot
[Slot
].MediaPresent
) {
1346 return EFI_NO_MEDIA
;
1349 if (!Private
->Slot
[Slot
].Initialized
) {
1350 return EFI_DEVICE_ERROR
;
1353 // Free all async I/O requests in the queue
1355 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1357 for (Link
= GetFirstNode (&Private
->Queue
);
1358 !IsNull (&Private
->Queue
, Link
);
1360 NextLink
= GetNextNode (&Private
->Queue
, Link
);
1361 RemoveEntryList (Link
);
1362 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
1363 Trb
->Packet
->TransactionStatus
= EFI_ABORTED
;
1364 gBS
->SignalEvent (Trb
->Event
);
1368 gBS
->RestoreTPL (OldTpl
);