1 /*
2  * Copyright (c) 2021-2023 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 /*---------------------------------------------------------------------
9  * Includes
10  *---------------------------------------------------------------------
11  */
12 #include "hpm_enet_drv.h"
13 #include "hpm_rtl8211_regs.h"
14 #include "hpm_rtl8211.h"
15 
16 /*---------------------------------------------------------------------
17  * Internal API
18  *---------------------------------------------------------------------
19  */
rtl8211_check_id(ENET_Type * ptr)20 static bool rtl8211_check_id(ENET_Type *ptr)
21 {
22     uint16_t id1, id2;
23 
24     id1 = enet_read_phy(ptr, RTL8211_ADDR, RTL8211_PHYID1);
25     id2 = enet_read_phy(ptr, RTL8211_ADDR, RTL8211_PHYID2);
26 
27     if (RTL8211_PHYID1_OUI_MSB_GET(id1) == RTL8211_ID1 && RTL8211_PHYID2_OUI_LSB_GET(id2) == RTL8211_ID2) {
28         return true;
29     } else {
30         return false;
31     }
32 }
33 
34 /*---------------------------------------------------------------------
35  * API
36  *---------------------------------------------------------------------
37  */
rtl8211_reset(ENET_Type * ptr)38 void rtl8211_reset(ENET_Type *ptr)
39 {
40     uint16_t data;
41 
42     /* PHY reset */
43     enet_write_phy(ptr, RTL8211_ADDR, RTL8211_BMCR, RTL8211_BMCR_RESET_SET(1));
44 
45     /* wait until the reset is completed */
46     do {
47         data = enet_read_phy(ptr, RTL8211_ADDR, RTL8211_BMCR);
48     } while (RTL8211_BMCR_RESET_GET(data));
49 }
50 
rtl8211_basic_mode_default_config(ENET_Type * ptr,rtl8211_config_t * config)51 void rtl8211_basic_mode_default_config(ENET_Type *ptr, rtl8211_config_t *config)
52 {
53     (void)ptr;
54 
55     config->loopback         = false;                        /* Disable PCS loopback mode */
56     #if defined(__DISABLE_AUTO_NEGO) && (__DISABLE_AUTO_NEGO)
57     config->auto_negotiation = false;                        /* Disable Auto-Negotiation */
58     config->speed            = enet_phy_port_speed_100mbps;
59     config->duplex           = enet_phy_duplex_full;
60     #else
61     config->auto_negotiation = true;                         /* Enable Auto-Negotiation */
62     #endif
63 }
64 
rtl8211_basic_mode_init(ENET_Type * ptr,rtl8211_config_t * config)65 bool rtl8211_basic_mode_init(ENET_Type *ptr, rtl8211_config_t *config)
66 {
67     uint16_t data = 0;
68 
69     data |= RTL8211_BMCR_RESET_SET(0)                        /* Normal operation */
70          |  RTL8211_BMCR_LOOPBACK_SET(config->loopback)      /* configure PCS loopback mode */
71          |  RTL8211_BMCR_ANE_SET(config->auto_negotiation)   /* configure Auto-Negotiation */
72          |  RTL8211_BMCR_PWD_SET(0)                          /* Normal operation */
73          |  RTL8211_BMCR_ISOLATE_SET(0)                      /* Normal operation */
74          |  RTL8211_BMCR_RESTART_AN_SET(0)                   /* Normal operation (ignored when Auto-Negotiation is disabled) */
75          |  RTL8211_BMCR_COLLISION_TEST_SET(0);              /* Normal operation */
76 
77     if (config->auto_negotiation == 0) {
78         data |= RTL8211_BMCR_SPEED0_SET(config->speed) | RTL8211_BMCR_SPEED1_SET(config->speed >> 1);   /* Set port speed */
79         data |= RTL8211_BMCR_DUPLEX_SET(config->duplex);                                                /* Set duplex mode */
80     }
81 
82     enet_write_phy(ptr, RTL8211_ADDR, RTL8211_BMCR, data);
83 
84     /* check the id of rtl8211 */
85     if (rtl8211_check_id(ptr) == false) {
86         return false;
87     }
88 
89     return true;
90 }
91 
92 
rtl8211_get_phy_status(ENET_Type * ptr,enet_phy_status_t * status)93 void rtl8211_get_phy_status(ENET_Type *ptr, enet_phy_status_t *status)
94 {
95     uint16_t data;
96 
97     data = enet_read_phy(ptr, RTL8211_ADDR, RTL8211_PHYSR);
98     status->enet_phy_link = RTL8211_PHYSR_LINK_REAL_TIME_GET(data);
99     status->enet_phy_speed = RTL8211_PHYSR_SPEED_GET(data) == 0 ? enet_phy_port_speed_10mbps : RTL8211_PHYSR_SPEED_GET(data) == 1 ? enet_phy_port_speed_100mbps : enet_phy_port_speed_1000mbps;
100     status->enet_phy_duplex = RTL8211_PHYSR_DUPLEX_GET(data);
101 }
102