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