]> git.proxmox.com Git - mirror_edk2.git/blame - QuarkSocPkg/QuarkNorthCluster/QNCInit/Dxe/QNCSmbusExec.c
QuarkSocPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / QuarkSocPkg / QuarkNorthCluster / QNCInit / Dxe / QNCSmbusExec.c
CommitLineData
9b6bbcdb
MK
1/** @file\r
2Common code to implement SMBus bus protocols. Smbus PEI and DXE modules\r
3share the same version of this file.\r
4\r
5Copyright (c) 2013-2015 Intel Corporation.\r
6\r
c9f231d0 7SPDX-License-Identifier: BSD-2-Clause-Patent\r
9b6bbcdb
MK
8\r
9**/\r
10#include "CommonHeader.h"\r
11\r
12#include "QNCSmbus.h"\r
13\r
14/**\r
15 Checks the parameter of SmbusExecute().\r
16\r
17 This function checks the input parameters of SmbusExecute(). If the input parameters are valid\r
18 for certain SMBus bus protocol, it will return EFI_SUCCESS; otherwise, it will return certain\r
19 error code based on the input SMBus bus protocol.\r
20\r
21 @param SlaveAddress The SMBus slave address of the device with which to communicate.\r
22 @param Command This command is transmitted by the SMBus host controller to the\r
23 SMBus slave device and the interpretation is SMBus slave device\r
24 specific. It can mean the offset to a list of functions inside an\r
25 SMBus slave device. Not all operations or slave devices support\r
26 this command's registers.\r
27 @param Operation Signifies which particular SMBus hardware protocol instance that\r
28 it will use to execute the SMBus transactions. This SMBus\r
29 hardware protocol is defined by the SMBus Specification and is\r
30 not related to EFI.\r
31 @param PecCheck Defines if Packet Error Code (PEC) checking is required for this\r
32 operation.\r
33 @param Length Signifies the number of bytes that this operation will do. The\r
34 maximum number of bytes can be revision specific and operation\r
35 specific. This field will contain the actual number of bytes that\r
36 are executed for this operation. Not all operations require this\r
37 argument.\r
38 @param Buffer Contains the value of data to execute to the SMBus slave device.\r
39 Not all operations require this argument. The length of this\r
40 buffer is identified by Length.\r
41\r
42 @retval EFI_SUCCESS All the parameters are valid for the corresponding SMBus bus\r
43 protocol.\r
44 @retval EFI_INVALID_PARAMETER Operation is not defined in EFI_SMBUS_OPERATION.\r
45 @retval EFI_INVALID_PARAMETER Length/Buffer is NULL for operations except for EfiSmbusQuickRead\r
46 and EfiSmbusQuickWrite. Length is outside the range of valid\r
47 values.\r
48 @retval EFI_UNSUPPORTED The SMBus operation or PEC is not supported.\r
49 @retval EFI_BUFFER_TOO_SMALL Buffer is not sufficient for this operation.\r
50\r
51**/\r
52EFI_STATUS\r
53QncSmbusExecCheckParameters (\r
54 IN EFI_SMBUS_DEVICE_ADDRESS SlaveAddress,\r
55 IN EFI_SMBUS_DEVICE_COMMAND Command,\r
56 IN EFI_SMBUS_OPERATION Operation,\r
57 IN BOOLEAN PecCheck,\r
58 IN OUT UINTN *Length,\r
59 IN OUT VOID *Buffer\r
60 )\r
61{\r
62 EFI_STATUS Status;\r
63 UINTN RequiredLen;\r
64\r
65 //\r
66 // Set default value to be 2:\r
67 // for SmbusReadWord, SmbusWriteWord and SmbusProcessCall.\r
68 //\r
69 RequiredLen = 2;\r
70 Status = EFI_SUCCESS;\r
71 switch (Operation) {\r
72 case EfiSmbusQuickRead:\r
73 case EfiSmbusQuickWrite:\r
74 if (PecCheck || Command != 0) {\r
75 return EFI_UNSUPPORTED;\r
76 }\r
77 break;\r
78 case EfiSmbusReceiveByte:\r
79 case EfiSmbusSendByte:\r
80 if (Command != 0) {\r
81 return EFI_UNSUPPORTED;\r
82 }\r
83 //\r
84 // Cascade to check length parameter.\r
85 //\r
86 case EfiSmbusReadByte:\r
87 case EfiSmbusWriteByte:\r
88 RequiredLen = 1;\r
89 //\r
90 // Cascade to check length parameter.\r
91 //\r
92 case EfiSmbusReadWord:\r
93 case EfiSmbusWriteWord:\r
94 case EfiSmbusProcessCall:\r
95 if (Buffer == NULL || Length == NULL) {\r
96 return EFI_INVALID_PARAMETER;\r
97 } else if (*Length < RequiredLen) {\r
98 Status = EFI_BUFFER_TOO_SMALL;\r
99 }\r
100 *Length = RequiredLen;\r
101 break;\r
102 case EfiSmbusReadBlock:\r
103 case EfiSmbusWriteBlock:\r
104 if ((Buffer == NULL) ||\r
105 (Length == NULL) ||\r
106 (*Length < MIN_SMBUS_BLOCK_LEN) ||\r
107 (*Length > MAX_SMBUS_BLOCK_LEN)) {\r
108 return EFI_INVALID_PARAMETER;\r
109 }\r
110 break;\r
111 case EfiSmbusBWBRProcessCall:\r
112 return EFI_UNSUPPORTED;\r
113 default:\r
114 return EFI_INVALID_PARAMETER;\r
115 }\r
116 return Status;\r
117}\r
118\r
119/**\r
120 Executes an SMBus operation to an SMBus controller. Returns when either the command has been\r
121 executed or an error is encountered in doing the operation.\r
122\r
123 The internal worker function provides a standard way to execute an operation as defined in the\r
124 System Management Bus (SMBus) Specification. The resulting transaction will be either that the\r
125 SMBus slave devices accept this transaction or that this function returns with error.\r
126\r
127 @param SlaveAddress The SMBus slave address of the device with which to communicate.\r
128 @param Command This command is transmitted by the SMBus host controller to the\r
129 SMBus slave device and the interpretation is SMBus slave device\r
130 specific. It can mean the offset to a list of functions inside an\r
131 SMBus slave device. Not all operations or slave devices support\r
132 this command's registers.\r
133 @param Operation Signifies which particular SMBus hardware protocol instance that\r
134 it will use to execute the SMBus transactions. This SMBus\r
135 hardware protocol is defined by the SMBus Specification and is\r
136 not related to EFI.\r
137 @param PecCheck Defines if Packet Error Code (PEC) checking is required for this\r
138 operation.\r
139 @param Length Signifies the number of bytes that this operation will do. The\r
140 maximum number of bytes can be revision specific and operation\r
141 specific. This field will contain the actual number of bytes that\r
142 are executed for this operation. Not all operations require this\r
143 argument.\r
144 @param Buffer Contains the value of data to execute to the SMBus slave device.\r
145 Not all operations require this argument. The length of this\r
146 buffer is identified by Length.\r
147\r
148 @retval EFI_SUCCESS The last data that was returned from the access matched the poll\r
149 exit criteria.\r
150 @retval EFI_CRC_ERROR Checksum is not correct (PEC is incorrect).\r
151 @retval EFI_TIMEOUT Timeout expired before the operation was completed. Timeout is\r
152 determined by the SMBus host controller device.\r
153 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
154 @retval EFI_DEVICE_ERROR The request was not completed because a failure that was\r
155 reflected in the Host Status Register bit. Device errors are a\r
156 result of a transaction collision, illegal command field,\r
157 unclaimed cycle (host initiated), or bus errors (collisions).\r
158 @retval EFI_INVALID_PARAMETER Operation is not defined in EFI_SMBUS_OPERATION.\r
159 @retval EFI_INVALID_PARAMETER Length/Buffer is NULL for operations except for EfiSmbusQuickRead\r
160 and EfiSmbusQuickWrite. Length is outside the range of valid\r
161 values.\r
162 @retval EFI_UNSUPPORTED The SMBus operation or PEC is not supported.\r
163 @retval EFI_BUFFER_TOO_SMALL Buffer is not sufficient for this operation.\r
164\r
165**/\r
166EFI_STATUS\r
167Execute (\r
168 IN EFI_SMBUS_DEVICE_ADDRESS SlaveAddress,\r
169 IN EFI_SMBUS_DEVICE_COMMAND Command,\r
170 IN EFI_SMBUS_OPERATION Operation,\r
171 IN BOOLEAN PecCheck,\r
172 IN OUT UINTN *Length,\r
173 IN OUT VOID *Buffer\r
174 )\r
175{\r
176 EFI_STATUS Status;\r
177 UINTN SmbusAddress;\r
178 UINTN WorkBufferLen;\r
179 UINT8 WorkBuffer[MAX_SMBUS_BLOCK_LEN];\r
180\r
181 Status = QncSmbusExecCheckParameters (\r
182 SlaveAddress,\r
183 Command,\r
184 Operation,\r
185 PecCheck,\r
186 Length,\r
187 Buffer);\r
188 if (EFI_ERROR (Status)) {\r
189 return Status;\r
190 }\r
191\r
192 SmbusAddress = SMBUS_LIB_ADDRESS (SlaveAddress.SmbusDeviceAddress, Command, *Length, PecCheck);\r
193\r
194 switch (Operation) {\r
195 case EfiSmbusQuickRead:\r
196 SmBusQuickRead (SmbusAddress, &Status);\r
197 break;\r
198 case EfiSmbusQuickWrite:\r
199 SmBusQuickWrite (SmbusAddress, &Status);\r
200 break;\r
201 case EfiSmbusReceiveByte:\r
202 *(UINT8 *) Buffer = SmBusReceiveByte (SmbusAddress, &Status);\r
203 break;\r
204 case EfiSmbusSendByte:\r
205 SmBusSendByte (SmbusAddress, *(UINT8 *) Buffer, &Status);\r
206 break;\r
207 case EfiSmbusReadByte:\r
208 *(UINT8 *) Buffer = SmBusReadDataByte (SmbusAddress, &Status);\r
209 break;\r
210 case EfiSmbusWriteByte:\r
211 SmBusWriteDataByte (SmbusAddress, *(UINT8 *) Buffer, &Status);\r
212 break;\r
213 case EfiSmbusReadWord:\r
214 *(UINT16 *) Buffer = SmBusReadDataWord (SmbusAddress, &Status);\r
215 break;\r
216 case EfiSmbusWriteWord:\r
217 SmBusWriteDataWord (SmbusAddress, *(UINT16 *) Buffer, &Status);\r
218 break;\r
219 case EfiSmbusProcessCall:\r
220 *(UINT16 *) Buffer = SmBusProcessCall (SmbusAddress, *(UINT16 *) Buffer, &Status);\r
221 break;\r
222 case EfiSmbusReadBlock:\r
223 WorkBufferLen = SmBusReadBlock (SmbusAddress, WorkBuffer, &Status);\r
224 if (!EFI_ERROR (Status)) {\r
225 //\r
226 // Read block transaction is complete successfully, and then\r
227 // check whether the output buffer is large enough.\r
228 //\r
229 if (*Length >= WorkBufferLen) {\r
230 CopyMem (Buffer, WorkBuffer, WorkBufferLen);\r
231 } else {\r
232 Status = EFI_BUFFER_TOO_SMALL;\r
233 }\r
234 *Length = WorkBufferLen;\r
235 }\r
236 break;\r
237 case EfiSmbusWriteBlock:\r
238 SmBusWriteBlock (ADD_LENGTH (SmbusAddress, *Length), Buffer, &Status);\r
239 break;\r
240 default:\r
241 break;\r
242 }\r
243\r
244 return Status;\r
245}\r
246\r