]> git.proxmox.com Git - mirror_edk2.git/blob - QuarkSocPkg/QuarkNorthCluster/QNCInit/Dxe/QNCSmbusExec.c
QuarkSocPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / QuarkSocPkg / QuarkNorthCluster / QNCInit / Dxe / QNCSmbusExec.c
1 /** @file
2 Common code to implement SMBus bus protocols. Smbus PEI and DXE modules
3 share the same version of this file.
4
5 Copyright (c) 2013-2015 Intel Corporation.
6
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10 #include "CommonHeader.h"
11
12 #include "QNCSmbus.h"
13
14 /**
15 Checks the parameter of SmbusExecute().
16
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.
20
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
30 not related to EFI.
31 @param PecCheck Defines if Packet Error Code (PEC) checking is required for this
32 operation.
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
37 argument.
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.
41
42 @retval EFI_SUCCESS All the parameters are valid for the corresponding SMBus bus
43 protocol.
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
47 values.
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.
50
51 **/
52 EFI_STATUS
53 QncSmbusExecCheckParameters (
54 IN EFI_SMBUS_DEVICE_ADDRESS SlaveAddress,
55 IN EFI_SMBUS_DEVICE_COMMAND Command,
56 IN EFI_SMBUS_OPERATION Operation,
57 IN BOOLEAN PecCheck,
58 IN OUT UINTN *Length,
59 IN OUT VOID *Buffer
60 )
61 {
62 EFI_STATUS Status;
63 UINTN RequiredLen;
64
65 //
66 // Set default value to be 2:
67 // for SmbusReadWord, SmbusWriteWord and SmbusProcessCall.
68 //
69 RequiredLen = 2;
70 Status = EFI_SUCCESS;
71 switch (Operation) {
72 case EfiSmbusQuickRead:
73 case EfiSmbusQuickWrite:
74 if (PecCheck || Command != 0) {
75 return EFI_UNSUPPORTED;
76 }
77 break;
78 case EfiSmbusReceiveByte:
79 case EfiSmbusSendByte:
80 if (Command != 0) {
81 return EFI_UNSUPPORTED;
82 }
83 //
84 // Cascade to check length parameter.
85 //
86 case EfiSmbusReadByte:
87 case EfiSmbusWriteByte:
88 RequiredLen = 1;
89 //
90 // Cascade to check length parameter.
91 //
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;
99 }
100 *Length = RequiredLen;
101 break;
102 case EfiSmbusReadBlock:
103 case EfiSmbusWriteBlock:
104 if ((Buffer == NULL) ||
105 (Length == NULL) ||
106 (*Length < MIN_SMBUS_BLOCK_LEN) ||
107 (*Length > MAX_SMBUS_BLOCK_LEN)) {
108 return EFI_INVALID_PARAMETER;
109 }
110 break;
111 case EfiSmbusBWBRProcessCall:
112 return EFI_UNSUPPORTED;
113 default:
114 return EFI_INVALID_PARAMETER;
115 }
116 return Status;
117 }
118
119 /**
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.
122
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.
126
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
136 not related to EFI.
137 @param PecCheck Defines if Packet Error Code (PEC) checking is required for this
138 operation.
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
143 argument.
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.
147
148 @retval EFI_SUCCESS The last data that was returned from the access matched the poll
149 exit criteria.
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
161 values.
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.
164
165 **/
166 EFI_STATUS
167 Execute (
168 IN EFI_SMBUS_DEVICE_ADDRESS SlaveAddress,
169 IN EFI_SMBUS_DEVICE_COMMAND Command,
170 IN EFI_SMBUS_OPERATION Operation,
171 IN BOOLEAN PecCheck,
172 IN OUT UINTN *Length,
173 IN OUT VOID *Buffer
174 )
175 {
176 EFI_STATUS Status;
177 UINTN SmbusAddress;
178 UINTN WorkBufferLen;
179 UINT8 WorkBuffer[MAX_SMBUS_BLOCK_LEN];
180
181 Status = QncSmbusExecCheckParameters (
182 SlaveAddress,
183 Command,
184 Operation,
185 PecCheck,
186 Length,
187 Buffer);
188 if (EFI_ERROR (Status)) {
189 return Status;
190 }
191
192 SmbusAddress = SMBUS_LIB_ADDRESS (SlaveAddress.SmbusDeviceAddress, Command, *Length, PecCheck);
193
194 switch (Operation) {
195 case EfiSmbusQuickRead:
196 SmBusQuickRead (SmbusAddress, &Status);
197 break;
198 case EfiSmbusQuickWrite:
199 SmBusQuickWrite (SmbusAddress, &Status);
200 break;
201 case EfiSmbusReceiveByte:
202 *(UINT8 *) Buffer = SmBusReceiveByte (SmbusAddress, &Status);
203 break;
204 case EfiSmbusSendByte:
205 SmBusSendByte (SmbusAddress, *(UINT8 *) Buffer, &Status);
206 break;
207 case EfiSmbusReadByte:
208 *(UINT8 *) Buffer = SmBusReadDataByte (SmbusAddress, &Status);
209 break;
210 case EfiSmbusWriteByte:
211 SmBusWriteDataByte (SmbusAddress, *(UINT8 *) Buffer, &Status);
212 break;
213 case EfiSmbusReadWord:
214 *(UINT16 *) Buffer = SmBusReadDataWord (SmbusAddress, &Status);
215 break;
216 case EfiSmbusWriteWord:
217 SmBusWriteDataWord (SmbusAddress, *(UINT16 *) Buffer, &Status);
218 break;
219 case EfiSmbusProcessCall:
220 *(UINT16 *) Buffer = SmBusProcessCall (SmbusAddress, *(UINT16 *) Buffer, &Status);
221 break;
222 case EfiSmbusReadBlock:
223 WorkBufferLen = SmBusReadBlock (SmbusAddress, WorkBuffer, &Status);
224 if (!EFI_ERROR (Status)) {
225 //
226 // Read block transaction is complete successfully, and then
227 // check whether the output buffer is large enough.
228 //
229 if (*Length >= WorkBufferLen) {
230 CopyMem (Buffer, WorkBuffer, WorkBufferLen);
231 } else {
232 Status = EFI_BUFFER_TOO_SMALL;
233 }
234 *Length = WorkBufferLen;
235 }
236 break;
237 case EfiSmbusWriteBlock:
238 SmBusWriteBlock (ADD_LENGTH (SmbusAddress, *Length), Buffer, &Status);
239 break;
240 default:
241 break;
242 }
243
244 return Status;
245 }
246