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