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"
21 // Driver Global Variables
23 EFI_DRIVER_BINDING_PROTOCOL gSdMmcPciHcDriverBinding
= {
24 SdMmcPciHcDriverBindingSupported
,
25 SdMmcPciHcDriverBindingStart
,
26 SdMmcPciHcDriverBindingStop
,
33 // Template for SD/MMC host controller private data.
35 SD_MMC_HC_PRIVATE_DATA gSdMmcPciHcTemplate
= {
36 SD_MMC_HC_PRIVATE_SIGNATURE
, // Signature
37 NULL
, // ControllerHandle
41 SdMmcPassThruPassThru
,
42 SdMmcPassThruGetNextSlot
,
43 SdMmcPassThruBuildDevicePath
,
44 SdMmcPassThruGetSlotNumber
,
45 SdMmcPassThruResetDevice
52 INITIALIZE_LIST_HEAD_VARIABLE (gSdMmcPciHcTemplate
.Queue
),
54 {0, UnknownSlot
, 0, 0, 0}, {0, UnknownSlot
, 0, 0, 0}, {0, UnknownSlot
, 0, 0, 0},
55 {0, UnknownSlot
, 0, 0, 0}, {0, UnknownSlot
, 0, 0, 0}, {0, UnknownSlot
, 0, 0, 0}
63 0 // ControllerVersion
66 SD_DEVICE_PATH mSdDpTemplate
= {
68 MESSAGING_DEVICE_PATH
,
71 (UINT8
) (sizeof (SD_DEVICE_PATH
)),
72 (UINT8
) ((sizeof (SD_DEVICE_PATH
)) >> 8)
78 EMMC_DEVICE_PATH mEmmcDpTemplate
= {
80 MESSAGING_DEVICE_PATH
,
83 (UINT8
) (sizeof (EMMC_DEVICE_PATH
)),
84 (UINT8
) ((sizeof (EMMC_DEVICE_PATH
)) >> 8)
91 // Prioritized function list to detect card type.
92 // User could add other card detection logic here.
94 CARD_TYPE_DETECT_ROUTINE mCardTypeDetectRoutineTable
[] = {
101 The entry point for SD host controller driver, used to install this driver on the ImageHandle.
103 @param[in] ImageHandle The firmware allocated handle for this driver image.
104 @param[in] SystemTable Pointer to the EFI system table.
106 @retval EFI_SUCCESS Driver loaded.
107 @retval other Driver not loaded.
112 InitializeSdMmcPciHcDxe (
113 IN EFI_HANDLE ImageHandle
,
114 IN EFI_SYSTEM_TABLE
*SystemTable
119 Status
= EfiLibInstallDriverBindingComponentName2 (
122 &gSdMmcPciHcDriverBinding
,
124 &gSdMmcPciHcComponentName
,
125 &gSdMmcPciHcComponentName2
127 ASSERT_EFI_ERROR (Status
);
133 Call back function when the timer event is signaled.
135 @param[in] Event The Event this notify function registered to.
136 @param[in] Context Pointer to the context data registered to the
142 ProcessAsyncTaskList (
147 SD_MMC_HC_PRIVATE_DATA
*Private
;
151 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
;
152 BOOLEAN InfiniteWait
;
155 Private
= (SD_MMC_HC_PRIVATE_DATA
*)Context
;
158 // Check if the first entry in the async I/O queue is done or not.
160 Status
= EFI_SUCCESS
;
162 Link
= GetFirstNode (&Private
->Queue
);
163 if (!IsNull (&Private
->Queue
, Link
)) {
164 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
165 if (!Private
->Slot
[Trb
->Slot
].MediaPresent
) {
166 Status
= EFI_NO_MEDIA
;
171 // Check whether the cmd/data line is ready for transfer.
173 Status
= SdMmcCheckTrbEnv (Private
, Trb
);
174 if (!EFI_ERROR (Status
)) {
176 Status
= SdMmcExecTrb (Private
, Trb
);
177 if (EFI_ERROR (Status
)) {
184 Status
= SdMmcCheckTrbResult (Private
, Trb
);
188 if ((Trb
!= NULL
) && (Status
== EFI_NOT_READY
)) {
189 Packet
= Trb
->Packet
;
190 if (Packet
->Timeout
== 0) {
193 InfiniteWait
= FALSE
;
195 if ((!InfiniteWait
) && (Trb
->Timeout
-- == 0)) {
196 RemoveEntryList (Link
);
197 Trb
->Packet
->TransactionStatus
= EFI_TIMEOUT
;
198 TrbEvent
= Trb
->Event
;
200 DEBUG ((EFI_D_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p EFI_TIMEOUT\n", TrbEvent
));
201 gBS
->SignalEvent (TrbEvent
);
205 if ((Trb
!= NULL
) && (Status
!= EFI_NOT_READY
)) {
206 RemoveEntryList (Link
);
207 Trb
->Packet
->TransactionStatus
= Status
;
208 TrbEvent
= Trb
->Event
;
210 DEBUG ((EFI_D_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p with %r\n", TrbEvent
, Status
));
211 gBS
->SignalEvent (TrbEvent
);
217 Sd removable device enumeration callback function when the timer event is signaled.
219 @param[in] Event The Event this notify function registered to.
220 @param[in] Context Pointer to the context data registered to the
226 SdMmcPciHcEnumerateDevice (
231 SD_MMC_HC_PRIVATE_DATA
*Private
;
234 BOOLEAN MediaPresent
;
236 CARD_TYPE_DETECT_ROUTINE
*Routine
;
239 LIST_ENTRY
*NextLink
;
242 Private
= (SD_MMC_HC_PRIVATE_DATA
*)Context
;
244 for (Slot
= 0; Slot
< SD_MMC_HC_MAX_SLOT
; Slot
++) {
245 if ((Private
->Slot
[Slot
].Enable
) && (Private
->Slot
[Slot
].SlotType
== RemovableSlot
)) {
246 Status
= SdMmcHcCardDetect (Private
->PciIo
, Slot
, &MediaPresent
);
247 if ((Status
== EFI_MEDIA_CHANGED
) && !MediaPresent
) {
248 DEBUG ((EFI_D_INFO
, "SdMmcPciHcEnumerateDevice: device disconnected at slot %d of pci %p\n", Slot
, Private
->PciIo
));
249 Private
->Slot
[Slot
].MediaPresent
= FALSE
;
250 Private
->Slot
[Slot
].Initialized
= FALSE
;
252 // Signal all async task events at the slot with EFI_NO_MEDIA status.
254 for (Link
= GetFirstNode (&Private
->Queue
);
255 !IsNull (&Private
->Queue
, Link
);
257 NextLink
= GetNextNode (&Private
->Queue
, Link
);
258 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
259 if (Trb
->Slot
== Slot
) {
260 RemoveEntryList (Link
);
261 Trb
->Packet
->TransactionStatus
= EFI_NO_MEDIA
;
262 gBS
->SignalEvent (Trb
->Event
);
267 // Notify the upper layer the connect state change through ReinstallProtocolInterface.
269 gBS
->ReinstallProtocolInterface (
270 Private
->ControllerHandle
,
271 &gEfiSdMmcPassThruProtocolGuid
,
276 if ((Status
== EFI_MEDIA_CHANGED
) && MediaPresent
) {
277 DEBUG ((EFI_D_INFO
, "SdMmcPciHcEnumerateDevice: device connected at slot %d of pci %p\n", Slot
, Private
->PciIo
));
279 // Reset the specified slot of the SD/MMC Pci Host Controller
281 Status
= SdMmcHcReset (Private
->PciIo
, Slot
);
282 if (EFI_ERROR (Status
)) {
286 // Reinitialize slot and restart identification process for the new attached device
288 Status
= SdMmcHcInitHost (Private
->PciIo
, Slot
, Private
->Capability
[Slot
]);
289 if (EFI_ERROR (Status
)) {
293 Private
->Slot
[Slot
].MediaPresent
= TRUE
;
294 Private
->Slot
[Slot
].Initialized
= TRUE
;
295 RoutineNum
= sizeof (mCardTypeDetectRoutineTable
) / sizeof (CARD_TYPE_DETECT_ROUTINE
);
296 for (Index
= 0; Index
< RoutineNum
; Index
++) {
297 Routine
= &mCardTypeDetectRoutineTable
[Index
];
298 if (*Routine
!= NULL
) {
299 Status
= (*Routine
) (Private
, Slot
);
300 if (!EFI_ERROR (Status
)) {
306 // This card doesn't get initialized correctly.
308 if (Index
== RoutineNum
) {
309 Private
->Slot
[Slot
].Initialized
= FALSE
;
313 // Notify the upper layer the connect state change through ReinstallProtocolInterface.
315 gBS
->ReinstallProtocolInterface (
316 Private
->ControllerHandle
,
317 &gEfiSdMmcPassThruProtocolGuid
,
328 Tests to see if this driver supports a given controller. If a child device is provided,
329 it further tests to see if this driver supports creating a handle for the specified child device.
331 This function checks to see if the driver specified by This supports the device specified by
332 ControllerHandle. Drivers will typically use the device path attached to
333 ControllerHandle and/or the services from the bus I/O abstraction attached to
334 ControllerHandle to determine if the driver supports ControllerHandle. This function
335 may be called many times during platform initialization. In order to reduce boot times, the tests
336 performed by this function must be very small, and take as little time as possible to execute. This
337 function must not change the state of any hardware devices, and this function must be aware that the
338 device specified by ControllerHandle may already be managed by the same driver or a
339 different driver. This function must match its calls to AllocatePages() with FreePages(),
340 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
341 Since ControllerHandle may have been previously started by the same driver, if a protocol is
342 already in the opened state, then it must not be closed with CloseProtocol(). This is required
343 to guarantee the state of ControllerHandle is not modified by this function.
345 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
346 @param[in] ControllerHandle The handle of the controller to test. This handle
347 must support a protocol interface that supplies
348 an I/O abstraction to the driver.
349 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
350 parameter is ignored by device drivers, and is optional for bus
351 drivers. For bus drivers, if this parameter is not NULL, then
352 the bus driver must determine if the bus controller specified
353 by ControllerHandle and the child controller specified
354 by RemainingDevicePath are both supported by this
357 @retval EFI_SUCCESS The device specified by ControllerHandle and
358 RemainingDevicePath is supported by the driver specified by This.
359 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
360 RemainingDevicePath is already being managed by the driver
362 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
363 RemainingDevicePath is already being managed by a different
364 driver or an application that requires exclusive access.
365 Currently not implemented.
366 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
367 RemainingDevicePath is not supported by the driver specified by This.
371 SdMmcPciHcDriverBindingSupported (
372 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
373 IN EFI_HANDLE Controller
,
374 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
378 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
379 EFI_PCI_IO_PROTOCOL
*PciIo
;
383 ParentDevicePath
= NULL
;
386 // SdPciHcDxe is a device driver, and should ingore the
387 // "RemainingDevicePath" according to EFI spec.
389 Status
= gBS
->OpenProtocol (
391 &gEfiDevicePathProtocolGuid
,
392 (VOID
*) &ParentDevicePath
,
393 This
->DriverBindingHandle
,
395 EFI_OPEN_PROTOCOL_BY_DRIVER
397 if (EFI_ERROR (Status
)) {
399 // EFI_ALREADY_STARTED is also an error.
404 // Close the protocol because we don't use it here.
408 &gEfiDevicePathProtocolGuid
,
409 This
->DriverBindingHandle
,
414 // Now test the EfiPciIoProtocol.
416 Status
= gBS
->OpenProtocol (
418 &gEfiPciIoProtocolGuid
,
420 This
->DriverBindingHandle
,
422 EFI_OPEN_PROTOCOL_BY_DRIVER
424 if (EFI_ERROR (Status
)) {
429 // Now further check the PCI header: Base class (offset 0x08) and
430 // Sub Class (offset 0x05). This controller should be an SD/MMC PCI
433 Status
= PciIo
->Pci
.Read (
440 if (EFI_ERROR (Status
)) {
443 &gEfiPciIoProtocolGuid
,
444 This
->DriverBindingHandle
,
447 return EFI_UNSUPPORTED
;
450 // Since we already got the PciData, we can close protocol to avoid to carry it
451 // on for multiple exit points.
455 &gEfiPciIoProtocolGuid
,
456 This
->DriverBindingHandle
,
461 // Examine SD PCI Host Controller PCI Configuration table fields.
463 if ((PciData
.Hdr
.ClassCode
[2] == PCI_CLASS_SYSTEM_PERIPHERAL
) &&
464 (PciData
.Hdr
.ClassCode
[1] == PCI_SUBCLASS_SD_HOST_CONTROLLER
) &&
465 ((PciData
.Hdr
.ClassCode
[0] == 0x00) || (PciData
.Hdr
.ClassCode
[0] == 0x01))) {
469 return EFI_UNSUPPORTED
;
473 Starts a device controller or a bus controller.
475 The Start() function is designed to be invoked from the EFI boot service ConnectController().
476 As a result, much of the error checking on the parameters to Start() has been moved into this
477 common boot service. It is legal to call Start() from other locations,
478 but the following calling restrictions must be followed or the system behavior will not be deterministic.
479 1. ControllerHandle must be a valid EFI_HANDLE.
480 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
481 EFI_DEVICE_PATH_PROTOCOL.
482 3. Prior to calling Start(), the Supported() function for the driver specified by This must
483 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
485 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
486 @param[in] ControllerHandle The handle of the controller to start. This handle
487 must support a protocol interface that supplies
488 an I/O abstraction to the driver.
489 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
490 parameter is ignored by device drivers, and is optional for bus
491 drivers. For a bus driver, if this parameter is NULL, then handles
492 for all the children of Controller are created by this driver.
493 If this parameter is not NULL and the first Device Path Node is
494 not the End of Device Path Node, then only the handle for the
495 child device specified by the first Device Path Node of
496 RemainingDevicePath is created by this driver.
497 If the first Device Path Node of RemainingDevicePath is
498 the End of Device Path Node, no child handle is created by this
501 @retval EFI_SUCCESS The device was started.
502 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
503 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
504 @retval Others The driver failded to start the device.
509 SdMmcPciHcDriverBindingStart (
510 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
511 IN EFI_HANDLE Controller
,
512 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
516 SD_MMC_HC_PRIVATE_DATA
*Private
;
517 EFI_PCI_IO_PROTOCOL
*PciIo
;
519 UINT64 PciAttributes
;
524 CARD_TYPE_DETECT_ROUTINE
*Routine
;
526 BOOLEAN MediaPresent
;
528 DEBUG ((EFI_D_INFO
, "SdMmcPciHcDriverBindingStart: Start\n"));
531 // Open PCI I/O Protocol and save pointer to open protocol
532 // in private data area.
535 Status
= gBS
->OpenProtocol (
537 &gEfiPciIoProtocolGuid
,
539 This
->DriverBindingHandle
,
541 EFI_OPEN_PROTOCOL_BY_DRIVER
543 if (EFI_ERROR (Status
)) {
548 // Enable the SD Host Controller MMIO space
551 Status
= PciIo
->Attributes (
553 EfiPciIoAttributeOperationGet
,
558 if (EFI_ERROR (Status
)) {
562 Status
= PciIo
->Attributes (
564 EfiPciIoAttributeOperationSupported
,
569 if (!EFI_ERROR (Status
)) {
570 Supports
&= (UINT64
)EFI_PCI_DEVICE_ENABLE
;
571 Status
= PciIo
->Attributes (
573 EfiPciIoAttributeOperationEnable
,
581 Private
= AllocateCopyPool (sizeof (SD_MMC_HC_PRIVATE_DATA
), &gSdMmcPciHcTemplate
);
582 if (Private
== NULL
) {
583 Status
= EFI_OUT_OF_RESOURCES
;
587 Private
->ControllerHandle
= Controller
;
588 Private
->PciIo
= PciIo
;
589 Private
->PciAttributes
= PciAttributes
;
590 InitializeListHead (&Private
->Queue
);
593 // Get SD/MMC Pci Host Controller Slot info
595 Status
= SdMmcHcGetSlotInfo (PciIo
, &FirstBar
, &SlotNum
);
596 if (EFI_ERROR (Status
)) {
600 for (Slot
= FirstBar
; Slot
< (FirstBar
+ SlotNum
); Slot
++) {
601 Private
->Slot
[Slot
].Enable
= TRUE
;
603 Status
= SdMmcHcGetCapability (PciIo
, Slot
, &Private
->Capability
[Slot
]);
604 if (EFI_ERROR (Status
)) {
607 DumpCapabilityReg (Slot
, &Private
->Capability
[Slot
]);
609 Status
= SdMmcHcGetMaxCurrent (PciIo
, Slot
, &Private
->MaxCurrent
[Slot
]);
610 if (EFI_ERROR (Status
)) {
614 Private
->Slot
[Slot
].SlotType
= Private
->Capability
[Slot
].SlotType
;
615 if ((Private
->Slot
[Slot
].SlotType
!= RemovableSlot
) && (Private
->Slot
[Slot
].SlotType
!= EmbeddedSlot
)) {
616 DEBUG ((EFI_D_INFO
, "SdMmcPciHcDxe doesn't support the slot type [%d]!!!\n", Private
->Slot
[Slot
].SlotType
));
621 // Reset the specified slot of the SD/MMC Pci Host Controller
623 Status
= SdMmcHcReset (PciIo
, Slot
);
624 if (EFI_ERROR (Status
)) {
628 // Check whether there is a SD/MMC card attached
630 Status
= SdMmcHcCardDetect (PciIo
, Slot
, &MediaPresent
);
631 if (EFI_ERROR (Status
) && (Status
!= EFI_MEDIA_CHANGED
)) {
633 } else if (!MediaPresent
) {
634 DEBUG ((EFI_D_ERROR
, "SdMmcHcCardDetect: No device attached in Slot[%d]!!!\n", Slot
));
638 Status
= SdMmcHcInitHost (PciIo
, Slot
, Private
->Capability
[Slot
]);
639 if (EFI_ERROR (Status
)) {
643 Private
->Slot
[Slot
].MediaPresent
= TRUE
;
644 Private
->Slot
[Slot
].Initialized
= TRUE
;
645 RoutineNum
= sizeof (mCardTypeDetectRoutineTable
) / sizeof (CARD_TYPE_DETECT_ROUTINE
);
646 for (Index
= 0; Index
< RoutineNum
; Index
++) {
647 Routine
= &mCardTypeDetectRoutineTable
[Index
];
648 if (*Routine
!= NULL
) {
649 Status
= (*Routine
) (Private
, Slot
);
650 if (!EFI_ERROR (Status
)) {
656 // This card doesn't get initialized correctly.
658 if (Index
== RoutineNum
) {
659 Private
->Slot
[Slot
].Initialized
= FALSE
;
664 // Start the asynchronous I/O monitor
666 Status
= gBS
->CreateEvent (
667 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
669 ProcessAsyncTaskList
,
673 if (EFI_ERROR (Status
)) {
677 Status
= gBS
->SetTimer (Private
->TimerEvent
, TimerPeriodic
, SD_MMC_HC_ASYNC_TIMER
);
678 if (EFI_ERROR (Status
)) {
683 // Start the Sd removable device connection enumeration
685 Status
= gBS
->CreateEvent (
686 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
688 SdMmcPciHcEnumerateDevice
,
690 &Private
->ConnectEvent
692 if (EFI_ERROR (Status
)) {
696 Status
= gBS
->SetTimer (Private
->ConnectEvent
, TimerPeriodic
, SD_MMC_HC_ENUM_TIMER
);
697 if (EFI_ERROR (Status
)) {
701 Status
= gBS
->InstallMultipleProtocolInterfaces (
703 &gEfiSdMmcPassThruProtocolGuid
,
704 &(Private
->PassThru
),
708 DEBUG ((EFI_D_INFO
, "SdMmcPciHcDriverBindingStart: %r End on %x\n", Status
, Controller
));
711 if (EFI_ERROR (Status
)) {
712 if ((Private
!= NULL
) && (Private
->PciAttributes
!= 0)) {
714 // Restore original PCI attributes
718 EfiPciIoAttributeOperationSet
,
719 Private
->PciAttributes
,
725 &gEfiPciIoProtocolGuid
,
726 This
->DriverBindingHandle
,
730 if ((Private
!= NULL
) && (Private
->TimerEvent
!= NULL
)) {
731 gBS
->CloseEvent (Private
->TimerEvent
);
734 if ((Private
!= NULL
) && (Private
->ConnectEvent
!= NULL
)) {
735 gBS
->CloseEvent (Private
->ConnectEvent
);
738 if (Private
!= NULL
) {
747 Stops a device controller or a bus controller.
749 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
750 As a result, much of the error checking on the parameters to Stop() has been moved
751 into this common boot service. It is legal to call Stop() from other locations,
752 but the following calling restrictions must be followed or the system behavior will not be deterministic.
753 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
754 same driver's Start() function.
755 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
756 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
757 Start() function, and the Start() function must have called OpenProtocol() on
758 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
760 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
761 @param[in] ControllerHandle A handle to the device being stopped. The handle must
762 support a bus specific I/O protocol for the driver
763 to use to stop the device.
764 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
765 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
766 if NumberOfChildren is 0.
768 @retval EFI_SUCCESS The device was stopped.
769 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
774 SdMmcPciHcDriverBindingStop (
775 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
776 IN EFI_HANDLE Controller
,
777 IN UINTN NumberOfChildren
,
778 IN EFI_HANDLE
*ChildHandleBuffer
782 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
783 SD_MMC_HC_PRIVATE_DATA
*Private
;
784 EFI_PCI_IO_PROTOCOL
*PciIo
;
786 LIST_ENTRY
*NextLink
;
789 DEBUG ((EFI_D_INFO
, "SdMmcPciHcDriverBindingStop: Start\n"));
791 Status
= gBS
->OpenProtocol (
793 &gEfiSdMmcPassThruProtocolGuid
,
795 This
->DriverBindingHandle
,
797 EFI_OPEN_PROTOCOL_GET_PROTOCOL
799 if (EFI_ERROR (Status
)) {
803 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (PassThru
);
805 // Close Non-Blocking timer and free Task list.
807 if (Private
->TimerEvent
!= NULL
) {
808 gBS
->CloseEvent (Private
->TimerEvent
);
809 Private
->TimerEvent
= NULL
;
811 if (Private
->ConnectEvent
!= NULL
) {
812 gBS
->CloseEvent (Private
->ConnectEvent
);
813 Private
->ConnectEvent
= NULL
;
816 // As the timer is closed, there is no needs to use TPL lock to
817 // protect the critical region "queue".
819 for (Link
= GetFirstNode (&Private
->Queue
);
820 !IsNull (&Private
->Queue
, Link
);
822 NextLink
= GetNextNode (&Private
->Queue
, Link
);
823 RemoveEntryList (Link
);
824 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
825 Trb
->Packet
->TransactionStatus
= EFI_ABORTED
;
826 gBS
->SignalEvent (Trb
->Event
);
831 // Uninstall Block I/O protocol from the device handle
833 Status
= gBS
->UninstallProtocolInterface (
835 &gEfiSdMmcPassThruProtocolGuid
,
839 if (EFI_ERROR (Status
)) {
845 &gEfiPciIoProtocolGuid
,
846 This
->DriverBindingHandle
,
850 // Restore original PCI attributes
852 PciIo
= Private
->PciIo
;
853 Status
= PciIo
->Attributes (
855 EfiPciIoAttributeOperationSet
,
856 Private
->PciAttributes
,
859 ASSERT_EFI_ERROR (Status
);
863 DEBUG ((EFI_D_INFO
, "SdMmcPciHcDriverBindingStop: End with %r\n", Status
));
869 Sends SD command to an SD card that is attached to the SD controller.
871 The PassThru() function sends the SD command specified by Packet to the SD card
874 If Packet is successfully sent to the SD card, then EFI_SUCCESS is returned.
876 If a device error occurs while sending the Packet, then EFI_DEVICE_ERROR is returned.
878 If Slot is not in a valid range for the SD controller, then EFI_INVALID_PARAMETER
881 If Packet defines a data command but both InDataBuffer and OutDataBuffer are NULL,
882 EFI_INVALID_PARAMETER is returned.
884 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
885 @param[in] Slot The slot number of the SD card to send the command to.
886 @param[in,out] Packet A pointer to the SD command data structure.
887 @param[in] Event If Event is NULL, blocking I/O is performed. If Event is
888 not NULL, then nonblocking I/O is performed, and Event
889 will be signaled when the Packet completes.
891 @retval EFI_SUCCESS The SD Command Packet was sent by the host.
892 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SD
894 @retval EFI_INVALID_PARAMETER Packet, Slot, or the contents of the Packet is invalid.
895 @retval EFI_INVALID_PARAMETER Packet defines a data command but both InDataBuffer and
896 OutDataBuffer are NULL.
897 @retval EFI_NO_MEDIA SD Device not present in the Slot.
898 @retval EFI_UNSUPPORTED The command described by the SD Command Packet is not
899 supported by the host controller.
900 @retval EFI_BAD_BUFFER_SIZE The InTransferLength or OutTransferLength exceeds the
901 limit supported by SD card ( i.e. if the number of bytes
902 exceed the Last LBA).
907 SdMmcPassThruPassThru (
908 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
910 IN OUT EFI_SD_MMC_PASS_THRU_COMMAND_PACKET
*Packet
,
911 IN EFI_EVENT Event OPTIONAL
915 SD_MMC_HC_PRIVATE_DATA
*Private
;
919 if ((This
== NULL
) || (Packet
== NULL
)) {
920 return EFI_INVALID_PARAMETER
;
923 if ((Packet
->SdMmcCmdBlk
== NULL
) || (Packet
->SdMmcStatusBlk
== NULL
)) {
924 return EFI_INVALID_PARAMETER
;
927 if ((Packet
->OutDataBuffer
== NULL
) && (Packet
->OutTransferLength
!= 0)) {
928 return EFI_INVALID_PARAMETER
;
931 if ((Packet
->InDataBuffer
== NULL
) && (Packet
->InTransferLength
!= 0)) {
932 return EFI_INVALID_PARAMETER
;
935 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
937 if (!Private
->Slot
[Slot
].Enable
) {
938 return EFI_INVALID_PARAMETER
;
941 if (!Private
->Slot
[Slot
].MediaPresent
) {
945 if (!Private
->Slot
[Slot
].Initialized
) {
946 return EFI_DEVICE_ERROR
;
949 Trb
= SdMmcCreateTrb (Private
, Slot
, Packet
, Event
);
951 return EFI_OUT_OF_RESOURCES
;
954 // Immediately return for async I/O.
961 // Wait async I/O list is empty before execute sync I/O operation.
964 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
965 if (IsListEmpty (&Private
->Queue
)) {
966 gBS
->RestoreTPL (OldTpl
);
969 gBS
->RestoreTPL (OldTpl
);
972 Status
= SdMmcWaitTrbEnv (Private
, Trb
);
973 if (EFI_ERROR (Status
)) {
977 Status
= SdMmcExecTrb (Private
, Trb
);
978 if (EFI_ERROR (Status
)) {
982 Status
= SdMmcWaitTrbResult (Private
, Trb
);
983 if (EFI_ERROR (Status
)) {
988 if ((Trb
!= NULL
) && (Trb
->AdmaDesc
!= NULL
)) {
989 FreePages (Trb
->AdmaDesc
, Trb
->AdmaPages
);
1000 Used to retrieve next slot numbers supported by the SD controller. The function
1001 returns information about all available slots (populated or not-populated).
1003 The GetNextSlot() function retrieves the next slot number on an SD controller.
1004 If on input Slot is 0xFF, then the slot number of the first slot on the SD controller
1007 If Slot is a slot number that was returned on a previous call to GetNextSlot(), then
1008 the slot number of the next slot on the SD controller is returned.
1010 If Slot is not 0xFF and Slot was not returned on a previous call to GetNextSlot(),
1011 EFI_INVALID_PARAMETER is returned.
1013 If Slot is the slot number of the last slot on the SD controller, then EFI_NOT_FOUND
1016 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
1017 @param[in,out] Slot On input, a pointer to a slot number on the SD controller.
1018 On output, a pointer to the next slot number on the SD controller.
1019 An input value of 0xFF retrieves the first slot number on the SD
1022 @retval EFI_SUCCESS The next slot number on the SD controller was returned in Slot.
1023 @retval EFI_NOT_FOUND There are no more slots on this SD controller.
1024 @retval EFI_INVALID_PARAMETER Slot is not 0xFF and Slot was not returned on a previous call
1030 SdMmcPassThruGetNextSlot (
1031 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1035 SD_MMC_HC_PRIVATE_DATA
*Private
;
1038 if ((This
== NULL
) || (Slot
== NULL
)) {
1039 return EFI_INVALID_PARAMETER
;
1042 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1044 if (*Slot
== 0xFF) {
1045 for (Index
= 0; Index
< SD_MMC_HC_MAX_SLOT
; Index
++) {
1046 if (Private
->Slot
[Index
].Enable
) {
1048 Private
->PreviousSlot
= Index
;
1052 return EFI_NOT_FOUND
;
1053 } else if (*Slot
== Private
->PreviousSlot
) {
1054 for (Index
= *Slot
+ 1; Index
< SD_MMC_HC_MAX_SLOT
; Index
++) {
1055 if (Private
->Slot
[Index
].Enable
) {
1057 Private
->PreviousSlot
= Index
;
1061 return EFI_NOT_FOUND
;
1063 return EFI_INVALID_PARAMETER
;
1068 Used to allocate and build a device path node for an SD card on the SD controller.
1070 The BuildDevicePath() function allocates and builds a single device node for the SD
1071 card specified by Slot.
1073 If the SD card specified by Slot is not present on the SD controller, then EFI_NOT_FOUND
1076 If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned.
1078 If there are not enough resources to allocate the device path node, then EFI_OUT_OF_RESOURCES
1081 Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of
1082 DevicePath are initialized to describe the SD card specified by Slot, and EFI_SUCCESS is
1085 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
1086 @param[in] Slot Specifies the slot number of the SD card for which a device
1087 path node is to be allocated and built.
1088 @param[in,out] DevicePath A pointer to a single device path node that describes the SD
1089 card specified by Slot. This function is responsible for
1090 allocating the buffer DevicePath with the boot service
1091 AllocatePool(). It is the caller's responsibility to free
1092 DevicePath when the caller is finished with DevicePath.
1094 @retval EFI_SUCCESS The device path node that describes the SD card specified by
1095 Slot was allocated and returned in DevicePath.
1096 @retval EFI_NOT_FOUND The SD card specified by Slot does not exist on the SD controller.
1097 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1098 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
1103 SdMmcPassThruBuildDevicePath (
1104 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1106 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
1109 SD_MMC_HC_PRIVATE_DATA
*Private
;
1110 SD_DEVICE_PATH
*SdNode
;
1111 EMMC_DEVICE_PATH
*EmmcNode
;
1113 if ((This
== NULL
) || (DevicePath
== NULL
) || (Slot
>= SD_MMC_HC_MAX_SLOT
)) {
1114 return EFI_INVALID_PARAMETER
;
1117 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1119 if ((!Private
->Slot
[Slot
].Enable
) || (!Private
->Slot
[Slot
].MediaPresent
)) {
1120 return EFI_NOT_FOUND
;
1123 if (Private
->Slot
[Slot
].CardType
== SdCardType
) {
1124 SdNode
= AllocateCopyPool (sizeof (SD_DEVICE_PATH
), &mSdDpTemplate
);
1125 if (SdNode
== NULL
) {
1126 return EFI_OUT_OF_RESOURCES
;
1128 SdNode
->SlotNumber
= Slot
;
1130 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) SdNode
;
1131 } else if (Private
->Slot
[Slot
].CardType
== EmmcCardType
) {
1132 EmmcNode
= AllocateCopyPool (sizeof (EMMC_DEVICE_PATH
), &mEmmcDpTemplate
);
1133 if (EmmcNode
== NULL
) {
1134 return EFI_OUT_OF_RESOURCES
;
1136 EmmcNode
->SlotNumber
= Slot
;
1138 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) EmmcNode
;
1141 // Currently we only support SD and EMMC two device nodes.
1143 return EFI_NOT_FOUND
;
1150 This function retrieves an SD card slot number based on the input device path.
1152 The GetSlotNumber() function retrieves slot number for the SD card specified by
1153 the DevicePath node. If DevicePath is NULL, EFI_INVALID_PARAMETER is returned.
1155 If DevicePath is not a device path node type that the SD Pass Thru driver supports,
1156 EFI_UNSUPPORTED is returned.
1158 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1159 @param[in] DevicePath A pointer to the device path node that describes a SD
1160 card on the SD controller.
1161 @param[out] Slot On return, points to the slot number of an SD card on
1164 @retval EFI_SUCCESS SD card slot number is returned in Slot.
1165 @retval EFI_INVALID_PARAMETER Slot or DevicePath is NULL.
1166 @retval EFI_UNSUPPORTED DevicePath is not a device path node type that the SD
1167 Pass Thru driver supports.
1172 SdMmcPassThruGetSlotNumber (
1173 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1174 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1178 SD_MMC_HC_PRIVATE_DATA
*Private
;
1179 SD_DEVICE_PATH
*SdNode
;
1180 EMMC_DEVICE_PATH
*EmmcNode
;
1183 if ((This
== NULL
) || (DevicePath
== NULL
) || (Slot
== NULL
)) {
1184 return EFI_INVALID_PARAMETER
;
1187 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1190 // Check whether the DevicePath belongs to SD_DEVICE_PATH or EMMC_DEVICE_PATH
1192 if ((DevicePath
->Type
!= MESSAGING_DEVICE_PATH
) ||
1193 ((DevicePath
->SubType
!= MSG_SD_DP
) &&
1194 (DevicePath
->SubType
!= MSG_EMMC_DP
)) ||
1195 (DevicePathNodeLength(DevicePath
) != sizeof(SD_DEVICE_PATH
)) ||
1196 (DevicePathNodeLength(DevicePath
) != sizeof(EMMC_DEVICE_PATH
))) {
1197 return EFI_UNSUPPORTED
;
1200 if (DevicePath
->SubType
== MSG_SD_DP
) {
1201 SdNode
= (SD_DEVICE_PATH
*) DevicePath
;
1202 SlotNumber
= SdNode
->SlotNumber
;
1204 EmmcNode
= (EMMC_DEVICE_PATH
*) DevicePath
;
1205 SlotNumber
= EmmcNode
->SlotNumber
;
1208 if (SlotNumber
>= SD_MMC_HC_MAX_SLOT
) {
1209 return EFI_NOT_FOUND
;
1212 if (Private
->Slot
[SlotNumber
].Enable
) {
1216 return EFI_NOT_FOUND
;
1221 Resets an SD card that is connected to the SD controller.
1223 The ResetDevice() function resets the SD card specified by Slot.
1225 If this SD controller does not support a device reset operation, EFI_UNSUPPORTED is
1228 If Slot is not in a valid slot number for this SD controller, EFI_INVALID_PARAMETER
1231 If the device reset operation is completed, EFI_SUCCESS is returned.
1233 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1234 @param[in] Slot Specifies the slot number of the SD card to be reset.
1236 @retval EFI_SUCCESS The SD card specified by Slot was reset.
1237 @retval EFI_UNSUPPORTED The SD controller does not support a device reset operation.
1238 @retval EFI_INVALID_PARAMETER Slot number is invalid.
1239 @retval EFI_NO_MEDIA SD Device not present in the Slot.
1240 @retval EFI_DEVICE_ERROR The reset command failed due to a device error
1245 SdMmcPassThruResetDevice (
1246 IN EFI_SD_MMC_PASS_THRU_PROTOCOL
*This
,
1250 SD_MMC_HC_PRIVATE_DATA
*Private
;
1252 LIST_ENTRY
*NextLink
;
1257 return EFI_INVALID_PARAMETER
;
1260 Private
= SD_MMC_HC_PRIVATE_FROM_THIS (This
);
1262 if (!Private
->Slot
[Slot
].Enable
) {
1263 return EFI_INVALID_PARAMETER
;
1266 if (!Private
->Slot
[Slot
].MediaPresent
) {
1267 return EFI_NO_MEDIA
;
1270 if (!Private
->Slot
[Slot
].Initialized
) {
1271 return EFI_DEVICE_ERROR
;
1274 // Free all async I/O requests in the queue
1276 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1278 for (Link
= GetFirstNode (&Private
->Queue
);
1279 !IsNull (&Private
->Queue
, Link
);
1281 NextLink
= GetNextNode (&Private
->Queue
, Link
);
1282 RemoveEntryList (Link
);
1283 Trb
= SD_MMC_HC_TRB_FROM_THIS (Link
);
1284 Trb
->Packet
->TransactionStatus
= EFI_ABORTED
;
1285 gBS
->SignalEvent (Trb
->Event
);
1289 gBS
->RestoreTPL (OldTpl
);