2 UfsHcDxe driver is used to provide platform-dependent info, mainly UFS host controller
3 MMIO base, to upper layer UFS drivers.
5 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "UfsPciHcDxe.h"
19 // NVM Express Driver Binding Protocol Instance
21 EFI_DRIVER_BINDING_PROTOCOL gUfsHcDriverBinding
= {
22 UfsHcDriverBindingSupported
,
23 UfsHcDriverBindingStart
,
24 UfsHcDriverBindingStop
,
31 // Template for Ufs host controller private data.
33 UFS_HOST_CONTROLLER_PRIVATE_DATA gUfsHcTemplate
= {
34 UFS_HC_PRIVATE_DATA_SIGNATURE
, // Signature
49 Get the MMIO base of the UFS host controller.
51 @param[in] This A pointer to the EFI_UFS_HOST_CONTROLLER_PROTOCOL instance.
52 @param[out] MmioBar The MMIO base address of UFS host controller.
54 @retval EFI_SUCCESS The operation succeeds.
55 @retval others The operation fails.
60 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*This
,
64 UFS_HOST_CONTROLLER_PRIVATE_DATA
*Private
;
65 EFI_PCI_IO_PROTOCOL
*PciIo
;
68 if ((This
== NULL
) || (MmioBar
== NULL
)) {
69 return EFI_INVALID_PARAMETER
;
72 Private
= UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (This
);
73 PciIo
= Private
->PciIo
;
75 Status
= PciIo
->Pci
.Read (
78 PCI_BASE_ADDRESSREG_OFFSET
,
83 if (!EFI_ERROR (Status
)) {
84 *MmioBar
&= (UINTN
)~0xF;
90 Provides the UFS controller-specific addresses needed to access system memory.
92 @param This A pointer to the EFI_UFS_HOST_CONTROLLER_PROTOCOL instance.
93 @param Operation Indicates if the bus master is going to read or write to system memory.
94 @param HostAddress The system memory address to map to the UFS controller.
95 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
97 @param DeviceAddress The resulting map address for the bus master UFS controller to use to
98 access the hosts HostAddress.
99 @param Mapping A resulting value to pass to Unmap().
101 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
102 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
103 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
104 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
105 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
111 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*This
,
112 IN EDKII_UFS_HOST_CONTROLLER_OPERATION Operation
,
113 IN VOID
*HostAddress
,
114 IN OUT UINTN
*NumberOfBytes
,
115 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
119 UFS_HOST_CONTROLLER_PRIVATE_DATA
*Private
;
120 EFI_PCI_IO_PROTOCOL
*PciIo
;
123 if ((This
== NULL
) || (HostAddress
== NULL
) || (NumberOfBytes
== NULL
) || (DeviceAddress
== NULL
) || (Mapping
== NULL
)) {
124 return EFI_INVALID_PARAMETER
;
127 Private
= UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (This
);
128 PciIo
= Private
->PciIo
;
130 Status
= PciIo
->Map (PciIo
, Operation
, HostAddress
, NumberOfBytes
, DeviceAddress
, Mapping
);
135 Completes the Map() operation and releases any corresponding resources.
137 @param This A pointer to the EFI_UFS_HOST_CONTROLLER_PROTOCOL instance.
138 @param Mapping The mapping value returned from Map().
140 @retval EFI_SUCCESS The range was unmapped.
141 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
147 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*This
,
151 UFS_HOST_CONTROLLER_PRIVATE_DATA
*Private
;
152 EFI_PCI_IO_PROTOCOL
*PciIo
;
155 if ((This
== NULL
) || (Mapping
== NULL
)) {
156 return EFI_INVALID_PARAMETER
;
159 Private
= UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (This
);
160 PciIo
= Private
->PciIo
;
162 Status
= PciIo
->Unmap (PciIo
, Mapping
);
167 Allocates pages that are suitable for an EfiUfsHcOperationBusMasterCommonBuffer
170 @param This A pointer to the EFI_UFS_HOST_CONTROLLER_PROTOCOL instance.
171 @param Type This parameter is not used and must be ignored.
172 @param MemoryType The type of memory to allocate, EfiBootServicesData or
173 EfiRuntimeServicesData.
174 @param Pages The number of pages to allocate.
175 @param HostAddress A pointer to store the base system memory address of the
177 @param Attributes The requested bit mask of attributes for the allocated range.
179 @retval EFI_SUCCESS The requested memory pages were allocated.
180 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
181 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
182 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
183 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
188 UfsHcAllocateBuffer (
189 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*This
,
190 IN EFI_ALLOCATE_TYPE Type
,
191 IN EFI_MEMORY_TYPE MemoryType
,
193 OUT VOID
**HostAddress
,
197 UFS_HOST_CONTROLLER_PRIVATE_DATA
*Private
;
198 EFI_PCI_IO_PROTOCOL
*PciIo
;
201 if ((This
== NULL
) || (HostAddress
== NULL
)) {
202 return EFI_INVALID_PARAMETER
;
205 Private
= UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (This
);
206 PciIo
= Private
->PciIo
;
208 Status
= PciIo
->AllocateBuffer (PciIo
, Type
, MemoryType
, Pages
, HostAddress
, Attributes
);
213 Frees memory that was allocated with AllocateBuffer().
215 @param This A pointer to the EFI_UFS_HOST_CONTROLLER_PROTOCOL instance.
216 @param Pages The number of pages to free.
217 @param HostAddress The base system memory address of the allocated range.
219 @retval EFI_SUCCESS The requested memory pages were freed.
220 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
221 was not allocated with AllocateBuffer().
227 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*This
,
232 UFS_HOST_CONTROLLER_PRIVATE_DATA
*Private
;
233 EFI_PCI_IO_PROTOCOL
*PciIo
;
236 if ((This
== NULL
) || (HostAddress
== NULL
)) {
237 return EFI_INVALID_PARAMETER
;
240 Private
= UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (This
);
241 PciIo
= Private
->PciIo
;
243 Status
= PciIo
->FreeBuffer (PciIo
, Pages
, HostAddress
);
248 Flushes all posted write transactions from the UFS bus to attached UFS device.
250 @param This A pointer to the EFI_UFS_HOST_CONTROLLER_PROTOCOL instance.
252 @retval EFI_SUCCESS The posted write transactions were flushed from the UFS bus
253 to attached UFS device.
254 @retval EFI_DEVICE_ERROR The posted write transactions were not flushed from the UFS
255 bus to attached UFS device due to a hardware error.
261 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*This
264 UFS_HOST_CONTROLLER_PRIVATE_DATA
*Private
;
265 EFI_PCI_IO_PROTOCOL
*PciIo
;
268 Private
= UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (This
);
269 PciIo
= Private
->PciIo
;
271 Status
= PciIo
->Flush (PciIo
);
276 Tests to see if this driver supports a given controller. If a child device is provided,
277 it further tests to see if this driver supports creating a handle for the specified child device.
279 This function checks to see if the driver specified by This supports the device specified by
280 ControllerHandle. Drivers will typically use the device path attached to
281 ControllerHandle and/or the services from the bus I/O abstraction attached to
282 ControllerHandle to determine if the driver supports ControllerHandle. This function
283 may be called many times during platform initialization. In order to reduce boot times, the tests
284 performed by this function must be very small, and take as little time as possible to execute. This
285 function must not change the state of any hardware devices, and this function must be aware that the
286 device specified by ControllerHandle may already be managed by the same driver or a
287 different driver. This function must match its calls to AllocatePages() with FreePages(),
288 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
289 Since ControllerHandle may have been previously started by the same driver, if a protocol is
290 already in the opened state, then it must not be closed with CloseProtocol(). This is required
291 to guarantee the state of ControllerHandle is not modified by this function.
293 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
294 @param[in] ControllerHandle The handle of the controller to test. This handle
295 must support a protocol interface that supplies
296 an I/O abstraction to the driver.
297 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
298 parameter is ignored by device drivers, and is optional for bus
299 drivers. For bus drivers, if this parameter is not NULL, then
300 the bus driver must determine if the bus controller specified
301 by ControllerHandle and the child controller specified
302 by RemainingDevicePath are both supported by this
305 @retval EFI_SUCCESS The device specified by ControllerHandle and
306 RemainingDevicePath is supported by the driver specified by This.
307 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
308 RemainingDevicePath is already being managed by the driver
310 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
311 RemainingDevicePath is already being managed by a different
312 driver or an application that requires exclusive access.
313 Currently not implemented.
314 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
315 RemainingDevicePath is not supported by the driver specified by This.
319 UfsHcDriverBindingSupported (
320 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
321 IN EFI_HANDLE Controller
,
322 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
327 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
328 EFI_PCI_IO_PROTOCOL
*PciIo
;
332 ParentDevicePath
= NULL
;
336 // UfsHcDxe is a device driver, and should ingore the
337 // "RemainingDevicePath" according to EFI spec
339 Status
= gBS
->OpenProtocol (
341 &gEfiDevicePathProtocolGuid
,
342 (VOID
*) &ParentDevicePath
,
343 This
->DriverBindingHandle
,
345 EFI_OPEN_PROTOCOL_BY_DRIVER
347 if (EFI_ERROR (Status
)) {
349 // EFI_ALREADY_STARTED is also an error
354 // Close the protocol because we don't use it here
358 &gEfiDevicePathProtocolGuid
,
359 This
->DriverBindingHandle
,
364 // Now test the EfiPciIoProtocol
366 Status
= gBS
->OpenProtocol (
368 &gEfiPciIoProtocolGuid
,
370 This
->DriverBindingHandle
,
372 EFI_OPEN_PROTOCOL_BY_DRIVER
374 if (EFI_ERROR (Status
)) {
378 // Now further check the PCI header: Base class (offset 0x0B) and
379 // Sub Class (offset 0x0A). This controller should be an UFS controller
381 Status
= PciIo
->Pci
.Read (
388 if (EFI_ERROR (Status
)) {
391 &gEfiPciIoProtocolGuid
,
392 This
->DriverBindingHandle
,
395 return EFI_UNSUPPORTED
;
398 // Since we already got the PciData, we can close protocol to avoid to carry it on for multiple exit points.
402 &gEfiPciIoProtocolGuid
,
403 This
->DriverBindingHandle
,
408 // Examine UFS Host Controller PCI Configuration table fields
410 if (PciData
.Hdr
.ClassCode
[2] == PCI_CLASS_MASS_STORAGE
) {
411 if (PciData
.Hdr
.ClassCode
[1] == 0x09 ) { //UFS Controller Subclass
417 return EFI_UNSUPPORTED
;
425 Starts a device controller or a bus controller.
427 The Start() function is designed to be invoked from the EFI boot service ConnectController().
428 As a result, much of the error checking on the parameters to Start() has been moved into this
429 common boot service. It is legal to call Start() from other locations,
430 but the following calling restrictions must be followed or the system behavior will not be deterministic.
431 1. ControllerHandle must be a valid EFI_HANDLE.
432 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
433 EFI_DEVICE_PATH_PROTOCOL.
434 3. Prior to calling Start(), the Supported() function for the driver specified by This must
435 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
437 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
438 @param[in] ControllerHandle The handle of the controller to start. This handle
439 must support a protocol interface that supplies
440 an I/O abstraction to the driver.
441 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
442 parameter is ignored by device drivers, and is optional for bus
443 drivers. For a bus driver, if this parameter is NULL, then handles
444 for all the children of Controller are created by this driver.
445 If this parameter is not NULL and the first Device Path Node is
446 not the End of Device Path Node, then only the handle for the
447 child device specified by the first Device Path Node of
448 RemainingDevicePath is created by this driver.
449 If the first Device Path Node of RemainingDevicePath is
450 the End of Device Path Node, no child handle is created by this
453 @retval EFI_SUCCESS The device was started.
454 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
455 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
456 @retval Others The driver failded to start the device.
461 UfsHcDriverBindingStart (
462 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
463 IN EFI_HANDLE Controller
,
464 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
468 EFI_PCI_IO_PROTOCOL
*PciIo
;
469 UFS_HOST_CONTROLLER_PRIVATE_DATA
*Private
;
477 // Now test and open the EfiPciIoProtocol
479 Status
= gBS
->OpenProtocol (
481 &gEfiPciIoProtocolGuid
,
483 This
->DriverBindingHandle
,
485 EFI_OPEN_PROTOCOL_BY_DRIVER
488 // Status == 0 - A normal execution flow, SUCCESS and the program proceeds.
489 // Status == ALREADY_STARTED - A non-zero Status code returned. It indicates
490 // that the protocol has been opened and should be treated as a
491 // normal condition and the program proceeds. The Protocol will not
492 // opened 'again' by this call.
493 // Status != ALREADY_STARTED - Error status, terminate program execution
495 if (EFI_ERROR (Status
)) {
497 // EFI_ALREADY_STARTED is also an error
502 Private
= AllocateCopyPool (sizeof (UFS_HOST_CONTROLLER_PRIVATE_DATA
), &gUfsHcTemplate
);
503 if (Private
== NULL
) {
504 Status
= EFI_OUT_OF_RESOURCES
;
508 Private
->PciIo
= PciIo
;
510 Status
= PciIo
->Attributes (
512 EfiPciIoAttributeOperationGet
,
514 &Private
->PciAttributes
517 if (EFI_ERROR (Status
)) {
521 Status
= PciIo
->Attributes (
523 EfiPciIoAttributeOperationSupported
,
528 if (!EFI_ERROR (Status
)) {
529 Supports
&= (UINT64
)EFI_PCI_DEVICE_ENABLE
;
530 Status
= PciIo
->Attributes (
532 EfiPciIoAttributeOperationEnable
,
541 /// Install UFS_HOST_CONTROLLER protocol
543 Status
= gBS
->InstallProtocolInterface (
545 &gEdkiiUfsHostControllerProtocolGuid
,
546 EFI_NATIVE_INTERFACE
,
547 (VOID
*)&(Private
->UfsHc
)
551 if (EFI_ERROR (Status
)) {
552 if ((Private
!= NULL
) && (Private
->PciAttributes
!= 0)) {
554 // Restore original PCI attributes
556 Status
= PciIo
->Attributes (
558 EfiPciIoAttributeOperationSet
,
559 Private
->PciAttributes
,
562 ASSERT_EFI_ERROR (Status
);
566 &gEfiPciIoProtocolGuid
,
567 This
->DriverBindingHandle
,
570 if (Private
!= NULL
) {
580 Stops a device controller or a bus controller.
582 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
583 As a result, much of the error checking on the parameters to Stop() has been moved
584 into this common boot service. It is legal to call Stop() from other locations,
585 but the following calling restrictions must be followed or the system behavior will not be deterministic.
586 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
587 same driver's Start() function.
588 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
589 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
590 Start() function, and the Start() function must have called OpenProtocol() on
591 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
593 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
594 @param[in] ControllerHandle A handle to the device being stopped. The handle must
595 support a bus specific I/O protocol for the driver
596 to use to stop the device.
597 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
598 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
599 if NumberOfChildren is 0.
601 @retval EFI_SUCCESS The device was stopped.
602 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
607 UfsHcDriverBindingStop (
608 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
609 IN EFI_HANDLE Controller
,
610 IN UINTN NumberOfChildren
,
611 IN EFI_HANDLE
*ChildHandleBuffer
615 UFS_HOST_CONTROLLER_PRIVATE_DATA
*Private
;
616 EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*UfsHc
;
621 Status
= gBS
->OpenProtocol (
623 &gEdkiiUfsHostControllerProtocolGuid
,
625 This
->DriverBindingHandle
,
627 EFI_OPEN_PROTOCOL_GET_PROTOCOL
630 if (EFI_ERROR (Status
)) {
631 return EFI_DEVICE_ERROR
;
634 Private
= UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (UfsHc
);
636 Status
= gBS
->UninstallProtocolInterface (
638 &gEdkiiUfsHostControllerProtocolGuid
,
641 if (!EFI_ERROR (Status
)) {
643 // Restore original PCI attributes
645 Status
= Private
->PciIo
->Attributes (
647 EfiPciIoAttributeOperationSet
,
648 Private
->PciAttributes
,
651 ASSERT_EFI_ERROR (Status
);
654 // Close protocols opened by UFS host controller driver
658 &gEfiPciIoProtocolGuid
,
659 This
->DriverBindingHandle
,
670 The entry point for UFS host controller driver, used to install this driver on the ImageHandle.
672 @param[in] ImageHandle The firmware allocated handle for this driver image.
673 @param[in] SystemTable Pointer to the EFI system table.
675 @retval EFI_SUCCESS Driver loaded.
676 @retval other Driver not loaded.
682 IN EFI_HANDLE ImageHandle
,
683 IN EFI_SYSTEM_TABLE
*SystemTable
688 Status
= EfiLibInstallDriverBindingComponentName2 (
691 &gUfsHcDriverBinding
,
693 &gUfsHcComponentName
,
694 &gUfsHcComponentName2
696 ASSERT_EFI_ERROR (Status
);