2 This file implements I2C Host Protocol which provides callers with the ability to
3 do I/O transactions to all of the devices on the I2C bus.
5 Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 EFI_DRIVER_BINDING_PROTOCOL gI2cHostDriverBinding
= {
19 I2cHostDriverSupported
,
30 GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mI2cHostDriverNameTable
[] = {
31 { "eng;en", L
"I2c Host Driver" },
36 // EFI Component Name Protocol
38 GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gI2cHostComponentName
= {
39 (EFI_COMPONENT_NAME_GET_DRIVER_NAME
) I2cHostComponentNameGetDriverName
,
40 (EFI_COMPONENT_NAME_GET_CONTROLLER_NAME
) I2cHostComponentNameGetControllerName
,
45 // EFI Component Name 2 Protocol
47 GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gI2cHostComponentName2
= {
48 I2cHostComponentNameGetDriverName
,
49 I2cHostComponentNameGetControllerName
,
54 Retrieves a Unicode string that is the user readable name of the driver.
56 This function retrieves the user readable name of a driver in the form of a
57 Unicode string. If the driver specified by This has a user readable name in
58 the language specified by Language, then a pointer to the driver name is
59 returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
60 by This does not support the language specified by Language,
61 then EFI_UNSUPPORTED is returned.
63 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
64 EFI_COMPONENT_NAME_PROTOCOL instance.
66 @param Language[in] A pointer to a Null-terminated ASCII string
67 array indicating the language. This is the
68 language of the driver name that the caller is
69 requesting, and it must match one of the
70 languages specified in SupportedLanguages. The
71 number of languages supported by a driver is up
72 to the driver writer. Language is specified
73 in RFC 4646 or ISO 639-2 language code format.
75 @param DriverName[out] A pointer to the Unicode string to return.
76 This Unicode string is the name of the
77 driver specified by This in the language
78 specified by Language.
80 @retval EFI_SUCCESS The Unicode string for the Driver specified by
81 This and the language specified by Language was
82 returned in DriverName.
84 @retval EFI_INVALID_PARAMETER Language is NULL.
86 @retval EFI_INVALID_PARAMETER DriverName is NULL.
88 @retval EFI_UNSUPPORTED The driver specified by This does not support
89 the language specified by Language.
94 I2cHostComponentNameGetDriverName (
95 IN EFI_COMPONENT_NAME2_PROTOCOL
*This
,
97 OUT CHAR16
**DriverName
100 return LookupUnicodeString2 (
102 This
->SupportedLanguages
,
103 mI2cHostDriverNameTable
,
105 (BOOLEAN
)(This
!= &gI2cHostComponentName2
)
110 Retrieves a Unicode string that is the user readable name of the controller
111 that is being managed by a driver.
113 This function retrieves the user readable name of the controller specified by
114 ControllerHandle and ChildHandle in the form of a Unicode string. If the
115 driver specified by This has a user readable name in the language specified by
116 Language, then a pointer to the controller name is returned in ControllerName,
117 and EFI_SUCCESS is returned. If the driver specified by This is not currently
118 managing the controller specified by ControllerHandle and ChildHandle,
119 then EFI_UNSUPPORTED is returned. If the driver specified by This does not
120 support the language specified by Language, then EFI_UNSUPPORTED is returned.
122 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
123 EFI_COMPONENT_NAME_PROTOCOL instance.
125 @param ControllerHandle[in] The handle of a controller that the driver
126 specified by This is managing. This handle
127 specifies the controller whose name is to be
130 @param ChildHandle[in] The handle of the child controller to retrieve
131 the name of. This is an optional parameter that
132 may be NULL. It will be NULL for device
133 drivers. It will also be NULL for a bus drivers
134 that wish to retrieve the name of the bus
135 controller. It will not be NULL for a bus
136 driver that wishes to retrieve the name of a
139 @param Language[in] A pointer to a Null-terminated ASCII string
140 array indicating the language. This is the
141 language of the driver name that the caller is
142 requesting, and it must match one of the
143 languages specified in SupportedLanguages. The
144 number of languages supported by a driver is up
145 to the driver writer. Language is specified in
146 RFC 4646 or ISO 639-2 language code format.
148 @param ControllerName[out] A pointer to the Unicode string to return.
149 This Unicode string is the name of the
150 controller specified by ControllerHandle and
151 ChildHandle in the language specified by
152 Language from the point of view of the driver
155 @retval EFI_SUCCESS The Unicode string for the user readable name in
156 the language specified by Language for the
157 driver specified by This was returned in
160 @retval EFI_INVALID_PARAMETER ControllerHandle is NULL.
162 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
165 @retval EFI_INVALID_PARAMETER Language is NULL.
167 @retval EFI_INVALID_PARAMETER ControllerName is NULL.
169 @retval EFI_UNSUPPORTED The driver specified by This is not currently
170 managing the controller specified by
171 ControllerHandle and ChildHandle.
173 @retval EFI_UNSUPPORTED The driver specified by This does not support
174 the language specified by Language.
179 I2cHostComponentNameGetControllerName (
180 IN EFI_COMPONENT_NAME2_PROTOCOL
*This
,
181 IN EFI_HANDLE ControllerHandle
,
182 IN EFI_HANDLE ChildHandle OPTIONAL
,
184 OUT CHAR16
**ControllerName
187 return EFI_UNSUPPORTED
;
191 Tests to see if this driver supports a given controller. If a child device is provided,
192 it further tests to see if this driver supports creating a handle for the specified child device.
194 This function checks to see if the driver specified by This supports the device specified by
195 ControllerHandle. Drivers will typically use the device path attached to
196 ControllerHandle and/or the services from the bus I/O abstraction attached to
197 ControllerHandle to determine if the driver supports ControllerHandle. This function
198 may be called many times during platform initialization. In order to reduce boot times, the tests
199 performed by this function must be very small, and take as little time as possible to execute. This
200 function must not change the state of any hardware devices, and this function must be aware that the
201 device specified by ControllerHandle may already be managed by the same driver or a
202 different driver. This function must match its calls to AllocatePages() with FreePages(),
203 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
204 Since ControllerHandle may have been previously started by the same driver, if a protocol is
205 already in the opened state, then it must not be closed with CloseProtocol(). This is required
206 to guarantee the state of ControllerHandle is not modified by this function.
208 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
209 @param[in] ControllerHandle The handle of the controller to test. This handle
210 must support a protocol interface that supplies
211 an I/O abstraction to the driver.
212 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
213 parameter is ignored by device drivers, and is optional for bus
214 drivers. For bus drivers, if this parameter is not NULL, then
215 the bus driver must determine if the bus controller specified
216 by ControllerHandle and the child controller specified
217 by RemainingDevicePath are both supported by this
220 @retval EFI_SUCCESS The device specified by ControllerHandle and
221 RemainingDevicePath is supported by the driver specified by This.
222 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
223 RemainingDevicePath is already being managed by the driver
225 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
226 RemainingDevicePath is already being managed by a different
227 driver or an application that requires exclusive access.
228 Currently not implemented.
229 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
230 RemainingDevicePath is not supported by the driver specified by This.
234 I2cHostDriverSupported (
235 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
236 IN EFI_HANDLE Controller
,
237 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
240 EFI_I2C_MASTER_PROTOCOL
*I2cMaster
;
241 EFI_I2C_BUS_CONFIGURATION_MANAGEMENT_PROTOCOL
*I2cBusConfigurationManagement
;
245 // Locate I2C Bus Configuration Management Protocol
247 Status
= gBS
->OpenProtocol (
249 &gEfiI2cBusConfigurationManagementProtocolGuid
,
250 (VOID
**)&I2cBusConfigurationManagement
,
251 This
->DriverBindingHandle
,
253 EFI_OPEN_PROTOCOL_BY_DRIVER
255 if (EFI_ERROR (Status
)) {
260 // Close the protocol because we don't use it here
264 &gEfiI2cBusConfigurationManagementProtocolGuid
,
265 This
->DriverBindingHandle
,
270 // Locate I2C Master Protocol
272 Status
= gBS
->OpenProtocol (
274 &gEfiI2cMasterProtocolGuid
,
276 This
->DriverBindingHandle
,
278 EFI_OPEN_PROTOCOL_GET_PROTOCOL
280 if (EFI_ERROR (Status
)) {
288 Starts a device controller or a bus controller.
290 The Start() function is designed to be invoked from the EFI boot service ConnectController().
291 As a result, much of the error checking on the parameters to Start() has been moved into this
292 common boot service. It is legal to call Start() from other locations,
293 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
294 1. ControllerHandle must be a valid EFI_HANDLE.
295 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
296 EFI_DEVICE_PATH_PROTOCOL.
297 3. Prior to calling Start(), the Supported() function for the driver specified by This must
298 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
300 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
301 @param[in] ControllerHandle The handle of the controller to start. This handle
302 must support a protocol interface that supplies
303 an I/O abstraction to the driver.
304 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
305 parameter is ignored by device drivers, and is optional for bus
306 drivers. For a bus driver, if this parameter is NULL, then handles
307 for all the children of Controller are created by this driver.
308 If this parameter is not NULL and the first Device Path Node is
309 not the End of Device Path Node, then only the handle for the
310 child device specified by the first Device Path Node of
311 RemainingDevicePath is created by this driver.
312 If the first Device Path Node of RemainingDevicePath is
313 the End of Device Path Node, no child handle is created by this
316 @retval EFI_SUCCESS The device was started.
317 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
318 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
319 @retval Others The driver failded to start the device.
325 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
326 IN EFI_HANDLE Controller
,
327 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
331 EFI_I2C_MASTER_PROTOCOL
*I2cMaster
;
332 EFI_I2C_BUS_CONFIGURATION_MANAGEMENT_PROTOCOL
*I2cBusConfigurationManagement
;
333 I2C_HOST_CONTEXT
*I2cHostContext
;
336 I2cHostContext
= NULL
;
337 I2cBusConfigurationManagement
= NULL
;
340 // Locate I2C Bus Configuration Management Protocol
342 Status
= gBS
->OpenProtocol (
344 &gEfiI2cBusConfigurationManagementProtocolGuid
,
345 (VOID
**)&I2cBusConfigurationManagement
,
346 This
->DriverBindingHandle
,
348 EFI_OPEN_PROTOCOL_BY_DRIVER
350 if (EFI_ERROR (Status
)) {
351 DEBUG ((EFI_D_ERROR
, "I2cHost: Open I2C bus configuration error, Status = %r\n", Status
));
356 // Locate I2C Master Protocol
358 Status
= gBS
->OpenProtocol (
360 &gEfiI2cMasterProtocolGuid
,
362 This
->DriverBindingHandle
,
364 EFI_OPEN_PROTOCOL_GET_PROTOCOL
366 if (EFI_ERROR (Status
)) {
367 DEBUG ((EFI_D_ERROR
, "I2cHost: Open I2C master error, Status = %r\n", Status
));
372 // Allocate the I2C Host Context structure
374 I2cHostContext
= AllocateZeroPool (sizeof (I2C_HOST_CONTEXT
));
375 if (I2cHostContext
== NULL
) {
376 DEBUG ((EFI_D_ERROR
, "I2cHost: there is no enough memory to allocate.\n"));
377 Status
= EFI_OUT_OF_RESOURCES
;
382 // Initialize the context structure for the current I2C Controller
384 I2cHostContext
->Signature
= I2C_HOST_SIGNATURE
;
385 I2cHostContext
->I2cMaster
= I2cMaster
;
386 I2cHostContext
->I2cBusConfigurationManagement
= I2cBusConfigurationManagement
;
387 I2cHostContext
->I2cBusConfiguration
= (UINTN
) -1;
388 InitializeListHead(&I2cHostContext
->RequestList
);
391 // Reset the controller
393 Status
= I2cMaster
->Reset (I2cMaster
);
394 if (EFI_ERROR (Status
)) {
395 DEBUG ((EFI_D_ERROR
, "I2cHost: I2C controller reset failed!\n"));
400 // Create the I2C transaction complete event
402 Status
= gBS
->CreateEvent (
405 I2cHostRequestCompleteEvent
,
407 &I2cHostContext
->I2cEvent
409 if (EFI_ERROR (Status
)) {
410 DEBUG ((EFI_D_ERROR
, "I2cHost: create complete event error, Status = %r\n", Status
));
415 // Get the bus management event
417 Status
= gBS
->CreateEvent (
420 I2cHostI2cBusConfigurationAvailable
,
422 &I2cHostContext
->I2cBusConfigurationEvent
424 if (EFI_ERROR (Status
)) {
425 DEBUG ((EFI_D_ERROR
, "I2cHost: create bus available event error, Status = %r\n", Status
));
430 // Build the I2C host protocol for the current I2C controller
432 I2cHostContext
->I2cHost
.QueueRequest
= I2cHostQueueRequest
;
433 I2cHostContext
->I2cHost
.I2cControllerCapabilities
= I2cMaster
->I2cControllerCapabilities
;
436 // Install the driver protocol
438 Status
= gBS
->InstallMultipleProtocolInterfaces (
440 &gEfiI2cHostProtocolGuid
,
441 &I2cHostContext
->I2cHost
,
445 if (EFI_ERROR (Status
)) {
446 DEBUG ((EFI_D_ERROR
, "I2cHost: Start() function failed, Status = %r\n", Status
));
447 if (I2cBusConfigurationManagement
!= NULL
) {
450 &gEfiI2cBusConfigurationManagementProtocolGuid
,
451 This
->DriverBindingHandle
,
456 if ((I2cHostContext
!= NULL
) && (I2cHostContext
->I2cEvent
!= NULL
)) {
457 gBS
->CloseEvent (I2cHostContext
->I2cEvent
);
458 I2cHostContext
->I2cEvent
= NULL
;
461 if ((I2cHostContext
!= NULL
) && (I2cHostContext
->I2cBusConfigurationEvent
!= NULL
)) {
462 gBS
->CloseEvent (I2cHostContext
->I2cBusConfigurationEvent
);
463 I2cHostContext
->I2cBusConfigurationEvent
= NULL
;
467 // Release the context structure upon failure
469 if (I2cHostContext
!= NULL
) {
470 FreePool (I2cHostContext
);
475 // Return the operation status.
481 Stops a device controller or a bus controller.
483 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
484 As a result, much of the error checking on the parameters to Stop() has been moved
485 into this common boot service. It is legal to call Stop() from other locations,
486 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
487 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
488 same driver's Start() function.
489 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
490 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
491 Start() function, and the Start() function must have called OpenProtocol() on
492 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
494 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
495 @param[in] ControllerHandle A handle to the device being stopped. The handle must
496 support a bus specific I/O protocol for the driver
497 to use to stop the device.
498 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
499 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
500 if NumberOfChildren is 0.
502 @retval EFI_SUCCESS The device was stopped.
503 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
509 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
510 IN EFI_HANDLE Controller
,
511 IN UINTN NumberOfChildren
,
512 IN EFI_HANDLE
*ChildHandleBuffer
516 I2C_HOST_CONTEXT
*I2cHostContext
;
517 EFI_I2C_HOST_PROTOCOL
*I2cHost
;
520 TplPrevious
= EfiGetCurrentTpl ();
521 if (TplPrevious
> TPL_I2C_SYNC
) {
522 DEBUG ((EFI_D_ERROR
, "I2cHost: TPL %d is too high in Stop.\n", TplPrevious
));
523 return EFI_DEVICE_ERROR
;
526 Status
= gBS
->OpenProtocol (
528 &gEfiI2cHostProtocolGuid
,
530 This
->DriverBindingHandle
,
532 EFI_OPEN_PROTOCOL_GET_PROTOCOL
535 if (EFI_ERROR (Status
)) {
536 return EFI_DEVICE_ERROR
;
539 I2cHostContext
= I2C_HOST_CONTEXT_FROM_PROTOCOL (I2cHost
);
542 // Raise TPL for critical section
544 TplPrevious
= gBS
->RaiseTPL (TPL_I2C_SYNC
);
547 // If there is pending request or pending bus configuration, do not stop
549 Status
= EFI_DEVICE_ERROR
;
550 if (( !I2cHostContext
->I2cBusConfigurationManagementPending
)
551 && IsListEmpty (&I2cHostContext
->RequestList
)) {
554 // Remove the I2C host protocol
556 Status
= gBS
->UninstallMultipleProtocolInterfaces (
558 &gEfiI2cHostProtocolGuid
,
565 // Leave critical section
567 gBS
->RestoreTPL (TplPrevious
);
568 if (!EFI_ERROR (Status
)) {
571 &gEfiI2cBusConfigurationManagementProtocolGuid
,
572 This
->DriverBindingHandle
,
577 // Release I2c Host resources
579 if (I2cHostContext
->I2cBusConfigurationEvent
!= NULL
) {
580 gBS
->CloseEvent (I2cHostContext
->I2cBusConfigurationEvent
);
581 I2cHostContext
->I2cBusConfigurationEvent
= NULL
;
584 if (I2cHostContext
->I2cEvent
!= NULL
) {
585 gBS
->CloseEvent (I2cHostContext
->I2cEvent
);
586 I2cHostContext
->I2cEvent
= NULL
;
589 FreePool (I2cHostContext
);
593 // Return the stop status
599 Handle the I2C bus configuration available event
601 This routine is called at TPL_I2C_SYNC.
603 @param[in] Event Address of an EFI_EVENT handle
604 @param[in] Context Address of an I2C_HOST_CONTEXT structure
609 I2cHostI2cBusConfigurationAvailable (
614 I2C_HOST_CONTEXT
*I2cHostContext
;
615 EFI_I2C_MASTER_PROTOCOL
*I2cMaster
;
616 I2C_REQUEST
*I2cRequest
;
617 LIST_ENTRY
*EntryHeader
;
622 // Mark this I2C bus configuration management operation as complete
624 I2cHostContext
= (I2C_HOST_CONTEXT
*)Context
;
625 I2cMaster
= I2cHostContext
->I2cMaster
;
626 ASSERT (I2cMaster
!= NULL
);
628 // Clear flag to indicate I2C bus configuration is finished
630 I2cHostContext
->I2cBusConfigurationManagementPending
= FALSE
;
633 // Validate the completion status
635 if (EFI_ERROR (I2cHostContext
->Status
)) {
637 // Setting I2C bus configuration failed before
639 I2cHostRequestComplete (I2cHostContext
, I2cHostContext
->Status
);
642 // Unknown I2C bus configuration
643 // Force next operation to enable the I2C bus configuration
645 I2cHostContext
->I2cBusConfiguration
= (UINTN
) -1;
648 // Do not continue current I2C request
654 // Get the first request in the link with FIFO order
656 EntryHeader
= &I2cHostContext
->RequestList
;
657 Entry
= GetFirstNode (EntryHeader
);
658 I2cRequest
= I2C_REQUEST_FROM_ENTRY (Entry
);
661 // Update the I2C bus configuration of the current I2C request
663 I2cHostContext
->I2cBusConfiguration
= I2cRequest
->I2cBusConfiguration
;
666 // Start an I2C operation on the host, the status is returned by I2cHostContext->Status
668 Status
= I2cMaster
->StartRequest (
670 I2cRequest
->SlaveAddress
,
671 I2cRequest
->RequestPacket
,
672 I2cHostContext
->I2cEvent
,
673 &I2cHostContext
->Status
678 Complete the current request
680 This routine is called at TPL_I2C_SYNC.
682 @param[in] I2cHostContext Address of an I2C_HOST_CONTEXT structure.
683 @param[in] Status Status of the I2C operation.
685 @return This routine returns the input status value.
689 I2cHostRequestComplete (
690 I2C_HOST_CONTEXT
*I2cHostContext
,
694 I2C_REQUEST
*I2cRequest
;
695 LIST_ENTRY
*EntryHeader
;
699 // Remove the current I2C request from the list
701 EntryHeader
= &I2cHostContext
->RequestList
;
702 Entry
= GetFirstNode (EntryHeader
);
703 I2cRequest
= I2C_REQUEST_FROM_ENTRY (Entry
);
706 // Save the status for QueueRequest
708 if ( NULL
!= I2cRequest
->Status
) {
709 *I2cRequest
->Status
= Status
;
713 // Notify the user of the I2C request completion
715 if ( NULL
!= I2cRequest
->Event
) {
716 gBS
->SignalEvent (I2cRequest
->Event
);
720 // Done with this request, remove the current request from list
722 RemoveEntryList (&I2cRequest
->Link
);
723 FreePool (I2cRequest
->RequestPacket
);
724 FreePool (I2cRequest
);
727 // If there is more I2C request, start next one
729 if(!IsListEmpty (EntryHeader
)) {
730 I2cHostRequestEnable (I2cHostContext
);
737 Handle the bus available event
739 This routine is called at TPL_I2C_SYNC.
741 @param[in] Event Address of an EFI_EVENT handle
742 @param[in] Context Address of an I2C_HOST_CONTEXT structure
747 I2cHostRequestCompleteEvent (
752 I2C_HOST_CONTEXT
*I2cHostContext
;
755 // Handle the completion event
757 I2cHostContext
= (I2C_HOST_CONTEXT
*)Context
;
758 I2cHostRequestComplete (I2cHostContext
, I2cHostContext
->Status
);
762 Enable access to the I2C bus configuration
764 @param[in] I2cHostContext Address of an I2C_HOST_CONTEXT structure
766 @retval EFI_SUCCESS The operation completed successfully.
767 @retval EFI_ABORTED The request did not complete because the driver
769 @retval EFI_BAD_BUFFER_SIZE The WriteBytes or ReadBytes buffer size is too large.
770 @retval EFI_DEVICE_ERROR There was an I2C error (NACK) during the operation.
771 This could indicate the slave device is not present.
772 @retval EFI_INVALID_PARAMETER RequestPacket is NULL
773 @retval EFI_NO_MAPPING Invalid I2cBusConfiguration value
774 @retval EFI_NO_RESPONSE The I2C device is not responding to the
775 slave address. EFI_DEVICE_ERROR may also be
776 returned if the controller can not distinguish
777 when the NACK occurred.
778 @retval EFI_NOT_FOUND I2C slave address exceeds maximum address
779 @retval EFI_NOT_READY I2C bus is busy or operation pending, wait for
780 the event and then read status.
781 @retval EFI_OUT_OF_RESOURCES Insufficient memory for I2C operation
782 @retval EFI_TIMEOUT The transaction did not complete within an internally
783 specified timeout period.
787 I2cHostRequestEnable (
788 I2C_HOST_CONTEXT
*I2cHostContext
791 UINTN I2cBusConfiguration
;
792 CONST EFI_I2C_BUS_CONFIGURATION_MANAGEMENT_PROTOCOL
*I2cBusConfigurationManagement
;
793 I2C_REQUEST
*I2cRequest
;
796 LIST_ENTRY
*EntryHeader
;
800 // Assume pending request
802 Status
= EFI_NOT_READY
;
804 I2cBusConfigurationManagement
= I2cHostContext
->I2cBusConfigurationManagement
;
807 // Validate the I2c bus configuration
809 EntryHeader
= &I2cHostContext
->RequestList
;
810 Entry
= GetFirstNode (EntryHeader
);
811 I2cRequest
= I2C_REQUEST_FROM_ENTRY (Entry
);
813 I2cBusConfiguration
= I2cRequest
->I2cBusConfiguration
;
815 if (I2cHostContext
->I2cBusConfiguration
!= I2cBusConfiguration
) {
817 // Set flag to indicate I2C bus configuration is in progress
819 I2cHostContext
->I2cBusConfigurationManagementPending
= TRUE
;
821 // Update bus configuration for this device's requesting bus configuration
823 Status
= I2cBusConfigurationManagement
->EnableI2cBusConfiguration (
824 I2cBusConfigurationManagement
,
826 I2cHostContext
->I2cBusConfigurationEvent
,
827 &I2cHostContext
->Status
831 // I2C bus configuration is same, no need change configuration and start I2c transaction directly
833 TplPrevious
= gBS
->RaiseTPL ( TPL_I2C_SYNC
);
836 // Same I2C bus configuration
838 I2cHostContext
->Status
= EFI_SUCCESS
;
839 I2cHostI2cBusConfigurationAvailable (I2cHostContext
->I2cBusConfigurationEvent
, I2cHostContext
);
842 // Release the thread synchronization
844 gBS
->RestoreTPL ( TplPrevious
);
850 Queue an I2C operation for execution on the I2C controller.
852 This routine must be called at or below TPL_NOTIFY. For synchronous
853 requests this routine must be called at or below TPL_CALLBACK.
855 N.B. The typical consumers of this API are the I2C bus driver and
856 on rare occasions the I2C test application. Extreme care must be
857 taken by other consumers of this API to prevent confusing the
858 third party I2C drivers due to a state change at the I2C device
859 which the third party I2C drivers did not initiate. I2C platform
860 drivers may use this API within these guidelines.
862 This layer uses the concept of I2C bus configurations to describe
863 the I2C bus. An I2C bus configuration is defined as a unique
864 setting of the multiplexers and switches in the I2C bus which
865 enable access to one or more I2C devices. When using a switch
866 to divide a bus, due to speed differences, the I2C platform layer
867 would define an I2C bus configuration for the I2C devices on each
868 side of the switch. When using a multiplexer, the I2C platform
869 layer defines an I2C bus configuration for each of the selector
870 values required to control the multiplexer. See Figure 1 in the
871 <a href="http://www.nxp.com/documents/user_manual/UM10204.pdf">I<sup>2</sup>C
872 Specification</a> for a complex I2C bus configuration.
874 The I2C host driver processes all operations in FIFO order. Prior to
875 performing the operation, the I2C host driver calls the I2C platform
876 driver to reconfigure the switches and multiplexers in the I2C bus
877 enabling access to the specified I2C device. The I2C platform driver
878 also selects the maximum bus speed for the device. After the I2C bus
879 is configured, the I2C host driver calls the I2C port driver to
880 initialize the I2C controller and start the I2C operation.
882 @param[in] This Address of an EFI_I2C_HOST_PROTOCOL instance.
883 @param[in] I2cBusConfiguration I2C bus configuration to access the I2C
885 @param[in] SlaveAddress Address of the device on the I2C bus.
886 @param[in] Event Event to set for asynchronous operations,
887 NULL for synchronous operations
888 @param[in] RequestPacket Address of an EFI_I2C_REQUEST_PACKET
889 structure describing the I2C operation
890 @param[out] I2cStatus Optional buffer to receive the I2C operation
893 @retval EFI_SUCCESS The operation completed successfully.
894 @retval EFI_ABORTED The request did not complete because the driver
896 @retval EFI_BAD_BUFFER_SIZE The WriteBytes or ReadBytes buffer size is too large.
897 @retval EFI_DEVICE_ERROR There was an I2C error (NACK) during the operation.
898 This could indicate the slave device is not present.
899 @retval EFI_INVALID_PARAMETER RequestPacket is NULL
900 @retval EFI_INVALID_PARAMETER TPL is too high
901 @retval EFI_NO_MAPPING Invalid I2cBusConfiguration value
902 @retval EFI_NO_RESPONSE The I2C device is not responding to the
903 slave address. EFI_DEVICE_ERROR may also be
904 returned if the controller can not distinguish
905 when the NACK occurred.
906 @retval EFI_NOT_FOUND I2C slave address exceeds maximum address
907 @retval EFI_NOT_READY I2C bus is busy or operation pending, wait for
908 the event and then read status pointed to by
910 @retval EFI_OUT_OF_RESOURCES Insufficient memory for I2C operation
911 @retval EFI_TIMEOUT The transaction did not complete within an internally
912 specified timeout period.
917 I2cHostQueueRequest (
918 IN CONST EFI_I2C_HOST_PROTOCOL
*This
,
919 IN UINTN I2cBusConfiguration
,
920 IN UINTN SlaveAddress
,
921 IN EFI_EVENT Event OPTIONAL
,
922 IN EFI_I2C_REQUEST_PACKET
*RequestPacket
,
923 OUT EFI_STATUS
*I2cStatus OPTIONAL
929 I2C_REQUEST
*I2cRequest
;
930 I2C_HOST_CONTEXT
*I2cHostContext
;
931 BOOLEAN FirstRequest
;
932 UINTN RequestPacketSize
;
936 FirstRequest
= FALSE
;
937 Status
= EFI_SUCCESS
;
939 if (RequestPacket
== NULL
) {
940 return EFI_INVALID_PARAMETER
;
943 if ((SlaveAddress
& I2C_ADDRESSING_10_BIT
) != 0) {
945 // 10-bit address, bits 0-9 are used for 10-bit I2C slave addresses,
946 // bits 10-30 are reserved bits and must be zero
951 // 7-bit address, Bits 0-6 are used for 7-bit I2C slave addresses,
952 // bits 7-30 are reserved bits and must be zero
957 if (BitFieldRead32 ((UINT32
)SlaveAddress
, StartBit
, 30) != 0) {
959 // Reserved bit set in the SlaveAddress parameter
961 return EFI_NOT_FOUND
;
964 I2cHostContext
= I2C_HOST_CONTEXT_FROM_PROTOCOL (This
);
968 // For synchronous transaction, register an event used to wait for finishing synchronous transaction
970 Status
= gBS
->CreateEvent (
977 if (EFI_ERROR (Status
)) {
983 // TPL should be at or below TPL_NOTIFY.
984 // For synchronous requests this routine must be called at or below TPL_CALLBACK.
986 TplPrevious
= EfiGetCurrentTpl ();
987 if ((TplPrevious
> TPL_I2C_SYNC
) || ((Event
== NULL
) && (TplPrevious
> TPL_CALLBACK
))) {
988 DEBUG ((EFI_D_ERROR
, "ERROR - TPL %d is too high!\n", TplPrevious
));
989 return EFI_INVALID_PARAMETER
;
993 // Allocate the request structure
995 I2cRequest
= AllocateZeroPool (sizeof (I2C_REQUEST
));
996 if (I2cRequest
== NULL
) {
997 DEBUG ((EFI_D_ERROR
, "WARNING - Failed to allocate I2C_REQUEST!\n"));
998 return EFI_OUT_OF_RESOURCES
;
1002 // Initialize the request
1004 I2cRequest
->Signature
= I2C_REQUEST_SIGNATURE
;
1005 I2cRequest
->I2cBusConfiguration
= I2cBusConfiguration
;
1006 I2cRequest
->SlaveAddress
= SlaveAddress
;
1007 I2cRequest
->Event
= (Event
== NULL
) ? SyncEvent
: Event
;
1008 I2cRequest
->Status
= I2cStatus
;
1011 // Copy request packet into private buffer, as RequestPacket may be freed during asynchronous transaction
1013 RequestPacketSize
= sizeof (UINTN
) + RequestPacket
->OperationCount
* sizeof (EFI_I2C_OPERATION
);
1014 I2cRequest
->RequestPacket
= AllocateZeroPool (RequestPacketSize
);
1015 CopyMem (I2cRequest
->RequestPacket
, RequestPacket
, RequestPacketSize
);
1018 // Synchronize with the other threads
1020 gBS
->RaiseTPL ( TPL_I2C_SYNC
);
1022 FirstRequest
= IsListEmpty (&I2cHostContext
->RequestList
);
1025 // Insert new I2C request in the list
1027 InsertTailList (&I2cHostContext
->RequestList
, &I2cRequest
->Link
);
1030 // Release the thread synchronization
1032 gBS
->RestoreTPL (TplPrevious
);
1036 // Start the first I2C request, then the subsequent of I2C request will continue
1038 Status
= I2cHostRequestEnable (I2cHostContext
);
1041 if (Event
!= NULL
) {
1043 // For asynchronous, return EFI_SUCCESS indicating that the asynchronously I2C transaction was queued.
1044 // No real I2C operation status in I2cStatus
1050 // For synchronous transaction, wait for the operation completion
1053 Status
= gBS
->CheckEvent (SyncEvent
);
1054 } while (Status
== EFI_NOT_READY
);
1057 // Get the I2C operation status
1059 Status
= I2cHostContext
->Status
;
1062 // Return the I2C operation status
1064 if (I2cStatus
!= NULL
) {
1065 *I2cStatus
= Status
;
1069 // Close the event if necessary
1071 if (SyncEvent
!= NULL
) {
1072 gBS
->CloseEvent (SyncEvent
);
1079 The user Entry Point for I2C host module. The user code starts with this function.
1081 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1082 @param[in] SystemTable A pointer to the EFI System Table.
1084 @retval EFI_SUCCESS The entry point is executed successfully.
1085 @retval other Some error occurs when executing this entry point.
1091 IN EFI_HANDLE ImageHandle
,
1092 IN EFI_SYSTEM_TABLE
*SystemTable
1098 // Install driver model protocol(s).
1100 Status
= EfiLibInstallDriverBindingComponentName2 (
1103 &gI2cHostDriverBinding
,
1105 &gI2cHostComponentName
,
1106 &gI2cHostComponentName2
1108 ASSERT_EFI_ERROR (Status
);
1113 This is the unload handle for I2C host module.
1115 Disconnect the driver specified by ImageHandle from all the devices in the handle database.
1116 Uninstall all the protocols installed in the driver entry point.
1118 @param[in] ImageHandle The drivers' driver image.
1120 @retval EFI_SUCCESS The image is unloaded.
1121 @retval Others Failed to unload the image.
1127 IN EFI_HANDLE ImageHandle
1131 EFI_HANDLE
*DeviceHandleBuffer
;
1132 UINTN DeviceHandleCount
;
1136 // Get the list of all I2C Controller handles in the handle database.
1137 // If there is an error getting the list, then the unload
1140 Status
= gBS
->LocateHandleBuffer (
1142 &gEfiI2cHostProtocolGuid
,
1148 if (EFI_ERROR (Status
)) {
1153 // Disconnect the driver specified by ImageHandle from all
1154 // the devices in the handle database.
1156 for (Index
= 0; Index
< DeviceHandleCount
; Index
++) {
1157 Status
= gBS
->DisconnectController (
1158 DeviceHandleBuffer
[Index
],
1162 if (EFI_ERROR (Status
)) {
1168 // Uninstall all the protocols installed in the driver entry point
1170 Status
= gBS
->UninstallMultipleProtocolInterfaces (
1171 gI2cHostDriverBinding
.DriverBindingHandle
,
1172 &gEfiDriverBindingProtocolGuid
,
1173 &gI2cHostDriverBinding
,
1174 &gEfiComponentNameProtocolGuid
,
1175 &gI2cHostComponentName
,
1176 &gEfiComponentName2ProtocolGuid
,
1177 &gI2cHostComponentName2
,
1180 ASSERT_EFI_ERROR (Status
);
1184 // Free the buffer containing the list of handles from the handle database
1186 if (DeviceHandleBuffer
!= NULL
) {
1187 gBS
->FreePool (DeviceHandleBuffer
);