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