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