summaryrefslogtreecommitdiff
path: root/Platform/Hisilicon/D02/OemNicConfigD02/OemNicConfigD02.c
blob: d327fa43430ffde04d47659c9edcb618ff9f0da7 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/** @file
*
*  Copyright (c) 2015, Hisilicon Limited. All rights reserved.
*  Copyright (c) 2015, Linaro Limited. All rights reserved.
*
*  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 <Library/IoLib.h>
#include <Library/DebugLib.h>
#include <Library/TimerLib.h>
#include <Library/UefiBootServicesTableLib.h>

#include <Protocol/HisiBoardNicProtocol.h>
#include <Library/I2CLib.h>
#include <OemNicConfig.h>

#define EEPROM_I2C_PORT 7

EFI_STATUS
EFIAPI OemGetMac (IN OUT EFI_MAC_ADDRESS *Mac, IN UINTN Port);

EFI_STATUS
EFIAPI OemSetMac (IN EFI_MAC_ADDRESS *Mac, IN UINTN Port);

HISI_BOARD_NIC_PROTOCOL mOemNicProtocol = {
  .GetMac = OemGetMac,
  .SetMac = OemSetMac,
};


EFI_STATUS OemGetMacE2prom(IN UINT32 Port, OUT UINT8 *pucAddr)
{
    I2C_DEVICE stI2cDev = {0};
    EFI_STATUS Status;
    UINT16     I2cOffset;

    Status = I2CInit(0, EEPROM_I2C_PORT, Normal);
    if (EFI_ERROR(Status))
    {
        DEBUG((EFI_D_ERROR, "[%a]:[%dL] Call I2CInit failed! p1=0x%x.\n", __FUNCTION__, __LINE__, Status));
        return Status;
    }

    I2cOffset = I2C_OFFSET_EEPROM_ETH0 + (Port * 6);

    stI2cDev.DeviceType = DEVICE_TYPE_E2PROM;
    stI2cDev.Port = EEPROM_I2C_PORT;
    stI2cDev.SlaveDeviceAddress = I2C_SLAVEADDR_EEPROM;
    stI2cDev.Socket = 0;
    Status = I2CRead(&stI2cDev, I2cOffset, 6, pucAddr);
    if (EFI_ERROR(Status))
    {
        DEBUG((EFI_D_ERROR, "[%a]:[%dL] Call I2cRead failed! p1=0x%x.\n", __FUNCTION__, __LINE__, Status));
        return Status;
    }

    return EFI_SUCCESS;
}


EFI_STATUS OemSetMacE2prom(IN UINT32 Port, IN UINT8 *pucAddr)
{
    I2C_DEVICE stI2cDev = {0};
    EFI_STATUS Status;
    UINT16     I2cOffset;

    Status = I2CInit(0, EEPROM_I2C_PORT, Normal);
    if (EFI_ERROR(Status))
    {
        DEBUG((EFI_D_ERROR, "[%a]:[%dL] Call I2CInit failed! p1=0x%x.\n", __FUNCTION__, __LINE__, Status));
        return Status;
    }

    I2cOffset = I2C_OFFSET_EEPROM_ETH0 + (Port * 6);

    stI2cDev.DeviceType = DEVICE_TYPE_E2PROM;
    stI2cDev.Port = EEPROM_I2C_PORT;
    stI2cDev.SlaveDeviceAddress = I2C_SLAVEADDR_EEPROM;
    stI2cDev.Socket = 0;
    Status = I2CWrite(&stI2cDev, I2cOffset, 6, pucAddr);
    if (EFI_ERROR(Status))
    {
        DEBUG((EFI_D_ERROR, "[%a]:[%dL] Call I2cWrite failed! p1=0x%x.\n", __FUNCTION__, __LINE__, Status));
        return Status;
    }

    return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI OemGetMac (
  IN OUT EFI_MAC_ADDRESS *Mac,
  IN UINTN Port
  )
{
  EFI_STATUS Status;

  if (NULL == Mac)
  {
    DEBUG((EFI_D_ERROR, "[%a]:[%dL] Mac buffer is null!\n", __FUNCTION__, __LINE__));
    return EFI_INVALID_PARAMETER;
  }

  //TODO: discard port number, only support one port
  // Only 6 bytes are used
  Status = OemGetMacE2prom(Port, Mac->Addr);
  if ((EFI_ERROR(Status)))
  {
      DEBUG((EFI_D_ERROR, "[%a]:[%dL] Get mac failed!\n", __FUNCTION__, __LINE__));
      return Status;
  }

  return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI OemSetMac (
  IN EFI_MAC_ADDRESS *Mac,
  IN UINTN Port
  )
{
  EFI_STATUS Status;

  if (NULL == Mac)
  {
    DEBUG((EFI_D_ERROR, "[%a]:[%dL] Mac buffer is null!\n", __FUNCTION__, __LINE__));
    return EFI_INVALID_PARAMETER;
  }

  Status = OemSetMacE2prom(Port, Mac->Addr);
  if ((EFI_ERROR(Status)))
  {
      DEBUG((EFI_D_ERROR, "[%a]:[%dL] Set mac failed!\n", __FUNCTION__, __LINE__));
      return Status;
  }

  return EFI_SUCCESS;
}

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

  Status = gBS->InstallProtocolInterface(
    &ImageHandle,
    &gHisiBoardNicProtocolGuid,
    EFI_NATIVE_INTERFACE,
    &mOemNicProtocol
    );

  if(EFI_ERROR(Status))
  {
    DEBUG((EFI_D_ERROR, "[%a]:[%dL] Install Protocol failed %r\n", __FUNCTION__, __LINE__, Status));
    return Status;
  }

  return EFI_SUCCESS;
}