• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 HPMicro
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include "lwip/tcpip.h"
18 #include "ohos_init.h"
19 #include "hpm_lwip.h"
20 #include "ethernetif.h"
21 #include "lwip/tcpip.h"
22 #include <los_task.h>
23 
24 
25 static ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(ENET_SOC_DESC_ADDR_ALIGNMENT)
26 __RW enet_rx_desc_t rxDescTab0[ENET_RX_BUFF_COUNT] ; /* Ethernet Rx DMA Descriptor */
27 
28 static ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(ENET_SOC_DESC_ADDR_ALIGNMENT)
29 __RW enet_tx_desc_t txDescTab0[ENET_TX_BUFF_COUNT] ; /* Ethernet Tx DMA Descriptor */
30 
31 static ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(ENET_SOC_BUFF_ADDR_ALIGNMENT)
32 __RW uint8_t rxBuff0[ENET_RX_BUFF_COUNT][ENET_RX_BUFF_SIZE]; /* Ethernet Receive Buffer */
33 
34 static ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(ENET_SOC_BUFF_ADDR_ALIGNMENT)
35 __RW uint8_t txBuff0[ENET_TX_BUFF_COUNT][ENET_TX_BUFF_SIZE]; /* Ethernet Transmit Buffer */
36 
37 struct HpmEnetDevice enetDev[1] = {
38     [0] = {
39         .isEnable = 1,
40         .isDefault = 1,
41         .name = "geth",
42         .base = BOARD_ENET_RGMII,
43         .irqNum = IRQn_ENET0,
44         .infType = enet_inf_rgmii,
45         .macAddr = {0x98, 0x2C, 0xBC, 0xB1, 0x9F, 0x15},
46         .ip = {10, 10, 10, 224},
47         .netmask = {255, 255, 255, 0},
48         .gw = {10, 10, 10, 1},
49         .desc = {
50             .tx_desc_list_head = txDescTab0,
51             .rx_desc_list_head = rxDescTab0,
52             .tx_buff_cfg = {
53                 .buffer = (uint32_t)txBuff0,
54                 .count = ENET_TX_BUFF_COUNT,
55                 .size = ENET_TX_BUFF_SIZE,
56             },
57              .rx_buff_cfg = {
58                 .buffer = (uint32_t)rxBuff0,
59                 .count = ENET_RX_BUFF_COUNT,
60                 .size = ENET_RX_BUFF_SIZE,
61             },
62 
63         },
64     },
65 };
66 
67 
enetDevInit(struct HpmEnetDevice * dev)68 void enetDevInit(struct HpmEnetDevice *dev)
69 {
70     if (!dev->isEnable) {
71         return 0;
72     }
73 
74     board_init_enet_pins(dev->base);
75     board_reset_enet_phy(dev->base);
76 
77     if (dev->infType == enet_inf_rgmii) {
78         board_init_enet_rgmii_clock_delay(dev->base);
79     }
80 
81     memset(dev->desc.rx_desc_list_head, 0x00, sizeof(enet_rx_desc_t) * dev->desc.rx_buff_cfg.count);
82     memset(dev->desc.tx_desc_list_head, 0x00, sizeof(enet_tx_desc_t) * dev->desc.tx_buff_cfg.count);
83 
84     enet_mac_config_t macCfg;
85     macCfg.mac_addr_high[0] = dev->macAddr[5];
86     macCfg.mac_addr_high[0] <<= 8;
87     macCfg.mac_addr_high[0] |= dev->macAddr[4];
88 
89     macCfg.mac_addr_low[0]  = dev->macAddr[3];
90     macCfg.mac_addr_low[0] <<= 8;
91     macCfg.mac_addr_low[0] |= dev->macAddr[2];
92     macCfg.mac_addr_low[0] <<= 8;
93     macCfg.mac_addr_low[0] |= dev->macAddr[1];
94     macCfg.mac_addr_low[0] <<= 8;
95     macCfg.mac_addr_low[0] |= dev->macAddr[0];
96     macCfg.valid_max_count  = 1;
97 
98     /* Set DMA PBL */
99     macCfg.dma_pbl = enet_pbl_32;
100     /* Set SARC */
101     macCfg.sarc = enet_sarc_replace_mac0;
102     macCfg.valid_max_count  = 1;
103 
104     enet_int_config_t int_config = {.int_enable = 0, .int_mask = 0};
105 
106     /* Set the interrupt enable mask */
107     int_config.int_enable = enet_normal_int_sum_en    /* Enable normal interrupt summary */
108                           | enet_receive_int_en;      /* Enable receive interrupt */
109     int_config.int_mask = enet_rgsmii_int_mask; /* Disable RGSMII interrupt */
110     int_config.mmc_intr_mask_rx = 0x03ffffff;   /* Disable all mmc rx interrupt events */
111     int_config.mmc_intr_mask_tx = 0x03ffffff;   /* Disable all mmc tx interrupt events */
112 
113     enet_tx_control_config_t enet_tx_control_config;
114 
115     /*Get a default control config for tx descriptor */
116     enet_get_default_tx_control_config(dev->base, &enet_tx_control_config);
117 
118     /* Set the control config for tx descriptor */
119     memcpy(&dev->desc.tx_control_config, &enet_tx_control_config, sizeof(enet_tx_control_config_t));
120 
121     /* Initialize enet controller */
122     enet_controller_init(dev->base, dev->infType, &dev->desc, &macCfg, &int_config);
123     /* Disable LPI interrupt */
124     enet_disable_lpi_interrupt(dev->base);
125 
126     if (dev->infType == enet_inf_rgmii) {
127         rtl8211_config_t phyConfig;
128         rtl8211_reset(dev->base);
129         rtl8211_basic_mode_default_config(dev->base, &phyConfig);
130         rtl8211_basic_mode_init(dev->base, &phyConfig);
131     }
132 
133     ip_addr_t ipaddr;
134     ip_addr_t netmask;
135     ip_addr_t gw;
136 
137     IP_ADDR4(&ipaddr, dev->ip[0], dev->ip[1], dev->ip[2], dev->ip[3]);
138     IP_ADDR4(&netmask, dev->netmask[0], dev->netmask[1], dev->netmask[2], dev->netmask[3]);
139     IP_ADDR4(&gw, dev->gw[0], dev->gw[1], dev->gw[2], dev->gw[3]);
140 
141     netif_add(&dev->netif, &ipaddr, &netmask, &gw, dev, ethernetif_init, tcpip_input);
142 
143     if (dev->isDefault) {
144         netif_set_default(&dev->netif);
145     }
146 
147     netif_set_up(&dev->netif);
148 }
149 
150 void ethernetif_phy_adaptive_thread_start(void);
151 
HpmLwipInit(void)152 void HpmLwipInit(void)
153 {
154     printf("HpmLwipInit...\n");
155 
156     tcpip_init(NULL, NULL);
157 
158     enetDevInit(&enetDev[0]);
159     ethernetif_phy_adaptive_thread_start();
160 }
161 
enet_self_adaptive_port_speed(struct HpmEnetDevice * dev)162 void enet_self_adaptive_port_speed(struct HpmEnetDevice *dev)
163 {
164     enet_phy_status_t status = {0};
165     enet_phy_status_t *last_status = &dev->last_status;
166 
167     enet_line_speed_t line_speed[] = {enet_line_speed_10mbps, enet_line_speed_100mbps, enet_line_speed_1000mbps};
168     char *speed_str[] = {"10Mbps", "100Mbps", "1000Mbps"};
169     char *duplex_str[] = {"Half duplex", "Full duplex"};
170 
171     if (!dev->isEnable)
172         return;
173 
174     if (dev->infType == enet_inf_rgmii)
175         rtl8211_get_phy_status(dev->base, &status);
176 
177     if (dev->infType == enet_inf_rmii)
178         rtl8201_get_phy_status(dev->base, &status);
179 
180     if (memcmp(last_status, &status, sizeof(enet_phy_status_t)) != 0) {
181         memcpy(last_status, &status, sizeof(enet_phy_status_t));
182         if (status.enet_phy_link) {
183             printf("Link Status: Up\n");
184             printf("Link Speed:  %s\n", speed_str[status.enet_phy_speed]);
185             printf("Link Duplex: %s\n", duplex_str[status.enet_phy_duplex]);
186             enet_set_line_speed(dev->base, line_speed[status.enet_phy_speed]);
187             enet_set_duplex_mode(dev->base, status.enet_phy_duplex);
188         } else {
189             printf("Link Status: Down\n");
190         }
191     }
192 }
193 
ethernetif_phy_adaptive_thread(UINT32 arg)194 static VOID *ethernetif_phy_adaptive_thread(UINT32 arg)
195 {
196     struct netif *netif = (struct netif *)arg;
197     struct HpmEnetDevice *devs = (struct netif *)arg;
198     printf("ethernetif_adaptive_thread run...\n");
199 
200     while (1) {
201         enet_self_adaptive_port_speed(&devs[0]);
202         sleep(1);
203     }
204 }
205 
ethernetif_phy_adaptive_thread_start(void)206 void ethernetif_phy_adaptive_thread_start(void)
207 {
208     UINT32 taskID = LOS_ERRNO_TSK_ID_INVALID;
209     UINT32 ret;
210     TSK_INIT_PARAM_S task = {0};
211     /* Create host Task */
212     task.pfnTaskEntry = (TSK_ENTRY_FUNC)ethernetif_phy_adaptive_thread;
213     task.uwStackSize = 4096;
214     task.pcName = "phy";
215     task.usTaskPrio = 3;
216     task.uwArg = (UINTPTR)enetDev;
217     task.uwResved = LOS_TASK_STATUS_DETACHED;
218     ret = LOS_TaskCreate(&taskID, &task);
219     if (ret != LOS_OK) {
220         LWIP_DEBUGF(SYS_DEBUG, ("sys_thread_new: LOS_TaskCreate error %u\n", ret));
221         return -1;
222     }
223 }
224 
225 APP_SERVICE_INIT(HpmLwipInit);
226