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