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_dp83848_regs.h"
14 #include "hpm_dp83848.h"
15 /*---------------------------------------------------------------------
16 * Internal API
17 *---------------------------------------------------------------------
18 */
dp83848_check_id(ENET_Type * ptr)19 static bool dp83848_check_id(ENET_Type *ptr)
20 {
21 uint16_t id1, id2;
22
23 id1 = enet_read_phy(ptr, DP83848_ADDR, DP83848_PHYIDR1);
24 id2 = enet_read_phy(ptr, DP83848_ADDR, DP83848_PHYIDR2);
25
26 if (DP83848_PHYIDR1_OUI_MSB_GET(id1) == DP83848_ID1 && DP83848_PHYIDR2_OUI_LSB_GET(id2) == DP83848_ID2) {
27 return true;
28 } else {
29 return false;
30 }
31 }
32
33 /*---------------------------------------------------------------------
34 * API
35 *---------------------------------------------------------------------
36 */
dp83848_reset(ENET_Type * ptr)37 void dp83848_reset(ENET_Type *ptr)
38 {
39 uint16_t data;
40
41 /* PHY reset */
42 enet_write_phy(ptr, DP83848_ADDR, DP83848_BMCR, DP83848_BMCR_RESET_SET(1));
43
44 /* wait until the reset is completed */
45 do {
46 data = enet_read_phy(ptr, DP83848_ADDR, DP83848_BMCR);
47 } while (DP83848_BMCR_RESET_GET(data));
48 }
49
dp83848_basic_mode_default_config(ENET_Type * ptr,dp83848_config_t * config)50 void dp83848_basic_mode_default_config(ENET_Type *ptr, dp83848_config_t *config)
51 {
52 (void)ptr;
53
54 config->loopback = false; /* Disable PCS loopback mode */
55 #if defined(__DISABLE_AUTO_NEGO) && (__DISABLE_AUTO_NEGO)
56 config->auto_negotiation = false; /* Disable Auto-Negotiation */
57 config->speed = enet_phy_port_speed_100mbps;
58 config->duplex = enet_phy_duplex_full;
59 #else
60 config->auto_negotiation = true; /* Enable Auto-Negotiation */
61 #endif
62 }
63
dp83848_basic_mode_init(ENET_Type * ptr,dp83848_config_t * config)64 bool dp83848_basic_mode_init(ENET_Type *ptr, dp83848_config_t *config)
65 {
66 uint16_t data = 0;
67
68 data |= DP83848_BMCR_RESET_SET(0) /* Normal operation */
69 | DP83848_BMCR_LOOPBACK_SET(config->loopback) /* configure PCS loopback mode */
70 | DP83848_BMCR_ANE_SET(config->auto_negotiation) /* configure Auto-Negotiation */
71 | DP83848_BMCR_PWD_SET(0) /* Normal operation */
72 | DP83848_BMCR_ISOLATE_SET(0) /* Normal operation */
73 | DP83848_BMCR_RESTART_AN_SET(0) /* Normal operation (ignored when Auto-Negotiation is disabled) */
74 | DP83848_BMCR_COLLISION_TEST_SET(0); /* Normal operation */
75
76 if (config->auto_negotiation == false) {
77 data |= DP83848_BMCR_SPEED0_SET(config->speed); /* Set port speed */
78 data |= DP83848_BMCR_DUPLEX_SET(config->duplex); /* Set duplex mode */
79 }
80
81 /* check the id of dp83848 */
82 if (dp83848_check_id(ptr) == false) {
83 return false;
84 }
85
86 enet_write_phy(ptr, DP83848_ADDR, DP83848_BMCR, data);
87
88 return true;
89 }
90
dp83848_get_phy_status(ENET_Type * ptr,enet_phy_status_t * status)91 void dp83848_get_phy_status(ENET_Type *ptr, enet_phy_status_t *status)
92 {
93 uint16_t data;
94
95 data = enet_read_phy(ptr, DP83848_ADDR, DP83848_PHYSTS);
96 status->enet_phy_link = DP83848_PHYSTS_LINK_STATUS_GET(data);
97 status->enet_phy_speed = DP83848_PHYSTS_SPEED_STATUS_GET(data) ? enet_phy_port_speed_10mbps : enet_phy_port_speed_100mbps;
98 status->enet_phy_duplex = DP83848_PHYSTS_DUPLEX_STATUS_GET(data);
99 }
100