OemNicConfigD02.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #include <Uefi.h>
  16. #include <Library/IoLib.h>
  17. #include <Library/DebugLib.h>
  18. #include <Library/TimerLib.h>
  19. #include <Library/UefiBootServicesTableLib.h>
  20. #include <Protocol/HisiBoardNicProtocol.h>
  21. #include <Library/I2CLib.h>
  22. #include <OemNicConfig.h>
  23. #define EEPROM_I2C_PORT 7
  24. EFI_STATUS
  25. EFIAPI OemGetMac (IN OUT EFI_MAC_ADDRESS *Mac, IN UINTN Port);
  26. EFI_STATUS
  27. EFIAPI OemSetMac (IN EFI_MAC_ADDRESS *Mac, IN UINTN Port);
  28. HISI_BOARD_NIC_PROTOCOL mOemNicProtocol = {
  29. .GetMac = OemGetMac,
  30. .SetMac = OemSetMac,
  31. };
  32. EFI_STATUS OemGetMacE2prom(IN UINT32 Port, OUT UINT8 *pucAddr)
  33. {
  34. I2C_DEVICE stI2cDev = {0};
  35. EFI_STATUS Status;
  36. UINT16 I2cOffset;
  37. Status = I2CInit(0, EEPROM_I2C_PORT, Normal);
  38. if (EFI_ERROR(Status))
  39. {
  40. DEBUG((EFI_D_ERROR, "[%a]:[%dL] Call I2CInit failed! p1=0x%x.\n", __FUNCTION__, __LINE__, Status));
  41. return Status;
  42. }
  43. I2cOffset = I2C_OFFSET_EEPROM_ETH0 + (Port * 6);
  44. stI2cDev.DeviceType = DEVICE_TYPE_E2PROM;
  45. stI2cDev.Port = EEPROM_I2C_PORT;
  46. stI2cDev.SlaveDeviceAddress = I2C_SLAVEADDR_EEPROM;
  47. stI2cDev.Socket = 0;
  48. Status = I2CRead(&stI2cDev, I2cOffset, 6, pucAddr);
  49. if (EFI_ERROR(Status))
  50. {
  51. DEBUG((EFI_D_ERROR, "[%a]:[%dL] Call I2cRead failed! p1=0x%x.\n", __FUNCTION__, __LINE__, Status));
  52. return Status;
  53. }
  54. return EFI_SUCCESS;
  55. }
  56. EFI_STATUS OemSetMacE2prom(IN UINT32 Port, IN UINT8 *pucAddr)
  57. {
  58. I2C_DEVICE stI2cDev = {0};
  59. EFI_STATUS Status;
  60. UINT16 I2cOffset;
  61. Status = I2CInit(0, EEPROM_I2C_PORT, Normal);
  62. if (EFI_ERROR(Status))
  63. {
  64. DEBUG((EFI_D_ERROR, "[%a]:[%dL] Call I2CInit failed! p1=0x%x.\n", __FUNCTION__, __LINE__, Status));
  65. return Status;
  66. }
  67. I2cOffset = I2C_OFFSET_EEPROM_ETH0 + (Port * 6);
  68. stI2cDev.DeviceType = DEVICE_TYPE_E2PROM;
  69. stI2cDev.Port = EEPROM_I2C_PORT;
  70. stI2cDev.SlaveDeviceAddress = I2C_SLAVEADDR_EEPROM;
  71. stI2cDev.Socket = 0;
  72. Status = I2CWrite(&stI2cDev, I2cOffset, 6, pucAddr);
  73. if (EFI_ERROR(Status))
  74. {
  75. DEBUG((EFI_D_ERROR, "[%a]:[%dL] Call I2cWrite failed! p1=0x%x.\n", __FUNCTION__, __LINE__, Status));
  76. return Status;
  77. }
  78. return EFI_SUCCESS;
  79. }
  80. EFI_STATUS
  81. EFIAPI OemGetMac (
  82. IN OUT EFI_MAC_ADDRESS *Mac,
  83. IN UINTN Port
  84. )
  85. {
  86. EFI_STATUS Status;
  87. if (NULL == Mac)
  88. {
  89. DEBUG((EFI_D_ERROR, "[%a]:[%dL] Mac buffer is null!\n", __FUNCTION__, __LINE__));
  90. return EFI_INVALID_PARAMETER;
  91. }
  92. //TODO: discard port number, only support one port
  93. // Only 6 bytes are used
  94. Status = OemGetMacE2prom(Port, Mac->Addr);
  95. if ((EFI_ERROR(Status)))
  96. {
  97. DEBUG((EFI_D_ERROR, "[%a]:[%dL] Get mac failed!\n", __FUNCTION__, __LINE__));
  98. return Status;
  99. }
  100. return EFI_SUCCESS;
  101. }
  102. EFI_STATUS
  103. EFIAPI OemSetMac (
  104. IN EFI_MAC_ADDRESS *Mac,
  105. IN UINTN Port
  106. )
  107. {
  108. EFI_STATUS Status;
  109. if (NULL == Mac)
  110. {
  111. DEBUG((EFI_D_ERROR, "[%a]:[%dL] Mac buffer is null!\n", __FUNCTION__, __LINE__));
  112. return EFI_INVALID_PARAMETER;
  113. }
  114. Status = OemSetMacE2prom(Port, Mac->Addr);
  115. if ((EFI_ERROR(Status)))
  116. {
  117. DEBUG((EFI_D_ERROR, "[%a]:[%dL] Set mac failed!\n", __FUNCTION__, __LINE__));
  118. return Status;
  119. }
  120. return EFI_SUCCESS;
  121. }
  122. EFI_STATUS
  123. EFIAPI
  124. OemNicConfigEntry (
  125. IN EFI_HANDLE ImageHandle,
  126. IN EFI_SYSTEM_TABLE *SystemTable
  127. )
  128. {
  129. EFI_STATUS Status;
  130. Status = gBS->InstallProtocolInterface(
  131. &ImageHandle,
  132. &gHisiBoardNicProtocolGuid,
  133. EFI_NATIVE_INTERFACE,
  134. &mOemNicProtocol
  135. );
  136. if(EFI_ERROR(Status))
  137. {
  138. DEBUG((EFI_D_ERROR, "[%a]:[%dL] Install Protocol failed %r\n", __FUNCTION__, __LINE__, Status));
  139. return Status;
  140. }
  141. return EFI_SUCCESS;
  142. }