1 /** @file 2 * 3 * Copyright (c) 2015, Hisilicon Limited. All rights reserved. 4 * Copyright (c) 2015, Linaro Limited. All rights reserved. 5 * 6 * This program and the accompanying materials 7 * are licensed and made available under the terms and conditions of the BSD License 8 * which accompanies this distribution. The full text of the license may be found at 9 * http://opensource.org/licenses/bsd-license.php 10 * 11 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 13 * 14 **/ 15 16 #include <Uefi.h> 17 #include <Library/IoLib.h> 18 #include <Library/DebugLib.h> 19 #include <Library/TimerLib.h> 20 #include <Library/UefiBootServicesTableLib.h> 21 22 #include <Protocol/HisiBoardNicProtocol.h> 23 #include <Library/I2CLib.h> 24 #include <OemNicConfig.h> 25 26 #define EEPROM_I2C_PORT 7 27 28 EFI_STATUS 29 EFIAPI OemGetMac (IN OUT EFI_MAC_ADDRESS *Mac, IN UINTN Port); 30 31 EFI_STATUS 32 EFIAPI OemSetMac (IN EFI_MAC_ADDRESS *Mac, IN UINTN Port); 33 34 HISI_BOARD_NIC_PROTOCOL mOemNicProtocol = { 35 .GetMac = OemGetMac, 36 .SetMac = OemSetMac, 37 }; 38 39 40 EFI_STATUS OemGetMacE2prom(IN UINT32 Port, OUT UINT8 *pucAddr) 41 { 42 I2C_DEVICE stI2cDev = {0}; 43 EFI_STATUS Status; 44 UINT16 I2cOffset; 45 46 Status = I2CInit(0, EEPROM_I2C_PORT, Normal); 47 if (EFI_ERROR(Status)) 48 { 49 DEBUG((EFI_D_ERROR, "[%a]:[%dL] Call I2CInit failed! p1=0x%x.\n", __FUNCTION__, __LINE__, Status)); 50 return Status; 51 } 52 53 I2cOffset = I2C_OFFSET_EEPROM_ETH0 + (Port * 6); 54 55 stI2cDev.DeviceType = DEVICE_TYPE_E2PROM; 56 stI2cDev.Port = EEPROM_I2C_PORT; 57 stI2cDev.SlaveDeviceAddress = I2C_SLAVEADDR_EEPROM; 58 stI2cDev.Socket = 0; 59 Status = I2CRead(&stI2cDev, I2cOffset, 6, pucAddr); 60 if (EFI_ERROR(Status)) 61 { 62 DEBUG((EFI_D_ERROR, "[%a]:[%dL] Call I2cRead failed! p1=0x%x.\n", __FUNCTION__, __LINE__, Status)); 63 return Status; 64 } 65 66 return EFI_SUCCESS; 67 } 68 69 70 EFI_STATUS OemSetMacE2prom(IN UINT32 Port, IN UINT8 *pucAddr) 71 { 72 I2C_DEVICE stI2cDev = {0}; 73 EFI_STATUS Status; 74 UINT16 I2cOffset; 75 76 Status = I2CInit(0, EEPROM_I2C_PORT, Normal); 77 if (EFI_ERROR(Status)) 78 { 79 DEBUG((EFI_D_ERROR, "[%a]:[%dL] Call I2CInit failed! p1=0x%x.\n", __FUNCTION__, __LINE__, Status)); 80 return Status; 81 } 82 83 I2cOffset = I2C_OFFSET_EEPROM_ETH0 + (Port * 6); 84 85 stI2cDev.DeviceType = DEVICE_TYPE_E2PROM; 86 stI2cDev.Port = EEPROM_I2C_PORT; 87 stI2cDev.SlaveDeviceAddress = I2C_SLAVEADDR_EEPROM; 88 stI2cDev.Socket = 0; 89 Status = I2CWrite(&stI2cDev, I2cOffset, 6, pucAddr); 90 if (EFI_ERROR(Status)) 91 { 92 DEBUG((EFI_D_ERROR, "[%a]:[%dL] Call I2cWrite failed! p1=0x%x.\n", __FUNCTION__, __LINE__, Status)); 93 return Status; 94 } 95 96 return EFI_SUCCESS; 97 } 98 99 EFI_STATUS 100 EFIAPI OemGetMac ( 101 IN OUT EFI_MAC_ADDRESS *Mac, 102 IN UINTN Port 103 ) 104 { 105 EFI_STATUS Status; 106 107 if (NULL == Mac) 108 { 109 DEBUG((EFI_D_ERROR, "[%a]:[%dL] Mac buffer is null!\n", __FUNCTION__, __LINE__)); 110 return EFI_INVALID_PARAMETER; 111 } 112 113 //TODO: discard port number, only support one port 114 // Only 6 bytes are used 115 Status = OemGetMacE2prom(Port, Mac->Addr); 116 if ((EFI_ERROR(Status))) 117 { 118 DEBUG((EFI_D_ERROR, "[%a]:[%dL] Get mac failed!\n", __FUNCTION__, __LINE__)); 119 return Status; 120 } 121 122 return EFI_SUCCESS; 123 } 124 125 EFI_STATUS 126 EFIAPI OemSetMac ( 127 IN EFI_MAC_ADDRESS *Mac, 128 IN UINTN Port 129 ) 130 { 131 EFI_STATUS Status; 132 133 if (NULL == Mac) 134 { 135 DEBUG((EFI_D_ERROR, "[%a]:[%dL] Mac buffer is null!\n", __FUNCTION__, __LINE__)); 136 return EFI_INVALID_PARAMETER; 137 } 138 139 Status = OemSetMacE2prom(Port, Mac->Addr); 140 if ((EFI_ERROR(Status))) 141 { 142 DEBUG((EFI_D_ERROR, "[%a]:[%dL] Set mac failed!\n", __FUNCTION__, __LINE__)); 143 return Status; 144 } 145 146 return EFI_SUCCESS; 147 } 148 149 EFI_STATUS 150 EFIAPI 151 OemNicConfigEntry ( 152 IN EFI_HANDLE ImageHandle, 153 IN EFI_SYSTEM_TABLE *SystemTable 154 ) 155 { 156 EFI_STATUS Status; 157 158 Status = gBS->InstallProtocolInterface( 159 &ImageHandle, 160 &gHisiBoardNicProtocolGuid, 161 EFI_NATIVE_INTERFACE, 162 &mOemNicProtocol 163 ); 164 165 if(EFI_ERROR(Status)) 166 { 167 DEBUG((EFI_D_ERROR, "[%a]:[%dL] Install Protocol failed %r\n", __FUNCTION__, __LINE__, Status)); 168 return Status; 169 } 170 171 return EFI_SUCCESS; 172 } 173 174