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