1 /*
2 * Copyright (c) 2021 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 #include "board.h"
16
17 /*---------------------------------------------------------------------
18 *
19 * Interal API
20 *---------------------------------------------------------------------
21 */
rtl8211_id_check(ENET_Type * ptr)22 static bool rtl8211_id_check(ENET_Type *ptr)
23 {
24 uint16_t id1, id2;
25
26 id1 = enet_read_phy(ptr, PHY_ADDR, RTL8211_REG_PHYID1);
27 id2 = enet_read_phy(ptr, PHY_ADDR, RTL8211_REG_PHYID2);
28
29 if (RTL8211_PHYID1_OUI_MSB_GET(id1) == PHY_ID1 && RTL8211_PHYID2_OUI_MSB_GET(id2) == PHY_ID2) {
30 return true;
31 } else {
32 return false;
33 }
34 }
35
36 /*---------------------------------------------------------------------
37 * API
38 *---------------------------------------------------------------------
39 */
rtl8211_register_check(ENET_Type * ptr,uint32_t addr)40 uint16_t rtl8211_register_check(ENET_Type *ptr, uint32_t addr)
41 {
42 return enet_read_phy(ptr, PHY_ADDR, addr);
43 }
44
rtl8211_reset(ENET_Type * ptr)45 void rtl8211_reset(ENET_Type *ptr)
46 {
47 uint16_t data;
48
49 /* PHY reset */
50 enet_write_phy(ptr, PHY_ADDR, RTL8211_REG_BMCR, RTL8211_BMCR_RESET_SET(1));
51
52 /* wait until the reset is completed */
53 do {
54 data = enet_read_phy(ptr, PHY_ADDR, RTL8211_REG_BMCR);
55 } while (RTL8211_BMCR_RESET_GET(data));
56 }
57
rtl8211_basic_mode_default_config(ENET_Type * ptr,rtl8211_config_t * config)58 void rtl8211_basic_mode_default_config(ENET_Type *ptr, rtl8211_config_t *config)
59 {
60 config->loopback = 0; /* Enable PCS loopback mode */
61 config->speed = 1; /* reserved:3/2; 100mbps: 1; 10mbps: 0 */
62 config->auto_negotiation = 1; /* Enable Auto-Negotiation */
63 config->duplex_mode = 1; /* Full duplex mode */
64 }
65
rtl8211_basic_mode_init(ENET_Type * ptr,rtl8211_config_t * config)66 bool rtl8211_basic_mode_init(ENET_Type *ptr, rtl8211_config_t *config)
67 {
68 uint16_t para = 0;
69
70 para |= RTL8211_BMCR_RESET_SET(0) /* Normal operation */
71 | RTL8211_BMCR_LOOPBACK_SET(config->loopback) /* configure PCS loopback mode */
72 | RTL8211_BMCR_ANE_SET(config->auto_negotiation) /* configure Auto-Negotiation */
73 | RTL8211_BMCR_PWD_SET(0) /* Normal operation */
74 | RTL8211_BMCR_ISOLATE_SET(0) /* Normal operation */
75 | RTL8211_BMCR_RESTART_AN_SET(0) /* Normal operation (ignored when Auto-Negotiation is disabled) */
76 | RTL8211_BMCR_COLLISION_TEST_SET(0); /* Normal operation */
77
78 if (config->auto_negotiation == 0) {
79 para |= RTL8211_BMCR_SPEED0_SET(config->speed) | RTL8211_BMCR_SPEED1_SET(config->speed >> 1);
80 }
81
82 enet_write_phy(ptr, PHY_ADDR, RTL8211_REG_BMCR, para);
83
84 /* check the id of rtl8211 */
85 if (rtl8211_id_check(ptr) == false) {
86 return false;
87 }
88
89 return true;
90 }
91
rtl8211_auto_negotiation_init(void)92 void rtl8211_auto_negotiation_init(void)
93 {
94 /* TODO */
95 }
96
97
rtl8211_read_status(ENET_Type * ptr)98 void rtl8211_read_status(ENET_Type *ptr)
99 {
100 uint16_t status;
101
102 status = enet_read_phy(ptr, PHY_ADDR, RTL8211_REG_BMSR);
103 printf("BMSR: %08x\n", status);
104 status = enet_read_phy(ptr, PHY_ADDR, RTL8211_REG_GBSR);
105 printf("GBSR: %08x\n", status);
106 status = enet_read_phy(ptr, PHY_ADDR, RTL8211_REG_GBESR);
107 printf("GBESR: %08x\n", status);
108 status = enet_read_phy(ptr, PHY_ADDR, RTL8211_REG_PHYSR);
109 printf("PHYSR: %08x\n", status);
110 status = enet_read_phy(ptr, PHY_ADDR, RTL8211_REG_RXERC);
111 printf("RXERC: %08x\n", status);
112
113 status = enet_read_phy(ptr, PHY_ADDR, RTL8211_REG_PHYCR);
114 printf("PHYCR, %x\n", status);
115 }
116
rtl8211_control_config(ENET_Type * ptr)117 void rtl8211_control_config(ENET_Type *ptr)
118 {
119 uint16_t para = 0;
120
121 para = enet_read_phy(ptr, PHY_ADDR, RTL8211_REG_PHYCR) | (1 << 10);
122
123 enet_write_phy(ptr, PHY_ADDR, RTL8211_REG_PHYCR, para);
124
125 }
126