Home | History | Annotate | Download | only in Dxe
      1 /** @file
      2 Common code to implement SMBus bus protocols. Smbus PEI and DXE modules
      3 share the same version of this file.
      4 
      5 Copyright (c) 2013-2015 Intel Corporation.
      6 
      7 This program and the accompanying materials
      8 are licensed and made available under the terms and conditions of the BSD License
      9 which accompanies this distribution.  The full text of the license may be found at
     10 http://opensource.org/licenses/bsd-license.php
     11 
     12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     14 
     15 **/
     16 #include "CommonHeader.h"
     17 
     18 #include "QNCSmbus.h"
     19 
     20 /**
     21   Checks the parameter of SmbusExecute().
     22 
     23   This function checks the input parameters of SmbusExecute().  If the input parameters are valid
     24   for certain SMBus bus protocol, it will return EFI_SUCCESS; otherwise, it will return certain
     25   error code based on the input SMBus bus protocol.
     26 
     27   @param  SlaveAddress            The SMBus slave address of the device with which to communicate.
     28   @param  Command                 This command is transmitted by the SMBus host controller to the
     29                                   SMBus slave device and the interpretation is SMBus slave device
     30                                   specific. It can mean the offset to a list of functions inside an
     31                                   SMBus slave device. Not all operations or slave devices support
     32                                   this command's registers.
     33   @param  Operation               Signifies which particular SMBus hardware protocol instance that
     34                                   it will use to execute the SMBus transactions. This SMBus
     35                                   hardware protocol is defined by the SMBus Specification and is
     36                                   not related to EFI.
     37   @param  PecCheck                Defines if Packet Error Code (PEC) checking is required for this
     38                                   operation.
     39   @param  Length                  Signifies the number of bytes that this operation will do. The
     40                                   maximum number of bytes can be revision specific and operation
     41                                   specific. This field will contain the actual number of bytes that
     42                                   are executed for this operation. Not all operations require this
     43                                   argument.
     44   @param  Buffer                  Contains the value of data to execute to the SMBus slave device.
     45                                   Not all operations require this argument. The length of this
     46                                   buffer is identified by Length.
     47 
     48   @retval EFI_SUCCESS             All the parameters are valid for the corresponding SMBus bus
     49                                   protocol.
     50   @retval EFI_INVALID_PARAMETER   Operation is not defined in EFI_SMBUS_OPERATION.
     51   @retval EFI_INVALID_PARAMETER   Length/Buffer is NULL for operations except for EfiSmbusQuickRead
     52                                   and EfiSmbusQuickWrite. Length is outside the range of valid
     53                                   values.
     54   @retval EFI_UNSUPPORTED         The SMBus operation or PEC is not supported.
     55   @retval EFI_BUFFER_TOO_SMALL    Buffer is not sufficient for this operation.
     56 
     57 **/
     58 EFI_STATUS
     59 QncSmbusExecCheckParameters (
     60   IN     EFI_SMBUS_DEVICE_ADDRESS SlaveAddress,
     61   IN     EFI_SMBUS_DEVICE_COMMAND Command,
     62   IN     EFI_SMBUS_OPERATION      Operation,
     63   IN     BOOLEAN                  PecCheck,
     64   IN OUT UINTN                    *Length,
     65   IN OUT VOID                     *Buffer
     66   )
     67 {
     68   EFI_STATUS  Status;
     69   UINTN       RequiredLen;
     70 
     71   //
     72   // Set default value to be 2:
     73   // for SmbusReadWord, SmbusWriteWord and SmbusProcessCall.
     74   //
     75   RequiredLen = 2;
     76   Status      = EFI_SUCCESS;
     77   switch (Operation) {
     78   case EfiSmbusQuickRead:
     79   case EfiSmbusQuickWrite:
     80     if (PecCheck || Command != 0) {
     81       return EFI_UNSUPPORTED;
     82     }
     83     break;
     84   case EfiSmbusReceiveByte:
     85   case EfiSmbusSendByte:
     86     if (Command != 0) {
     87       return EFI_UNSUPPORTED;
     88     }
     89     //
     90     // Cascade to check length parameter.
     91     //
     92   case EfiSmbusReadByte:
     93   case EfiSmbusWriteByte:
     94     RequiredLen = 1;
     95     //
     96     // Cascade to check length parameter.
     97     //
     98   case EfiSmbusReadWord:
     99   case EfiSmbusWriteWord:
    100   case EfiSmbusProcessCall:
    101     if (Buffer == NULL || Length == NULL) {
    102       return EFI_INVALID_PARAMETER;
    103     } else if (*Length < RequiredLen) {
    104       Status = EFI_BUFFER_TOO_SMALL;
    105     }
    106     *Length = RequiredLen;
    107     break;
    108   case EfiSmbusReadBlock:
    109   case EfiSmbusWriteBlock:
    110     if ((Buffer == NULL) ||
    111         (Length == NULL) ||
    112         (*Length < MIN_SMBUS_BLOCK_LEN) ||
    113         (*Length > MAX_SMBUS_BLOCK_LEN)) {
    114       return EFI_INVALID_PARAMETER;
    115     }
    116     break;
    117   case EfiSmbusBWBRProcessCall:
    118     return EFI_UNSUPPORTED;
    119   default:
    120     return EFI_INVALID_PARAMETER;
    121   }
    122   return Status;
    123 }
    124 
    125 /**
    126   Executes an SMBus operation to an SMBus controller. Returns when either the command has been
    127   executed or an error is encountered in doing the operation.
    128 
    129   The internal worker function provides a standard way to execute an operation as defined in the
    130   System Management Bus (SMBus) Specification. The resulting transaction will be either that the
    131   SMBus slave devices accept this transaction or that this function returns with error.
    132 
    133   @param  SlaveAddress            The SMBus slave address of the device with which to communicate.
    134   @param  Command                 This command is transmitted by the SMBus host controller to the
    135                                   SMBus slave device and the interpretation is SMBus slave device
    136                                   specific. It can mean the offset to a list of functions inside an
    137                                   SMBus slave device. Not all operations or slave devices support
    138                                   this command's registers.
    139   @param  Operation               Signifies which particular SMBus hardware protocol instance that
    140                                   it will use to execute the SMBus transactions. This SMBus
    141                                   hardware protocol is defined by the SMBus Specification and is
    142                                   not related to EFI.
    143   @param  PecCheck                Defines if Packet Error Code (PEC) checking is required for this
    144                                   operation.
    145   @param  Length                  Signifies the number of bytes that this operation will do. The
    146                                   maximum number of bytes can be revision specific and operation
    147                                   specific. This field will contain the actual number of bytes that
    148                                   are executed for this operation. Not all operations require this
    149                                   argument.
    150   @param  Buffer                  Contains the value of data to execute to the SMBus slave device.
    151                                   Not all operations require this argument. The length of this
    152                                   buffer is identified by Length.
    153 
    154   @retval EFI_SUCCESS             The last data that was returned from the access matched the poll
    155                                   exit criteria.
    156   @retval EFI_CRC_ERROR           Checksum is not correct (PEC is incorrect).
    157   @retval EFI_TIMEOUT             Timeout expired before the operation was completed. Timeout is
    158                                   determined by the SMBus host controller device.
    159   @retval EFI_OUT_OF_RESOURCES    The request could not be completed due to a lack of resources.
    160   @retval EFI_DEVICE_ERROR        The request was not completed because a failure that was
    161                                   reflected in the Host Status Register bit. Device errors are a
    162                                   result of a transaction collision, illegal command field,
    163                                   unclaimed cycle (host initiated), or bus errors (collisions).
    164   @retval EFI_INVALID_PARAMETER   Operation is not defined in EFI_SMBUS_OPERATION.
    165   @retval EFI_INVALID_PARAMETER   Length/Buffer is NULL for operations except for EfiSmbusQuickRead
    166                                   and EfiSmbusQuickWrite. Length is outside the range of valid
    167                                   values.
    168   @retval EFI_UNSUPPORTED         The SMBus operation or PEC is not supported.
    169   @retval EFI_BUFFER_TOO_SMALL    Buffer is not sufficient for this operation.
    170 
    171 **/
    172 EFI_STATUS
    173 Execute (
    174   IN     EFI_SMBUS_DEVICE_ADDRESS SlaveAddress,
    175   IN     EFI_SMBUS_DEVICE_COMMAND Command,
    176   IN     EFI_SMBUS_OPERATION      Operation,
    177   IN     BOOLEAN                  PecCheck,
    178   IN OUT UINTN                    *Length,
    179   IN OUT VOID                     *Buffer
    180   )
    181 {
    182   EFI_STATUS                      Status;
    183   UINTN                           SmbusAddress;
    184   UINTN                           WorkBufferLen;
    185   UINT8                           WorkBuffer[MAX_SMBUS_BLOCK_LEN];
    186 
    187   Status = QncSmbusExecCheckParameters (
    188              SlaveAddress,
    189              Command,
    190              Operation,
    191              PecCheck,
    192              Length,
    193              Buffer);
    194   if (EFI_ERROR (Status)) {
    195     return Status;
    196   }
    197 
    198   SmbusAddress = SMBUS_LIB_ADDRESS (SlaveAddress.SmbusDeviceAddress, Command, *Length, PecCheck);
    199 
    200   switch (Operation) {
    201   case EfiSmbusQuickRead:
    202     SmBusQuickRead (SmbusAddress, &Status);
    203     break;
    204   case EfiSmbusQuickWrite:
    205     SmBusQuickWrite (SmbusAddress, &Status);
    206     break;
    207   case EfiSmbusReceiveByte:
    208     *(UINT8 *) Buffer = SmBusReceiveByte (SmbusAddress, &Status);
    209     break;
    210   case EfiSmbusSendByte:
    211     SmBusSendByte (SmbusAddress, *(UINT8 *) Buffer, &Status);
    212     break;
    213   case EfiSmbusReadByte:
    214     *(UINT8 *) Buffer = SmBusReadDataByte (SmbusAddress, &Status);
    215     break;
    216   case EfiSmbusWriteByte:
    217     SmBusWriteDataByte (SmbusAddress, *(UINT8 *) Buffer, &Status);
    218     break;
    219   case EfiSmbusReadWord:
    220     *(UINT16 *) Buffer = SmBusReadDataWord (SmbusAddress, &Status);
    221     break;
    222   case EfiSmbusWriteWord:
    223     SmBusWriteDataWord (SmbusAddress, *(UINT16 *) Buffer, &Status);
    224     break;
    225   case EfiSmbusProcessCall:
    226     *(UINT16 *) Buffer = SmBusProcessCall (SmbusAddress, *(UINT16 *) Buffer, &Status);
    227     break;
    228   case EfiSmbusReadBlock:
    229     WorkBufferLen = SmBusReadBlock (SmbusAddress, WorkBuffer, &Status);
    230     if (!EFI_ERROR (Status)) {
    231       //
    232       // Read block transaction is complete successfully, and then
    233       // check whether the output buffer is large enough.
    234       //
    235       if (*Length >= WorkBufferLen) {
    236         CopyMem (Buffer, WorkBuffer, WorkBufferLen);
    237       } else {
    238         Status = EFI_BUFFER_TOO_SMALL;
    239       }
    240       *Length = WorkBufferLen;
    241     }
    242     break;
    243   case EfiSmbusWriteBlock:
    244     SmBusWriteBlock (ADD_LENGTH (SmbusAddress, *Length), Buffer, &Status);
    245     break;
    246   default:
    247     break;
    248   }
    249 
    250   return Status;
    251 }
    252 
    253