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