]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/I2c/I2cDxe/I2cHost.c
Add I2C bus DXE driver and I2C host DXE driver following PI 1.3 spec.
[mirror_edk2.git] / MdeModulePkg / Bus / I2c / I2cDxe / I2cHost.c
CommitLineData
43e543bc
EL
1/** @file\r
2 This file implements I2C Host Protocol which provides callers with the ability to \r
3 do I/O transactions to all of the devices on the I2C bus.\r
4\r
5 Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>\r
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 "I2cDxe.h"\r
17\r
18EFI_DRIVER_BINDING_PROTOCOL gI2cHostDriverBinding = {\r
19 I2cHostDriverSupported,\r
20 I2cHostDriverStart,\r
21 I2cHostDriverStop,\r
22 0x10,\r
23 NULL,\r
24 NULL\r
25};\r
26\r
27//\r
28// Driver name table \r
29//\r
30GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mI2cHostDriverNameTable[] = {\r
31 { "eng;en", L"I2c Host Driver" },\r
32 { NULL , NULL }\r
33};\r
34\r
35//\r
36// EFI Component Name Protocol\r
37//\r
38GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gI2cHostComponentName = {\r
39 (EFI_COMPONENT_NAME_GET_DRIVER_NAME) I2cHostComponentNameGetDriverName,\r
40 (EFI_COMPONENT_NAME_GET_CONTROLLER_NAME) I2cHostComponentNameGetControllerName,\r
41 "eng"\r
42};\r
43\r
44//\r
45// EFI Component Name 2 Protocol\r
46//\r
47GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gI2cHostComponentName2 = {\r
48 I2cHostComponentNameGetDriverName,\r
49 I2cHostComponentNameGetControllerName,\r
50 "en"\r
51};\r
52\r
53/**\r
54 Retrieves a Unicode string that is the user readable name of the driver.\r
55\r
56 This function retrieves the user readable name of a driver in the form of a\r
57 Unicode string. If the driver specified by This has a user readable name in\r
58 the language specified by Language, then a pointer to the driver name is\r
59 returned in DriverName, and EFI_SUCCESS is returned. If the driver specified\r
60 by This does not support the language specified by Language,\r
61 then EFI_UNSUPPORTED is returned.\r
62\r
63 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or\r
64 EFI_COMPONENT_NAME_PROTOCOL instance.\r
65\r
66 @param Language[in] A pointer to a Null-terminated ASCII string\r
67 array indicating the language. This is the\r
68 language of the driver name that the caller is\r
69 requesting, and it must match one of the\r
70 languages specified in SupportedLanguages. The\r
71 number of languages supported by a driver is up\r
72 to the driver writer. Language is specified\r
73 in RFC 4646 or ISO 639-2 language code format.\r
74\r
75 @param DriverName[out] A pointer to the Unicode string to return.\r
76 This Unicode string is the name of the\r
77 driver specified by This in the language\r
78 specified by Language.\r
79\r
80 @retval EFI_SUCCESS The Unicode string for the Driver specified by\r
81 This and the language specified by Language was\r
82 returned in DriverName.\r
83\r
84 @retval EFI_INVALID_PARAMETER Language is NULL.\r
85\r
86 @retval EFI_INVALID_PARAMETER DriverName is NULL.\r
87\r
88 @retval EFI_UNSUPPORTED The driver specified by This does not support\r
89 the language specified by Language.\r
90\r
91**/\r
92EFI_STATUS\r
93EFIAPI\r
94I2cHostComponentNameGetDriverName (\r
95 IN EFI_COMPONENT_NAME2_PROTOCOL *This,\r
96 IN CHAR8 *Language,\r
97 OUT CHAR16 **DriverName\r
98 )\r
99{\r
100 return LookupUnicodeString2 (\r
101 Language,\r
102 This->SupportedLanguages,\r
103 mI2cHostDriverNameTable,\r
104 DriverName,\r
105 (BOOLEAN)(This != &gI2cHostComponentName2)\r
106 );\r
107}\r
108\r
109/**\r
110 Retrieves a Unicode string that is the user readable name of the controller\r
111 that is being managed by a driver.\r
112\r
113 This function retrieves the user readable name of the controller specified by\r
114 ControllerHandle and ChildHandle in the form of a Unicode string. If the\r
115 driver specified by This has a user readable name in the language specified by\r
116 Language, then a pointer to the controller name is returned in ControllerName,\r
117 and EFI_SUCCESS is returned. If the driver specified by This is not currently\r
118 managing the controller specified by ControllerHandle and ChildHandle,\r
119 then EFI_UNSUPPORTED is returned. If the driver specified by This does not\r
120 support the language specified by Language, then EFI_UNSUPPORTED is returned.\r
121\r
122 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or\r
123 EFI_COMPONENT_NAME_PROTOCOL instance.\r
124\r
125 @param ControllerHandle[in] The handle of a controller that the driver\r
126 specified by This is managing. This handle\r
127 specifies the controller whose name is to be\r
128 returned.\r
129\r
130 @param ChildHandle[in] The handle of the child controller to retrieve\r
131 the name of. This is an optional parameter that\r
132 may be NULL. It will be NULL for device\r
133 drivers. It will also be NULL for a bus drivers\r
134 that wish to retrieve the name of the bus\r
135 controller. It will not be NULL for a bus\r
136 driver that wishes to retrieve the name of a\r
137 child controller.\r
138\r
139 @param Language[in] A pointer to a Null-terminated ASCII string\r
140 array indicating the language. This is the\r
141 language of the driver name that the caller is\r
142 requesting, and it must match one of the\r
143 languages specified in SupportedLanguages. The\r
144 number of languages supported by a driver is up\r
145 to the driver writer. Language is specified in\r
146 RFC 4646 or ISO 639-2 language code format.\r
147\r
148 @param ControllerName[out] A pointer to the Unicode string to return.\r
149 This Unicode string is the name of the\r
150 controller specified by ControllerHandle and\r
151 ChildHandle in the language specified by\r
152 Language from the point of view of the driver\r
153 specified by This.\r
154\r
155 @retval EFI_SUCCESS The Unicode string for the user readable name in\r
156 the language specified by Language for the\r
157 driver specified by This was returned in\r
158 DriverName.\r
159\r
160 @retval EFI_INVALID_PARAMETER ControllerHandle is NULL.\r
161\r
162 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid\r
163 EFI_HANDLE.\r
164\r
165 @retval EFI_INVALID_PARAMETER Language is NULL.\r
166\r
167 @retval EFI_INVALID_PARAMETER ControllerName is NULL.\r
168\r
169 @retval EFI_UNSUPPORTED The driver specified by This is not currently\r
170 managing the controller specified by\r
171 ControllerHandle and ChildHandle.\r
172\r
173 @retval EFI_UNSUPPORTED The driver specified by This does not support\r
174 the language specified by Language.\r
175\r
176**/\r
177EFI_STATUS\r
178EFIAPI\r
179I2cHostComponentNameGetControllerName (\r
180 IN EFI_COMPONENT_NAME2_PROTOCOL *This,\r
181 IN EFI_HANDLE ControllerHandle,\r
182 IN EFI_HANDLE ChildHandle OPTIONAL,\r
183 IN CHAR8 *Language,\r
184 OUT CHAR16 **ControllerName\r
185 )\r
186{\r
187 return EFI_UNSUPPORTED;\r
188}\r
189\r
190/**\r
191 Tests to see if this driver supports a given controller. If a child device is provided,\r
192 it further tests to see if this driver supports creating a handle for the specified child device.\r
193\r
194 This function checks to see if the driver specified by This supports the device specified by\r
195 ControllerHandle. Drivers will typically use the device path attached to\r
196 ControllerHandle and/or the services from the bus I/O abstraction attached to\r
197 ControllerHandle to determine if the driver supports ControllerHandle. This function\r
198 may be called many times during platform initialization. In order to reduce boot times, the tests\r
199 performed by this function must be very small, and take as little time as possible to execute. This\r
200 function must not change the state of any hardware devices, and this function must be aware that the\r
201 device specified by ControllerHandle may already be managed by the same driver or a\r
202 different driver. This function must match its calls to AllocatePages() with FreePages(),\r
203 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().\r
204 Since ControllerHandle may have been previously started by the same driver, if a protocol is\r
205 already in the opened state, then it must not be closed with CloseProtocol(). This is required\r
206 to guarantee the state of ControllerHandle is not modified by this function.\r
207\r
208 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
209 @param[in] ControllerHandle The handle of the controller to test. This handle\r
210 must support a protocol interface that supplies\r
211 an I/O abstraction to the driver.\r
212 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
213 parameter is ignored by device drivers, and is optional for bus\r
214 drivers. For bus drivers, if this parameter is not NULL, then\r
215 the bus driver must determine if the bus controller specified\r
216 by ControllerHandle and the child controller specified\r
217 by RemainingDevicePath are both supported by this\r
218 bus driver.\r
219\r
220 @retval EFI_SUCCESS The device specified by ControllerHandle and\r
221 RemainingDevicePath is supported by the driver specified by This.\r
222 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and\r
223 RemainingDevicePath is already being managed by the driver\r
224 specified by This.\r
225 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and\r
226 RemainingDevicePath is already being managed by a different\r
227 driver or an application that requires exclusive access.\r
228 Currently not implemented.\r
229 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and\r
230 RemainingDevicePath is not supported by the driver specified by This.\r
231**/\r
232EFI_STATUS\r
233EFIAPI\r
234I2cHostDriverSupported (\r
235 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
236 IN EFI_HANDLE Controller,\r
237 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
238 )\r
239{\r
240 EFI_I2C_MASTER_PROTOCOL *I2cMaster;\r
241 EFI_I2C_BUS_CONFIGURATION_MANAGEMENT_PROTOCOL *I2cBusConfigurationManagement;\r
242 EFI_STATUS Status;\r
243\r
244 //\r
245 // Locate I2C Bus Configuration Management Protocol\r
246 //\r
247 Status = gBS->OpenProtocol (\r
248 Controller,\r
249 &gEfiI2cBusConfigurationManagementProtocolGuid,\r
250 (VOID **)&I2cBusConfigurationManagement,\r
251 This->DriverBindingHandle,\r
252 Controller,\r
253 EFI_OPEN_PROTOCOL_BY_DRIVER\r
254 );\r
255 if (EFI_ERROR (Status)) {\r
256 return Status;\r
257 }\r
258\r
259 //\r
260 // Close the protocol because we don't use it here\r
261 //\r
262 gBS->CloseProtocol (\r
263 Controller,\r
264 &gEfiI2cBusConfigurationManagementProtocolGuid,\r
265 This->DriverBindingHandle,\r
266 Controller\r
267 );\r
268\r
269 //\r
270 // Locate I2C Master Protocol\r
271 //\r
272 Status = gBS->OpenProtocol (\r
273 Controller,\r
274 &gEfiI2cMasterProtocolGuid,\r
275 (VOID **)&I2cMaster,\r
276 This->DriverBindingHandle,\r
277 Controller,\r
278 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
279 );\r
280 if (EFI_ERROR (Status)) {\r
281 return Status;\r
282 }\r
283\r
284 return EFI_SUCCESS;\r
285}\r
286\r
287/**\r
288 Starts a device controller or a bus controller.\r
289\r
290 The Start() function is designed to be invoked from the EFI boot service ConnectController().\r
291 As a result, much of the error checking on the parameters to Start() has been moved into this\r
292 common boot service. It is legal to call Start() from other locations,\r
293 but the following calling restrictions must be followed, or the system behavior will not be deterministic.\r
294 1. ControllerHandle must be a valid EFI_HANDLE.\r
295 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned\r
296 EFI_DEVICE_PATH_PROTOCOL.\r
297 3. Prior to calling Start(), the Supported() function for the driver specified by This must\r
298 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.\r
299\r
300 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
301 @param[in] ControllerHandle The handle of the controller to start. This handle\r
302 must support a protocol interface that supplies\r
303 an I/O abstraction to the driver.\r
304 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
305 parameter is ignored by device drivers, and is optional for bus\r
306 drivers. For a bus driver, if this parameter is NULL, then handles\r
307 for all the children of Controller are created by this driver.\r
308 If this parameter is not NULL and the first Device Path Node is\r
309 not the End of Device Path Node, then only the handle for the\r
310 child device specified by the first Device Path Node of\r
311 RemainingDevicePath is created by this driver.\r
312 If the first Device Path Node of RemainingDevicePath is\r
313 the End of Device Path Node, no child handle is created by this\r
314 driver.\r
315\r
316 @retval EFI_SUCCESS The device was started.\r
317 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.\r
318 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
319 @retval Others The driver failded to start the device.\r
320\r
321**/\r
322EFI_STATUS\r
323EFIAPI\r
324I2cHostDriverStart (\r
325 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
326 IN EFI_HANDLE Controller,\r
327 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
328 )\r
329{\r
330 EFI_STATUS Status;\r
331 EFI_I2C_MASTER_PROTOCOL *I2cMaster;\r
332 EFI_I2C_BUS_CONFIGURATION_MANAGEMENT_PROTOCOL *I2cBusConfigurationManagement;\r
333 I2C_HOST_CONTEXT *I2cHostContext;\r
334\r
335 I2cMaster = NULL;\r
336 I2cHostContext = NULL;\r
337 I2cBusConfigurationManagement = NULL;\r
338\r
339 //\r
340 // Locate I2C Bus Configuration Management Protocol\r
341 //\r
342 Status = gBS->OpenProtocol (\r
343 Controller,\r
344 &gEfiI2cBusConfigurationManagementProtocolGuid,\r
345 (VOID **)&I2cBusConfigurationManagement,\r
346 This->DriverBindingHandle,\r
347 Controller,\r
348 EFI_OPEN_PROTOCOL_BY_DRIVER\r
349 );\r
350 if (EFI_ERROR (Status)) {\r
351 DEBUG ((EFI_D_ERROR, "I2cHost: Open I2C bus configuration error, Status = %r\n", Status));\r
352 return Status;\r
353 }\r
354\r
355 //\r
356 // Locate I2C Master Protocol\r
357 //\r
358 Status = gBS->OpenProtocol ( \r
359 Controller,\r
360 &gEfiI2cMasterProtocolGuid,\r
361 (VOID **)&I2cMaster,\r
362 This->DriverBindingHandle,\r
363 Controller,\r
364 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
365 );\r
366 if (EFI_ERROR (Status)) {\r
367 DEBUG ((EFI_D_ERROR, "I2cHost: Open I2C master error, Status = %r\n", Status));\r
368 goto Exit;\r
369 }\r
370\r
371 //\r
372 // Allocate the I2C Host Context structure\r
373 //\r
374 I2cHostContext = AllocateZeroPool (sizeof (I2C_HOST_CONTEXT));\r
375 if (I2cHostContext == NULL) {\r
376 DEBUG ((EFI_D_ERROR, "I2cHost: there is no enough memory to allocate.\n"));\r
377 Status = EFI_OUT_OF_RESOURCES;\r
378 goto Exit;\r
379 }\r
380\r
381 //\r
382 // Initialize the context structure for the current I2C Controller\r
383 //\r
384 I2cHostContext->Signature = I2C_HOST_SIGNATURE;\r
385 I2cHostContext->I2cMaster = I2cMaster;\r
386 I2cHostContext->I2cBusConfigurationManagement = I2cBusConfigurationManagement;\r
387 I2cHostContext->I2cBusConfiguration = (UINTN) -1;\r
388 InitializeListHead(&I2cHostContext->RequestList);\r
389\r
390 //\r
391 // Reset the controller\r
392 //\r
393 Status = I2cMaster->Reset (I2cMaster);\r
394 if (EFI_ERROR (Status)) {\r
395 DEBUG ((EFI_D_ERROR, "I2cHost: I2C controller reset failed!\n"));\r
396 goto Exit;\r
397 }\r
398\r
399 //\r
400 // Create the I2C transaction complete event\r
401 //\r
402 Status = gBS->CreateEvent (\r
403 EVT_NOTIFY_SIGNAL,\r
404 TPL_I2C_SYNC,\r
405 I2cHostRequestCompleteEvent,\r
406 I2cHostContext,\r
407 &I2cHostContext->I2cEvent\r
408 );\r
409 if (EFI_ERROR (Status)) {\r
410 DEBUG ((EFI_D_ERROR, "I2cHost: create complete event error, Status = %r\n", Status));\r
411 goto Exit;\r
412 }\r
413\r
414 //\r
415 // Get the bus management event\r
416 //\r
417 Status = gBS->CreateEvent (\r
418 EVT_NOTIFY_SIGNAL,\r
419 TPL_I2C_SYNC,\r
420 I2cHostI2cBusConfigurationAvailable,\r
421 I2cHostContext,\r
422 &I2cHostContext->I2cBusConfigurationEvent\r
423 );\r
424 if (EFI_ERROR (Status)) {\r
425 DEBUG ((EFI_D_ERROR, "I2cHost: create bus available event error, Status = %r\n", Status));\r
426 goto Exit;\r
427 } \r
428\r
429 //\r
430 // Build the I2C host protocol for the current I2C controller\r
431 //\r
432 I2cHostContext->I2cHost.QueueRequest = I2cHostQueueRequest;\r
433 I2cHostContext->I2cHost.I2cControllerCapabilities = I2cMaster->I2cControllerCapabilities;\r
434\r
435 //\r
436 // Install the driver protocol\r
437 //\r
438 Status = gBS->InstallMultipleProtocolInterfaces (\r
439 &Controller,\r
440 &gEfiI2cHostProtocolGuid,\r
441 &I2cHostContext->I2cHost,\r
442 NULL\r
443 );\r
444Exit:\r
445 if (EFI_ERROR (Status)) {\r
446 DEBUG ((EFI_D_ERROR, "I2cHost: Start() function failed, Status = %r\n", Status));\r
447 if (I2cBusConfigurationManagement != NULL) {\r
448 gBS->CloseProtocol (\r
449 Controller,\r
450 &gEfiI2cBusConfigurationManagementProtocolGuid,\r
451 This->DriverBindingHandle,\r
452 Controller\r
453 ); \r
454 }\r
455 \r
456 if ((I2cHostContext != NULL) && (I2cHostContext->I2cEvent != NULL)) {\r
457 gBS->CloseEvent (I2cHostContext->I2cEvent);\r
458 I2cHostContext->I2cEvent = NULL;\r
459 }\r
460\r
461 if ((I2cHostContext != NULL) && (I2cHostContext->I2cBusConfigurationEvent != NULL)) {\r
462 gBS->CloseEvent (I2cHostContext->I2cBusConfigurationEvent);\r
463 I2cHostContext->I2cBusConfigurationEvent = NULL;\r
464 }\r
465\r
466 //\r
467 // Release the context structure upon failure\r
468 //\r
469 if (I2cHostContext != NULL) {\r
470 FreePool (I2cHostContext);\r
471 }\r
472 }\r
473\r
474 //\r
475 // Return the operation status.\r
476 //\r
477 return Status;\r
478}\r
479\r
480/**\r
481 Stops a device controller or a bus controller.\r
482\r
483 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().\r
484 As a result, much of the error checking on the parameters to Stop() has been moved\r
485 into this common boot service. It is legal to call Stop() from other locations,\r
486 but the following calling restrictions must be followed, or the system behavior will not be deterministic.\r
487 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this\r
488 same driver's Start() function.\r
489 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid\r
490 EFI_HANDLE. In addition, all of these handles must have been created in this driver's\r
491 Start() function, and the Start() function must have called OpenProtocol() on\r
492 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.\r
493\r
494 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
495 @param[in] ControllerHandle A handle to the device being stopped. The handle must\r
496 support a bus specific I/O protocol for the driver\r
497 to use to stop the device.\r
498 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.\r
499 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL\r
500 if NumberOfChildren is 0.\r
501\r
502 @retval EFI_SUCCESS The device was stopped.\r
503 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.\r
504\r
505**/\r
506EFI_STATUS\r
507EFIAPI\r
508I2cHostDriverStop (\r
509 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
510 IN EFI_HANDLE Controller,\r
511 IN UINTN NumberOfChildren,\r
512 IN EFI_HANDLE *ChildHandleBuffer\r
513 )\r
514{\r
515 EFI_STATUS Status; \r
516 I2C_HOST_CONTEXT *I2cHostContext;\r
517 EFI_I2C_HOST_PROTOCOL *I2cHost;\r
518 EFI_TPL TplPrevious;\r
519\r
520 TplPrevious = EfiGetCurrentTpl ();\r
521 if (TplPrevious > TPL_I2C_SYNC) {\r
522 DEBUG ((EFI_D_ERROR, "I2cHost: TPL %d is too high in Stop.\n", TplPrevious));\r
523 return EFI_DEVICE_ERROR;\r
524 }\r
525\r
526 Status = gBS->OpenProtocol (\r
527 Controller,\r
528 &gEfiI2cHostProtocolGuid,\r
529 (VOID **) &I2cHost,\r
530 This->DriverBindingHandle,\r
531 Controller,\r
532 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
533 );\r
534\r
535 if (EFI_ERROR (Status)) {\r
536 return EFI_DEVICE_ERROR;\r
537 }\r
538\r
539 I2cHostContext = I2C_HOST_CONTEXT_FROM_PROTOCOL (I2cHost);\r
540\r
541 //\r
542 // Raise TPL for critical section\r
543 //\r
544 TplPrevious = gBS->RaiseTPL (TPL_I2C_SYNC);\r
545 \r
546 //\r
547 // If there is pending request or pending bus configuration, do not stop\r
548 //\r
549 Status = EFI_DEVICE_ERROR;\r
550 if (( !I2cHostContext->I2cBusConfigurationManagementPending )\r
551 && IsListEmpty (&I2cHostContext->RequestList)) {\r
552 \r
553 //\r
554 // Remove the I2C host protocol\r
555 //\r
556 Status = gBS->UninstallMultipleProtocolInterfaces (\r
557 Controller,\r
558 &gEfiI2cHostProtocolGuid,\r
559 I2cHost,\r
560 NULL\r
561 );\r
562 }\r
563 \r
564 //\r
565 // Leave critical section\r
566 //\r
567 gBS->RestoreTPL (TplPrevious);\r
568 if (!EFI_ERROR (Status)) {\r
569 gBS->CloseProtocol (\r
570 Controller,\r
571 &gEfiI2cBusConfigurationManagementProtocolGuid,\r
572 This->DriverBindingHandle,\r
573 Controller\r
574 );\r
575\r
576 //\r
577 // Release I2c Host resources\r
578 //\r
579 if (I2cHostContext->I2cBusConfigurationEvent != NULL) {\r
580 gBS->CloseEvent (I2cHostContext->I2cBusConfigurationEvent);\r
581 I2cHostContext->I2cBusConfigurationEvent = NULL;\r
582 }\r
583 \r
584 if (I2cHostContext->I2cEvent != NULL) {\r
585 gBS->CloseEvent (I2cHostContext->I2cEvent);\r
586 I2cHostContext->I2cEvent = NULL;\r
587 }\r
588 \r
589 FreePool (I2cHostContext);\r
590 }\r
591\r
592 //\r
593 // Return the stop status\r
594 //\r
595 return Status;\r
596}\r
597\r
598/**\r
599 Handle the I2C bus configuration available event\r
600\r
601 This routine is called at TPL_I2C_SYNC.\r
602\r
603 @param[in] Event Address of an EFI_EVENT handle\r
604 @param[in] Context Address of an I2C_HOST_CONTEXT structure\r
605\r
606**/\r
607VOID\r
608EFIAPI\r
609I2cHostI2cBusConfigurationAvailable (\r
610 IN EFI_EVENT Event,\r
611 IN VOID *Context\r
612 )\r
613{\r
614 I2C_HOST_CONTEXT *I2cHostContext;\r
615 EFI_I2C_MASTER_PROTOCOL *I2cMaster;\r
616 I2C_REQUEST *I2cRequest;\r
617 LIST_ENTRY *EntryHeader;\r
618 LIST_ENTRY *Entry;\r
619 EFI_STATUS Status;\r
620\r
621 //\r
622 // Mark this I2C bus configuration management operation as complete\r
623 //\r
624 I2cHostContext = (I2C_HOST_CONTEXT *)Context;\r
625 I2cMaster = I2cHostContext->I2cMaster;\r
626 ASSERT (I2cMaster != NULL);\r
627 //\r
628 // Clear flag to indicate I2C bus configuration is finished\r
629 //\r
630 I2cHostContext->I2cBusConfigurationManagementPending = FALSE;\r
631\r
632 //\r
633 // Validate the completion status\r
634 //\r
635 if (EFI_ERROR (I2cHostContext->Status)) {\r
636 //\r
637 // Setting I2C bus configuration failed before\r
638 //\r
639 I2cHostRequestComplete (I2cHostContext, I2cHostContext->Status);\r
640\r
641 //\r
642 // Unknown I2C bus configuration\r
643 // Force next operation to enable the I2C bus configuration\r
644 //\r
645 I2cHostContext->I2cBusConfiguration = (UINTN) -1;\r
646 \r
647 //\r
648 // Do not continue current I2C request\r
649 //\r
650 return;\r
651 }\r
652\r
653 //\r
654 // Get the first request in the link with FIFO order\r
655 //\r
656 EntryHeader = &I2cHostContext->RequestList;\r
657 Entry = GetFirstNode (EntryHeader);\r
658 I2cRequest = I2C_REQUEST_FROM_ENTRY (Entry);\r
659\r
660 //\r
661 // Update the I2C bus configuration of the current I2C request\r
662 //\r
663 I2cHostContext->I2cBusConfiguration = I2cRequest->I2cBusConfiguration;\r
664\r
665 //\r
666 // Start an I2C operation on the host, the status is returned by I2cHostContext->Status\r
667 //\r
668 Status = I2cMaster->StartRequest ( \r
669 I2cMaster,\r
670 I2cRequest->SlaveAddress,\r
671 I2cRequest->RequestPacket,\r
672 I2cHostContext->I2cEvent,\r
673 &I2cHostContext->Status\r
674 );\r
675}\r
676\r
677/**\r
678 Complete the current request\r
679\r
680 This routine is called at TPL_I2C_SYNC.\r
681\r
682 @param[in] I2cHostContext Address of an I2C_HOST_CONTEXT structure.\r
683 @param[in] Status Status of the I2C operation.\r
684\r
685 @return This routine returns the input status value.\r
686\r
687**/\r
688EFI_STATUS\r
689I2cHostRequestComplete (\r
690 I2C_HOST_CONTEXT *I2cHostContext,\r
691 EFI_STATUS Status\r
692 )\r
693{\r
694 I2C_REQUEST *I2cRequest;\r
695 LIST_ENTRY *EntryHeader;\r
696 LIST_ENTRY *Entry;\r
697\r
698 //\r
699 // Remove the current I2C request from the list\r
700 //\r
701 EntryHeader = &I2cHostContext->RequestList;\r
702 Entry = GetFirstNode (EntryHeader);\r
703 I2cRequest = I2C_REQUEST_FROM_ENTRY (Entry);\r
704\r
705 //\r
706 // Save the status for QueueRequest\r
707 //\r
708 if ( NULL != I2cRequest->Status ) {\r
709 *I2cRequest->Status = Status;\r
710 }\r
711\r
712 //\r
713 // Notify the user of the I2C request completion\r
714 //\r
715 if ( NULL != I2cRequest->Event ) {\r
716 gBS->SignalEvent (I2cRequest->Event);\r
717 }\r
718\r
719 //\r
720 // Done with this request, remove the current request from list\r
721 //\r
722 RemoveEntryList (&I2cRequest->Link);\r
723 FreePool (I2cRequest->RequestPacket);\r
724 FreePool (I2cRequest);\r
725\r
726 //\r
727 // If there is more I2C request, start next one\r
728 //\r
729 if(!IsListEmpty (EntryHeader)) {\r
730 I2cHostRequestEnable (I2cHostContext);\r
731 }\r
732 \r
733 return Status;\r
734}\r
735\r
736/**\r
737 Handle the bus available event\r
738\r
739 This routine is called at TPL_I2C_SYNC.\r
740\r
741 @param[in] Event Address of an EFI_EVENT handle\r
742 @param[in] Context Address of an I2C_HOST_CONTEXT structure\r
743\r
744**/\r
745VOID\r
746EFIAPI\r
747I2cHostRequestCompleteEvent (\r
748 IN EFI_EVENT Event,\r
749 IN VOID *Context\r
750 )\r
751{\r
752 I2C_HOST_CONTEXT *I2cHostContext;\r
753\r
754 //\r
755 // Handle the completion event\r
756 //\r
757 I2cHostContext = (I2C_HOST_CONTEXT *)Context;\r
758 I2cHostRequestComplete (I2cHostContext, I2cHostContext->Status);\r
759}\r
760\r
761/**\r
762 Enable access to the I2C bus configuration\r
763\r
764 @param[in] I2cHostContext Address of an I2C_HOST_CONTEXT structure\r
765\r
766 @retval EFI_SUCCESS The operation completed successfully.\r
767 @retval EFI_ABORTED The request did not complete because the driver\r
768 was shutdown.\r
769 @retval EFI_BAD_BUFFER_SIZE The WriteBytes or ReadBytes buffer size is too large.\r
770 @retval EFI_DEVICE_ERROR There was an I2C error (NACK) during the operation.\r
771 This could indicate the slave device is not present.\r
772 @retval EFI_INVALID_PARAMETER RequestPacket is NULL\r
773 @retval EFI_NO_MAPPING Invalid I2cBusConfiguration value\r
774 @retval EFI_NO_RESPONSE The I2C device is not responding to the\r
775 slave address. EFI_DEVICE_ERROR may also be\r
776 returned if the controller can not distinguish\r
777 when the NACK occurred.\r
778 @retval EFI_NOT_FOUND I2C slave address exceeds maximum address\r
779 @retval EFI_NOT_READY I2C bus is busy or operation pending, wait for\r
780 the event and then read status.\r
781 @retval EFI_OUT_OF_RESOURCES Insufficient memory for I2C operation\r
782 @retval EFI_TIMEOUT The transaction did not complete within an internally\r
783 specified timeout period.\r
784\r
785**/\r
786EFI_STATUS\r
787I2cHostRequestEnable (\r
788 I2C_HOST_CONTEXT *I2cHostContext\r
789 )\r
790{\r
791 UINTN I2cBusConfiguration;\r
792 CONST EFI_I2C_BUS_CONFIGURATION_MANAGEMENT_PROTOCOL *I2cBusConfigurationManagement;\r
793 I2C_REQUEST *I2cRequest;\r
794 EFI_STATUS Status;\r
795 EFI_TPL TplPrevious;\r
796 LIST_ENTRY *EntryHeader;\r
797 LIST_ENTRY *Entry;\r
798\r
799 //\r
800 // Assume pending request\r
801 //\r
802 Status = EFI_NOT_READY;\r
803\r
804 I2cBusConfigurationManagement = I2cHostContext->I2cBusConfigurationManagement;\r
805\r
806 //\r
807 // Validate the I2c bus configuration\r
808 //\r
809 EntryHeader = &I2cHostContext->RequestList;\r
810 Entry = GetFirstNode (EntryHeader);\r
811 I2cRequest = I2C_REQUEST_FROM_ENTRY (Entry);\r
812\r
813 I2cBusConfiguration = I2cRequest->I2cBusConfiguration;\r
814\r
815 if (I2cHostContext->I2cBusConfiguration != I2cBusConfiguration ) {\r
816 //\r
817 // Set flag to indicate I2C bus configuration is in progress\r
818 //\r
819 I2cHostContext->I2cBusConfigurationManagementPending = TRUE;\r
820 //\r
821 // Update bus configuration for this device's requesting bus configuration\r
822 //\r
823 Status = I2cBusConfigurationManagement->EnableI2cBusConfiguration (\r
824 I2cBusConfigurationManagement,\r
825 I2cBusConfiguration,\r
826 I2cHostContext->I2cBusConfigurationEvent,\r
827 &I2cHostContext->Status\r
828 );\r
829 } else {\r
830 //\r
831 // I2C bus configuration is same, no need change configuration and start I2c transaction directly\r
832 //\r
833 TplPrevious = gBS->RaiseTPL ( TPL_I2C_SYNC );\r
834\r
835 //\r
836 // Same I2C bus configuration\r
837 //\r
838 I2cHostContext->Status = EFI_SUCCESS;\r
839 I2cHostI2cBusConfigurationAvailable (I2cHostContext->I2cBusConfigurationEvent, I2cHostContext);\r
840\r
841 //\r
842 // Release the thread synchronization\r
843 //\r
844 gBS->RestoreTPL ( TplPrevious );\r
845 }\r
846 return Status;\r
847}\r
848\r
849/**\r
850 Queue an I2C operation for execution on the I2C controller.\r
851\r
852 This routine must be called at or below TPL_NOTIFY. For synchronous\r
853 requests this routine must be called at or below TPL_CALLBACK.\r
854\r
855 N.B. The typical consumers of this API are the I2C bus driver and\r
856 on rare occasions the I2C test application. Extreme care must be\r
857 taken by other consumers of this API to prevent confusing the\r
858 third party I2C drivers due to a state change at the I2C device\r
859 which the third party I2C drivers did not initiate. I2C platform\r
860 drivers may use this API within these guidelines.\r
861\r
862 This layer uses the concept of I2C bus configurations to describe\r
863 the I2C bus. An I2C bus configuration is defined as a unique\r
864 setting of the multiplexers and switches in the I2C bus which\r
865 enable access to one or more I2C devices. When using a switch\r
866 to divide a bus, due to speed differences, the I2C platform layer\r
867 would define an I2C bus configuration for the I2C devices on each\r
868 side of the switch. When using a multiplexer, the I2C platform\r
869 layer defines an I2C bus configuration for each of the selector\r
870 values required to control the multiplexer. See Figure 1 in the\r
871 <a href="http://www.nxp.com/documents/user_manual/UM10204.pdf">I<sup>2</sup>C\r
872 Specification</a> for a complex I2C bus configuration.\r
873\r
874 The I2C host driver processes all operations in FIFO order. Prior to\r
875 performing the operation, the I2C host driver calls the I2C platform\r
876 driver to reconfigure the switches and multiplexers in the I2C bus\r
877 enabling access to the specified I2C device. The I2C platform driver\r
878 also selects the maximum bus speed for the device. After the I2C bus\r
879 is configured, the I2C host driver calls the I2C port driver to\r
880 initialize the I2C controller and start the I2C operation.\r
881\r
882 @param[in] This Address of an EFI_I2C_HOST_PROTOCOL instance.\r
883 @param[in] I2cBusConfiguration I2C bus configuration to access the I2C\r
884 device.\r
885 @param[in] SlaveAddress Address of the device on the I2C bus.\r
886 @param[in] Event Event to set for asynchronous operations,\r
887 NULL for synchronous operations\r
888 @param[in] RequestPacket Address of an EFI_I2C_REQUEST_PACKET\r
889 structure describing the I2C operation\r
890 @param[out] I2cStatus Optional buffer to receive the I2C operation\r
891 completion status\r
892\r
893 @retval EFI_SUCCESS The operation completed successfully.\r
894 @retval EFI_ABORTED The request did not complete because the driver\r
895 was shutdown.\r
896 @retval EFI_BAD_BUFFER_SIZE The WriteBytes or ReadBytes buffer size is too large.\r
897 @retval EFI_DEVICE_ERROR There was an I2C error (NACK) during the operation.\r
898 This could indicate the slave device is not present.\r
899 @retval EFI_INVALID_PARAMETER RequestPacket is NULL\r
900 @retval EFI_INVALID_PARAMETER TPL is too high\r
901 @retval EFI_NO_MAPPING Invalid I2cBusConfiguration value\r
902 @retval EFI_NO_RESPONSE The I2C device is not responding to the\r
903 slave address. EFI_DEVICE_ERROR may also be\r
904 returned if the controller can not distinguish\r
905 when the NACK occurred.\r
906 @retval EFI_NOT_FOUND I2C slave address exceeds maximum address\r
907 @retval EFI_NOT_READY I2C bus is busy or operation pending, wait for\r
908 the event and then read status pointed to by\r
909 the request packet.\r
910 @retval EFI_OUT_OF_RESOURCES Insufficient memory for I2C operation\r
911 @retval EFI_TIMEOUT The transaction did not complete within an internally\r
912 specified timeout period.\r
913\r
914**/\r
915EFI_STATUS\r
916EFIAPI\r
917I2cHostQueueRequest (\r
918 IN CONST EFI_I2C_HOST_PROTOCOL *This,\r
919 IN UINTN I2cBusConfiguration,\r
920 IN UINTN SlaveAddress,\r
921 IN EFI_EVENT Event OPTIONAL,\r
922 IN EFI_I2C_REQUEST_PACKET *RequestPacket,\r
923 OUT EFI_STATUS *I2cStatus OPTIONAL\r
924 )\r
925{\r
926 EFI_STATUS Status;\r
927 EFI_EVENT SyncEvent;\r
928 EFI_TPL TplPrevious;\r
929 I2C_REQUEST *I2cRequest;\r
930 I2C_HOST_CONTEXT *I2cHostContext;\r
931 BOOLEAN FirstRequest;\r
932 UINTN RequestPacketSize;\r
933\r
934 SyncEvent = NULL;\r
935 FirstRequest = FALSE;\r
936 Status = EFI_SUCCESS;\r
937\r
938 if (RequestPacket == NULL) {\r
939 return EFI_INVALID_PARAMETER;\r
940 }\r
941\r
942 I2cHostContext = I2C_HOST_CONTEXT_FROM_PROTOCOL (This);\r
943\r
944 if (Event == NULL) {\r
945 //\r
946 // For synchronous transaction, register an event used to wait for finishing synchronous transaction\r
947 //\r
948 Status = gBS->CreateEvent ( \r
949 0,\r
950 TPL_I2C_SYNC,\r
951 NULL,\r
952 NULL,\r
953 &SyncEvent\r
954 );\r
955 if (EFI_ERROR (Status)) {\r
956 return Status;\r
957 }\r
958 }\r
959 \r
960 //\r
961 // TPL should be at or below TPL_NOTIFY.\r
962 // For synchronous requests this routine must be called at or below TPL_CALLBACK.\r
963 //\r
964 TplPrevious = EfiGetCurrentTpl ();\r
965 if ((TplPrevious > TPL_I2C_SYNC) || ((Event == NULL) && (TplPrevious > TPL_CALLBACK))) {\r
966 DEBUG ((EFI_D_ERROR, "ERROR - TPL %d is too high!\n", TplPrevious));\r
967 return EFI_INVALID_PARAMETER;\r
968 }\r
969\r
970 //\r
971 // Allocate the request structure\r
972 //\r
973 I2cRequest = AllocateZeroPool (sizeof (I2C_REQUEST));\r
974 if (I2cRequest == NULL) {\r
975 DEBUG ((EFI_D_ERROR, "WARNING - Failed to allocate I2C_REQUEST!\n"));\r
976 return EFI_OUT_OF_RESOURCES;\r
977 }\r
978\r
979 //\r
980 // Initialize the request\r
981 //\r
982 I2cRequest->Signature = I2C_REQUEST_SIGNATURE;\r
983 I2cRequest->I2cBusConfiguration = I2cBusConfiguration;\r
984 I2cRequest->SlaveAddress = SlaveAddress;\r
985 I2cRequest->Event = (Event == NULL) ? SyncEvent : Event;\r
986 I2cRequest->Status = I2cStatus;\r
987\r
988 //\r
989 // Copy request packet into private buffer, as RequestPacket may be freed during asynchronous transaction\r
990 //\r
991 RequestPacketSize = sizeof (UINTN) + RequestPacket->OperationCount * sizeof (EFI_I2C_OPERATION);\r
992 I2cRequest->RequestPacket = AllocateZeroPool (RequestPacketSize);\r
993 CopyMem (I2cRequest->RequestPacket, RequestPacket, RequestPacketSize);\r
994\r
995 //\r
996 // Synchronize with the other threads\r
997 //\r
998 gBS->RaiseTPL ( TPL_I2C_SYNC );\r
999 \r
1000 FirstRequest = IsListEmpty (&I2cHostContext->RequestList);\r
1001 \r
1002 //\r
1003 // Insert new I2C request in the list\r
1004 //\r
1005 InsertTailList (&I2cHostContext->RequestList, &I2cRequest->Link);\r
1006\r
1007 //\r
1008 // Release the thread synchronization\r
1009 //\r
1010 gBS->RestoreTPL (TplPrevious);\r
1011 \r
1012 if (FirstRequest) {\r
1013 //\r
1014 // Start the first I2C request, then the subsequent of I2C request will continue\r
1015 //\r
1016 Status = I2cHostRequestEnable (I2cHostContext);\r
1017 }\r
1018\r
1019 if (Event != NULL) {\r
1020 //\r
1021 // For asynchronous, return EFI_SUCCESS indicating that the asynchronously I2C transaction was queued.\r
1022 // No real I2C operation status in I2cStatus\r
1023 //\r
1024 return EFI_SUCCESS;\r
1025 }\r
1026\r
1027 //\r
1028 // For synchronous transaction, wait for the operation completion\r
1029 //\r
1030 do {\r
1031 Status = gBS->CheckEvent (SyncEvent);\r
1032 } while (Status == EFI_NOT_READY);\r
1033\r
1034 //\r
1035 // Get the I2C operation status\r
1036 //\r
1037 Status = I2cHostContext->Status;\r
1038\r
1039 //\r
1040 // Return the I2C operation status\r
1041 //\r
1042 if (I2cStatus != NULL) {\r
1043 *I2cStatus = Status;\r
1044 }\r
1045\r
1046 //\r
1047 // Close the event if necessary\r
1048 //\r
1049 if (SyncEvent != NULL) {\r
1050 gBS->CloseEvent (SyncEvent);\r
1051 }\r
1052\r
1053 return Status;\r
1054}\r
1055\r
1056/**\r
1057 The user Entry Point for I2C host module. The user code starts with this function.\r
1058\r
1059 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
1060 @param[in] SystemTable A pointer to the EFI System Table.\r
1061\r
1062 @retval EFI_SUCCESS The entry point is executed successfully.\r
1063 @retval other Some error occurs when executing this entry point.\r
1064\r
1065**/\r
1066EFI_STATUS\r
1067EFIAPI\r
1068InitializeI2cHost(\r
1069 IN EFI_HANDLE ImageHandle,\r
1070 IN EFI_SYSTEM_TABLE *SystemTable\r
1071 )\r
1072{\r
1073 EFI_STATUS Status;\r
1074\r
1075 //\r
1076 // Install driver model protocol(s).\r
1077 //\r
1078 Status = EfiLibInstallDriverBindingComponentName2 (\r
1079 ImageHandle,\r
1080 SystemTable,\r
1081 &gI2cHostDriverBinding,\r
1082 ImageHandle,\r
1083 &gI2cHostComponentName,\r
1084 &gI2cHostComponentName2\r
1085 );\r
1086 ASSERT_EFI_ERROR (Status);\r
1087 return Status;\r
1088}\r
1089\r
1090/**\r
1091 This is the unload handle for I2C host module.\r
1092\r
1093 Disconnect the driver specified by ImageHandle from all the devices in the handle database.\r
1094 Uninstall all the protocols installed in the driver entry point.\r
1095\r
1096 @param[in] ImageHandle The drivers' driver image.\r
1097\r
1098 @retval EFI_SUCCESS The image is unloaded.\r
1099 @retval Others Failed to unload the image.\r
1100\r
1101**/\r
1102EFI_STATUS\r
1103EFIAPI\r
1104I2cHostUnload (\r
1105 IN EFI_HANDLE ImageHandle\r
1106 )\r
1107{\r
1108 EFI_STATUS Status;\r
1109 EFI_HANDLE *DeviceHandleBuffer;\r
1110 UINTN DeviceHandleCount;\r
1111 UINTN Index;\r
1112\r
1113 //\r
1114 // Get the list of all I2C Controller handles in the handle database.\r
1115 // If there is an error getting the list, then the unload\r
1116 // operation fails.\r
1117 //\r
1118 Status = gBS->LocateHandleBuffer (\r
1119 ByProtocol,\r
1120 &gEfiI2cHostProtocolGuid,\r
1121 NULL,\r
1122 &DeviceHandleCount,\r
1123 &DeviceHandleBuffer\r
1124 );\r
1125\r
1126 if (EFI_ERROR (Status)) {\r
1127 return Status;\r
1128 }\r
1129\r
1130 //\r
1131 // Disconnect the driver specified by ImageHandle from all\r
1132 // the devices in the handle database.\r
1133 //\r
1134 for (Index = 0; Index < DeviceHandleCount; Index++) {\r
1135 Status = gBS->DisconnectController (\r
1136 DeviceHandleBuffer[Index],\r
1137 ImageHandle,\r
1138 NULL\r
1139 );\r
1140 if (EFI_ERROR (Status)) {\r
1141 goto Done;\r
1142 }\r
1143 }\r
1144\r
1145 //\r
1146 // Uninstall all the protocols installed in the driver entry point\r
1147 //\r
1148 Status = gBS->UninstallMultipleProtocolInterfaces (\r
1149 gI2cHostDriverBinding.DriverBindingHandle,\r
1150 &gEfiDriverBindingProtocolGuid,\r
1151 &gI2cHostDriverBinding,\r
1152 &gEfiComponentNameProtocolGuid,\r
1153 &gI2cHostComponentName,\r
1154 &gEfiComponentName2ProtocolGuid,\r
1155 &gI2cHostComponentName2,\r
1156 NULL\r
1157 );\r
1158 ASSERT_EFI_ERROR (Status);\r
1159\r
1160Done:\r
1161 //\r
1162 // Free the buffer containing the list of handles from the handle database\r
1163 //\r
1164 if (DeviceHandleBuffer != NULL) {\r
1165 gBS->FreePool (DeviceHandleBuffer);\r
1166 }\r
1167\r
1168 return Status;\r
1169}\r