]>
Commit | Line | Data |
---|---|---|
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 | |
15 | EFI_DRIVER_BINDING_PROTOCOL gUfsHcDriverBinding = {\r | |
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 | |
27 | UFS_HOST_CONTROLLER_PRIVATE_DATA gUfsHcTemplate = {\r | |
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 | |
53 | EFI_STATUS\r | |
54 | EFIAPI\r | |
55 | UfsHcGetMmioBar (\r | |
56 | IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL *This,\r | |
57 | OUT UINTN *MmioBar\r | |
58 | )\r | |
59 | {\r | |
60 | UFS_HOST_CONTROLLER_PRIVATE_DATA *Private;\r | |
61 | EFI_PCI_IO_PROTOCOL *PciIo;\r | |
62 | EFI_STATUS Status;\r | |
095f0779 FT |
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 | |
79 | (VOID**) &BarDesc\r | |
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 |
111 | EFI_STATUS\r | |
112 | EFIAPI\r | |
113 | UfsHcMap (\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 | |
118 | OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,\r | |
119 | OUT VOID **Mapping\r | |
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 | |
4c31caef | 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 |
147 | EFI_STATUS\r | |
148 | EFIAPI\r | |
149 | UfsHcUnmap (\r | |
150 | IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL *This,\r | |
151 | IN VOID *Mapping\r | |
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 | |
165 | Status = PciIo->Unmap (PciIo, Mapping);\r | |
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 |
189 | EFI_STATUS\r | |
190 | EFIAPI\r | |
191 | UfsHcAllocateBuffer (\r | |
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 | |
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 | |
211 | Status = PciIo->AllocateBuffer (PciIo, Type, MemoryType, Pages, HostAddress, Attributes);\r | |
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 |
227 | EFI_STATUS\r | |
228 | EFIAPI\r | |
229 | UfsHcFreeBuffer (\r | |
230 | IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL *This,\r | |
231 | IN UINTN Pages,\r | |
232 | IN VOID *HostAddress\r | |
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 | |
246 | Status = PciIo->FreeBuffer (PciIo, Pages, HostAddress);\r | |
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 |
261 | EFI_STATUS\r | |
262 | EFIAPI\r | |
263 | UfsHcFlush (\r | |
264 | IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL *This\r | |
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 | |
274 | Status = PciIo->Flush (PciIo);\r | |
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 | |
296 | EFI_STATUS\r | |
297 | EFIAPI\r | |
298 | UfsHcMmioRead (\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 | |
315 | Status = PciIo->Mem.Read (PciIo, (EFI_PCI_IO_PROTOCOL_WIDTH)Width, BarIndex, Offset, Count, Buffer);\r | |
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 | |
338 | EFI_STATUS\r | |
339 | EFIAPI\r | |
340 | UfsHcMmioWrite (\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 | |
357 | Status = PciIo->Mem.Write (PciIo, (EFI_PCI_IO_PROTOCOL_WIDTH)Width, BarIndex, Offset, Count, Buffer);\r | |
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 | |
404 | EFI_STATUS\r | |
405 | EFIAPI\r | |
406 | UfsHcDriverBindingSupported (\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 | |
429 | (VOID *) &ParentDevicePath,\r | |
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 | |
440 | //\r | |
441 | // Close the protocol because we don't use it here\r | |
442 | //\r | |
443 | gBS->CloseProtocol (\r | |
444 | Controller,\r | |
445 | &gEfiDevicePathProtocolGuid,\r | |
446 | This->DriverBindingHandle,\r | |
447 | Controller\r | |
448 | );\r | |
449 | \r | |
450 | //\r | |
451 | // Now test the EfiPciIoProtocol\r | |
452 | //\r | |
453 | Status = gBS->OpenProtocol (\r | |
454 | Controller,\r | |
455 | &gEfiPciIoProtocolGuid,\r | |
456 | (VOID **) &PciIo,\r | |
457 | This->DriverBindingHandle,\r | |
458 | Controller,\r | |
459 | EFI_OPEN_PROTOCOL_BY_DRIVER\r | |
460 | );\r | |
461 | if (EFI_ERROR (Status)) {\r | |
462 | return Status;\r | |
463 | }\r | |
464 | //\r | |
465 | // Now further check the PCI header: Base class (offset 0x0B) and\r | |
466 | // Sub Class (offset 0x0A). This controller should be an UFS controller\r | |
467 | //\r | |
468 | Status = PciIo->Pci.Read (\r | |
469 | PciIo,\r | |
470 | EfiPciIoWidthUint8,\r | |
471 | 0,\r | |
472 | sizeof (PciData),\r | |
473 | &PciData\r | |
474 | );\r | |
475 | if (EFI_ERROR (Status)) {\r | |
476 | gBS->CloseProtocol (\r | |
477 | Controller,\r | |
478 | &gEfiPciIoProtocolGuid,\r | |
479 | This->DriverBindingHandle,\r | |
480 | Controller\r | |
481 | );\r | |
482 | return EFI_UNSUPPORTED;\r | |
483 | }\r | |
484 | //\r | |
485 | // Since we already got the PciData, we can close protocol to avoid to carry it on for multiple exit points.\r | |
486 | //\r | |
487 | gBS->CloseProtocol (\r | |
488 | Controller,\r | |
489 | &gEfiPciIoProtocolGuid,\r | |
490 | This->DriverBindingHandle,\r | |
491 | Controller\r | |
492 | );\r | |
493 | \r | |
494 | //\r | |
495 | // Examine UFS Host Controller PCI Configuration table fields\r | |
496 | //\r | |
497 | if (PciData.Hdr.ClassCode[2] == PCI_CLASS_MASS_STORAGE) {\r | |
498 | if (PciData.Hdr.ClassCode[1] == 0x09 ) { //UFS Controller Subclass\r | |
499 | UfsHcFound = TRUE;\r | |
500 | }\r | |
501 | }\r | |
502 | \r | |
503 | if (!UfsHcFound) {\r | |
504 | return EFI_UNSUPPORTED;\r | |
505 | }\r | |
506 | \r | |
507 | return Status;\r | |
508 | }\r | |
509 | \r | |
510 | \r | |
511 | /**\r | |
512 | Starts a device controller or a bus controller.\r | |
513 | \r | |
514 | The Start() function is designed to be invoked from the EFI boot service ConnectController().\r | |
515 | As a result, much of the error checking on the parameters to Start() has been moved into this\r | |
516 | common boot service. It is legal to call Start() from other locations,\r | |
517 | but the following calling restrictions must be followed or the system behavior will not be deterministic.\r | |
518 | 1. ControllerHandle must be a valid EFI_HANDLE.\r | |
519 | 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned\r | |
520 | EFI_DEVICE_PATH_PROTOCOL.\r | |
521 | 3. Prior to calling Start(), the Supported() function for the driver specified by This must\r | |
522 | have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.\r | |
523 | \r | |
524 | @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r | |
525 | @param[in] ControllerHandle The handle of the controller to start. This handle\r | |
526 | must support a protocol interface that supplies\r | |
527 | an I/O abstraction to the driver.\r | |
528 | @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r | |
529 | parameter is ignored by device drivers, and is optional for bus\r | |
530 | drivers. For a bus driver, if this parameter is NULL, then handles\r | |
531 | for all the children of Controller are created by this driver.\r | |
532 | If this parameter is not NULL and the first Device Path Node is\r | |
533 | not the End of Device Path Node, then only the handle for the\r | |
534 | child device specified by the first Device Path Node of\r | |
535 | RemainingDevicePath is created by this driver.\r | |
536 | If the first Device Path Node of RemainingDevicePath is\r | |
537 | the End of Device Path Node, no child handle is created by this\r | |
538 | driver.\r | |
539 | \r | |
540 | @retval EFI_SUCCESS The device was started.\r | |
541 | @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.\r | |
542 | @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r | |
543 | @retval Others The driver failded to start the device.\r | |
544 | \r | |
545 | **/\r | |
546 | EFI_STATUS\r | |
547 | EFIAPI\r | |
548 | UfsHcDriverBindingStart (\r | |
549 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r | |
550 | IN EFI_HANDLE Controller,\r | |
551 | IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r | |
552 | )\r | |
553 | {\r | |
554 | EFI_STATUS Status;\r | |
555 | EFI_PCI_IO_PROTOCOL *PciIo;\r | |
556 | UFS_HOST_CONTROLLER_PRIVATE_DATA *Private;\r | |
557 | UINT64 Supports;\r | |
095f0779 FT |
558 | UINT8 BarIndex;\r |
559 | EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *BarDesc;\r | |
0591696e FT |
560 | \r |
561 | PciIo = NULL;\r | |
562 | Private = NULL;\r | |
563 | Supports = 0;\r | |
095f0779 | 564 | BarDesc = NULL;\r |
0591696e FT |
565 | \r |
566 | //\r | |
567 | // Now test and open the EfiPciIoProtocol\r | |
568 | //\r | |
569 | Status = gBS->OpenProtocol (\r | |
570 | Controller,\r | |
571 | &gEfiPciIoProtocolGuid,\r | |
572 | (VOID **) &PciIo,\r | |
573 | This->DriverBindingHandle,\r | |
574 | Controller,\r | |
575 | EFI_OPEN_PROTOCOL_BY_DRIVER\r | |
576 | );\r | |
577 | //\r | |
578 | // Status == 0 - A normal execution flow, SUCCESS and the program proceeds.\r | |
579 | // Status == ALREADY_STARTED - A non-zero Status code returned. It indicates\r | |
580 | // that the protocol has been opened and should be treated as a\r | |
581 | // normal condition and the program proceeds. The Protocol will not\r | |
582 | // opened 'again' by this call.\r | |
583 | // Status != ALREADY_STARTED - Error status, terminate program execution\r | |
584 | //\r | |
585 | if (EFI_ERROR (Status)) {\r | |
586 | //\r | |
587 | // EFI_ALREADY_STARTED is also an error\r | |
588 | //\r | |
589 | return Status;\r | |
590 | }\r | |
591 | \r | |
592 | Private = AllocateCopyPool (sizeof (UFS_HOST_CONTROLLER_PRIVATE_DATA), &gUfsHcTemplate);\r | |
593 | if (Private == NULL) {\r | |
594 | Status = EFI_OUT_OF_RESOURCES;\r | |
595 | goto Done;\r | |
596 | }\r | |
597 | \r | |
598 | Private->PciIo = PciIo;\r | |
599 | \r | |
095f0779 FT |
600 | for (BarIndex = 0; BarIndex < PCI_MAX_BAR; BarIndex++) {\r |
601 | Status = PciIo->GetBarAttributes (\r | |
602 | PciIo,\r | |
603 | BarIndex,\r | |
604 | NULL,\r | |
605 | (VOID**) &BarDesc\r | |
606 | );\r | |
607 | if (Status == EFI_UNSUPPORTED) {\r | |
608 | continue;\r | |
609 | } else if (EFI_ERROR (Status)) {\r | |
610 | goto Done;\r | |
611 | }\r | |
612 | \r | |
613 | if (BarDesc->ResType == ACPI_ADDRESS_SPACE_TYPE_MEM) {\r | |
614 | Private->BarIndex = BarIndex;\r | |
615 | FreePool (BarDesc);\r | |
616 | break;\r | |
617 | }\r | |
618 | \r | |
619 | FreePool (BarDesc);\r | |
620 | }\r | |
621 | \r | |
0591696e FT |
622 | Status = PciIo->Attributes (\r |
623 | PciIo,\r | |
624 | EfiPciIoAttributeOperationGet,\r | |
625 | 0,\r | |
626 | &Private->PciAttributes\r | |
627 | );\r | |
628 | \r | |
629 | if (EFI_ERROR (Status)) {\r | |
630 | goto Done;\r | |
631 | }\r | |
632 | \r | |
633 | Status = PciIo->Attributes (\r | |
634 | PciIo,\r | |
635 | EfiPciIoAttributeOperationSupported,\r | |
636 | 0,\r | |
637 | &Supports\r | |
638 | );\r | |
639 | \r | |
640 | if (!EFI_ERROR (Status)) {\r | |
641 | Supports &= (UINT64)EFI_PCI_DEVICE_ENABLE;\r | |
642 | Status = PciIo->Attributes (\r | |
643 | PciIo,\r | |
644 | EfiPciIoAttributeOperationEnable,\r | |
645 | Supports,\r | |
646 | NULL\r | |
647 | );\r | |
648 | } else {\r | |
649 | goto Done;\r | |
650 | }\r | |
651 | \r | |
652 | ///\r | |
653 | /// Install UFS_HOST_CONTROLLER protocol\r | |
654 | ///\r | |
655 | Status = gBS->InstallProtocolInterface (\r | |
656 | &Controller,\r | |
657 | &gEdkiiUfsHostControllerProtocolGuid,\r | |
658 | EFI_NATIVE_INTERFACE,\r | |
659 | (VOID*)&(Private->UfsHc)\r | |
660 | );\r | |
661 | \r | |
662 | Done:\r | |
663 | if (EFI_ERROR (Status)) {\r | |
664 | if ((Private != NULL) && (Private->PciAttributes != 0)) {\r | |
665 | //\r | |
666 | // Restore original PCI attributes\r | |
667 | //\r | |
1a5ae661 HW |
668 | PciIo->Attributes (\r |
669 | PciIo,\r | |
670 | EfiPciIoAttributeOperationSet,\r | |
671 | Private->PciAttributes,\r | |
672 | NULL\r | |
673 | );\r | |
0591696e FT |
674 | }\r |
675 | gBS->CloseProtocol (\r | |
676 | Controller,\r | |
677 | &gEfiPciIoProtocolGuid,\r | |
678 | This->DriverBindingHandle,\r | |
679 | Controller\r | |
680 | );\r | |
681 | if (Private != NULL) {\r | |
682 | FreePool (Private);\r | |
683 | }\r | |
684 | }\r | |
685 | \r | |
686 | return Status;\r | |
687 | }\r | |
688 | \r | |
689 | \r | |
690 | /**\r | |
691 | Stops a device controller or a bus controller.\r | |
692 | \r | |
693 | The Stop() function is designed to be invoked from the EFI boot service DisconnectController().\r | |
694 | As a result, much of the error checking on the parameters to Stop() has been moved\r | |
695 | into this common boot service. It is legal to call Stop() from other locations,\r | |
696 | but the following calling restrictions must be followed or the system behavior will not be deterministic.\r | |
697 | 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this\r | |
698 | same driver's Start() function.\r | |
699 | 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid\r | |
700 | EFI_HANDLE. In addition, all of these handles must have been created in this driver's\r | |
701 | Start() function, and the Start() function must have called OpenProtocol() on\r | |
702 | ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.\r | |
703 | \r | |
704 | @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r | |
705 | @param[in] ControllerHandle A handle to the device being stopped. The handle must\r | |
706 | support a bus specific I/O protocol for the driver\r | |
707 | to use to stop the device.\r | |
708 | @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.\r | |
709 | @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL\r | |
710 | if NumberOfChildren is 0.\r | |
711 | \r | |
712 | @retval EFI_SUCCESS The device was stopped.\r | |
713 | @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.\r | |
714 | \r | |
715 | **/\r | |
716 | EFI_STATUS\r | |
717 | EFIAPI\r | |
718 | UfsHcDriverBindingStop (\r | |
719 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r | |
720 | IN EFI_HANDLE Controller,\r | |
721 | IN UINTN NumberOfChildren,\r | |
722 | IN EFI_HANDLE *ChildHandleBuffer\r | |
723 | )\r | |
724 | {\r | |
725 | EFI_STATUS Status;\r | |
726 | UFS_HOST_CONTROLLER_PRIVATE_DATA *Private;\r | |
727 | EDKII_UFS_HOST_CONTROLLER_PROTOCOL *UfsHc;\r | |
728 | \r | |
729 | ///\r | |
730 | /// Get private data\r | |
731 | ///\r | |
732 | Status = gBS->OpenProtocol (\r | |
733 | Controller,\r | |
734 | &gEdkiiUfsHostControllerProtocolGuid,\r | |
735 | (VOID **) &UfsHc,\r | |
736 | This->DriverBindingHandle,\r | |
737 | Controller,\r | |
738 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
739 | );\r | |
740 | \r | |
741 | if (EFI_ERROR (Status)) {\r | |
742 | return EFI_DEVICE_ERROR;\r | |
743 | }\r | |
744 | \r | |
745 | Private = UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (UfsHc);\r | |
746 | \r | |
747 | Status = gBS->UninstallProtocolInterface (\r | |
748 | Controller,\r | |
749 | &gEdkiiUfsHostControllerProtocolGuid,\r | |
750 | &(Private->UfsHc)\r | |
751 | );\r | |
752 | if (!EFI_ERROR (Status)) {\r | |
753 | //\r | |
754 | // Restore original PCI attributes\r | |
755 | //\r | |
756 | Status = Private->PciIo->Attributes (\r | |
757 | Private->PciIo,\r | |
758 | EfiPciIoAttributeOperationSet,\r | |
759 | Private->PciAttributes,\r | |
760 | NULL\r | |
761 | );\r | |
762 | ASSERT_EFI_ERROR (Status);\r | |
763 | \r | |
764 | //\r | |
765 | // Close protocols opened by UFS host controller driver\r | |
766 | //\r | |
767 | gBS->CloseProtocol (\r | |
768 | Controller,\r | |
769 | &gEfiPciIoProtocolGuid,\r | |
770 | This->DriverBindingHandle,\r | |
771 | Controller\r | |
772 | );\r | |
773 | \r | |
774 | FreePool (Private);\r | |
775 | }\r | |
776 | \r | |
777 | return Status;\r | |
778 | }\r | |
779 | \r | |
780 | /**\r | |
781 | The entry point for UFS host controller driver, used to install this driver on the ImageHandle.\r | |
782 | \r | |
783 | @param[in] ImageHandle The firmware allocated handle for this driver image.\r | |
784 | @param[in] SystemTable Pointer to the EFI system table.\r | |
785 | \r | |
786 | @retval EFI_SUCCESS Driver loaded.\r | |
787 | @retval other Driver not loaded.\r | |
788 | \r | |
789 | **/\r | |
790 | EFI_STATUS\r | |
791 | EFIAPI\r | |
792 | UfsHcDriverEntry (\r | |
793 | IN EFI_HANDLE ImageHandle,\r | |
794 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
795 | )\r | |
796 | {\r | |
797 | EFI_STATUS Status;\r | |
798 | \r | |
799 | Status = EfiLibInstallDriverBindingComponentName2 (\r | |
800 | ImageHandle,\r | |
801 | SystemTable,\r | |
802 | &gUfsHcDriverBinding,\r | |
803 | ImageHandle,\r | |
804 | &gUfsHcComponentName,\r | |
805 | &gUfsHcComponentName2\r | |
806 | );\r | |
807 | ASSERT_EFI_ERROR (Status);\r | |
808 | \r | |
809 | return Status;\r | |
810 | }\r |