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