]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkPkg/Library/PeiSmbusLibSmbusPpi/SmbusLib.c
Remove IntelFrameworkModulePkg
[mirror_edk2.git] / IntelFrameworkPkg / Library / PeiSmbusLibSmbusPpi / SmbusLib.c
CommitLineData
a67d1f18 1/** @file\r
2Implementation of SmBusLib class library for PEI phase.\r
3\r
1c2f052d 4Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
22a69a5e 5SPDX-License-Identifier: BSD-2-Clause-Patent\r
a67d1f18 6\r
7\r
8Module Name: SmbusLib.c\r
9\r
10**/\r
11\r
12#include "InternalSmbusLib.h"\r
13\r
14/**\r
15 Executes an SMBUS quick read command.\r
16\r
17 Executes an SMBUS quick read command on the SMBUS device specified by SmBusAddress.\r
18 Only the SMBUS slave address field of SmBusAddress is required.\r
19 If Status is not NULL, then the status of the executed command is returned in Status.\r
20 If PEC is set in SmBusAddress, then ASSERT().\r
21 If Command in SmBusAddress is not zero, then ASSERT().\r
22 If Length in SmBusAddress is not zero, then ASSERT().\r
23 If any reserved bits of SmBusAddress are set, then ASSERT().\r
24\r
25 @param SmBusAddress Address that encodes the SMBUS Slave Address,\r
26 SMBUS Command, SMBUS Data Length, and PEC.\r
27 @param Status Return status for the executed command.\r
28 This is an optional parameter and may be NULL.\r
29\r
30**/\r
31VOID\r
32EFIAPI\r
33SmBusQuickRead (\r
34 IN UINTN SmBusAddress,\r
35 OUT RETURN_STATUS *Status OPTIONAL\r
36 )\r
37{\r
38 ASSERT (!SMBUS_LIB_PEC (SmBusAddress));\r
39 ASSERT (SMBUS_LIB_COMMAND (SmBusAddress) == 0);\r
40 ASSERT (SMBUS_LIB_LENGTH (SmBusAddress) == 0);\r
66eebcf9 41 ASSERT (SMBUS_LIB_RESERVED (SmBusAddress) == 0);\r
a67d1f18 42\r
43 InternalSmBusExec (EfiSmbusQuickRead, SmBusAddress, 0, NULL, Status);\r
44}\r
45\r
46/**\r
47 Executes an SMBUS quick write command.\r
48\r
49 Executes an SMBUS quick write command on the SMBUS device specified by SmBusAddress.\r
50 Only the SMBUS slave address field of SmBusAddress is required.\r
51 If Status is not NULL, then the status of the executed command is returned in Status.\r
52 If PEC is set in SmBusAddress, then ASSERT().\r
53 If Command in SmBusAddress is not zero, then ASSERT().\r
54 If Length in SmBusAddress is not zero, then ASSERT().\r
55 If any reserved bits of SmBusAddress are set, then ASSERT().\r
56\r
57 @param SmBusAddress Address that encodes the SMBUS Slave Address,\r
58 SMBUS Command, SMBUS Data Length, and PEC.\r
59 @param Status Return status for the executed command.\r
60 This is an optional parameter and may be NULL.\r
61\r
62**/\r
63VOID\r
64EFIAPI\r
65SmBusQuickWrite (\r
66 IN UINTN SmBusAddress,\r
67 OUT RETURN_STATUS *Status OPTIONAL\r
68 )\r
69{\r
70 ASSERT (!SMBUS_LIB_PEC (SmBusAddress));\r
71 ASSERT (SMBUS_LIB_COMMAND (SmBusAddress) == 0);\r
72 ASSERT (SMBUS_LIB_LENGTH (SmBusAddress) == 0);\r
66eebcf9 73 ASSERT (SMBUS_LIB_RESERVED (SmBusAddress) == 0);\r
a67d1f18 74\r
75 InternalSmBusExec (EfiSmbusQuickWrite, SmBusAddress, 0, NULL, Status);\r
76}\r
77\r
78/**\r
79 Executes an SMBUS receive byte command.\r
80\r
81 Executes an SMBUS receive byte command on the SMBUS device specified by SmBusAddress.\r
82 Only the SMBUS slave address field of SmBusAddress is required.\r
83 The byte received from the SMBUS is returned.\r
84 If Status is not NULL, then the status of the executed command is returned in Status.\r
85 If Command in SmBusAddress is not zero, then ASSERT().\r
86 If Length in SmBusAddress is not zero, then ASSERT().\r
87 If any reserved bits of SmBusAddress are set, then ASSERT().\r
88\r
89 @param SmBusAddress Address that encodes the SMBUS Slave Address,\r
90 SMBUS Command, SMBUS Data Length, and PEC.\r
91 @param Status Return status for the executed command.\r
92 This is an optional parameter and may be NULL.\r
93\r
94 @return The byte received from the SMBUS.\r
95\r
96**/\r
97UINT8\r
98EFIAPI\r
99SmBusReceiveByte (\r
100 IN UINTN SmBusAddress,\r
101 OUT RETURN_STATUS *Status OPTIONAL\r
102 )\r
103{\r
104 UINT8 Byte;\r
105\r
106 ASSERT (SMBUS_LIB_COMMAND (SmBusAddress) == 0);\r
107 ASSERT (SMBUS_LIB_LENGTH (SmBusAddress) == 0);\r
66eebcf9 108 ASSERT (SMBUS_LIB_RESERVED (SmBusAddress) == 0);\r
a67d1f18 109\r
110 InternalSmBusExec (EfiSmbusReceiveByte, SmBusAddress, 1, &Byte, Status);\r
111\r
112 return Byte;\r
113}\r
114\r
115/**\r
116 Executes an SMBUS send byte command.\r
117\r
118 Executes an SMBUS send byte command on the SMBUS device specified by SmBusAddress.\r
119 The byte specified by Value is sent.\r
120 Only the SMBUS slave address field of SmBusAddress is required. Value is returned.\r
121 If Status is not NULL, then the status of the executed command is returned in Status.\r
122 If Command in SmBusAddress is not zero, then ASSERT().\r
123 If Length in SmBusAddress is not zero, then ASSERT().\r
124 If any reserved bits of SmBusAddress are set, then ASSERT().\r
125\r
126 @param SmBusAddress Address that encodes the SMBUS Slave Address,\r
127 SMBUS Command, SMBUS Data Length, and PEC.\r
128 @param Value The 8-bit value to send.\r
129 @param Status Return status for the executed command.\r
130 This is an optional parameter and may be NULL.\r
131\r
132 @return The parameter of Value.\r
133\r
134**/\r
135UINT8\r
136EFIAPI\r
137SmBusSendByte (\r
138 IN UINTN SmBusAddress,\r
139 IN UINT8 Value,\r
140 OUT RETURN_STATUS *Status OPTIONAL\r
141 )\r
142{\r
143 UINT8 Byte;\r
144\r
145 ASSERT (SMBUS_LIB_COMMAND (SmBusAddress) == 0);\r
146 ASSERT (SMBUS_LIB_LENGTH (SmBusAddress) == 0);\r
66eebcf9 147 ASSERT (SMBUS_LIB_RESERVED (SmBusAddress) == 0);\r
a67d1f18 148\r
149 Byte = Value;\r
150 InternalSmBusExec (EfiSmbusSendByte, SmBusAddress, 1, &Byte, Status);\r
151\r
152 return Value;\r
153}\r
154\r
155/**\r
156 Executes an SMBUS read data byte command.\r
157\r
158 Executes an SMBUS read data byte command on the SMBUS device specified by SmBusAddress.\r
159 Only the SMBUS slave address and SMBUS command fields of SmBusAddress are required.\r
160 The 8-bit value read from the SMBUS is returned.\r
161 If Status is not NULL, then the status of the executed command is returned in Status.\r
162 If Length in SmBusAddress is not zero, then ASSERT().\r
163 If any reserved bits of SmBusAddress are set, then ASSERT().\r
164\r
165 @param SmBusAddress Address that encodes the SMBUS Slave Address,\r
166 SMBUS Command, SMBUS Data Length, and PEC.\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 byte read from the SMBUS.\r
171\r
172**/\r
173UINT8\r
174EFIAPI\r
175SmBusReadDataByte (\r
176 IN UINTN SmBusAddress,\r
177 OUT RETURN_STATUS *Status OPTIONAL\r
178 )\r
179{\r
180 UINT8 Byte;\r
181\r
182 ASSERT (SMBUS_LIB_LENGTH (SmBusAddress) == 0);\r
66eebcf9 183 ASSERT (SMBUS_LIB_RESERVED (SmBusAddress) == 0);\r
a67d1f18 184\r
185 InternalSmBusExec (EfiSmbusReadByte, SmBusAddress, 1, &Byte, Status);\r
1c2f052d 186\r
a67d1f18 187 return Byte;\r
188}\r
189\r
190/**\r
191 Executes an SMBUS write data byte command.\r
192\r
193 Executes an SMBUS write data byte command on the SMBUS device specified by SmBusAddress.\r
194 The 8-bit value specified by Value is written.\r
195 Only the SMBUS slave address and SMBUS command fields of SmBusAddress are required.\r
196 Value 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 Value The 8-bit value to write.\r
204 @param Status Return status for the executed command.\r
205 This is an optional parameter and may be NULL.\r
206\r
207 @return The parameter of Value.\r
208\r
209**/\r
210UINT8\r
211EFIAPI\r
212SmBusWriteDataByte (\r
213 IN UINTN SmBusAddress,\r
214 IN UINT8 Value,\r
215 OUT RETURN_STATUS *Status OPTIONAL\r
216 )\r
217{\r
218 UINT8 Byte;\r
219\r
220 ASSERT (SMBUS_LIB_LENGTH (SmBusAddress) == 0);\r
66eebcf9 221 ASSERT (SMBUS_LIB_RESERVED (SmBusAddress) == 0);\r
a67d1f18 222\r
223 Byte = Value;\r
224 InternalSmBusExec (EfiSmbusWriteByte, SmBusAddress, 1, &Byte, Status);\r
1c2f052d 225\r
a67d1f18 226 return Value;\r
227}\r
228\r
229/**\r
230 Executes an SMBUS read data word command.\r
231\r
232 Executes an SMBUS read data word command on the SMBUS device specified by SmBusAddress.\r
233 Only the SMBUS slave address and SMBUS command fields of SmBusAddress are required.\r
234 The 16-bit value read from the SMBUS is returned.\r
235 If Status is not NULL, then the status of the executed command is returned in Status.\r
236 If Length in SmBusAddress is not zero, then ASSERT().\r
237 If any reserved bits of SmBusAddress are set, then ASSERT().\r
1c2f052d 238\r
a67d1f18 239 @param SmBusAddress Address that encodes the SMBUS Slave Address,\r
240 SMBUS Command, SMBUS Data Length, and PEC.\r
241 @param Status Return status for the executed command.\r
242 This is an optional parameter and may be NULL.\r
243\r
244 @return The byte read from the SMBUS.\r
245\r
246**/\r
247UINT16\r
248EFIAPI\r
249SmBusReadDataWord (\r
250 IN UINTN SmBusAddress,\r
251 OUT RETURN_STATUS *Status OPTIONAL\r
252 )\r
253{\r
254 UINT16 Word;\r
255\r
256 ASSERT (SMBUS_LIB_LENGTH (SmBusAddress) == 0);\r
66eebcf9 257 ASSERT (SMBUS_LIB_RESERVED (SmBusAddress) == 0);\r
a67d1f18 258\r
259 InternalSmBusExec (EfiSmbusReadWord, SmBusAddress, 2, &Word, Status);\r
1c2f052d 260\r
a67d1f18 261 return Word;\r
262}\r
263\r
264/**\r
265 Executes an SMBUS write data word command.\r
266\r
267 Executes an SMBUS write data word command on the SMBUS device specified by SmBusAddress.\r
268 The 16-bit value specified by Value is written.\r
269 Only the SMBUS slave address and SMBUS command fields of SmBusAddress are required.\r
270 Value is returned.\r
271 If Status is not NULL, then the status of the executed command is returned in Status.\r
272 If Length in SmBusAddress is not zero, then ASSERT().\r
273 If any reserved bits of SmBusAddress are set, then ASSERT().\r
274\r
275 @param SmBusAddress Address that encodes the SMBUS Slave Address,\r
276 SMBUS Command, SMBUS Data Length, and PEC.\r
277 @param Value The 16-bit value to write.\r
278 @param Status Return status for the executed command.\r
279 This is an optional parameter and may be NULL.\r
280\r
281 @return The parameter of Value.\r
282\r
283**/\r
284UINT16\r
285EFIAPI\r
286SmBusWriteDataWord (\r
287 IN UINTN SmBusAddress,\r
288 IN UINT16 Value,\r
289 OUT RETURN_STATUS *Status OPTIONAL\r
290 )\r
291{\r
292 UINT16 Word;\r
293\r
294 ASSERT (SMBUS_LIB_LENGTH (SmBusAddress) == 0);\r
66eebcf9 295 ASSERT (SMBUS_LIB_RESERVED (SmBusAddress) == 0);\r
a67d1f18 296\r
297 Word = Value;\r
298 InternalSmBusExec (EfiSmbusWriteWord, SmBusAddress, 2, &Word, Status);\r
299\r
300 return Value;\r
301}\r
302\r
303/**\r
304 Executes an SMBUS process call command.\r
305\r
306 Executes an SMBUS process call command on the SMBUS device specified by SmBusAddress.\r
307 The 16-bit value specified by Value is written.\r
308 Only the SMBUS slave address and SMBUS command fields of SmBusAddress are required.\r
309 The 16-bit value returned by the process call command is returned.\r
310 If Status is not NULL, then the status of the executed command is returned in Status.\r
311 If Length in SmBusAddress is not zero, then ASSERT().\r
312 If any reserved bits of SmBusAddress are set, then ASSERT().\r
313\r
314 @param SmBusAddress Address that encodes the SMBUS Slave Address,\r
315 SMBUS Command, SMBUS Data Length, and PEC.\r
316 @param Value The 16-bit value to write.\r
317 @param Status Return status for the executed command.\r
318 This is an optional parameter and may be NULL.\r
319\r
320 @return The 16-bit value returned by the process call command.\r
321\r
322**/\r
323UINT16\r
324EFIAPI\r
325SmBusProcessCall (\r
326 IN UINTN SmBusAddress,\r
327 IN UINT16 Value,\r
328 OUT RETURN_STATUS *Status OPTIONAL\r
329 )\r
330{\r
331 ASSERT (SMBUS_LIB_LENGTH (SmBusAddress) == 0);\r
66eebcf9 332 ASSERT (SMBUS_LIB_RESERVED (SmBusAddress) == 0);\r
a67d1f18 333\r
334 InternalSmBusExec (EfiSmbusProcessCall, SmBusAddress, 2, &Value, Status);\r
1c2f052d 335\r
a67d1f18 336 return Value;\r
337}\r
338\r
339/**\r
340 Executes an SMBUS read block command.\r
341\r
342 Executes an SMBUS read block command on the SMBUS device specified by SmBusAddress.\r
343 Only the SMBUS slave address and SMBUS command fields of SmBusAddress are required.\r
344 Bytes are read from the SMBUS and stored in Buffer.\r
345 The number of bytes read is returned, and will never return a value larger than 32-bytes.\r
346 If Status is not NULL, then the status of the executed command is returned in Status.\r
347 It is the caller's responsibility to make sure Buffer is large enough for the total number of bytes read.\r
348 SMBUS supports a maximum transfer size of 32 bytes, so Buffer does not need to be any larger than 32 bytes.\r
349 If Length in SmBusAddress is not zero, then ASSERT().\r
350 If Buffer is NULL, then ASSERT().\r
351 If any reserved bits of SmBusAddress are set, then ASSERT().\r
352\r
353 @param SmBusAddress Address that encodes the SMBUS Slave Address,\r
354 SMBUS Command, SMBUS Data Length, and PEC.\r
355 @param Buffer Pointer to the buffer to store the bytes read from the SMBUS.\r
356 @param Status Return status for the executed command.\r
357 This is an optional parameter and may be NULL.\r
358\r
359 @return The number of bytes read.\r
360\r
361**/\r
362UINTN\r
363EFIAPI\r
364SmBusReadBlock (\r
365 IN UINTN SmBusAddress,\r
366 OUT VOID *Buffer,\r
367 OUT RETURN_STATUS *Status OPTIONAL\r
368 )\r
369{\r
370 ASSERT (Buffer != NULL);\r
371 ASSERT (SMBUS_LIB_LENGTH (SmBusAddress) == 0);\r
66eebcf9 372 ASSERT (SMBUS_LIB_RESERVED (SmBusAddress) == 0);\r
a67d1f18 373\r
374 return InternalSmBusExec (EfiSmbusReadBlock, SmBusAddress, 0x20, Buffer, Status);\r
375}\r
376\r
377/**\r
378 Executes an SMBUS write block command.\r
379\r
380 Executes an SMBUS write block command on the SMBUS device specified by SmBusAddress.\r
381 The SMBUS slave address, SMBUS command, and SMBUS length fields of SmBusAddress are required.\r
382 Bytes are written to the SMBUS from Buffer.\r
383 The number of bytes written is returned, and will never return a value larger than 32-bytes.\r
1c2f052d 384 If Status is not NULL, then the status of the executed command is returned in Status.\r
a67d1f18 385 If Length in SmBusAddress is zero or greater than 32, 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 written.\r
396\r
397**/\r
398UINTN\r
399EFIAPI\r
400SmBusWriteBlock (\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 ASSERT (Buffer != NULL);\r
409 ASSERT (SMBUS_LIB_LENGTH (SmBusAddress) >= 1);\r
410 ASSERT (SMBUS_LIB_LENGTH (SmBusAddress) <= 32);\r
66eebcf9 411 ASSERT (SMBUS_LIB_RESERVED (SmBusAddress) == 0);\r
a67d1f18 412\r
413 Length = SMBUS_LIB_LENGTH (SmBusAddress);\r
414 return InternalSmBusExec (EfiSmbusWriteBlock, SmBusAddress, Length, Buffer, Status);\r
415}\r
416\r
417/**\r
418 Executes an SMBUS block process call command.\r
419\r
420 Executes an SMBUS block process call command on the SMBUS device specified by SmBusAddress.\r
421 The SMBUS slave address, SMBUS command, and SMBUS length fields of SmBusAddress are required.\r
422 Bytes are written to the SMBUS from WriteBuffer. Bytes are then read from the SMBUS into ReadBuffer.\r
423 If Status is not NULL, then the status of the executed command is returned in Status.\r
424 It is the caller's responsibility to make sure ReadBuffer is large enough for the total number of bytes read.\r
425 SMBUS supports a maximum transfer size of 32 bytes, so Buffer does not need to be any larger than 32 bytes.\r
426 If Length in SmBusAddress is zero or greater than 32, then ASSERT().\r
427 If WriteBuffer is NULL, then ASSERT().\r
428 If ReadBuffer is NULL, then ASSERT().\r
429 If any reserved bits of SmBusAddress are set, then ASSERT().\r
430\r
431 @param SmBusAddress Address that encodes the SMBUS Slave Address,\r
432 SMBUS Command, SMBUS Data Length, and PEC.\r
433 @param WriteBuffer Pointer to the buffer of bytes to write to the SMBUS.\r
434 @param ReadBuffer Pointer to the buffer of bytes to read from the SMBUS.\r
435 @param Status Return status for the executed command.\r
436 This is an optional parameter and may be NULL.\r
437\r
438 @return The number of bytes written.\r
439\r
440**/\r
441UINTN\r
442EFIAPI\r
443SmBusBlockProcessCall (\r
444 IN UINTN SmBusAddress,\r
445 IN VOID *WriteBuffer,\r
446 OUT VOID *ReadBuffer,\r
447 OUT RETURN_STATUS *Status OPTIONAL\r
448 )\r
449{\r
450 UINTN Length;\r
451\r
452 ASSERT (WriteBuffer != NULL);\r
453 ASSERT (ReadBuffer != NULL);\r
454 ASSERT (SMBUS_LIB_LENGTH (SmBusAddress) >= 1);\r
455 ASSERT (SMBUS_LIB_LENGTH (SmBusAddress) <= 32);\r
66eebcf9 456 ASSERT (SMBUS_LIB_RESERVED (SmBusAddress) == 0);\r
a67d1f18 457\r
458 Length = SMBUS_LIB_LENGTH (SmBusAddress);\r
459 //\r
460 // Assuming that ReadBuffer is large enough to save another memory copy.\r
461 //\r
462 ReadBuffer = CopyMem (ReadBuffer, WriteBuffer, Length);\r
463 return InternalSmBusExec (EfiSmbusBWBRProcessCall, SmBusAddress, Length, ReadBuffer, Status);\r
464}\r