blob: 985085c136de3f9e5622e2a135a5d402858328bd [file] [log] [blame]
yshang1dd51a992007-06-29 05:29:46 +00001/** @file
lgao4484c7782008-11-13 08:30:16 +00002Implementation of SmBusLib class library for DXE phase.
yshang1dd51a992007-06-29 05:29:46 +00003
myronporter58380e92010-06-30 00:13:25 +00004Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
hhtian19388d22010-04-23 16:37:43 +00005This program and the accompanying materials
vanjeffbad46382007-07-05 06:59:50 +00006are licensed and made available under the terms and conditions of the BSD License
7which accompanies this distribution. The full text of the license may be found at
myronporter58380e92010-06-30 00:13:25 +00008http://opensource.org/licenses/bsd-license.php.
vanjeffbad46382007-07-05 06:59:50 +00009
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
yshang1dd51a992007-06-29 05:29:46 +000012
13
yshang1dd51a992007-06-29 05:29:46 +000014**/
15
vanjeffbad46382007-07-05 06:59:50 +000016
yshang1dd51a992007-06-29 05:29:46 +000017#include "InternalSmbusLib.h"
18
yshang1dd51a992007-06-29 05:29:46 +000019
20//
21// Globle varible to cache pointer to Smbus protocol.
22//
jji4fe467412008-10-30 05:58:52 +000023EFI_SMBUS_HC_PROTOCOL *mSmbus = NULL;
yshang1dd51a992007-06-29 05:29:46 +000024
25/**
26 The constructor function caches the pointer to Smbus protocol.
vanjeffbad46382007-07-05 06:59:50 +000027
yshang1dd51a992007-06-29 05:29:46 +000028 The constructor function locates Smbus protocol from protocol database.
vanjeffbad46382007-07-05 06:59:50 +000029 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
yshang1dd51a992007-06-29 05:29:46 +000030
31 @param ImageHandle The firmware allocated handle for the EFI image.
32 @param SystemTable A pointer to the EFI System Table.
vanjeffbad46382007-07-05 06:59:50 +000033
yshang1dd51a992007-06-29 05:29:46 +000034 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
35
36**/
37EFI_STATUS
38EFIAPI
39SmbusLibConstructor (
40 IN EFI_HANDLE ImageHandle,
41 IN EFI_SYSTEM_TABLE *SystemTable
42 )
43{
44 EFI_STATUS Status;
vanjeffbad46382007-07-05 06:59:50 +000045
yshang1dd51a992007-06-29 05:29:46 +000046 Status = gBS->LocateProtocol (&gEfiSmbusHcProtocolGuid, NULL, (VOID**) &mSmbus);
47 ASSERT_EFI_ERROR (Status);
48 ASSERT (mSmbus != NULL);
49
50 return Status;
51}
52
53/**
vanjeffbad46382007-07-05 06:59:50 +000054 Executes an SMBus operation to an SMBus controller.
yshang1dd51a992007-06-29 05:29:46 +000055
56 This function provides a standard way to execute Smbus script
57 as defined in the SmBus Specification. The data can either be of
58 the Length byte, word, or a block of data.
59
myronporter58380e92010-06-30 00:13:25 +000060 @param SmbusOperation Signifies which particular SMBus hardware protocol instance
61 that it will use to execute the SMBus transactions.
myronporter2fc59a02010-06-25 21:56:02 +000062 @param SmBusAddress The address that encodes the SMBUS Slave Address,
yshang1dd51a992007-06-29 05:29:46 +000063 SMBUS Command, SMBUS Data Length, and PEC.
myronporter58380e92010-06-30 00:13:25 +000064 @param Length Signifies the number of bytes that this operation will do.
65 The maximum number of bytes can be revision specific
66 and operation specific.
67 @param Buffer Contains the value of data to execute to the SMBus slave
68 device. Not all operations require this argument. The
69 length of this buffer is identified by Length.
yshang1dd51a992007-06-29 05:29:46 +000070 @param Status Return status for the executed command.
71 This is an optional parameter and may be NULL.
72
klu2070a76b2008-12-10 03:28:54 +000073 @return The actual number of bytes that are executed for this operation.
yshang1dd51a992007-06-29 05:29:46 +000074
75**/
76UINTN
77InternalSmBusExec (
78 IN EFI_SMBUS_OPERATION SmbusOperation,
79 IN UINTN SmBusAddress,
80 IN UINTN Length,
81 IN OUT VOID *Buffer,
82 OUT RETURN_STATUS *Status OPTIONAL
83 )
84{
85 RETURN_STATUS ReturnStatus;
86 EFI_SMBUS_DEVICE_ADDRESS SmbusDeviceAddress;
87
88 SmbusDeviceAddress.SmbusDeviceAddress = SMBUS_LIB_SLAVE_ADDRESS (SmBusAddress);
89
90 ReturnStatus = mSmbus->Execute (
91 mSmbus,
92 SmbusDeviceAddress,
93 SMBUS_LIB_COMMAND (SmBusAddress),
94 SmbusOperation,
vanjeffbad46382007-07-05 06:59:50 +000095 SMBUS_LIB_PEC (SmBusAddress),
yshang1dd51a992007-06-29 05:29:46 +000096 &Length,
97 Buffer
98 );
99 if (Status != NULL) {
100 *Status = ReturnStatus;
101 }
102
103 return Length;
104}