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