47ffd190084511f0ea738adc2bb4c56918a9b57b
[mirror_edk2.git] / MdePkg / Library / PeiSmbusLibSmbus2 / PeiSmbusLib.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: PeiSmbusLib.c
15
16 **/
17
18 #include <Ppi/Smbus2.h>
19
20 #include "InternalSmbusLib.h"
21
22 /**
23 Gets Smbus PPIs.
24
25 This internal function retrieves Smbus PPI from PPI database.
26
27 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES published by the PEI Foundation.
28
29 @return The pointer to Smbus PPI.
30
31 **/
32 EFI_PEI_SMBUS2_PPI *
33 InternalGetSmbusPpi (
34 EFI_PEI_SERVICES **PeiServices
35 )
36 {
37 EFI_STATUS Status;
38 EFI_PEI_SMBUS2_PPI *SmbusPpi;
39
40 Status = (*PeiServices)->LocatePpi (PeiServices, &gEfiPeiSmbus2PpiGuid, 0, NULL, (VOID **) &SmbusPpi);
41 ASSERT_EFI_ERROR (Status);
42 ASSERT (SmbusPpi != NULL);
43
44 return SmbusPpi;
45 }
46
47 /**
48 Executes an SMBus operation to an SMBus controller.
49
50 This function provides a standard way to execute Smbus script
51 as defined in the SmBus Specification. The data can either be of
52 the Length byte, word, or a block of data.
53
54 @param SmbusOperation Signifies which particular SMBus hardware protocol instance that it will use to
55 execute the SMBus transactions.
56 @param SmBusAddress Address that encodes the SMBUS Slave Address,
57 SMBUS Command, SMBUS Data Length, and PEC.
58 @param Length Signifies the number of bytes that this operation will do. The maximum number of
59 bytes can be revision specific and operation specific.
60 @param Buffer Contains the value of data to execute to the SMBus slave device. Not all operations
61 require this argument. The length of this buffer is identified by Length.
62 @param Status Return status for the executed command.
63 This is an optional parameter and may be NULL.
64
65 @return The actual number of bytes that are executed for this operation..
66
67 **/
68 UINTN
69 InternalSmBusExec (
70 IN EFI_SMBUS_OPERATION SmbusOperation,
71 IN UINTN SmBusAddress,
72 IN UINTN Length,
73 IN OUT VOID *Buffer,
74 OUT RETURN_STATUS *Status OPTIONAL
75 )
76 {
77 EFI_PEI_SMBUS2_PPI *SmbusPpi;
78 EFI_PEI_SERVICES **PeiServices;
79 RETURN_STATUS ReturnStatus;
80 EFI_SMBUS_DEVICE_ADDRESS SmbusDeviceAddress;
81
82 PeiServices = GetPeiServicesTablePointer ();
83 SmbusPpi = InternalGetSmbusPpi (PeiServices);
84 SmbusDeviceAddress.SmbusDeviceAddress = SMBUS_LIB_SLAVE_ADDRESS (SmBusAddress);
85
86 ReturnStatus = SmbusPpi->Execute (
87 SmbusPpi,
88 SmbusDeviceAddress,
89 SMBUS_LIB_COMMAND (SmBusAddress),
90 SmbusOperation,
91 SMBUS_LIB_PEC (SmBusAddress),
92 &Length,
93 Buffer
94 );
95 if (Status != NULL) {
96 *Status = ReturnStatus;
97 }
98
99 return Length;
100 }