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 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "CommonHeader.h"
21 Checks the parameter of SmbusExecute().
23 This function checks the input parameters of SmbusExecute(). If the input parameters are valid
24 for certain SMBus bus protocol, it will return EFI_SUCCESS; otherwise, it will return certain
25 error code based on the input SMBus bus protocol.
27 @param SlaveAddress The SMBus slave address of the device with which to communicate.
28 @param Command This command is transmitted by the SMBus host controller to the
29 SMBus slave device and the interpretation is SMBus slave device
30 specific. It can mean the offset to a list of functions inside an
31 SMBus slave device. Not all operations or slave devices support
32 this command's registers.
33 @param Operation Signifies which particular SMBus hardware protocol instance that
34 it will use to execute the SMBus transactions. This SMBus
35 hardware protocol is defined by the SMBus Specification and is
37 @param PecCheck Defines if Packet Error Code (PEC) checking is required for this
39 @param Length Signifies the number of bytes that this operation will do. The
40 maximum number of bytes can be revision specific and operation
41 specific. This field will contain the actual number of bytes that
42 are executed for this operation. Not all operations require this
44 @param Buffer Contains the value of data to execute to the SMBus slave device.
45 Not all operations require this argument. The length of this
46 buffer is identified by Length.
48 @retval EFI_SUCCESS All the parameters are valid for the corresponding SMBus bus
50 @retval EFI_INVALID_PARAMETER Operation is not defined in EFI_SMBUS_OPERATION.
51 @retval EFI_INVALID_PARAMETER Length/Buffer is NULL for operations except for EfiSmbusQuickRead
52 and EfiSmbusQuickWrite. Length is outside the range of valid
54 @retval EFI_UNSUPPORTED The SMBus operation or PEC is not supported.
55 @retval EFI_BUFFER_TOO_SMALL Buffer is not sufficient for this operation.
59 QncSmbusExecCheckParameters (
60 IN EFI_SMBUS_DEVICE_ADDRESS SlaveAddress
,
61 IN EFI_SMBUS_DEVICE_COMMAND Command
,
62 IN EFI_SMBUS_OPERATION Operation
,
72 // Set default value to be 2:
73 // for SmbusReadWord, SmbusWriteWord and SmbusProcessCall.
78 case EfiSmbusQuickRead
:
79 case EfiSmbusQuickWrite
:
80 if (PecCheck
|| Command
!= 0) {
81 return EFI_UNSUPPORTED
;
84 case EfiSmbusReceiveByte
:
85 case EfiSmbusSendByte
:
87 return EFI_UNSUPPORTED
;
90 // Cascade to check length parameter.
92 case EfiSmbusReadByte
:
93 case EfiSmbusWriteByte
:
96 // Cascade to check length parameter.
98 case EfiSmbusReadWord
:
99 case EfiSmbusWriteWord
:
100 case EfiSmbusProcessCall
:
101 if (Buffer
== NULL
|| Length
== NULL
) {
102 return EFI_INVALID_PARAMETER
;
103 } else if (*Length
< RequiredLen
) {
104 Status
= EFI_BUFFER_TOO_SMALL
;
106 *Length
= RequiredLen
;
108 case EfiSmbusReadBlock
:
109 case EfiSmbusWriteBlock
:
110 if ((Buffer
== NULL
) ||
112 (*Length
< MIN_SMBUS_BLOCK_LEN
) ||
113 (*Length
> MAX_SMBUS_BLOCK_LEN
)) {
114 return EFI_INVALID_PARAMETER
;
117 case EfiSmbusBWBRProcessCall
:
118 return EFI_UNSUPPORTED
;
120 return EFI_INVALID_PARAMETER
;
126 Executes an SMBus operation to an SMBus controller. Returns when either the command has been
127 executed or an error is encountered in doing the operation.
129 The internal worker function provides a standard way to execute an operation as defined in the
130 System Management Bus (SMBus) Specification. The resulting transaction will be either that the
131 SMBus slave devices accept this transaction or that this function returns with error.
133 @param SlaveAddress The SMBus slave address of the device with which to communicate.
134 @param Command This command is transmitted by the SMBus host controller to the
135 SMBus slave device and the interpretation is SMBus slave device
136 specific. It can mean the offset to a list of functions inside an
137 SMBus slave device. Not all operations or slave devices support
138 this command's registers.
139 @param Operation Signifies which particular SMBus hardware protocol instance that
140 it will use to execute the SMBus transactions. This SMBus
141 hardware protocol is defined by the SMBus Specification and is
143 @param PecCheck Defines if Packet Error Code (PEC) checking is required for this
145 @param Length Signifies the number of bytes that this operation will do. The
146 maximum number of bytes can be revision specific and operation
147 specific. This field will contain the actual number of bytes that
148 are executed for this operation. Not all operations require this
150 @param Buffer Contains the value of data to execute to the SMBus slave device.
151 Not all operations require this argument. The length of this
152 buffer is identified by Length.
154 @retval EFI_SUCCESS The last data that was returned from the access matched the poll
156 @retval EFI_CRC_ERROR Checksum is not correct (PEC is incorrect).
157 @retval EFI_TIMEOUT Timeout expired before the operation was completed. Timeout is
158 determined by the SMBus host controller device.
159 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
160 @retval EFI_DEVICE_ERROR The request was not completed because a failure that was
161 reflected in the Host Status Register bit. Device errors are a
162 result of a transaction collision, illegal command field,
163 unclaimed cycle (host initiated), or bus errors (collisions).
164 @retval EFI_INVALID_PARAMETER Operation is not defined in EFI_SMBUS_OPERATION.
165 @retval EFI_INVALID_PARAMETER Length/Buffer is NULL for operations except for EfiSmbusQuickRead
166 and EfiSmbusQuickWrite. Length is outside the range of valid
168 @retval EFI_UNSUPPORTED The SMBus operation or PEC is not supported.
169 @retval EFI_BUFFER_TOO_SMALL Buffer is not sufficient for this operation.
174 IN EFI_SMBUS_DEVICE_ADDRESS SlaveAddress
,
175 IN EFI_SMBUS_DEVICE_COMMAND Command
,
176 IN EFI_SMBUS_OPERATION Operation
,
178 IN OUT UINTN
*Length
,
185 UINT8 WorkBuffer
[MAX_SMBUS_BLOCK_LEN
];
187 Status
= QncSmbusExecCheckParameters (
194 if (EFI_ERROR (Status
)) {
198 SmbusAddress
= SMBUS_LIB_ADDRESS (SlaveAddress
.SmbusDeviceAddress
, Command
, *Length
, PecCheck
);
201 case EfiSmbusQuickRead
:
202 SmBusQuickRead (SmbusAddress
, &Status
);
204 case EfiSmbusQuickWrite
:
205 SmBusQuickWrite (SmbusAddress
, &Status
);
207 case EfiSmbusReceiveByte
:
208 *(UINT8
*) Buffer
= SmBusReceiveByte (SmbusAddress
, &Status
);
210 case EfiSmbusSendByte
:
211 SmBusSendByte (SmbusAddress
, *(UINT8
*) Buffer
, &Status
);
213 case EfiSmbusReadByte
:
214 *(UINT8
*) Buffer
= SmBusReadDataByte (SmbusAddress
, &Status
);
216 case EfiSmbusWriteByte
:
217 SmBusWriteDataByte (SmbusAddress
, *(UINT8
*) Buffer
, &Status
);
219 case EfiSmbusReadWord
:
220 *(UINT16
*) Buffer
= SmBusReadDataWord (SmbusAddress
, &Status
);
222 case EfiSmbusWriteWord
:
223 SmBusWriteDataWord (SmbusAddress
, *(UINT16
*) Buffer
, &Status
);
225 case EfiSmbusProcessCall
:
226 *(UINT16
*) Buffer
= SmBusProcessCall (SmbusAddress
, *(UINT16
*) Buffer
, &Status
);
228 case EfiSmbusReadBlock
:
229 WorkBufferLen
= SmBusReadBlock (SmbusAddress
, WorkBuffer
, &Status
);
230 if (!EFI_ERROR (Status
)) {
232 // Read block transaction is complete successfully, and then
233 // check whether the output buffer is large enough.
235 if (*Length
>= WorkBufferLen
) {
236 CopyMem (Buffer
, WorkBuffer
, WorkBufferLen
);
238 Status
= EFI_BUFFER_TOO_SMALL
;
240 *Length
= WorkBufferLen
;
243 case EfiSmbusWriteBlock
:
244 SmBusWriteBlock (ADD_LENGTH (SmbusAddress
, *Length
), Buffer
, &Status
);