2 Common code to implement SMBus bus protocols. Smbus PEI and DXE modules
3 share the same version of this file.
5 Copyright (c) 2013-2015 Intel Corporation.
7 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "CommonHeader.h"
15 Checks the parameter of SmbusExecute().
17 This function checks the input parameters of SmbusExecute(). If the input parameters are valid
18 for certain SMBus bus protocol, it will return EFI_SUCCESS; otherwise, it will return certain
19 error code based on the input SMBus bus protocol.
21 @param SlaveAddress The SMBus slave address of the device with which to communicate.
22 @param Command This command is transmitted by the SMBus host controller to the
23 SMBus slave device and the interpretation is SMBus slave device
24 specific. It can mean the offset to a list of functions inside an
25 SMBus slave device. Not all operations or slave devices support
26 this command's registers.
27 @param Operation Signifies which particular SMBus hardware protocol instance that
28 it will use to execute the SMBus transactions. This SMBus
29 hardware protocol is defined by the SMBus Specification and is
31 @param PecCheck Defines if Packet Error Code (PEC) checking is required for this
33 @param Length Signifies the number of bytes that this operation will do. The
34 maximum number of bytes can be revision specific and operation
35 specific. This field will contain the actual number of bytes that
36 are executed for this operation. Not all operations require this
38 @param Buffer Contains the value of data to execute to the SMBus slave device.
39 Not all operations require this argument. The length of this
40 buffer is identified by Length.
42 @retval EFI_SUCCESS All the parameters are valid for the corresponding SMBus bus
44 @retval EFI_INVALID_PARAMETER Operation is not defined in EFI_SMBUS_OPERATION.
45 @retval EFI_INVALID_PARAMETER Length/Buffer is NULL for operations except for EfiSmbusQuickRead
46 and EfiSmbusQuickWrite. Length is outside the range of valid
48 @retval EFI_UNSUPPORTED The SMBus operation or PEC is not supported.
49 @retval EFI_BUFFER_TOO_SMALL Buffer is not sufficient for this operation.
53 QncSmbusExecCheckParameters (
54 IN EFI_SMBUS_DEVICE_ADDRESS SlaveAddress
,
55 IN EFI_SMBUS_DEVICE_COMMAND Command
,
56 IN EFI_SMBUS_OPERATION Operation
,
66 // Set default value to be 2:
67 // for SmbusReadWord, SmbusWriteWord and SmbusProcessCall.
72 case EfiSmbusQuickRead
:
73 case EfiSmbusQuickWrite
:
74 if (PecCheck
|| Command
!= 0) {
75 return EFI_UNSUPPORTED
;
78 case EfiSmbusReceiveByte
:
79 case EfiSmbusSendByte
:
81 return EFI_UNSUPPORTED
;
84 // Cascade to check length parameter.
86 case EfiSmbusReadByte
:
87 case EfiSmbusWriteByte
:
90 // Cascade to check length parameter.
92 case EfiSmbusReadWord
:
93 case EfiSmbusWriteWord
:
94 case EfiSmbusProcessCall
:
95 if (Buffer
== NULL
|| Length
== NULL
) {
96 return EFI_INVALID_PARAMETER
;
97 } else if (*Length
< RequiredLen
) {
98 Status
= EFI_BUFFER_TOO_SMALL
;
100 *Length
= RequiredLen
;
102 case EfiSmbusReadBlock
:
103 case EfiSmbusWriteBlock
:
104 if ((Buffer
== NULL
) ||
106 (*Length
< MIN_SMBUS_BLOCK_LEN
) ||
107 (*Length
> MAX_SMBUS_BLOCK_LEN
)) {
108 return EFI_INVALID_PARAMETER
;
111 case EfiSmbusBWBRProcessCall
:
112 return EFI_UNSUPPORTED
;
114 return EFI_INVALID_PARAMETER
;
120 Executes an SMBus operation to an SMBus controller. Returns when either the command has been
121 executed or an error is encountered in doing the operation.
123 The internal worker function provides a standard way to execute an operation as defined in the
124 System Management Bus (SMBus) Specification. The resulting transaction will be either that the
125 SMBus slave devices accept this transaction or that this function returns with error.
127 @param SlaveAddress The SMBus slave address of the device with which to communicate.
128 @param Command This command is transmitted by the SMBus host controller to the
129 SMBus slave device and the interpretation is SMBus slave device
130 specific. It can mean the offset to a list of functions inside an
131 SMBus slave device. Not all operations or slave devices support
132 this command's registers.
133 @param Operation Signifies which particular SMBus hardware protocol instance that
134 it will use to execute the SMBus transactions. This SMBus
135 hardware protocol is defined by the SMBus Specification and is
137 @param PecCheck Defines if Packet Error Code (PEC) checking is required for this
139 @param Length Signifies the number of bytes that this operation will do. The
140 maximum number of bytes can be revision specific and operation
141 specific. This field will contain the actual number of bytes that
142 are executed for this operation. Not all operations require this
144 @param Buffer Contains the value of data to execute to the SMBus slave device.
145 Not all operations require this argument. The length of this
146 buffer is identified by Length.
148 @retval EFI_SUCCESS The last data that was returned from the access matched the poll
150 @retval EFI_CRC_ERROR Checksum is not correct (PEC is incorrect).
151 @retval EFI_TIMEOUT Timeout expired before the operation was completed. Timeout is
152 determined by the SMBus host controller device.
153 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
154 @retval EFI_DEVICE_ERROR The request was not completed because a failure that was
155 reflected in the Host Status Register bit. Device errors are a
156 result of a transaction collision, illegal command field,
157 unclaimed cycle (host initiated), or bus errors (collisions).
158 @retval EFI_INVALID_PARAMETER Operation is not defined in EFI_SMBUS_OPERATION.
159 @retval EFI_INVALID_PARAMETER Length/Buffer is NULL for operations except for EfiSmbusQuickRead
160 and EfiSmbusQuickWrite. Length is outside the range of valid
162 @retval EFI_UNSUPPORTED The SMBus operation or PEC is not supported.
163 @retval EFI_BUFFER_TOO_SMALL Buffer is not sufficient for this operation.
168 IN EFI_SMBUS_DEVICE_ADDRESS SlaveAddress
,
169 IN EFI_SMBUS_DEVICE_COMMAND Command
,
170 IN EFI_SMBUS_OPERATION Operation
,
172 IN OUT UINTN
*Length
,
179 UINT8 WorkBuffer
[MAX_SMBUS_BLOCK_LEN
];
181 Status
= QncSmbusExecCheckParameters (
188 if (EFI_ERROR (Status
)) {
192 SmbusAddress
= SMBUS_LIB_ADDRESS (SlaveAddress
.SmbusDeviceAddress
, Command
, *Length
, PecCheck
);
195 case EfiSmbusQuickRead
:
196 SmBusQuickRead (SmbusAddress
, &Status
);
198 case EfiSmbusQuickWrite
:
199 SmBusQuickWrite (SmbusAddress
, &Status
);
201 case EfiSmbusReceiveByte
:
202 *(UINT8
*) Buffer
= SmBusReceiveByte (SmbusAddress
, &Status
);
204 case EfiSmbusSendByte
:
205 SmBusSendByte (SmbusAddress
, *(UINT8
*) Buffer
, &Status
);
207 case EfiSmbusReadByte
:
208 *(UINT8
*) Buffer
= SmBusReadDataByte (SmbusAddress
, &Status
);
210 case EfiSmbusWriteByte
:
211 SmBusWriteDataByte (SmbusAddress
, *(UINT8
*) Buffer
, &Status
);
213 case EfiSmbusReadWord
:
214 *(UINT16
*) Buffer
= SmBusReadDataWord (SmbusAddress
, &Status
);
216 case EfiSmbusWriteWord
:
217 SmBusWriteDataWord (SmbusAddress
, *(UINT16
*) Buffer
, &Status
);
219 case EfiSmbusProcessCall
:
220 *(UINT16
*) Buffer
= SmBusProcessCall (SmbusAddress
, *(UINT16
*) Buffer
, &Status
);
222 case EfiSmbusReadBlock
:
223 WorkBufferLen
= SmBusReadBlock (SmbusAddress
, WorkBuffer
, &Status
);
224 if (!EFI_ERROR (Status
)) {
226 // Read block transaction is complete successfully, and then
227 // check whether the output buffer is large enough.
229 if (*Length
>= WorkBufferLen
) {
230 CopyMem (Buffer
, WorkBuffer
, WorkBufferLen
);
232 Status
= EFI_BUFFER_TOO_SMALL
;
234 *Length
= WorkBufferLen
;
237 case EfiSmbusWriteBlock
:
238 SmBusWriteBlock (ADD_LENGTH (SmbusAddress
, *Length
), Buffer
, &Status
);