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_rtl8201_regs.h"
14 #include "hpm_rtl8201.h"
15 
16 /*---------------------------------------------------------------------
17  * Internal API
18  *---------------------------------------------------------------------
19  */
rtl8201_check_id(ENET_Type * ptr)20 static bool rtl8201_check_id(ENET_Type *ptr)
21 {
22     uint16_t id1, id2;
23 
24     id1 = enet_read_phy(ptr, RTL8201_ADDR, RTL8201_PHYID1);
25     id2 = enet_read_phy(ptr, RTL8201_ADDR, RTL8201_PHYID2);
26 
27     if (RTL8201_PHYID1_OUI_MSB_GET(id1) == RTL8201_ID1 && RTL8201_PHYID2_OUI_LSB_GET(id2) == RTL8201_ID2) {
28         return true;
29     } else {
30         return false;
31     }
32 }
33 
34 /*---------------------------------------------------------------------
35  * API
36  *---------------------------------------------------------------------
37  */
rtl8201_reset(ENET_Type * ptr)38 void rtl8201_reset(ENET_Type *ptr)
39 {
40     uint16_t data;
41 
42     /* PHY reset */
43     enet_write_phy(ptr, RTL8201_ADDR, RTL8201_BMCR, RTL8201_BMCR_RESET_SET(1));
44 
45     /* wait until the reset is completed */
46     do {
47         data = enet_read_phy(ptr, RTL8201_ADDR, RTL8201_BMCR);
48     } while (RTL8201_BMCR_RESET_GET(data));
49 }
50 
rtl8201_basic_mode_default_config(ENET_Type * ptr,rtl8201_config_t * config)51 void rtl8201_basic_mode_default_config(ENET_Type *ptr, rtl8201_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     config->txc_input        = true;                         /* Set TXC as input mode */
64 }
65 
rtl8201_basic_mode_init(ENET_Type * ptr,rtl8201_config_t * config)66 bool rtl8201_basic_mode_init(ENET_Type *ptr, rtl8201_config_t *config)
67 {
68     uint16_t data = 0;
69 
70     data |= RTL8201_BMCR_RESET_SET(0)                        /* Normal operation */
71          |  RTL8201_BMCR_LOOPBACK_SET(config->loopback)      /* configure PCS loopback mode */
72          |  RTL8201_BMCR_ANE_SET(config->auto_negotiation)   /* configure Auto-Negotiation */
73          |  RTL8201_BMCR_PWD_SET(0)                          /* Normal operation */
74          |  RTL8201_BMCR_ISOLATE_SET(0)                      /* Normal operation */
75          |  RTL8201_BMCR_RESTART_AN_SET(0)                   /* Normal operation (ignored when Auto-Negotiation is disabled) */
76          |  RTL8201_BMCR_COLLISION_TEST_SET(0);              /* Normal operation */
77 
78     if (config->auto_negotiation == 0) {
79         data |= RTL8201_BMCR_SPEED0_SET(config->speed);      /* Set port speed */
80         data |= RTL8201_BMCR_DUPLEX_SET(config->duplex);     /* Set duplex mode */
81     }
82 
83     /* check the id of rtl8201 */
84     if (rtl8201_check_id(ptr) == false) {
85         return false;
86     }
87 
88     enet_write_phy(ptr, RTL8201_ADDR, RTL8201_BMCR, data);
89 
90     /* select page 7 */
91     enet_write_phy(ptr, RTL8201_ADDR, RTL8201_PAGESEL, 7);
92 
93     /* set txc direction */
94     data = enet_read_phy(ptr, RTL8201_ADDR, RTL8201_RMSR_P7);
95     data &= ~RTL8201_RMSR_P7_RG_RMII_CLKDIR_MASK;
96     data |= RTL8201_RMSR_P7_RG_RMII_CLKDIR_SET(config->txc_input);
97     enet_write_phy(ptr, RTL8201_ADDR, RTL8201_RMSR_P7, data);
98 
99     return true;
100 }
101 
rtl8201_get_phy_status(ENET_Type * ptr,enet_phy_status_t * status)102 void rtl8201_get_phy_status(ENET_Type *ptr, enet_phy_status_t *status)
103 {
104     uint16_t data;
105 
106     data = enet_read_phy(ptr, RTL8201_ADDR, RTL8201_BMSR);
107     status->enet_phy_link = RTL8201_BMSR_LINK_STATUS_GET(data);
108 
109     data = enet_read_phy(ptr, RTL8201_ADDR, RTL8201_BMCR);
110     status->enet_phy_speed = RTL8201_BMCR_SPEED0_GET(data) == 0 ? enet_phy_port_speed_10mbps : enet_phy_port_speed_100mbps;
111     status->enet_phy_duplex = RTL8201_BMCR_DUPLEX_GET(data);
112 }
113