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
)) {
628 if (mOverride
!= NULL
&& mOverride
->Capability
!= NULL
) {
629 Status
= mOverride
->Capability (
632 &Private
->Capability
[Slot
]);
633 if (EFI_ERROR (Status
)) {
634 DEBUG ((DEBUG_WARN
, "%a: Failed to override capability - %r\n",
635 __FUNCTION__
, Status
));
639 DumpCapabilityReg (Slot
, &Private
->Capability
[Slot
]);
641 Support64BitDma
&= Private
->Capability
[Slot
].SysBus64
;
643 Status
= SdMmcHcGetMaxCurrent (PciIo
, Slot
, &Private
->MaxCurrent
[Slot
]);
644 if (EFI_ERROR (Status
)) {
648 Private
->Slot
[Slot
].SlotType
= Private
->Capability
[Slot
].SlotType
;
649 if ((Private
->Slot
[Slot
].SlotType
!= RemovableSlot
) && (Private
->Slot
[Slot
].SlotType
!= EmbeddedSlot
)) {
650 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDxe doesn't support the slot type [%d]!!!\n", Private
->Slot
[Slot
].SlotType
));
655 // Reset the specified slot of the SD/MMC Pci Host Controller
657 Status
= SdMmcHcReset (Private
, Slot
);
658 if (EFI_ERROR (Status
)) {
662 // Check whether there is a SD/MMC card attached
664 Status
= SdMmcHcCardDetect (PciIo
, Slot
, &MediaPresent
);
665 if (EFI_ERROR (Status
) && (Status
!= EFI_MEDIA_CHANGED
)) {
667 } else if (!MediaPresent
) {
668 DEBUG ((DEBUG_INFO
, "SdMmcHcCardDetect: No device attached in Slot[%d]!!!\n", Slot
));
672 Status
= SdMmcHcInitHost (Private
, Slot
);
673 if (EFI_ERROR (Status
)) {
677 Private
->Slot
[Slot
].MediaPresent
= TRUE
;
678 Private
->Slot
[Slot
].Initialized
= TRUE
;
679 RoutineNum
= sizeof (mCardTypeDetectRoutineTable
) / sizeof (CARD_TYPE_DETECT_ROUTINE
);
680 for (Index
= 0; Index
< RoutineNum
; Index
++) {
681 Routine
= &mCardTypeDetectRoutineTable
[Index
];
682 if (*Routine
!= NULL
) {
683 Status
= (*Routine
) (Private
, Slot
);
684 if (!EFI_ERROR (Status
)) {
690 // This card doesn't get initialized correctly.
692 if (Index
== RoutineNum
) {
693 Private
->Slot
[Slot
].Initialized
= FALSE
;
698 // Enable 64-bit DMA support in the PCI layer if this controller
701 if (Support64BitDma
) {
702 Status
= PciIo
->Attributes (
704 EfiPciIoAttributeOperationEnable
,
705 EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE
,
708 if (EFI_ERROR (Status
)) {
709 DEBUG ((DEBUG_WARN
, "SdMmcPciHcDriverBindingStart: failed to enable 64-bit DMA (%r)\n", Status
));
714 // Start the asynchronous I/O monitor
716 Status
= gBS
->CreateEvent (
717 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
719 ProcessAsyncTaskList
,
723 if (EFI_ERROR (Status
)) {
727 Status
= gBS
->SetTimer (Private
->TimerEvent
, TimerPeriodic
, SD_MMC_HC_ASYNC_TIMER
);
728 if (EFI_ERROR (Status
)) {
733 // Start the Sd removable device connection enumeration
735 Status
= gBS
->CreateEvent (
736 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
738 SdMmcPciHcEnumerateDevice
,
740 &Private
->ConnectEvent
742 if (EFI_ERROR (Status
)) {
746 Status
= gBS
->SetTimer (Private
->ConnectEvent
, TimerPeriodic
, SD_MMC_HC_ENUM_TIMER
);
747 if (EFI_ERROR (Status
)) {
751 Status
= gBS
->InstallMultipleProtocolInterfaces (
753 &gEfiSdMmcPassThruProtocolGuid
,
754 &(Private
->PassThru
),
758 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStart: %r End on %x\n", Status
, Controller
));
761 if (EFI_ERROR (Status
)) {
762 if ((Private
!= NULL
) && (Private
->PciAttributes
!= 0)) {
764 // Restore original PCI attributes
768 EfiPciIoAttributeOperationSet
,
769 Private
->PciAttributes
,
775 &gEfiPciIoProtocolGuid
,
776 This
->DriverBindingHandle
,
780 if ((Private
!= NULL
) && (Private
->TimerEvent
!= NULL
)) {
781 gBS
->CloseEvent (Private
->TimerEvent
);
784 if ((Private
!= NULL
) && (Private
->ConnectEvent
!= NULL
)) {
785 gBS
->CloseEvent (Private
->ConnectEvent
);
788 if (Private
!= NULL
) {
797 Stops a device controller or a bus controller.
799 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
800 As a result, much of the error checking on the parameters to Stop() has been moved
801 into this common boot service. It is legal to call Stop() from other locations,
802 but the following calling restrictions must be followed or the system behavior will not be deterministic.
803 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
804 same driver's Start() function.
805 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
806 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
807 Start() function, and the Start() function must have called OpenProtocol() on
808 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
810 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
811 @param[in] ControllerHandle A handle to the device being stopped. The handle must
812 support a bus specific I/O protocol for the driver
813 to use to stop the device.
814 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
815 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
816 if NumberOfChildren is 0.
818 @retval EFI_SUCCESS The device was stopped.
819 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
824 SdMmcPciHcDriverBindingStop (
825 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
826 IN EFI_HANDLE Controller
,
827 IN UINTN NumberOfChildren
,
828 IN EFI_HANDLE
*ChildHandleBuffer
832 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
833 SD_MMC_HC_PRIVATE_DATA
*Private
;
834 EFI_PCI_IO_PROTOCOL
*PciIo
;
836 LIST_ENTRY
*NextLink
;
839 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStop: Start\n"));
841 Status
= gBS
->OpenProtocol (
843 &gEfiSdMmcPassThruProtocolGuid
,
845 This
->DriverBindingHandle
,
847 EFI_OPEN_PROTOCOL_GET_PROTOCOL
849 if (EFI_ERROR (Status
)) {
853 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (PassThru
);
855 // Close Non-Blocking timer and free Task list.
857 if (Private
->TimerEvent
!= NULL
) {
858 gBS
->CloseEvent (Private
->TimerEvent
);
859 Private
->TimerEvent
= NULL
;
861 if (Private
->ConnectEvent
!= NULL
) {
862 gBS
->CloseEvent (Private
->ConnectEvent
);
863 Private
->ConnectEvent
= NULL
;
866 // As the timer is closed, there is no needs to use TPL lock to
867 // protect the critical region "queue".
869 for (Link
= GetFirstNode (&Private
->Queue
);
870 !IsNull (&Private
->Queue
, Link
);
872 NextLink
= GetNextNode (&Private
->Queue
, Link
);
873 RemoveEntryList (Link
);
874 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
875 Trb
->Packet
->TransactionStatus
= EFI_ABORTED
;
876 gBS
->SignalEvent (Trb
->Event
);
881 // Uninstall Block I/O protocol from the device handle
883 Status
= gBS
->UninstallProtocolInterface (
885 &gEfiSdMmcPassThruProtocolGuid
,
889 if (EFI_ERROR (Status
)) {
895 &gEfiPciIoProtocolGuid
,
896 This
->DriverBindingHandle
,
900 // Restore original PCI attributes
902 PciIo
= Private
->PciIo
;
903 Status
= PciIo
->Attributes (
905 EfiPciIoAttributeOperationSet
,
906 Private
->PciAttributes
,
909 ASSERT_EFI_ERROR (Status
);
913 DEBUG ((DEBUG_INFO
, "SdMmcPciHcDriverBindingStop: End with %r\n", Status
));
919 Sends SD command to an SD card that is attached to the SD controller.
921 The PassThru() function sends the SD command specified by Packet to the SD card
924 If Packet is successfully sent to the SD card, then EFI_SUCCESS is returned.
926 If a device error occurs while sending the Packet, then EFI_DEVICE_ERROR is returned.
928 If Slot is not in a valid range for the SD controller, then EFI_INVALID_PARAMETER
931 If Packet defines a data command but both InDataBuffer and OutDataBuffer are NULL,
932 EFI_INVALID_PARAMETER is returned.
934 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
935 @param[in] Slot The slot number of the SD card to send the command to.
936 @param[in,out] Packet A pointer to the SD command data structure.
937 @param[in] Event If Event is NULL, blocking I/O is performed. If Event is
938 not NULL, then nonblocking I/O is performed, and Event
939 will be signaled when the Packet completes.
941 @retval EFI_SUCCESS The SD Command Packet was sent by the host.
942 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SD
944 @retval EFI_INVALID_PARAMETER Packet, Slot, or the contents of the Packet is invalid.
945 @retval EFI_INVALID_PARAMETER Packet defines a data command but both InDataBuffer and
946 OutDataBuffer are NULL.
947 @retval EFI_NO_MEDIA SD Device not present in the Slot.
948 @retval EFI_UNSUPPORTED The command described by the SD Command Packet is not
949 supported by the host controller.
950 @retval EFI_BAD_BUFFER_SIZE The InTransferLength or OutTransferLength exceeds the
951 limit supported by SD card ( i.e. if the number of bytes
952 exceed the Last LBA).
957 SdMmcPassThruPassThru (
958 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
960 IN OUT EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
,
961 IN EFI_EVENT Event OPTIONAL
965 SD_MMC_HC_PRIVATE_DATA
*Private
;
969 if ((This
== NULL
) || (Packet
== NULL
)) {
970 return EFI_INVALID_PARAMETER
;
973 if ((Packet
->SdMmcCmdBlk
== NULL
) || (Packet
->SdMmcStatusBlk
== NULL
)) {
974 return EFI_INVALID_PARAMETER
;
977 if ((Packet
->OutDataBuffer
== NULL
) && (Packet
->OutTransferLength
!= 0)) {
978 return EFI_INVALID_PARAMETER
;
981 if ((Packet
->InDataBuffer
== NULL
) && (Packet
->InTransferLength
!= 0)) {
982 return EFI_INVALID_PARAMETER
;
985 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
987 if (!Private
->Slot
[Slot
].Enable
) {
988 return EFI_INVALID_PARAMETER
;
991 if (!Private
->Slot
[Slot
].MediaPresent
) {
995 if (!Private
->Slot
[Slot
].Initialized
) {
996 return EFI_DEVICE_ERROR
;
999 Trb
= SdMmcCreateTrb (Private
, Slot
, Packet
, Event
);
1001 return EFI_OUT_OF_RESOURCES
;
1004 // Immediately return for async I/O.
1006 if (Event
!= NULL
) {
1011 // Wait async I/O list is empty before execute sync I/O operation.
1014 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1015 if (IsListEmpty (&Private
->Queue
)) {
1016 gBS
->RestoreTPL (OldTpl
);
1019 gBS
->RestoreTPL (OldTpl
);
1022 Status
= SdMmcWaitTrbEnv (Private
, Trb
);
1023 if (EFI_ERROR (Status
)) {
1027 Status
= SdMmcExecTrb (Private
, Trb
);
1028 if (EFI_ERROR (Status
)) {
1032 Status
= SdMmcWaitTrbResult (Private
, Trb
);
1033 if (EFI_ERROR (Status
)) {
1044 Used to retrieve next slot numbers supported by the SD controller. The function
1045 returns information about all available slots (populated or not-populated).
1047 The GetNextSlot() function retrieves the next slot number on an SD controller.
1048 If on input Slot is 0xFF, then the slot number of the first slot on the SD controller
1051 If Slot is a slot number that was returned on a previous call to GetNextSlot(), then
1052 the slot number of the next slot on the SD controller is returned.
1054 If Slot is not 0xFF and Slot was not returned on a previous call to GetNextSlot(),
1055 EFI_INVALID_PARAMETER is returned.
1057 If Slot is the slot number of the last slot on the SD controller, then EFI_NOT_FOUND
1060 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
1061 @param[in,out] Slot On input, a pointer to a slot number on the SD controller.
1062 On output, a pointer to the next slot number on the SD controller.
1063 An input value of 0xFF retrieves the first slot number on the SD
1066 @retval EFI_SUCCESS The next slot number on the SD controller was returned in Slot.
1067 @retval EFI_NOT_FOUND There are no more slots on this SD controller.
1068 @retval EFI_INVALID_PARAMETER Slot is not 0xFF and Slot was not returned on a previous call
1074 SdMmcPassThruGetNextSlot (
1075 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1079 SD_MMC_HC_PRIVATE_DATA
*Private
;
1082 if ((This
== NULL
) || (Slot
== NULL
)) {
1083 return EFI_INVALID_PARAMETER
;
1086 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1088 if (*Slot
== 0xFF) {
1089 for (Index
= 0; Index
< SD_MMC_HC_MAX_SLOT
; Index
++) {
1090 if (Private
->Slot
[Index
].Enable
) {
1092 Private
->PreviousSlot
= Index
;
1096 return EFI_NOT_FOUND
;
1097 } else if (*Slot
== Private
->PreviousSlot
) {
1098 for (Index
= *Slot
+ 1; Index
< SD_MMC_HC_MAX_SLOT
; Index
++) {
1099 if (Private
->Slot
[Index
].Enable
) {
1101 Private
->PreviousSlot
= Index
;
1105 return EFI_NOT_FOUND
;
1107 return EFI_INVALID_PARAMETER
;
1112 Used to allocate and build a device path node for an SD card on the SD controller.
1114 The BuildDevicePath() function allocates and builds a single device node for the SD
1115 card specified by Slot.
1117 If the SD card specified by Slot is not present on the SD controller, then EFI_NOT_FOUND
1120 If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned.
1122 If there are not enough resources to allocate the device path node, then EFI_OUT_OF_RESOURCES
1125 Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of
1126 DevicePath are initialized to describe the SD card specified by Slot, and EFI_SUCCESS is
1129 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
1130 @param[in] Slot Specifies the slot number of the SD card for which a device
1131 path node is to be allocated and built.
1132 @param[in,out] DevicePath A pointer to a single device path node that describes the SD
1133 card specified by Slot. This function is responsible for
1134 allocating the buffer DevicePath with the boot service
1135 AllocatePool(). It is the caller's responsibility to free
1136 DevicePath when the caller is finished with DevicePath.
1138 @retval EFI_SUCCESS The device path node that describes the SD card specified by
1139 Slot was allocated and returned in DevicePath.
1140 @retval EFI_NOT_FOUND The SD card specified by Slot does not exist on the SD controller.
1141 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1142 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
1147 SdMmcPassThruBuildDevicePath (
1148 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1150 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
1153 SD_MMC_HC_PRIVATE_DATA
*Private
;
1154 SD_DEVICE_PATH
*SdNode
;
1155 EMMC_DEVICE_PATH
*EmmcNode
;
1157 if ((This
== NULL
) || (DevicePath
== NULL
) || (Slot
>= SD_MMC_HC_MAX_SLOT
)) {
1158 return EFI_INVALID_PARAMETER
;
1161 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1163 if ((!Private
->Slot
[Slot
].Enable
) || (!Private
->Slot
[Slot
].MediaPresent
)) {
1164 return EFI_NOT_FOUND
;
1167 if (Private
->Slot
[Slot
].CardType
== SdCardType
) {
1168 SdNode
= AllocateCopyPool (sizeof (SD_DEVICE_PATH
), &mSdDpTemplate
);
1169 if (SdNode
== NULL
) {
1170 return EFI_OUT_OF_RESOURCES
;
1172 SdNode
->SlotNumber
= Slot
;
1174 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) SdNode
;
1175 } else if (Private
->Slot
[Slot
].CardType
== EmmcCardType
) {
1176 EmmcNode
= AllocateCopyPool (sizeof (EMMC_DEVICE_PATH
), &mEmmcDpTemplate
);
1177 if (EmmcNode
== NULL
) {
1178 return EFI_OUT_OF_RESOURCES
;
1180 EmmcNode
->SlotNumber
= Slot
;
1182 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) EmmcNode
;
1185 // Currently we only support SD and EMMC two device nodes.
1187 return EFI_NOT_FOUND
;
1194 This function retrieves an SD card slot number based on the input device path.
1196 The GetSlotNumber() function retrieves slot number for the SD card specified by
1197 the DevicePath node. If DevicePath is NULL, EFI_INVALID_PARAMETER is returned.
1199 If DevicePath is not a device path node type that the SD Pass Thru driver supports,
1200 EFI_UNSUPPORTED is returned.
1202 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1203 @param[in] DevicePath A pointer to the device path node that describes a SD
1204 card on the SD controller.
1205 @param[out] Slot On return, points to the slot number of an SD card on
1208 @retval EFI_SUCCESS SD card slot number is returned in Slot.
1209 @retval EFI_INVALID_PARAMETER Slot or DevicePath is NULL.
1210 @retval EFI_UNSUPPORTED DevicePath is not a device path node type that the SD
1211 Pass Thru driver supports.
1216 SdMmcPassThruGetSlotNumber (
1217 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1218 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1222 SD_MMC_HC_PRIVATE_DATA
*Private
;
1223 SD_DEVICE_PATH
*SdNode
;
1224 EMMC_DEVICE_PATH
*EmmcNode
;
1227 if ((This
== NULL
) || (DevicePath
== NULL
) || (Slot
== NULL
)) {
1228 return EFI_INVALID_PARAMETER
;
1231 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1234 // Check whether the DevicePath belongs to SD_DEVICE_PATH or EMMC_DEVICE_PATH
1236 if ((DevicePath
->Type
!= MESSAGING_DEVICE_PATH
) ||
1237 ((DevicePath
->SubType
!= MSG_SD_DP
) &&
1238 (DevicePath
->SubType
!= MSG_EMMC_DP
)) ||
1239 (DevicePathNodeLength(DevicePath
) != sizeof(SD_DEVICE_PATH
)) ||
1240 (DevicePathNodeLength(DevicePath
) != sizeof(EMMC_DEVICE_PATH
))) {
1241 return EFI_UNSUPPORTED
;
1244 if (DevicePath
->SubType
== MSG_SD_DP
) {
1245 SdNode
= (SD_DEVICE_PATH
*) DevicePath
;
1246 SlotNumber
= SdNode
->SlotNumber
;
1248 EmmcNode
= (EMMC_DEVICE_PATH
*) DevicePath
;
1249 SlotNumber
= EmmcNode
->SlotNumber
;
1252 if (SlotNumber
>= SD_MMC_HC_MAX_SLOT
) {
1253 return EFI_NOT_FOUND
;
1256 if (Private
->Slot
[SlotNumber
].Enable
) {
1260 return EFI_NOT_FOUND
;
1265 Resets an SD card that is connected to the SD controller.
1267 The ResetDevice() function resets the SD card specified by Slot.
1269 If this SD controller does not support a device reset operation, EFI_UNSUPPORTED is
1272 If Slot is not in a valid slot number for this SD controller, EFI_INVALID_PARAMETER
1275 If the device reset operation is completed, EFI_SUCCESS is returned.
1277 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1278 @param[in] Slot Specifies the slot number of the SD card to be reset.
1280 @retval EFI_SUCCESS The SD card specified by Slot was reset.
1281 @retval EFI_UNSUPPORTED The SD controller does not support a device reset operation.
1282 @retval EFI_INVALID_PARAMETER Slot number is invalid.
1283 @retval EFI_NO_MEDIA SD Device not present in the Slot.
1284 @retval EFI_DEVICE_ERROR The reset command failed due to a device error
1289 SdMmcPassThruResetDevice (
1290 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1294 SD_MMC_HC_PRIVATE_DATA
*Private
;
1296 LIST_ENTRY
*NextLink
;
1301 return EFI_INVALID_PARAMETER
;
1304 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1306 if (!Private
->Slot
[Slot
].Enable
) {
1307 return EFI_INVALID_PARAMETER
;
1310 if (!Private
->Slot
[Slot
].MediaPresent
) {
1311 return EFI_NO_MEDIA
;
1314 if (!Private
->Slot
[Slot
].Initialized
) {
1315 return EFI_DEVICE_ERROR
;
1318 // Free all async I/O requests in the queue
1320 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1322 for (Link
= GetFirstNode (&Private
->Queue
);
1323 !IsNull (&Private
->Queue
, Link
);
1325 NextLink
= GetNextNode (&Private
->Queue
, Link
);
1326 RemoveEntryList (Link
);
1327 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
1328 Trb
->Packet
->TransactionStatus
= EFI_ABORTED
;
1329 gBS
->SignalEvent (Trb
->Event
);
1333 gBS
->RestoreTPL (OldTpl
);