summaryrefslogtreecommitdiffstats
path: root/Omap35xxPkg/TPS65950Dxe/TPS65950.c
blob: 66f9d853ff11b5dd3c5abf921cc3801d9cda3ae8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/** @file

  Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>

  This program and the accompanying materials
  are licensed and made available under the terms and conditions of the BSD License
  which accompanies this distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include <Uefi.h>

#include <TPS65950.h>

#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>

#include <Protocol/EmbeddedExternalDevice.h>
#include <Protocol/SmbusHc.h>

EFI_SMBUS_HC_PROTOCOL *Smbus;

EFI_STATUS
Read (
  IN  EMBEDDED_EXTERNAL_DEVICE    *This,
  IN  UINTN                       Register,
  IN  UINTN                       Length,
  OUT VOID                        *Buffer
  )
{
  EFI_STATUS               Status;
  EFI_SMBUS_DEVICE_ADDRESS SlaveAddress;
  UINT8                    DeviceRegister;
  UINTN                    DeviceRegisterLength = 1;

  SlaveAddress.SmbusDeviceAddress = EXTERNAL_DEVICE_REGISTER_TO_SLAVE_ADDRESS(Register);
  DeviceRegister = (UINT8)EXTERNAL_DEVICE_REGISTER_TO_REGISTER(Register);

  //Write DeviceRegister.
  Status = Smbus->Execute(Smbus, SlaveAddress, 0, EfiSmbusWriteBlock, FALSE, &DeviceRegisterLength, &DeviceRegister);
  if (EFI_ERROR(Status)) {
    return Status;
  }

  //Read Data
  Status = Smbus->Execute(Smbus, SlaveAddress, 0, EfiSmbusReadBlock, FALSE, &Length, Buffer);
  return Status;
}

EFI_STATUS
Write (
  IN EMBEDDED_EXTERNAL_DEVICE   *This,
  IN UINTN                      Register,
  IN UINTN                      Length,
  IN VOID                       *Buffer
  )
{
  EFI_STATUS               Status;
  EFI_SMBUS_DEVICE_ADDRESS SlaveAddress;
  UINT8                    DeviceRegister;
  UINTN                    DeviceBufferLength = Length + 1;
  UINT8                    *DeviceBuffer;

  SlaveAddress.SmbusDeviceAddress = EXTERNAL_DEVICE_REGISTER_TO_SLAVE_ADDRESS(Register);
  DeviceRegister = (UINT8)EXTERNAL_DEVICE_REGISTER_TO_REGISTER(Register);

  //Prepare buffer for writing
  DeviceBuffer = (UINT8 *)AllocatePool(DeviceBufferLength);
  if (DeviceBuffer == NULL) {
    Status = EFI_OUT_OF_RESOURCES;
    goto exit;
  }

  //Set Device register followed by data to write.
  DeviceBuffer[0] = DeviceRegister;
  CopyMem(&DeviceBuffer[1], Buffer, Length);

  //Write Data
  Status = Smbus->Execute(Smbus, SlaveAddress, 0, EfiSmbusWriteBlock, FALSE, &DeviceBufferLength, DeviceBuffer);
  if (EFI_ERROR(Status)) {
    goto exit;
  }

exit:
  if (DeviceBuffer) {
    FreePool(DeviceBuffer);
  }

  return Status;
}

EMBEDDED_EXTERNAL_DEVICE ExternalDevice = {
  Read,
  Write
};

EFI_STATUS
TPS65950Initialize (
  IN EFI_HANDLE         ImageHandle,
  IN EFI_SYSTEM_TABLE   *SystemTable
  )
{
  EFI_STATUS  Status;

  Status = gBS->LocateProtocol(&gEfiSmbusHcProtocolGuid, NULL, (VOID **)&Smbus);
  ASSERT_EFI_ERROR(Status);

  Status = gBS->InstallMultipleProtocolInterfaces(&ImageHandle, &gEmbeddedExternalDeviceProtocolGuid, &ExternalDevice, NULL);
  return Status;
}