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