]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseS3SmbusLib/S3SmbusLib.c
MdePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdePkg / Library / BaseS3SmbusLib / S3SmbusLib.c
CommitLineData
fe69ac84 1/** @file\r
2 Smbus Library Services that do SMBus transactions and also enable the operatation\r
3 to be replayed during an S3 resume. This library class maps directly on top\r
9095d37b 4 of the SmbusLib class.\r
fe69ac84 5\r
9095d37b 6 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
fe69ac84 7\r
9344f092 8 SPDX-License-Identifier: BSD-2-Clause-Patent\r
fe69ac84 9\r
10**/\r
11\r
12\r
13#include <Base.h>\r
14\r
15#include <Library/DebugLib.h>\r
16#include <Library/S3BootScriptLib.h>\r
17#include <Library/SmbusLib.h>\r
18#include <Library/S3SmbusLib.h>\r
19\r
20/**\r
9095d37b 21 Saves an SMBus operation to S3 script to be replayed on S3 resume.\r
fe69ac84 22\r
23 This function provides a standard way to save SMBus operation to S3 boot Script.\r
24 The data can either be of the Length byte, word, or a block of data.\r
25 If it falis to save S3 boot script, then ASSERT ().\r
26\r
27 @param SmbusOperation Signifies which particular SMBus hardware protocol instance that it will use to\r
28 execute the SMBus transactions.\r
29 @param SmBusAddress Address that encodes the SMBUS Slave Address,\r
30 SMBUS Command, SMBUS Data Length, and PEC.\r
31 @param Length Signifies the number of bytes that this operation will do. The maximum number of\r
32 bytes can be revision specific and operation specific.\r
33 @param Buffer Contains the value of data to execute to the SMBus slave device. Not all operations\r
34 require this argument. The length of this buffer is identified by Length.\r
35\r
36**/\r
37VOID\r
38InternalSaveSmBusExecToBootScript (\r
39 IN EFI_SMBUS_OPERATION SmbusOperation,\r
40 IN UINTN SmBusAddress,\r
41 IN UINTN Length,\r
42 IN OUT VOID *Buffer\r
43 )\r
44{\r
45 RETURN_STATUS Status;\r
46\r
47 Status = S3BootScriptSaveSmbusExecute (\r
48 SmBusAddress,\r
49 SmbusOperation,\r
50 &Length,\r
51 Buffer\r
52 );\r
53 ASSERT (Status == RETURN_SUCCESS);\r
54}\r
55\r
56/**\r
57 Executes an SMBUS quick read command and saves the value in the S3 script to be replayed\r
58 on S3 resume.\r
59\r
60 Executes an SMBUS quick read command on the SMBUS device specified by SmBusAddress.\r
61 Only the SMBUS slave address field of SmBusAddress is required.\r
62 If Status is not NULL, then the status of the executed command is returned in Status.\r
63 If PEC is set in SmBusAddress, then ASSERT().\r
64 If Command in SmBusAddress is not zero, then ASSERT().\r
65 If Length in SmBusAddress is not zero, then ASSERT().\r
66 If any reserved bits of SmBusAddress are set, then ASSERT().\r
67\r
68 @param SmBusAddress Address that encodes the SMBUS Slave Address,\r
69 SMBUS Command, SMBUS Data Length, and PEC.\r
70 @param Status Return status for the executed command.\r
71 This is an optional parameter and may be NULL.\r
72\r
73**/\r
74VOID\r
75EFIAPI\r
76S3SmBusQuickRead (\r
77 IN UINTN SmBusAddress,\r
78 OUT RETURN_STATUS *Status OPTIONAL\r
79 )\r
80{\r
81 SmBusQuickRead (SmBusAddress, Status);\r
9095d37b 82\r
fe69ac84 83 InternalSaveSmBusExecToBootScript (EfiSmbusQuickRead, SmBusAddress, 0, NULL);\r
84}\r
85\r
86/**\r
87 Executes an SMBUS quick write command and saves the value in the S3 script to be replayed\r
88 on S3 resume.\r
89\r
90 Executes an SMBUS quick write command on the SMBUS device specified by SmBusAddress.\r
91 Only the SMBUS slave address field of SmBusAddress is required.\r
92 If Status is not NULL, then the status of the executed command is returned in Status.\r
93 If PEC is set in SmBusAddress, then ASSERT().\r
94 If Command in SmBusAddress is not zero, then ASSERT().\r
95 If Length in SmBusAddress is not zero, then ASSERT().\r
96 If any reserved bits of SmBusAddress are set, then ASSERT().\r
97\r
98 @param SmBusAddress Address that encodes the SMBUS Slave Address,\r
99 SMBUS Command, SMBUS Data Length, and PEC.\r
100 @param Status Return status for the executed command.\r
101 This is an optional parameter and may be NULL.\r
102\r
103**/\r
104VOID\r
105EFIAPI\r
106S3SmBusQuickWrite (\r
107 IN UINTN SmBusAddress,\r
108 OUT RETURN_STATUS *Status OPTIONAL\r
109 )\r
110{\r
111 SmBusQuickWrite (SmBusAddress, Status);\r
112\r
113 InternalSaveSmBusExecToBootScript (EfiSmbusQuickWrite, SmBusAddress, 0, NULL);\r
114}\r
9095d37b 115\r
fe69ac84 116/**\r
117 Executes an SMBUS receive byte command and saves the value in the S3 script to be replayed\r
118 on S3 resume.\r
119\r
120 Executes an SMBUS receive byte command on the SMBUS device specified by SmBusAddress.\r
121 Only the SMBUS slave address field of SmBusAddress is required.\r
122 The byte received from the SMBUS is returned.\r
123 If Status is not NULL, then the status of the executed command is returned in Status.\r
124 If Command in SmBusAddress is not zero, then ASSERT().\r
125 If Length in SmBusAddress is not zero, then ASSERT().\r
126 If any reserved bits of SmBusAddress are set, then ASSERT().\r
127\r
128 @param SmBusAddress Address that encodes the SMBUS Slave Address,\r
129 SMBUS Command, SMBUS Data Length, and PEC.\r
130 @param Status Return status for the executed command.\r
131 This is an optional parameter and may be NULL.\r
132\r
133 @return The byte received from the SMBUS.\r
134\r
135**/\r
136UINT8\r
137EFIAPI\r
138S3SmBusReceiveByte (\r
139 IN UINTN SmBusAddress,\r
140 OUT RETURN_STATUS *Status OPTIONAL\r
141 )\r
142{\r
143 UINT8 Byte;\r
144\r
145 Byte = SmBusReceiveByte (SmBusAddress, Status);\r
146\r
147 InternalSaveSmBusExecToBootScript (EfiSmbusReceiveByte, SmBusAddress, 1, &Byte);\r
148\r
149 return Byte;\r
150}\r
151\r
152/**\r
153 Executes an SMBUS send byte command and saves the value in the S3 script to be replayed\r
154 on S3 resume.\r
155\r
156 Executes an SMBUS send byte command on the SMBUS device specified by SmBusAddress.\r
157 The byte specified by Value is sent.\r
158 Only the SMBUS slave address field of SmBusAddress is required. Value is returned.\r
159 If Status is not NULL, then the status of the executed command is returned in Status.\r
160 If Command in SmBusAddress is not zero, then ASSERT().\r
161 If Length in SmBusAddress is not zero, then ASSERT().\r
162 If any reserved bits of SmBusAddress are set, then ASSERT().\r
163\r
164 @param SmBusAddress Address that encodes the SMBUS Slave Address,\r
165 SMBUS Command, SMBUS Data Length, and PEC.\r
166 @param Value The 8-bit value to send.\r
167 @param Status Return status for the executed command.\r
168 This is an optional parameter and may be NULL.\r
169\r
170 @return The parameter of Value.\r
171\r
172**/\r
173UINT8\r
174EFIAPI\r
175S3SmBusSendByte (\r
176 IN UINTN SmBusAddress,\r
177 IN UINT8 Value,\r
178 OUT RETURN_STATUS *Status OPTIONAL\r
179 )\r
180{\r
181 UINT8 Byte;\r
182\r
183 Byte = SmBusSendByte (SmBusAddress, Value, Status);\r
184\r
185 InternalSaveSmBusExecToBootScript (EfiSmbusSendByte, SmBusAddress, 1, &Byte);\r
186\r
187 return Byte;\r
188}\r
189\r
190/**\r
191 Executes an SMBUS read data byte command and saves the value in the S3 script to be replayed\r
192 on S3 resume.\r
193\r
194 Executes an SMBUS read data byte command on the SMBUS device specified by SmBusAddress.\r
195 Only the SMBUS slave address and SMBUS command fields of SmBusAddress are required.\r
196 The 8-bit value read from the SMBUS is returned.\r
197 If Status is not NULL, then the status of the executed command is returned in Status.\r
198 If Length in SmBusAddress is not zero, then ASSERT().\r
199 If any reserved bits of SmBusAddress are set, then ASSERT().\r
200\r
201 @param SmBusAddress Address that encodes the SMBUS Slave Address,\r
202 SMBUS Command, SMBUS Data Length, and PEC.\r
203 @param Status Return status for the executed command.\r
204 This is an optional parameter and may be NULL.\r
205\r
206 @return The byte read from the SMBUS.\r
207\r
208**/\r
209UINT8\r
210EFIAPI\r
211S3SmBusReadDataByte (\r
212 IN UINTN SmBusAddress,\r
213 OUT RETURN_STATUS *Status OPTIONAL\r
214 )\r
215{\r
216 UINT8 Byte;\r
217\r
218 Byte = SmBusReadDataByte (SmBusAddress, Status);\r
219\r
220 InternalSaveSmBusExecToBootScript (EfiSmbusReadByte, SmBusAddress, 1, &Byte);\r
221\r
222 return Byte;\r
223}\r
224\r
225/**\r
226 Executes an SMBUS write data byte command and saves the value in the S3 script to be replayed\r
227 on S3 resume.\r
228\r
229 Executes an SMBUS write data byte command on the SMBUS device specified by SmBusAddress.\r
230 The 8-bit value specified by Value is written.\r
231 Only the SMBUS slave address and SMBUS command fields of SmBusAddress are required.\r
232 Value is returned.\r
233 If Status is not NULL, then the status of the executed command is returned in Status.\r
234 If Length in SmBusAddress is not zero, then ASSERT().\r
235 If any reserved bits of SmBusAddress are set, then ASSERT().\r
236\r
237 @param SmBusAddress Address that encodes the SMBUS Slave Address,\r
238 SMBUS Command, SMBUS Data Length, and PEC.\r
239 @param Value The 8-bit value to write.\r
240 @param Status Return status for the executed command.\r
241 This is an optional parameter and may be NULL.\r
242\r
243 @return The parameter of Value.\r
244\r
245**/\r
246UINT8\r
247EFIAPI\r
248S3SmBusWriteDataByte (\r
249 IN UINTN SmBusAddress,\r
250 IN UINT8 Value,\r
251 OUT RETURN_STATUS *Status OPTIONAL\r
252 )\r
253{\r
254 UINT8 Byte;\r
255\r
256 Byte = SmBusWriteDataByte (SmBusAddress, Value, Status);\r
257\r
258 InternalSaveSmBusExecToBootScript (EfiSmbusWriteByte, SmBusAddress, 1, &Byte);\r
259\r
260 return Byte;\r
261}\r
262\r
263/**\r
264 Executes an SMBUS read data word command and saves the value in the S3 script to be replayed\r
265 on S3 resume.\r
266\r
267 Executes an SMBUS read data word command on the SMBUS device specified by SmBusAddress.\r
268 Only the SMBUS slave address and SMBUS command fields of SmBusAddress are required.\r
269 The 16-bit value read from the SMBUS is returned.\r
270 If Status is not NULL, then the status of the executed command is returned in Status.\r
271 If Length in SmBusAddress is not zero, then ASSERT().\r
272 If any reserved bits of SmBusAddress are set, then ASSERT().\r
9095d37b 273\r
fe69ac84 274 @param SmBusAddress Address that encodes the SMBUS Slave Address,\r
275 SMBUS Command, SMBUS Data Length, and PEC.\r
276 @param Status Return status for the executed command.\r
277 This is an optional parameter and may be NULL.\r
278\r
279 @return The byte read from the SMBUS.\r
280\r
281**/\r
282UINT16\r
283EFIAPI\r
284S3SmBusReadDataWord (\r
285 IN UINTN SmBusAddress,\r
286 OUT RETURN_STATUS *Status OPTIONAL\r
287 )\r
288{\r
289 UINT16 Word;\r
9095d37b 290\r
fe69ac84 291 Word = SmBusReadDataWord (SmBusAddress, Status);\r
292\r
293 InternalSaveSmBusExecToBootScript (EfiSmbusReadWord, SmBusAddress, 2, &Word);\r
294\r
295 return Word;\r
296}\r
297\r
298/**\r
299 Executes an SMBUS write data word command and saves the value in the S3 script to be replayed\r
300 on S3 resume.\r
301\r
302 Executes an SMBUS write data word command on the SMBUS device specified by SmBusAddress.\r
303 The 16-bit value specified by Value is written.\r
304 Only the SMBUS slave address and SMBUS command fields of SmBusAddress are required.\r
305 Value is returned.\r
306 If Status is not NULL, then the status of the executed command is returned in Status.\r
307 If Length in SmBusAddress is not zero, then ASSERT().\r
308 If any reserved bits of SmBusAddress are set, then ASSERT().\r
309\r
310 @param SmBusAddress Address that encodes the SMBUS Slave Address,\r
311 SMBUS Command, SMBUS Data Length, and PEC.\r
312 @param Value The 16-bit value to write.\r
313 @param Status Return status for the executed command.\r
314 This is an optional parameter and may be NULL.\r
315\r
316 @return The parameter of Value.\r
317\r
318**/\r
319UINT16\r
320EFIAPI\r
321S3SmBusWriteDataWord (\r
322 IN UINTN SmBusAddress,\r
323 IN UINT16 Value,\r
324 OUT RETURN_STATUS *Status OPTIONAL\r
325 )\r
326{\r
327 UINT16 Word;\r
328\r
329 Word = SmBusWriteDataWord (SmBusAddress, Value, Status);\r
330\r
331 InternalSaveSmBusExecToBootScript (EfiSmbusWriteWord, SmBusAddress, 2, &Word);\r
332\r
333 return Word;\r
334}\r
335\r
336/**\r
337 Executes an SMBUS process call command and saves the value in the S3 script to be replayed\r
338 on S3 resume.\r
339\r
340 Executes an SMBUS process call command on the SMBUS device specified by SmBusAddress.\r
341 The 16-bit value specified by Value is written.\r
342 Only the SMBUS slave address and SMBUS command fields of SmBusAddress are required.\r
343 The 16-bit value returned by the process call command is returned.\r
344 If Status is not NULL, then the status of the executed command is returned in Status.\r
345 If Length in SmBusAddress is not zero, then ASSERT().\r
346 If any reserved bits of SmBusAddress are set, then ASSERT().\r
347\r
348 @param SmBusAddress Address that encodes the SMBUS Slave Address,\r
349 SMBUS Command, SMBUS Data Length, and PEC.\r
350 @param Value The 16-bit value to write.\r
351 @param Status Return status for the executed command.\r
352 This is an optional parameter and may be NULL.\r
353\r
354 @return The 16-bit value returned by the process call command.\r
355\r
356**/\r
357UINT16\r
358EFIAPI\r
359S3SmBusProcessCall (\r
360 IN UINTN SmBusAddress,\r
361 IN UINT16 Value,\r
362 OUT RETURN_STATUS *Status OPTIONAL\r
363 )\r
364{\r
365 UINT16 Word;\r
366\r
367 Word = SmBusProcessCall (SmBusAddress, Value, Status);\r
368\r
369 InternalSaveSmBusExecToBootScript (EfiSmbusProcessCall, SmBusAddress, 2, &Value);\r
370\r
9095d37b 371 return Word;\r
fe69ac84 372}\r
373\r
374/**\r
375 Executes an SMBUS read block command and saves the value in the S3 script to be replayed\r
376 on S3 resume.\r
377\r
378 Executes an SMBUS read block command on the SMBUS device specified by SmBusAddress.\r
379 Only the SMBUS slave address and SMBUS command fields of SmBusAddress are required.\r
380 Bytes are read from the SMBUS and stored in Buffer.\r
381 The number of bytes read is returned, and will never return a value larger than 32-bytes.\r
382 If Status is not NULL, then the status of the executed command is returned in Status.\r
383 It is the caller's responsibility to make sure Buffer is large enough for the total number of bytes read.\r
384 SMBUS supports a maximum transfer size of 32 bytes, so Buffer does not need to be any larger than 32 bytes.\r
385 If Length in SmBusAddress is not zero, then ASSERT().\r
386 If Buffer is NULL, then ASSERT().\r
387 If any reserved bits of SmBusAddress are set, then ASSERT().\r
388\r
389 @param SmBusAddress Address that encodes the SMBUS Slave Address,\r
390 SMBUS Command, SMBUS Data Length, and PEC.\r
391 @param Buffer Pointer to the buffer to store the bytes read from the SMBUS.\r
392 @param Status Return status for the executed command.\r
393 This is an optional parameter and may be NULL.\r
394\r
395 @return The number of bytes read.\r
396\r
397**/\r
398UINTN\r
399EFIAPI\r
400S3SmBusReadBlock (\r
401 IN UINTN SmBusAddress,\r
402 OUT VOID *Buffer,\r
403 OUT RETURN_STATUS *Status OPTIONAL\r
404 )\r
405{\r
406 UINTN Length;\r
407\r
408 Length = SmBusReadBlock (SmBusAddress, Buffer, Status);\r
409\r
410 InternalSaveSmBusExecToBootScript (EfiSmbusReadBlock, SmBusAddress, Length, Buffer);\r
411\r
412 return Length;\r
413}\r
414\r
415/**\r
416 Executes an SMBUS write block command and saves the value in the S3 script to be replayed\r
417 on S3 resume.\r
418\r
419 Executes an SMBUS write block command on the SMBUS device specified by SmBusAddress.\r
420 The SMBUS slave address, SMBUS command, and SMBUS length fields of SmBusAddress are required.\r
421 Bytes are written to the SMBUS from Buffer.\r
422 The number of bytes written is returned, and will never return a value larger than 32-bytes.\r
9095d37b 423 If Status is not NULL, then the status of the executed command is returned in Status.\r
fe69ac84 424 If Length in SmBusAddress is zero or greater than 32, then ASSERT().\r
425 If Buffer is NULL, then ASSERT().\r
426 If any reserved bits of SmBusAddress are set, then ASSERT().\r
427\r
428 @param SmBusAddress Address that encodes the SMBUS Slave Address,\r
429 SMBUS Command, SMBUS Data Length, and PEC.\r
430 @param Buffer Pointer to the buffer to store the bytes read from the SMBUS.\r
431 @param Status Return status for the executed command.\r
432 This is an optional parameter and may be NULL.\r
433\r
434 @return The number of bytes written.\r
435\r
436**/\r
437UINTN\r
438EFIAPI\r
439S3SmBusWriteBlock (\r
440 IN UINTN SmBusAddress,\r
441 OUT VOID *Buffer,\r
442 OUT RETURN_STATUS *Status OPTIONAL\r
443 )\r
444{\r
445 UINTN Length;\r
446\r
447 Length = SmBusWriteBlock (SmBusAddress, Buffer, Status);\r
448\r
449 InternalSaveSmBusExecToBootScript (EfiSmbusWriteBlock, SmBusAddress, SMBUS_LIB_LENGTH (SmBusAddress), Buffer);\r
9095d37b 450\r
fe69ac84 451 return Length;\r
452}\r
453\r
454/**\r
455 Executes an SMBUS block process call command and saves the value in the S3 script to be replayed\r
456 on S3 resume.\r
457\r
458 Executes an SMBUS block process call command on the SMBUS device specified by SmBusAddress.\r
459 The SMBUS slave address, SMBUS command, and SMBUS length fields of SmBusAddress are required.\r
460 Bytes are written to the SMBUS from WriteBuffer. Bytes are then read from the SMBUS into ReadBuffer.\r
461 If Status is not NULL, then the status of the executed command is returned in Status.\r
462 It is the caller's responsibility to make sure ReadBuffer is large enough for the total number of bytes read.\r
463 SMBUS supports a maximum transfer size of 32 bytes, so Buffer does not need to be any larger than 32 bytes.\r
464 If Length in SmBusAddress is zero or greater than 32, then ASSERT().\r
465 If WriteBuffer is NULL, then ASSERT().\r
466 If ReadBuffer is NULL, then ASSERT().\r
467 If any reserved bits of SmBusAddress are set, then ASSERT().\r
468\r
469 @param SmBusAddress Address that encodes the SMBUS Slave Address,\r
470 SMBUS Command, SMBUS Data Length, and PEC.\r
471 @param WriteBuffer Pointer to the buffer of bytes to write to the SMBUS.\r
472 @param ReadBuffer Pointer to the buffer of bytes to read from the SMBUS.\r
473 @param Status Return status for the executed command.\r
474 This is an optional parameter and may be NULL.\r
475\r
476 @return The number of bytes written.\r
477\r
478**/\r
479UINTN\r
480EFIAPI\r
481S3SmBusBlockProcessCall (\r
482 IN UINTN SmBusAddress,\r
483 IN VOID *WriteBuffer,\r
484 OUT VOID *ReadBuffer,\r
485 OUT RETURN_STATUS *Status OPTIONAL\r
486 )\r
487{\r
488 UINTN Length;\r
9095d37b 489\r
fe69ac84 490 Length = SmBusBlockProcessCall (SmBusAddress, WriteBuffer, ReadBuffer, Status);\r
9095d37b 491\r
fe69ac84 492 InternalSaveSmBusExecToBootScript (EfiSmbusBWBRProcessCall, SmBusAddress, SMBUS_LIB_LENGTH (SmBusAddress), ReadBuffer);\r
493\r
494 return Length;\r
495}\r