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) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include "SdMmcPciHcDxe.h"
20 EDKII_SD_MMC_OVERRIDE
*mOverride
;
23 // Driver Global Variables
25 EFI_DRIVER_BINDING_PROTOCOL gSdMmcPciHcDriverBinding
= {
26 SdMmcPciHcDriverBindingSupported
,
27 SdMmcPciHcDriverBindingStart
,
28 SdMmcPciHcDriverBindingStop
,
35 // Template for SD/MMC host controller private data.
37 SD_MMC_HC_PRIVATE_DATA gSdMmcPciHcTemplate
= {
38 SD_MMC_HC_PRIVATE_SIGNATURE
, // Signature
39 NULL
, // ControllerHandle
43 SdMmcPassThruPassThru
,
44 SdMmcPassThruGetNextSlot
,
45 SdMmcPassThruBuildDevicePath
,
46 SdMmcPassThruGetSlotNumber
,
47 SdMmcPassThruResetDevice
54 INITIALIZE_LIST_HEAD_VARIABLE (gSdMmcPciHcTemplate
.Queue
),
56 {0, UnknownSlot
, 0, 0, 0}, {0, UnknownSlot
, 0, 0, 0}, {0, UnknownSlot
, 0, 0, 0},
57 {0, UnknownSlot
, 0, 0, 0}, {0, UnknownSlot
, 0, 0, 0}, {0, UnknownSlot
, 0, 0, 0}
65 0 // ControllerVersion
68 SD_DEVICE_PATH mSdDpTemplate
= {
70 MESSAGING_DEVICE_PATH
,
73 (UINT8
) (sizeof (SD_DEVICE_PATH
)),
74 (UINT8
) ((sizeof (SD_DEVICE_PATH
)) >> 8)
80 EMMC_DEVICE_PATH mEmmcDpTemplate
= {
82 MESSAGING_DEVICE_PATH
,
85 (UINT8
) (sizeof (EMMC_DEVICE_PATH
)),
86 (UINT8
) ((sizeof (EMMC_DEVICE_PATH
)) >> 8)
93 // Prioritized function list to detect card type.
94 // User could add other card detection logic here.
96 CARD_TYPE_DETECT_ROUTINE mCardTypeDetectRoutineTable
[] = {
103 The entry point for SD host controller driver, used to install this driver on the ImageHandle.
105 @param[in] ImageHandle The firmware allocated handle for this driver image.
106 @param[in] SystemTable Pointer to the EFI system table.
108 @retval EFI_SUCCESS Driver loaded.
109 @retval other Driver not loaded.
114 InitializeSdMmcPciHcDxe (
115 IN EFI_HANDLE ImageHandle
,
116 IN EFI_SYSTEM_TABLE
*SystemTable
121 Status
= EfiLibInstallDriverBindingComponentName2 (
124 &gSdMmcPciHcDriverBinding
,
126 &gSdMmcPciHcComponentName
,
127 &gSdMmcPciHcComponentName2
129 ASSERT_EFI_ERROR (Status
);
135 Call back function when the timer event is signaled.
137 @param[in] Event The Event this notify function registered to.
138 @param[in] Context Pointer to the context data registered to the
144 ProcessAsyncTaskList (
149 SD_MMC_HC_PRIVATE_DATA
*Private
;
153 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
154 BOOLEAN InfiniteWait
;
157 Private
= (SD_MMC_HC_PRIVATE_DATA
*)Context
;
160 // Check if the first entry in the async I/O queue is done or not.
162 Status
= EFI_SUCCESS
;
164 Link
= GetFirstNode (&Private
->Queue
);
165 if (!IsNull (&Private
->Queue
, Link
)) {
166 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
167 if (!Private
->Slot
[Trb
->Slot
].MediaPresent
) {
168 Status
= EFI_NO_MEDIA
;
173 // Check whether the cmd/data line is ready for transfer.
175 Status
= SdMmcCheckTrbEnv (Private
, Trb
);
176 if (!EFI_ERROR (Status
)) {
178 Status
= SdMmcExecTrb (Private
, Trb
);
179 if (EFI_ERROR (Status
)) {
186 Status
= SdMmcCheckTrbResult (Private
, Trb
);
190 if ((Trb
!= NULL
) && (Status
== EFI_NOT_READY
)) {
191 Packet
= Trb
->Packet
;
192 if (Packet
->Timeout
== 0) {
195 InfiniteWait
= FALSE
;
197 if ((!InfiniteWait
) && (Trb
->Timeout
-- == 0)) {
198 RemoveEntryList (Link
);
199 Trb
->Packet
->TransactionStatus
= EFI_TIMEOUT
;
200 TrbEvent
= Trb
->Event
;
202 DEBUG ((DEBUG_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p EFI_TIMEOUT\n", TrbEvent
));
203 gBS
->SignalEvent (TrbEvent
);
207 if ((Trb
!= NULL
) && (Status
!= EFI_NOT_READY
)) {
208 RemoveEntryList (Link
);
209 Trb
->Packet
->TransactionStatus
= Status
;
210 TrbEvent
= Trb
->Event
;
212 DEBUG ((DEBUG_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p with %r\n", TrbEvent
, Status
));
213 gBS
->SignalEvent (TrbEvent
);
219 Sd removable device enumeration callback function when the timer event is signaled.
221 @param[in] Event The Event this notify function registered to.
222 @param[in] Context Pointer to the context data registered to the
228 SdMmcPciHcEnumerateDevice (
233 SD_MMC_HC_PRIVATE_DATA
*Private
;
236 BOOLEAN MediaPresent
;
238 CARD_TYPE_DETECT_ROUTINE
*Routine
;
241 LIST_ENTRY
*NextLink
;
245 Private
= (SD_MMC_HC_PRIVATE_DATA
*)Context
;
247 for (Slot
= 0; Slot
< SD_MMC_HC_MAX_SLOT
; Slot
++) {
248 if ((Private
->Slot
[Slot
].Enable
) && (Private
->Slot
[Slot
].SlotType
== RemovableSlot
)) {
249 Status
= SdMmcHcCardDetect (Private
->PciIo
, Slot
, &MediaPresent
);
250 if ((Status
== EFI_MEDIA_CHANGED
) && !MediaPresent
) {
251 DEBUG ((DEBUG_INFO
, "SdMmcPciHcEnumerateDevice: device disconnected at slot %d of pci %p\n", Slot
, Private
->PciIo
));
252 Private
->Slot
[Slot
].MediaPresent
= FALSE
;
253 Private
->Slot
[Slot
].Initialized
= FALSE
;
255 // Signal all async task events at the slot with EFI_NO_MEDIA status.
257 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
258 for (Link
= GetFirstNode (&Private
->Queue
);
259 !IsNull (&Private
->Queue
, Link
);
261 NextLink
= GetNextNode (&Private
->Queue
, Link
);
262 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
263 if (Trb
->Slot
== Slot
) {
264 RemoveEntryList (Link
);
265 Trb
->Packet
->TransactionStatus
= EFI_NO_MEDIA
;
266 gBS
->SignalEvent (Trb
->Event
);
270 gBS
->RestoreTPL (OldTpl
);
272 // Notify the upper layer the connect state change through ReinstallProtocolInterface.
274 gBS
->ReinstallProtocolInterface (
275 Private
->ControllerHandle
,
276 &gEfiSdMmcPassThruProtocolGuid
,
281 if ((Status
== EFI_MEDIA_CHANGED
) && MediaPresent
) {
282 DEBUG ((DEBUG_INFO
, "SdMmcPciHcEnumerateDevice: device connected at slot %d of pci %p\n", Slot
, Private
->PciIo
));
284 // Reset the specified slot of the SD/MMC Pci Host Controller
286 Status
= SdMmcHcReset (Private
, Slot
);
287 if (EFI_ERROR (Status
)) {
291 // Reinitialize slot and restart identification process for the new attached device
293 Status
= SdMmcHcInitHost (Private
, Slot
);
294 if (EFI_ERROR (Status
)) {
298 Private
->Slot
[Slot
].MediaPresent
= TRUE
;
299 Private
->Slot
[Slot
].Initialized
= TRUE
;
300 RoutineNum
= sizeof (mCardTypeDetectRoutineTable
) / sizeof (CARD_TYPE_DETECT_ROUTINE
);
301 for (Index
= 0; Index
< RoutineNum
; Index
++) {
302 Routine
= &mCardTypeDetectRoutineTable
[Index
];
303 if (*Routine
!= NULL
) {
304 Status
= (*Routine
) (Private
, Slot
);
305 if (!EFI_ERROR (Status
)) {
311 // This card doesn't get initialized correctly.
313 if (Index
== RoutineNum
) {
314 Private
->Slot
[Slot
].Initialized
= FALSE
;
318 // Notify the upper layer the connect state change through ReinstallProtocolInterface.
320 gBS
->ReinstallProtocolInterface (
321 Private
->ControllerHandle
,
322 &gEfiSdMmcPassThruProtocolGuid
,
333 Tests to see if this driver supports a given controller. If a child device is provided,
334 it further tests to see if this driver supports creating a handle for the specified child device.
336 This function checks to see if the driver specified by This supports the device specified by
337 ControllerHandle. Drivers will typically use the device path attached to
338 ControllerHandle and/or the services from the bus I/O abstraction attached to
339 ControllerHandle to determine if the driver supports ControllerHandle. This function
340 may be called many times during platform initialization. In order to reduce boot times, the tests
341 performed by this function must be very small, and take as little time as possible to execute. This
342 function must not change the state of any hardware devices, and this function must be aware that the
343 device specified by ControllerHandle may already be managed by the same driver or a
344 different driver. This function must match its calls to AllocatePages() with FreePages(),
345 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
346 Since ControllerHandle may have been previously started by the same driver, if a protocol is
347 already in the opened state, then it must not be closed with CloseProtocol(). This is required
348 to guarantee the state of ControllerHandle is not modified by this function.
350 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
351 @param[in] ControllerHandle The handle of the controller to test. This handle
352 must support a protocol interface that supplies
353 an I/O abstraction to the driver.
354 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
355 parameter is ignored by device drivers, and is optional for bus
356 drivers. For bus drivers, if this parameter is not NULL, then
357 the bus driver must determine if the bus controller specified
358 by ControllerHandle and the child controller specified
359 by RemainingDevicePath are both supported by this
362 @retval EFI_SUCCESS The device specified by ControllerHandle and
363 RemainingDevicePath is supported by the driver specified by This.
364 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
365 RemainingDevicePath is already being managed by the driver
367 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
368 RemainingDevicePath is already being managed by a different
369 driver or an application that requires exclusive access.
370 Currently not implemented.
371 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
372 RemainingDevicePath is not supported by the driver specified by This.
376 SdMmcPciHcDriverBindingSupported (
377 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
378 IN EFI_HANDLE Controller
,
379 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
383 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
384 EFI_PCI_IO_PROTOCOL
*PciIo
;
388 ParentDevicePath
= NULL
;
391 // SdPciHcDxe is a device driver, and should ingore the
392 // "RemainingDevicePath" according to EFI spec.
394 Status
= gBS
->OpenProtocol (
396 &gEfiDevicePathProtocolGuid
,
397 (VOID
*) &ParentDevicePath
,
398 This
->DriverBindingHandle
,
400 EFI_OPEN_PROTOCOL_BY_DRIVER
402 if (EFI_ERROR (Status
)) {
404 // EFI_ALREADY_STARTED is also an error.
409 // Close the protocol because we don't use it here.
413 &gEfiDevicePathProtocolGuid
,
414 This
->DriverBindingHandle
,
419 // Now test the EfiPciIoProtocol.
421 Status
= gBS
->OpenProtocol (
423 &gEfiPciIoProtocolGuid
,
425 This
->DriverBindingHandle
,
427 EFI_OPEN_PROTOCOL_BY_DRIVER
429 if (EFI_ERROR (Status
)) {
434 // Now further check the PCI header: Base class (offset 0x08) and
435 // Sub Class (offset 0x05). This controller should be an SD/MMC PCI
438 Status
= PciIo
->Pci
.Read (
445 if (EFI_ERROR (Status
)) {
448 &gEfiPciIoProtocolGuid
,
449 This
->DriverBindingHandle
,
452 return EFI_UNSUPPORTED
;
455 // Since we already got the PciData, we can close protocol to avoid to carry it
456 // on for multiple exit points.
460 &gEfiPciIoProtocolGuid
,
461 This
->DriverBindingHandle
,
466 // Examine SD PCI Host Controller PCI Configuration table fields.
468 if ((PciData
.Hdr
.ClassCode
[2] == PCI_CLASS_SYSTEM_PERIPHERAL
) &&
469 (PciData
.Hdr
.ClassCode
[1] == PCI_SUBCLASS_SD_HOST_CONTROLLER
) &&
470 ((PciData
.Hdr
.ClassCode
[0] == 0x00) || (PciData
.Hdr
.ClassCode
[0] == 0x01))) {
474 return EFI_UNSUPPORTED
;
478 Starts a device controller or a bus controller.
480 The Start() function is designed to be invoked from the EFI boot service ConnectController().
481 As a result, much of the error checking on the parameters to Start() has been moved into this
482 common boot service. It is legal to call Start() from other locations,
483 but the following calling restrictions must be followed or the system behavior will not be deterministic.
484 1. ControllerHandle must be a valid EFI_HANDLE.
485 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
486 EFI_DEVICE_PATH_PROTOCOL.
487 3. Prior to calling Start(), the Supported() function for the driver specified by This must
488 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
490 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
491 @param[in] ControllerHandle The handle of the controller to start. This handle
492 must support a protocol interface that supplies
493 an I/O abstraction to the driver.
494 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
495 parameter is ignored by device drivers, and is optional for bus
496 drivers. For a bus driver, if this parameter is NULL, then handles
497 for all the children of Controller are created by this driver.
498 If this parameter is not NULL and the first Device Path Node is
499 not the End of Device Path Node, then only the handle for the
500 child device specified by the first Device Path Node of
501 RemainingDevicePath is created by this driver.
502 If the first Device Path Node of RemainingDevicePath is
503 the End of Device Path Node, no child handle is created by this
506 @retval EFI_SUCCESS The device was started.
507 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
508 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
509 @retval Others The driver failded to start the device.
514 SdMmcPciHcDriverBindingStart (
515 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
516 IN EFI_HANDLE Controller
,
517 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
521 SD_MMC_HC_PRIVATE_DATA
*Private
;
522 EFI_PCI_IO_PROTOCOL
*PciIo
;
524 UINT64 PciAttributes
;
529 CARD_TYPE_DETECT_ROUTINE
*Routine
;
531 BOOLEAN MediaPresent
;
532 BOOLEAN Support64BitDma
;
534 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStart: Start\n"));
537 // Open PCI I/O Protocol and save pointer to open protocol
538 // in private data area.
541 Status
= gBS
->OpenProtocol (
543 &gEfiPciIoProtocolGuid
,
545 This
->DriverBindingHandle
,
547 EFI_OPEN_PROTOCOL_BY_DRIVER
549 if (EFI_ERROR (Status
)) {
554 // Enable the SD Host Controller MMIO space
557 Status
= PciIo
->Attributes (
559 EfiPciIoAttributeOperationGet
,
564 if (EFI_ERROR (Status
)) {
568 Status
= PciIo
->Attributes (
570 EfiPciIoAttributeOperationSupported
,
575 if (!EFI_ERROR (Status
)) {
576 Supports
&= (UINT64
)EFI_PCI_DEVICE_ENABLE
;
577 Status
= PciIo
->Attributes (
579 EfiPciIoAttributeOperationEnable
,
587 Private
= AllocateCopyPool (sizeof (SD_MMC_HC_PRIVATE_DATA
), &gSdMmcPciHcTemplate
);
588 if (Private
== NULL
) {
589 Status
= EFI_OUT_OF_RESOURCES
;
593 Private
->ControllerHandle
= Controller
;
594 Private
->PciIo
= PciIo
;
595 Private
->PciAttributes
= PciAttributes
;
596 InitializeListHead (&Private
->Queue
);
599 // Get SD/MMC Pci Host Controller Slot info
601 Status
= SdMmcHcGetSlotInfo (PciIo
, &FirstBar
, &SlotNum
);
602 if (EFI_ERROR (Status
)) {
607 // Attempt to locate the singleton instance of the SD/MMC override protocol,
608 // which implements platform specific workarounds for non-standard SDHCI
611 if (mOverride
== NULL
) {
612 Status
= gBS
->LocateProtocol (&gEdkiiSdMmcOverrideProtocolGuid
, NULL
,
613 (VOID
**)&mOverride
);
614 if (!EFI_ERROR (Status
)) {
615 DEBUG ((DEBUG_INFO
, "%a: found SD/MMC override protocol\n",
620 Support64BitDma
= TRUE
;
621 for (Slot
= FirstBar
; Slot
< (FirstBar
+ SlotNum
); Slot
++) {
622 Private
->Slot
[Slot
].Enable
= TRUE
;
624 Status
= SdMmcHcGetCapability (PciIo
, Slot
, &Private
->Capability
[Slot
]);
625 if (EFI_ERROR (Status
)) {
629 Private
->BaseClkFreq
[Slot
] = Private
->Capability
[Slot
].BaseClkFreq
;
631 if (mOverride
!= NULL
&& mOverride
->Capability
!= NULL
) {
632 Status
= mOverride
->Capability (
635 &Private
->Capability
[Slot
],
636 &Private
->BaseClkFreq
[Slot
]
638 if (EFI_ERROR (Status
)) {
639 DEBUG ((DEBUG_WARN
, "%a: Failed to override capability - %r\n",
640 __FUNCTION__
, Status
));
644 DumpCapabilityReg (Slot
, &Private
->Capability
[Slot
]);
647 "Slot[%d] Base Clock Frequency: %dMHz\n",
649 Private
->BaseClkFreq
[Slot
]
652 Support64BitDma
&= Private
->Capability
[Slot
].SysBus64
;
654 Status
= SdMmcHcGetMaxCurrent (PciIo
, Slot
, &Private
->MaxCurrent
[Slot
]);
655 if (EFI_ERROR (Status
)) {
659 Private
->Slot
[Slot
].SlotType
= Private
->Capability
[Slot
].SlotType
;
660 if ((Private
->Slot
[Slot
].SlotType
!= RemovableSlot
) && (Private
->Slot
[Slot
].SlotType
!= EmbeddedSlot
)) {
661 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDxe doesn't support the slot type [%d]!!!\n", Private
->Slot
[Slot
].SlotType
));
666 // Reset the specified slot of the SD/MMC Pci Host Controller
668 Status
= SdMmcHcReset (Private
, Slot
);
669 if (EFI_ERROR (Status
)) {
673 // Check whether there is a SD/MMC card attached
675 if (Private
->Slot
[Slot
].SlotType
== RemovableSlot
) {
676 Status
= SdMmcHcCardDetect (PciIo
, Slot
, &MediaPresent
);
677 if (EFI_ERROR (Status
) && (Status
!= EFI_MEDIA_CHANGED
)) {
679 } else if (!MediaPresent
) {
682 "SdMmcHcCardDetect: No device attached in Slot[%d]!!!\n",
689 Status
= SdMmcHcInitHost (Private
, Slot
);
690 if (EFI_ERROR (Status
)) {
694 Private
->Slot
[Slot
].MediaPresent
= TRUE
;
695 Private
->Slot
[Slot
].Initialized
= TRUE
;
696 RoutineNum
= sizeof (mCardTypeDetectRoutineTable
) / sizeof (CARD_TYPE_DETECT_ROUTINE
);
697 for (Index
= 0; Index
< RoutineNum
; Index
++) {
698 Routine
= &mCardTypeDetectRoutineTable
[Index
];
699 if (*Routine
!= NULL
) {
700 Status
= (*Routine
) (Private
, Slot
);
701 if (!EFI_ERROR (Status
)) {
707 // This card doesn't get initialized correctly.
709 if (Index
== RoutineNum
) {
710 Private
->Slot
[Slot
].Initialized
= FALSE
;
715 // Enable 64-bit DMA support in the PCI layer if this controller
718 if (Support64BitDma
) {
719 Status
= PciIo
->Attributes (
721 EfiPciIoAttributeOperationEnable
,
722 EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE
,
725 if (EFI_ERROR (Status
)) {
726 DEBUG ((DEBUG_WARN
, "SdMmcPciHcDriverBindingStart: failed to enable 64-bit DMA (%r)\n", Status
));
731 // Start the asynchronous I/O monitor
733 Status
= gBS
->CreateEvent (
734 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
736 ProcessAsyncTaskList
,
740 if (EFI_ERROR (Status
)) {
744 Status
= gBS
->SetTimer (Private
->TimerEvent
, TimerPeriodic
, SD_MMC_HC_ASYNC_TIMER
);
745 if (EFI_ERROR (Status
)) {
750 // Start the Sd removable device connection enumeration
752 Status
= gBS
->CreateEvent (
753 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
755 SdMmcPciHcEnumerateDevice
,
757 &Private
->ConnectEvent
759 if (EFI_ERROR (Status
)) {
763 Status
= gBS
->SetTimer (Private
->ConnectEvent
, TimerPeriodic
, SD_MMC_HC_ENUM_TIMER
);
764 if (EFI_ERROR (Status
)) {
768 Status
= gBS
->InstallMultipleProtocolInterfaces (
770 &gEfiSdMmcPassThruProtocolGuid
,
771 &(Private
->PassThru
),
775 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStart: %r End on %x\n", Status
, Controller
));
778 if (EFI_ERROR (Status
)) {
779 if ((Private
!= NULL
) && (Private
->PciAttributes
!= 0)) {
781 // Restore original PCI attributes
785 EfiPciIoAttributeOperationSet
,
786 Private
->PciAttributes
,
792 &gEfiPciIoProtocolGuid
,
793 This
->DriverBindingHandle
,
797 if ((Private
!= NULL
) && (Private
->TimerEvent
!= NULL
)) {
798 gBS
->CloseEvent (Private
->TimerEvent
);
801 if ((Private
!= NULL
) && (Private
->ConnectEvent
!= NULL
)) {
802 gBS
->CloseEvent (Private
->ConnectEvent
);
805 if (Private
!= NULL
) {
814 Stops a device controller or a bus controller.
816 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
817 As a result, much of the error checking on the parameters to Stop() has been moved
818 into this common boot service. It is legal to call Stop() from other locations,
819 but the following calling restrictions must be followed or the system behavior will not be deterministic.
820 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
821 same driver's Start() function.
822 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
823 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
824 Start() function, and the Start() function must have called OpenProtocol() on
825 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
827 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
828 @param[in] ControllerHandle A handle to the device being stopped. The handle must
829 support a bus specific I/O protocol for the driver
830 to use to stop the device.
831 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
832 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
833 if NumberOfChildren is 0.
835 @retval EFI_SUCCESS The device was stopped.
836 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
841 SdMmcPciHcDriverBindingStop (
842 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
843 IN EFI_HANDLE Controller
,
844 IN UINTN NumberOfChildren
,
845 IN EFI_HANDLE
*ChildHandleBuffer
849 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
850 SD_MMC_HC_PRIVATE_DATA
*Private
;
851 EFI_PCI_IO_PROTOCOL
*PciIo
;
853 LIST_ENTRY
*NextLink
;
856 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStop: Start\n"));
858 Status
= gBS
->OpenProtocol (
860 &gEfiSdMmcPassThruProtocolGuid
,
862 This
->DriverBindingHandle
,
864 EFI_OPEN_PROTOCOL_GET_PROTOCOL
866 if (EFI_ERROR (Status
)) {
870 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (PassThru
);
872 // Close Non-Blocking timer and free Task list.
874 if (Private
->TimerEvent
!= NULL
) {
875 gBS
->CloseEvent (Private
->TimerEvent
);
876 Private
->TimerEvent
= NULL
;
878 if (Private
->ConnectEvent
!= NULL
) {
879 gBS
->CloseEvent (Private
->ConnectEvent
);
880 Private
->ConnectEvent
= NULL
;
883 // As the timer is closed, there is no needs to use TPL lock to
884 // protect the critical region "queue".
886 for (Link
= GetFirstNode (&Private
->Queue
);
887 !IsNull (&Private
->Queue
, Link
);
889 NextLink
= GetNextNode (&Private
->Queue
, Link
);
890 RemoveEntryList (Link
);
891 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
892 Trb
->Packet
->TransactionStatus
= EFI_ABORTED
;
893 gBS
->SignalEvent (Trb
->Event
);
898 // Uninstall Block I/O protocol from the device handle
900 Status
= gBS
->UninstallProtocolInterface (
902 &gEfiSdMmcPassThruProtocolGuid
,
906 if (EFI_ERROR (Status
)) {
912 &gEfiPciIoProtocolGuid
,
913 This
->DriverBindingHandle
,
917 // Restore original PCI attributes
919 PciIo
= Private
->PciIo
;
920 Status
= PciIo
->Attributes (
922 EfiPciIoAttributeOperationSet
,
923 Private
->PciAttributes
,
926 ASSERT_EFI_ERROR (Status
);
930 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStop: End with %r\n", Status
));
936 Sends SD command to an SD card that is attached to the SD controller.
938 The PassThru() function sends the SD command specified by Packet to the SD card
941 If Packet is successfully sent to the SD card, then EFI_SUCCESS is returned.
943 If a device error occurs while sending the Packet, then EFI_DEVICE_ERROR is returned.
945 If Slot is not in a valid range for the SD controller, then EFI_INVALID_PARAMETER
948 If Packet defines a data command but both InDataBuffer and OutDataBuffer are NULL,
949 EFI_INVALID_PARAMETER is returned.
951 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
952 @param[in] Slot The slot number of the SD card to send the command to.
953 @param[in,out] Packet A pointer to the SD command data structure.
954 @param[in] Event If Event is NULL, blocking I/O is performed. If Event is
955 not NULL, then nonblocking I/O is performed, and Event
956 will be signaled when the Packet completes.
958 @retval EFI_SUCCESS The SD Command Packet was sent by the host.
959 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SD
961 @retval EFI_INVALID_PARAMETER Packet, Slot, or the contents of the Packet is invalid.
962 @retval EFI_INVALID_PARAMETER Packet defines a data command but both InDataBuffer and
963 OutDataBuffer are NULL.
964 @retval EFI_NO_MEDIA SD Device not present in the Slot.
965 @retval EFI_UNSUPPORTED The command described by the SD Command Packet is not
966 supported by the host controller.
967 @retval EFI_BAD_BUFFER_SIZE The InTransferLength or OutTransferLength exceeds the
968 limit supported by SD card ( i.e. if the number of bytes
969 exceed the Last LBA).
974 SdMmcPassThruPassThru (
975 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
977 IN OUT EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
,
978 IN EFI_EVENT Event OPTIONAL
982 SD_MMC_HC_PRIVATE_DATA
*Private
;
986 if ((This
== NULL
) || (Packet
== NULL
)) {
987 return EFI_INVALID_PARAMETER
;
990 if ((Packet
->SdMmcCmdBlk
== NULL
) || (Packet
->SdMmcStatusBlk
== NULL
)) {
991 return EFI_INVALID_PARAMETER
;
994 if ((Packet
->OutDataBuffer
== NULL
) && (Packet
->OutTransferLength
!= 0)) {
995 return EFI_INVALID_PARAMETER
;
998 if ((Packet
->InDataBuffer
== NULL
) && (Packet
->InTransferLength
!= 0)) {
999 return EFI_INVALID_PARAMETER
;
1002 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1004 if (!Private
->Slot
[Slot
].Enable
) {
1005 return EFI_INVALID_PARAMETER
;
1008 if (!Private
->Slot
[Slot
].MediaPresent
) {
1009 return EFI_NO_MEDIA
;
1012 if (!Private
->Slot
[Slot
].Initialized
) {
1013 return EFI_DEVICE_ERROR
;
1016 Trb
= SdMmcCreateTrb (Private
, Slot
, Packet
, Event
);
1018 return EFI_OUT_OF_RESOURCES
;
1021 // Immediately return for async I/O.
1023 if (Event
!= NULL
) {
1028 // Wait async I/O list is empty before execute sync I/O operation.
1031 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1032 if (IsListEmpty (&Private
->Queue
)) {
1033 gBS
->RestoreTPL (OldTpl
);
1036 gBS
->RestoreTPL (OldTpl
);
1039 Status
= SdMmcWaitTrbEnv (Private
, Trb
);
1040 if (EFI_ERROR (Status
)) {
1044 Status
= SdMmcExecTrb (Private
, Trb
);
1045 if (EFI_ERROR (Status
)) {
1049 Status
= SdMmcWaitTrbResult (Private
, Trb
);
1050 if (EFI_ERROR (Status
)) {
1061 Used to retrieve next slot numbers supported by the SD controller. The function
1062 returns information about all available slots (populated or not-populated).
1064 The GetNextSlot() function retrieves the next slot number on an SD controller.
1065 If on input Slot is 0xFF, then the slot number of the first slot on the SD controller
1068 If Slot is a slot number that was returned on a previous call to GetNextSlot(), then
1069 the slot number of the next slot on the SD controller is returned.
1071 If Slot is not 0xFF and Slot was not returned on a previous call to GetNextSlot(),
1072 EFI_INVALID_PARAMETER is returned.
1074 If Slot is the slot number of the last slot on the SD controller, then EFI_NOT_FOUND
1077 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
1078 @param[in,out] Slot On input, a pointer to a slot number on the SD controller.
1079 On output, a pointer to the next slot number on the SD controller.
1080 An input value of 0xFF retrieves the first slot number on the SD
1083 @retval EFI_SUCCESS The next slot number on the SD controller was returned in Slot.
1084 @retval EFI_NOT_FOUND There are no more slots on this SD controller.
1085 @retval EFI_INVALID_PARAMETER Slot is not 0xFF and Slot was not returned on a previous call
1091 SdMmcPassThruGetNextSlot (
1092 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1096 SD_MMC_HC_PRIVATE_DATA
*Private
;
1099 if ((This
== NULL
) || (Slot
== NULL
)) {
1100 return EFI_INVALID_PARAMETER
;
1103 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1105 if (*Slot
== 0xFF) {
1106 for (Index
= 0; Index
< SD_MMC_HC_MAX_SLOT
; Index
++) {
1107 if (Private
->Slot
[Index
].Enable
) {
1109 Private
->PreviousSlot
= Index
;
1113 return EFI_NOT_FOUND
;
1114 } else if (*Slot
== Private
->PreviousSlot
) {
1115 for (Index
= *Slot
+ 1; Index
< SD_MMC_HC_MAX_SLOT
; Index
++) {
1116 if (Private
->Slot
[Index
].Enable
) {
1118 Private
->PreviousSlot
= Index
;
1122 return EFI_NOT_FOUND
;
1124 return EFI_INVALID_PARAMETER
;
1129 Used to allocate and build a device path node for an SD card on the SD controller.
1131 The BuildDevicePath() function allocates and builds a single device node for the SD
1132 card specified by Slot.
1134 If the SD card specified by Slot is not present on the SD controller, then EFI_NOT_FOUND
1137 If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned.
1139 If there are not enough resources to allocate the device path node, then EFI_OUT_OF_RESOURCES
1142 Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of
1143 DevicePath are initialized to describe the SD card specified by Slot, and EFI_SUCCESS is
1146 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
1147 @param[in] Slot Specifies the slot number of the SD card for which a device
1148 path node is to be allocated and built.
1149 @param[in,out] DevicePath A pointer to a single device path node that describes the SD
1150 card specified by Slot. This function is responsible for
1151 allocating the buffer DevicePath with the boot service
1152 AllocatePool(). It is the caller's responsibility to free
1153 DevicePath when the caller is finished with DevicePath.
1155 @retval EFI_SUCCESS The device path node that describes the SD card specified by
1156 Slot was allocated and returned in DevicePath.
1157 @retval EFI_NOT_FOUND The SD card specified by Slot does not exist on the SD controller.
1158 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1159 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
1164 SdMmcPassThruBuildDevicePath (
1165 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1167 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
1170 SD_MMC_HC_PRIVATE_DATA
*Private
;
1171 SD_DEVICE_PATH
*SdNode
;
1172 EMMC_DEVICE_PATH
*EmmcNode
;
1174 if ((This
== NULL
) || (DevicePath
== NULL
) || (Slot
>= SD_MMC_HC_MAX_SLOT
)) {
1175 return EFI_INVALID_PARAMETER
;
1178 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1180 if ((!Private
->Slot
[Slot
].Enable
) || (!Private
->Slot
[Slot
].MediaPresent
)) {
1181 return EFI_NOT_FOUND
;
1184 if (Private
->Slot
[Slot
].CardType
== SdCardType
) {
1185 SdNode
= AllocateCopyPool (sizeof (SD_DEVICE_PATH
), &mSdDpTemplate
);
1186 if (SdNode
== NULL
) {
1187 return EFI_OUT_OF_RESOURCES
;
1189 SdNode
->SlotNumber
= Slot
;
1191 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) SdNode
;
1192 } else if (Private
->Slot
[Slot
].CardType
== EmmcCardType
) {
1193 EmmcNode
= AllocateCopyPool (sizeof (EMMC_DEVICE_PATH
), &mEmmcDpTemplate
);
1194 if (EmmcNode
== NULL
) {
1195 return EFI_OUT_OF_RESOURCES
;
1197 EmmcNode
->SlotNumber
= Slot
;
1199 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) EmmcNode
;
1202 // Currently we only support SD and EMMC two device nodes.
1204 return EFI_NOT_FOUND
;
1211 This function retrieves an SD card slot number based on the input device path.
1213 The GetSlotNumber() function retrieves slot number for the SD card specified by
1214 the DevicePath node. If DevicePath is NULL, EFI_INVALID_PARAMETER is returned.
1216 If DevicePath is not a device path node type that the SD Pass Thru driver supports,
1217 EFI_UNSUPPORTED is returned.
1219 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1220 @param[in] DevicePath A pointer to the device path node that describes a SD
1221 card on the SD controller.
1222 @param[out] Slot On return, points to the slot number of an SD card on
1225 @retval EFI_SUCCESS SD card slot number is returned in Slot.
1226 @retval EFI_INVALID_PARAMETER Slot or DevicePath is NULL.
1227 @retval EFI_UNSUPPORTED DevicePath is not a device path node type that the SD
1228 Pass Thru driver supports.
1233 SdMmcPassThruGetSlotNumber (
1234 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1235 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1239 SD_MMC_HC_PRIVATE_DATA
*Private
;
1240 SD_DEVICE_PATH
*SdNode
;
1241 EMMC_DEVICE_PATH
*EmmcNode
;
1244 if ((This
== NULL
) || (DevicePath
== NULL
) || (Slot
== NULL
)) {
1245 return EFI_INVALID_PARAMETER
;
1248 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1251 // Check whether the DevicePath belongs to SD_DEVICE_PATH or EMMC_DEVICE_PATH
1253 if ((DevicePath
->Type
!= MESSAGING_DEVICE_PATH
) ||
1254 ((DevicePath
->SubType
!= MSG_SD_DP
) &&
1255 (DevicePath
->SubType
!= MSG_EMMC_DP
)) ||
1256 (DevicePathNodeLength(DevicePath
) != sizeof(SD_DEVICE_PATH
)) ||
1257 (DevicePathNodeLength(DevicePath
) != sizeof(EMMC_DEVICE_PATH
))) {
1258 return EFI_UNSUPPORTED
;
1261 if (DevicePath
->SubType
== MSG_SD_DP
) {
1262 SdNode
= (SD_DEVICE_PATH
*) DevicePath
;
1263 SlotNumber
= SdNode
->SlotNumber
;
1265 EmmcNode
= (EMMC_DEVICE_PATH
*) DevicePath
;
1266 SlotNumber
= EmmcNode
->SlotNumber
;
1269 if (SlotNumber
>= SD_MMC_HC_MAX_SLOT
) {
1270 return EFI_NOT_FOUND
;
1273 if (Private
->Slot
[SlotNumber
].Enable
) {
1277 return EFI_NOT_FOUND
;
1282 Resets an SD card that is connected to the SD controller.
1284 The ResetDevice() function resets the SD card specified by Slot.
1286 If this SD controller does not support a device reset operation, EFI_UNSUPPORTED is
1289 If Slot is not in a valid slot number for this SD controller, EFI_INVALID_PARAMETER
1292 If the device reset operation is completed, EFI_SUCCESS is returned.
1294 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1295 @param[in] Slot Specifies the slot number of the SD card to be reset.
1297 @retval EFI_SUCCESS The SD card specified by Slot was reset.
1298 @retval EFI_UNSUPPORTED The SD controller does not support a device reset operation.
1299 @retval EFI_INVALID_PARAMETER Slot number is invalid.
1300 @retval EFI_NO_MEDIA SD Device not present in the Slot.
1301 @retval EFI_DEVICE_ERROR The reset command failed due to a device error
1306 SdMmcPassThruResetDevice (
1307 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1311 SD_MMC_HC_PRIVATE_DATA
*Private
;
1313 LIST_ENTRY
*NextLink
;
1318 return EFI_INVALID_PARAMETER
;
1321 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1323 if (!Private
->Slot
[Slot
].Enable
) {
1324 return EFI_INVALID_PARAMETER
;
1327 if (!Private
->Slot
[Slot
].MediaPresent
) {
1328 return EFI_NO_MEDIA
;
1331 if (!Private
->Slot
[Slot
].Initialized
) {
1332 return EFI_DEVICE_ERROR
;
1335 // Free all async I/O requests in the queue
1337 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1339 for (Link
= GetFirstNode (&Private
->Queue
);
1340 !IsNull (&Private
->Queue
, Link
);
1342 NextLink
= GetNextNode (&Private
->Queue
, Link
);
1343 RemoveEntryList (Link
);
1344 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
1345 Trb
->Packet
->TransactionStatus
= EFI_ABORTED
;
1346 gBS
->SignalEvent (Trb
->Event
);
1350 gBS
->RestoreTPL (OldTpl
);