]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Pci/UfsPciHcDxe/UfsPciHcDxe.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / UfsPciHcDxe / UfsPciHcDxe.c
1 /** @file
2 UfsHcDxe driver is used to provide platform-dependent info, mainly UFS host controller
3 MMIO base, to upper layer UFS drivers.
4
5 Copyright (c) 2014 - 2018, 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.
10
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.
13
14 **/
15
16 #include "UfsPciHcDxe.h"
17
18 //
19 // NVM Express Driver Binding Protocol Instance
20 //
21 EFI_DRIVER_BINDING_PROTOCOL gUfsHcDriverBinding = {
22 UfsHcDriverBindingSupported,
23 UfsHcDriverBindingStart,
24 UfsHcDriverBindingStop,
25 0x10,
26 NULL,
27 NULL
28 };
29
30 //
31 // Template for Ufs host controller private data.
32 //
33 UFS_HOST_CONTROLLER_PRIVATE_DATA gUfsHcTemplate = {
34 UFS_HC_PRIVATE_DATA_SIGNATURE, // Signature
35 { // UfsHcProtocol
36 UfsHcGetMmioBar,
37 UfsHcAllocateBuffer,
38 UfsHcFreeBuffer,
39 UfsHcMap,
40 UfsHcUnmap,
41 UfsHcFlush,
42 UfsHcMmioRead,
43 UfsHcMmioWrite
44 },
45 NULL, // PciIo
46 0, // BarIndex
47 0 // PciAttributes
48 };
49
50 /**
51 Get the MMIO base of the UFS host controller.
52
53 @param[in] This A pointer to the EFI_UFS_HOST_CONTROLLER_PROTOCOL instance.
54 @param[out] MmioBar The MMIO base address of UFS host controller.
55
56 @retval EFI_SUCCESS The operation succeeds.
57 @retval others The operation fails.
58 **/
59 EFI_STATUS
60 EFIAPI
61 UfsHcGetMmioBar (
62 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL *This,
63 OUT UINTN *MmioBar
64 )
65 {
66 UFS_HOST_CONTROLLER_PRIVATE_DATA *Private;
67 EFI_PCI_IO_PROTOCOL *PciIo;
68 EFI_STATUS Status;
69 UINT8 BarIndex;
70 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *BarDesc;
71
72 if ((This == NULL) || (MmioBar == NULL)) {
73 return EFI_INVALID_PARAMETER;
74 }
75
76 BarDesc = NULL;
77 Private = UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (This);
78 PciIo = Private->PciIo;
79 BarIndex = Private->BarIndex;
80
81 Status = PciIo->GetBarAttributes (
82 PciIo,
83 BarIndex,
84 NULL,
85 (VOID**) &BarDesc
86 );
87 if (EFI_ERROR (Status)) {
88 return Status;
89 }
90
91 *MmioBar = (UINTN)BarDesc->AddrRangeMin;
92
93 FreePool (BarDesc);
94
95 return Status;
96 }
97
98 /**
99 Provides the UFS controller-specific addresses needed to access system memory.
100
101 @param This A pointer to the EFI_UFS_HOST_CONTROLLER_PROTOCOL instance.
102 @param Operation Indicates if the bus master is going to read or write to system memory.
103 @param HostAddress The system memory address to map to the UFS controller.
104 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
105 that were mapped.
106 @param DeviceAddress The resulting map address for the bus master UFS controller to use to
107 access the hosts HostAddress.
108 @param Mapping A resulting value to pass to Unmap().
109
110 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
111 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
112 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
113 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
114 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
115
116 **/
117 EFI_STATUS
118 EFIAPI
119 UfsHcMap (
120 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL *This,
121 IN EDKII_UFS_HOST_CONTROLLER_OPERATION Operation,
122 IN VOID *HostAddress,
123 IN OUT UINTN *NumberOfBytes,
124 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
125 OUT VOID **Mapping
126 )
127 {
128 UFS_HOST_CONTROLLER_PRIVATE_DATA *Private;
129 EFI_PCI_IO_PROTOCOL *PciIo;
130 EFI_STATUS Status;
131
132 if ((This == NULL) || (HostAddress == NULL) || (NumberOfBytes == NULL) || (DeviceAddress == NULL) || (Mapping == NULL)) {
133 return EFI_INVALID_PARAMETER;
134 }
135
136 Private = UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (This);
137 PciIo = Private->PciIo;
138
139 Status = PciIo->Map (PciIo, (EFI_PCI_IO_PROTOCOL_OPERATION)Operation, HostAddress, NumberOfBytes, DeviceAddress, Mapping);
140 return Status;
141 }
142
143 /**
144 Completes the Map() operation and releases any corresponding resources.
145
146 @param This A pointer to the EFI_UFS_HOST_CONTROLLER_PROTOCOL instance.
147 @param Mapping The mapping value returned from Map().
148
149 @retval EFI_SUCCESS The range was unmapped.
150 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
151
152 **/
153 EFI_STATUS
154 EFIAPI
155 UfsHcUnmap (
156 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL *This,
157 IN VOID *Mapping
158 )
159 {
160 UFS_HOST_CONTROLLER_PRIVATE_DATA *Private;
161 EFI_PCI_IO_PROTOCOL *PciIo;
162 EFI_STATUS Status;
163
164 if ((This == NULL) || (Mapping == NULL)) {
165 return EFI_INVALID_PARAMETER;
166 }
167
168 Private = UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (This);
169 PciIo = Private->PciIo;
170
171 Status = PciIo->Unmap (PciIo, Mapping);
172 return Status;
173 }
174
175 /**
176 Allocates pages that are suitable for an EfiUfsHcOperationBusMasterCommonBuffer
177 mapping.
178
179 @param This A pointer to the EFI_UFS_HOST_CONTROLLER_PROTOCOL instance.
180 @param Type This parameter is not used and must be ignored.
181 @param MemoryType The type of memory to allocate, EfiBootServicesData or
182 EfiRuntimeServicesData.
183 @param Pages The number of pages to allocate.
184 @param HostAddress A pointer to store the base system memory address of the
185 allocated range.
186 @param Attributes The requested bit mask of attributes for the allocated range.
187
188 @retval EFI_SUCCESS The requested memory pages were allocated.
189 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
190 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
191 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
192 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
193
194 **/
195 EFI_STATUS
196 EFIAPI
197 UfsHcAllocateBuffer (
198 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL *This,
199 IN EFI_ALLOCATE_TYPE Type,
200 IN EFI_MEMORY_TYPE MemoryType,
201 IN UINTN Pages,
202 OUT VOID **HostAddress,
203 IN UINT64 Attributes
204 )
205 {
206 UFS_HOST_CONTROLLER_PRIVATE_DATA *Private;
207 EFI_PCI_IO_PROTOCOL *PciIo;
208 EFI_STATUS Status;
209
210 if ((This == NULL) || (HostAddress == NULL)) {
211 return EFI_INVALID_PARAMETER;
212 }
213
214 Private = UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (This);
215 PciIo = Private->PciIo;
216
217 Status = PciIo->AllocateBuffer (PciIo, Type, MemoryType, Pages, HostAddress, Attributes);
218 return Status;
219 }
220
221 /**
222 Frees memory that was allocated with AllocateBuffer().
223
224 @param This A pointer to the EFI_UFS_HOST_CONTROLLER_PROTOCOL instance.
225 @param Pages The number of pages to free.
226 @param HostAddress The base system memory address of the allocated range.
227
228 @retval EFI_SUCCESS The requested memory pages were freed.
229 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
230 was not allocated with AllocateBuffer().
231
232 **/
233 EFI_STATUS
234 EFIAPI
235 UfsHcFreeBuffer (
236 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL *This,
237 IN UINTN Pages,
238 IN VOID *HostAddress
239 )
240 {
241 UFS_HOST_CONTROLLER_PRIVATE_DATA *Private;
242 EFI_PCI_IO_PROTOCOL *PciIo;
243 EFI_STATUS Status;
244
245 if ((This == NULL) || (HostAddress == NULL)) {
246 return EFI_INVALID_PARAMETER;
247 }
248
249 Private = UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (This);
250 PciIo = Private->PciIo;
251
252 Status = PciIo->FreeBuffer (PciIo, Pages, HostAddress);
253 return Status;
254 }
255
256 /**
257 Flushes all posted write transactions from the UFS bus to attached UFS device.
258
259 @param This A pointer to the EFI_UFS_HOST_CONTROLLER_PROTOCOL instance.
260
261 @retval EFI_SUCCESS The posted write transactions were flushed from the UFS bus
262 to attached UFS device.
263 @retval EFI_DEVICE_ERROR The posted write transactions were not flushed from the UFS
264 bus to attached UFS device due to a hardware error.
265
266 **/
267 EFI_STATUS
268 EFIAPI
269 UfsHcFlush (
270 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL *This
271 )
272 {
273 UFS_HOST_CONTROLLER_PRIVATE_DATA *Private;
274 EFI_PCI_IO_PROTOCOL *PciIo;
275 EFI_STATUS Status;
276
277 Private = UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (This);
278 PciIo = Private->PciIo;
279
280 Status = PciIo->Flush (PciIo);
281 return Status;
282 }
283
284 /**
285 Enable a UFS bus driver to access UFS MMIO registers in the UFS Host Controller memory space.
286
287 @param This A pointer to the EDKII_UFS_HOST_CONTROLLER_PROTOCOL instance.
288 @param Width Signifies the width of the memory operations.
289 @param Offset The offset within the UFS Host Controller MMIO space to start the
290 memory operation.
291 @param Count The number of memory operations to perform.
292 @param Buffer For read operations, the destination buffer to store the results.
293 For write operations, the source buffer to write data from.
294
295 @retval EFI_SUCCESS The data was read from or written to the UFS host controller.
296 @retval EFI_UNSUPPORTED The address range specified by Offset, Width, and Count is not
297 valid for the UFS Host Controller memory space.
298 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
299 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
300
301 **/
302 EFI_STATUS
303 EFIAPI
304 UfsHcMmioRead (
305 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL *This,
306 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL_WIDTH Width,
307 IN UINT64 Offset,
308 IN UINTN Count,
309 IN OUT VOID *Buffer
310 )
311 {
312 UFS_HOST_CONTROLLER_PRIVATE_DATA *Private;
313 EFI_PCI_IO_PROTOCOL *PciIo;
314 EFI_STATUS Status;
315 UINT8 BarIndex;
316
317 Private = UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (This);
318 PciIo = Private->PciIo;
319 BarIndex = Private->BarIndex;
320
321 Status = PciIo->Mem.Read (PciIo, (EFI_PCI_IO_PROTOCOL_WIDTH)Width, BarIndex, Offset, Count, Buffer);
322
323 return Status;
324 }
325
326 /**
327 Enable a UFS bus driver to access UFS MMIO registers in the UFS Host Controller memory space.
328
329 @param This A pointer to the EDKII_UFS_HOST_CONTROLLER_PROTOCOL instance.
330 @param Width Signifies the width of the memory operations.
331 @param Offset The offset within the UFS Host Controller MMIO space to start the
332 memory operation.
333 @param Count The number of memory operations to perform.
334 @param Buffer For read operations, the destination buffer to store the results.
335 For write operations, the source buffer to write data from.
336
337 @retval EFI_SUCCESS The data was read from or written to the UFS host controller.
338 @retval EFI_UNSUPPORTED The address range specified by Offset, Width, and Count is not
339 valid for the UFS Host Controller memory space.
340 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
341 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
342
343 **/
344 EFI_STATUS
345 EFIAPI
346 UfsHcMmioWrite (
347 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL *This,
348 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL_WIDTH Width,
349 IN UINT64 Offset,
350 IN UINTN Count,
351 IN OUT VOID *Buffer
352 )
353 {
354 UFS_HOST_CONTROLLER_PRIVATE_DATA *Private;
355 EFI_PCI_IO_PROTOCOL *PciIo;
356 EFI_STATUS Status;
357 UINT8 BarIndex;
358
359 Private = UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (This);
360 PciIo = Private->PciIo;
361 BarIndex = Private->BarIndex;
362
363 Status = PciIo->Mem.Write (PciIo, (EFI_PCI_IO_PROTOCOL_WIDTH)Width, BarIndex, Offset, Count, Buffer);
364
365 return Status;
366 }
367
368 /**
369 Tests to see if this driver supports a given controller. If a child device is provided,
370 it further tests to see if this driver supports creating a handle for the specified child device.
371
372 This function checks to see if the driver specified by This supports the device specified by
373 ControllerHandle. Drivers will typically use the device path attached to
374 ControllerHandle and/or the services from the bus I/O abstraction attached to
375 ControllerHandle to determine if the driver supports ControllerHandle. This function
376 may be called many times during platform initialization. In order to reduce boot times, the tests
377 performed by this function must be very small, and take as little time as possible to execute. This
378 function must not change the state of any hardware devices, and this function must be aware that the
379 device specified by ControllerHandle may already be managed by the same driver or a
380 different driver. This function must match its calls to AllocatePages() with FreePages(),
381 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
382 Since ControllerHandle may have been previously started by the same driver, if a protocol is
383 already in the opened state, then it must not be closed with CloseProtocol(). This is required
384 to guarantee the state of ControllerHandle is not modified by this function.
385
386 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
387 @param[in] ControllerHandle The handle of the controller to test. This handle
388 must support a protocol interface that supplies
389 an I/O abstraction to the driver.
390 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
391 parameter is ignored by device drivers, and is optional for bus
392 drivers. For bus drivers, if this parameter is not NULL, then
393 the bus driver must determine if the bus controller specified
394 by ControllerHandle and the child controller specified
395 by RemainingDevicePath are both supported by this
396 bus driver.
397
398 @retval EFI_SUCCESS The device specified by ControllerHandle and
399 RemainingDevicePath is supported by the driver specified by This.
400 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
401 RemainingDevicePath is already being managed by the driver
402 specified by This.
403 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
404 RemainingDevicePath is already being managed by a different
405 driver or an application that requires exclusive access.
406 Currently not implemented.
407 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
408 RemainingDevicePath is not supported by the driver specified by This.
409 **/
410 EFI_STATUS
411 EFIAPI
412 UfsHcDriverBindingSupported (
413 IN EFI_DRIVER_BINDING_PROTOCOL *This,
414 IN EFI_HANDLE Controller,
415 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
416 )
417 {
418 EFI_STATUS Status;
419 BOOLEAN UfsHcFound;
420 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
421 EFI_PCI_IO_PROTOCOL *PciIo;
422 PCI_TYPE00 PciData;
423
424 PciIo = NULL;
425 ParentDevicePath = NULL;
426 UfsHcFound = FALSE;
427
428 //
429 // UfsHcDxe is a device driver, and should ingore the
430 // "RemainingDevicePath" according to EFI spec
431 //
432 Status = gBS->OpenProtocol (
433 Controller,
434 &gEfiDevicePathProtocolGuid,
435 (VOID *) &ParentDevicePath,
436 This->DriverBindingHandle,
437 Controller,
438 EFI_OPEN_PROTOCOL_BY_DRIVER
439 );
440 if (EFI_ERROR (Status)) {
441 //
442 // EFI_ALREADY_STARTED is also an error
443 //
444 return Status;
445 }
446 //
447 // Close the protocol because we don't use it here
448 //
449 gBS->CloseProtocol (
450 Controller,
451 &gEfiDevicePathProtocolGuid,
452 This->DriverBindingHandle,
453 Controller
454 );
455
456 //
457 // Now test the EfiPciIoProtocol
458 //
459 Status = gBS->OpenProtocol (
460 Controller,
461 &gEfiPciIoProtocolGuid,
462 (VOID **) &PciIo,
463 This->DriverBindingHandle,
464 Controller,
465 EFI_OPEN_PROTOCOL_BY_DRIVER
466 );
467 if (EFI_ERROR (Status)) {
468 return Status;
469 }
470 //
471 // Now further check the PCI header: Base class (offset 0x0B) and
472 // Sub Class (offset 0x0A). This controller should be an UFS controller
473 //
474 Status = PciIo->Pci.Read (
475 PciIo,
476 EfiPciIoWidthUint8,
477 0,
478 sizeof (PciData),
479 &PciData
480 );
481 if (EFI_ERROR (Status)) {
482 gBS->CloseProtocol (
483 Controller,
484 &gEfiPciIoProtocolGuid,
485 This->DriverBindingHandle,
486 Controller
487 );
488 return EFI_UNSUPPORTED;
489 }
490 //
491 // Since we already got the PciData, we can close protocol to avoid to carry it on for multiple exit points.
492 //
493 gBS->CloseProtocol (
494 Controller,
495 &gEfiPciIoProtocolGuid,
496 This->DriverBindingHandle,
497 Controller
498 );
499
500 //
501 // Examine UFS Host Controller PCI Configuration table fields
502 //
503 if (PciData.Hdr.ClassCode[2] == PCI_CLASS_MASS_STORAGE) {
504 if (PciData.Hdr.ClassCode[1] == 0x09 ) { //UFS Controller Subclass
505 UfsHcFound = TRUE;
506 }
507 }
508
509 if (!UfsHcFound) {
510 return EFI_UNSUPPORTED;
511 }
512
513 return Status;
514 }
515
516
517 /**
518 Starts a device controller or a bus controller.
519
520 The Start() function is designed to be invoked from the EFI boot service ConnectController().
521 As a result, much of the error checking on the parameters to Start() has been moved into this
522 common boot service. It is legal to call Start() from other locations,
523 but the following calling restrictions must be followed or the system behavior will not be deterministic.
524 1. ControllerHandle must be a valid EFI_HANDLE.
525 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
526 EFI_DEVICE_PATH_PROTOCOL.
527 3. Prior to calling Start(), the Supported() function for the driver specified by This must
528 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
529
530 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
531 @param[in] ControllerHandle The handle of the controller to start. This handle
532 must support a protocol interface that supplies
533 an I/O abstraction to the driver.
534 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
535 parameter is ignored by device drivers, and is optional for bus
536 drivers. For a bus driver, if this parameter is NULL, then handles
537 for all the children of Controller are created by this driver.
538 If this parameter is not NULL and the first Device Path Node is
539 not the End of Device Path Node, then only the handle for the
540 child device specified by the first Device Path Node of
541 RemainingDevicePath is created by this driver.
542 If the first Device Path Node of RemainingDevicePath is
543 the End of Device Path Node, no child handle is created by this
544 driver.
545
546 @retval EFI_SUCCESS The device was started.
547 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
548 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
549 @retval Others The driver failded to start the device.
550
551 **/
552 EFI_STATUS
553 EFIAPI
554 UfsHcDriverBindingStart (
555 IN EFI_DRIVER_BINDING_PROTOCOL *This,
556 IN EFI_HANDLE Controller,
557 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
558 )
559 {
560 EFI_STATUS Status;
561 EFI_PCI_IO_PROTOCOL *PciIo;
562 UFS_HOST_CONTROLLER_PRIVATE_DATA *Private;
563 UINT64 Supports;
564 UINT8 BarIndex;
565 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *BarDesc;
566
567 PciIo = NULL;
568 Private = NULL;
569 Supports = 0;
570 BarDesc = NULL;
571
572 //
573 // Now test and open the EfiPciIoProtocol
574 //
575 Status = gBS->OpenProtocol (
576 Controller,
577 &gEfiPciIoProtocolGuid,
578 (VOID **) &PciIo,
579 This->DriverBindingHandle,
580 Controller,
581 EFI_OPEN_PROTOCOL_BY_DRIVER
582 );
583 //
584 // Status == 0 - A normal execution flow, SUCCESS and the program proceeds.
585 // Status == ALREADY_STARTED - A non-zero Status code returned. It indicates
586 // that the protocol has been opened and should be treated as a
587 // normal condition and the program proceeds. The Protocol will not
588 // opened 'again' by this call.
589 // Status != ALREADY_STARTED - Error status, terminate program execution
590 //
591 if (EFI_ERROR (Status)) {
592 //
593 // EFI_ALREADY_STARTED is also an error
594 //
595 return Status;
596 }
597
598 Private = AllocateCopyPool (sizeof (UFS_HOST_CONTROLLER_PRIVATE_DATA), &gUfsHcTemplate);
599 if (Private == NULL) {
600 Status = EFI_OUT_OF_RESOURCES;
601 goto Done;
602 }
603
604 Private->PciIo = PciIo;
605
606 for (BarIndex = 0; BarIndex < PCI_MAX_BAR; BarIndex++) {
607 Status = PciIo->GetBarAttributes (
608 PciIo,
609 BarIndex,
610 NULL,
611 (VOID**) &BarDesc
612 );
613 if (Status == EFI_UNSUPPORTED) {
614 continue;
615 } else if (EFI_ERROR (Status)) {
616 goto Done;
617 }
618
619 if (BarDesc->ResType == ACPI_ADDRESS_SPACE_TYPE_MEM) {
620 Private->BarIndex = BarIndex;
621 FreePool (BarDesc);
622 break;
623 }
624
625 FreePool (BarDesc);
626 }
627
628 Status = PciIo->Attributes (
629 PciIo,
630 EfiPciIoAttributeOperationGet,
631 0,
632 &Private->PciAttributes
633 );
634
635 if (EFI_ERROR (Status)) {
636 goto Done;
637 }
638
639 Status = PciIo->Attributes (
640 PciIo,
641 EfiPciIoAttributeOperationSupported,
642 0,
643 &Supports
644 );
645
646 if (!EFI_ERROR (Status)) {
647 Supports &= (UINT64)EFI_PCI_DEVICE_ENABLE;
648 Status = PciIo->Attributes (
649 PciIo,
650 EfiPciIoAttributeOperationEnable,
651 Supports,
652 NULL
653 );
654 } else {
655 goto Done;
656 }
657
658 ///
659 /// Install UFS_HOST_CONTROLLER protocol
660 ///
661 Status = gBS->InstallProtocolInterface (
662 &Controller,
663 &gEdkiiUfsHostControllerProtocolGuid,
664 EFI_NATIVE_INTERFACE,
665 (VOID*)&(Private->UfsHc)
666 );
667
668 Done:
669 if (EFI_ERROR (Status)) {
670 if ((Private != NULL) && (Private->PciAttributes != 0)) {
671 //
672 // Restore original PCI attributes
673 //
674 PciIo->Attributes (
675 PciIo,
676 EfiPciIoAttributeOperationSet,
677 Private->PciAttributes,
678 NULL
679 );
680 }
681 gBS->CloseProtocol (
682 Controller,
683 &gEfiPciIoProtocolGuid,
684 This->DriverBindingHandle,
685 Controller
686 );
687 if (Private != NULL) {
688 FreePool (Private);
689 }
690 }
691
692 return Status;
693 }
694
695
696 /**
697 Stops a device controller or a bus controller.
698
699 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
700 As a result, much of the error checking on the parameters to Stop() has been moved
701 into this common boot service. It is legal to call Stop() from other locations,
702 but the following calling restrictions must be followed or the system behavior will not be deterministic.
703 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
704 same driver's Start() function.
705 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
706 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
707 Start() function, and the Start() function must have called OpenProtocol() on
708 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
709
710 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
711 @param[in] ControllerHandle A handle to the device being stopped. The handle must
712 support a bus specific I/O protocol for the driver
713 to use to stop the device.
714 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
715 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
716 if NumberOfChildren is 0.
717
718 @retval EFI_SUCCESS The device was stopped.
719 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
720
721 **/
722 EFI_STATUS
723 EFIAPI
724 UfsHcDriverBindingStop (
725 IN EFI_DRIVER_BINDING_PROTOCOL *This,
726 IN EFI_HANDLE Controller,
727 IN UINTN NumberOfChildren,
728 IN EFI_HANDLE *ChildHandleBuffer
729 )
730 {
731 EFI_STATUS Status;
732 UFS_HOST_CONTROLLER_PRIVATE_DATA *Private;
733 EDKII_UFS_HOST_CONTROLLER_PROTOCOL *UfsHc;
734
735 ///
736 /// Get private data
737 ///
738 Status = gBS->OpenProtocol (
739 Controller,
740 &gEdkiiUfsHostControllerProtocolGuid,
741 (VOID **) &UfsHc,
742 This->DriverBindingHandle,
743 Controller,
744 EFI_OPEN_PROTOCOL_GET_PROTOCOL
745 );
746
747 if (EFI_ERROR (Status)) {
748 return EFI_DEVICE_ERROR;
749 }
750
751 Private = UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (UfsHc);
752
753 Status = gBS->UninstallProtocolInterface (
754 Controller,
755 &gEdkiiUfsHostControllerProtocolGuid,
756 &(Private->UfsHc)
757 );
758 if (!EFI_ERROR (Status)) {
759 //
760 // Restore original PCI attributes
761 //
762 Status = Private->PciIo->Attributes (
763 Private->PciIo,
764 EfiPciIoAttributeOperationSet,
765 Private->PciAttributes,
766 NULL
767 );
768 ASSERT_EFI_ERROR (Status);
769
770 //
771 // Close protocols opened by UFS host controller driver
772 //
773 gBS->CloseProtocol (
774 Controller,
775 &gEfiPciIoProtocolGuid,
776 This->DriverBindingHandle,
777 Controller
778 );
779
780 FreePool (Private);
781 }
782
783 return Status;
784 }
785
786 /**
787 The entry point for UFS host controller driver, used to install this driver on the ImageHandle.
788
789 @param[in] ImageHandle The firmware allocated handle for this driver image.
790 @param[in] SystemTable Pointer to the EFI system table.
791
792 @retval EFI_SUCCESS Driver loaded.
793 @retval other Driver not loaded.
794
795 **/
796 EFI_STATUS
797 EFIAPI
798 UfsHcDriverEntry (
799 IN EFI_HANDLE ImageHandle,
800 IN EFI_SYSTEM_TABLE *SystemTable
801 )
802 {
803 EFI_STATUS Status;
804
805 Status = EfiLibInstallDriverBindingComponentName2 (
806 ImageHandle,
807 SystemTable,
808 &gUfsHcDriverBinding,
809 ImageHandle,
810 &gUfsHcComponentName,
811 &gUfsHcComponentName2
812 );
813 ASSERT_EFI_ERROR (Status);
814
815 return Status;
816 }