fgmac_hw.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright : (C) 2022 Phytium Information Technology, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is OPEN SOURCE software: you can redistribute it and/or modify it
  6. * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
  7. * either version 1.0 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the Phytium Public License for more details.
  12. *
  13. *
  14. * FilePath: fgmac_hw.c
  15. * Date: 2022-04-06 14:46:52
  16. * LastEditTime: 2022-04-06 14:46:58
  17. * Description:  This file is for
  18. *
  19. * Modify History:
  20. * Ver   Who        Date         Changes
  21. * ----- ------     --------    --------------------------------------
  22. */
  23. #include "fassert.h"
  24. #include "fdebug.h"
  25. #include "fgmac.h"
  26. #include "fgmac_hw.h"
  27. /***************************** Include Files *********************************/
  28. /************************** Constant Definitions *****************************/
  29. /**************************** Type Definitions *******************************/
  30. /************************** Variable Definitions *****************************/
  31. /***************** Macros (Inline Functions) Definitions *********************/
  32. #define FGMAC_DEBUG_TAG "FGMAC-HW"
  33. #define FGMAC_ERROR(format, ...) FT_DEBUG_PRINT_E(FGMAC_DEBUG_TAG, format, ##__VA_ARGS__)
  34. #define FGMAC_WARN(format, ...) FT_DEBUG_PRINT_W(FGMAC_DEBUG_TAG, format, ##__VA_ARGS__)
  35. #define FGMAC_INFO(format, ...) FT_DEBUG_PRINT_I(FGMAC_DEBUG_TAG, format, ##__VA_ARGS__)
  36. #define FGMAC_DEBUG(format, ...) FT_DEBUG_PRINT_D(FGMAC_DEBUG_TAG, format, ##__VA_ARGS__)
  37. /************************** Function Prototypes ******************************/
  38. /**
  39. * @name: FGmacGetMacAddr
  40. * @msg: 获取FGMAC控制器配置的MAC地址
  41. * @return {*}
  42. * @param {uintptr} base_addr 控制器寄存器基地址
  43. * @param {FGmacMacAddr} mac_addr 配置的MAC地址
  44. */
  45. void FGmacGetMacAddr(uintptr base_addr, FGmacMacAddr mac_addr)
  46. {
  47. u32 mac_high = FGMAC_READ_REG32(base_addr, FGMAC_MAC_ADDR0_UPPER16BIT_OFFSET);
  48. u32 mac_low = FGMAC_READ_REG32(base_addr, FGMAC_MAC_ADDR0_LOWER32BIT_OFFSET);
  49. /* get lower 32 bits of mac addr */
  50. mac_addr[0] = (u8)(mac_low & 0xff);
  51. mac_addr[1] = (u8)((mac_low >> 8) & 0xff);
  52. mac_addr[2] = (u8)((mac_low >> 16) & 0xff);
  53. mac_addr[3] = (u8)((mac_low >> 24) & 0xff);
  54. /* get upper 16 bits of mac addr */
  55. mac_addr[4] = (u8)(mac_high & 0xff);
  56. mac_addr[5] = (u8)((mac_high >> 8) & 0xff);
  57. return;
  58. }
  59. /**
  60. * @name: FGmacSetMacAddr
  61. * @msg: 设置FGMAC控制器的MAC地址
  62. * @return {*}
  63. * @param {uintptr} base_addr 控制器寄存器基地址
  64. * @param {FGmacMacAddr} mac_addr 配置的MAC地址
  65. */
  66. void FGmacSetMacAddr(uintptr base_addr, const FGmacMacAddr mac_addr)
  67. {
  68. u32 reg_val;
  69. reg_val = ((u32)mac_addr[5] << 8) | (u32)mac_addr[4];
  70. FGMAC_WRITE_REG32(base_addr, FGMAC_MAC_ADDR0_UPPER16BIT_OFFSET, reg_val);
  71. reg_val = ((u32)mac_addr[3] << 24) | ((u32)mac_addr[2] << 16) | ((u32)mac_addr[1] << 8) | mac_addr[0];
  72. FGMAC_WRITE_REG32(base_addr, FGMAC_MAC_ADDR0_LOWER32BIT_OFFSET, reg_val);
  73. return;
  74. }
  75. /**
  76. * @name: FGmacSoftwareReset
  77. * @msg: 触发FGMAC控制器软件复位 GMac DMA寄存器列表 和 控制寄存器列表
  78. * @return {*}
  79. * @param {uintptr} base_addr 控制器寄存器基地址
  80. * @param {int} timeout 等待复位完成的状态检测周期数目
  81. */
  82. FError FGmacSoftwareReset(uintptr base_addr, int timeout)
  83. {
  84. FASSERT(timeout > 1); /* 至少等待一个周期 */
  85. u32 reg_val;
  86. FGMAC_SET_REG32(base_addr, FGMAC_DMA_BUS_MODE_OFFSET, FGMAC_DMA_BUS_SWR); /*最后一位写为1 此时MAC DMA 控制器将复位所有 GMAC子系统内部寄存器和逻辑。 完成后会自动清0*/
  87. do
  88. {
  89. reg_val = FGMAC_READ_REG32(base_addr, FGMAC_DMA_BUS_MODE_OFFSET);
  90. }
  91. while ((reg_val & FGMAC_DMA_BUS_SWR) && (--timeout > 0)); /*判断swr位是否为1,当读到0时此时判断 复位操作已完成 软件复位成功*/
  92. if ((0 >= timeout) && (reg_val & FGMAC_DMA_BUS_SWR))
  93. {
  94. FGMAC_ERROR("reset timeout, please check phy connection!!!");
  95. return FGMAC_ERR_TIMEOUT;
  96. }
  97. return FGMAC_SUCCESS;
  98. }
  99. FError FGmacFlushTxFifo(uintptr base_addr, int timeout)
  100. {
  101. FASSERT(timeout > 1); /* 至少等待一个周期 */
  102. u32 reg_val;
  103. FGMAC_SET_REG32(base_addr, FGMAC_DMA_OP_OFFSET, FGMAC_DMA_OP_FTF);
  104. do
  105. {
  106. reg_val = FGMAC_READ_REG32(base_addr, FGMAC_DMA_OP_OFFSET);
  107. }
  108. while ((reg_val & FGMAC_DMA_OP_FTF) && (--timeout > 0));
  109. if ((0 >= timeout) && (reg_val & FGMAC_DMA_OP_FTF))
  110. {
  111. FGMAC_ERROR("flush tx fifo timeout !!!");
  112. return FGMAC_ERR_TIMEOUT;
  113. }
  114. return FGMAC_SUCCESS;
  115. }
  116. /**
  117. * @name: FGmacPhyWaitBusBusy
  118. * @msg: wait phy gmii is not busy
  119. * @param {uintptr} base_addr, base address of FGmac controller register
  120. * @param {int} timeout, wait timeout
  121. * @return err code information, FGMAC_SUCCESS indicates success,others indicates failed
  122. */
  123. FError FGmacPhyWaitBusBusy(uintptr base_addr, int timeout)
  124. {
  125. u32 reg_val;
  126. /* Check for the Busy flag */
  127. do
  128. {
  129. reg_val = FGMAC_READ_REG32(base_addr, FGMAC_GMII_ADDR_OFFSET);
  130. }
  131. while ((FGMAC_MII_ADDR_GB & reg_val) && (0 <= --timeout));
  132. if (0 >= timeout)
  133. {
  134. FGMAC_ERROR("timeout when wait GMII timeout");
  135. return FGMAC_ERR_TIMEOUT;
  136. }
  137. return FGMAC_SUCCESS;
  138. }