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