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