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
23
24 static ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(ENET_SOC_DESC_ADDR_ALIGNMENT)
25 __RW enet_rx_desc_t rxDescTab0[ENET_RX_BUFF_COUNT] ; /* Ethernet Rx DMA Descriptor */
26
27 static ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(ENET_SOC_DESC_ADDR_ALIGNMENT)
28 __RW enet_tx_desc_t txDescTab0[ENET_TX_BUFF_COUNT] ; /* Ethernet Tx DMA Descriptor */
29
30 static ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(ENET_SOC_BUFF_ADDR_ALIGNMENT)
31 __RW uint8_t rxBuff0[ENET_RX_BUFF_COUNT][ENET_RX_BUFF_SIZE]; /* Ethernet Receive Buffer */
32
33 static ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(ENET_SOC_BUFF_ADDR_ALIGNMENT)
34 __RW uint8_t txBuff0[ENET_TX_BUFF_COUNT][ENET_TX_BUFF_SIZE]; /* Ethernet Transmit Buffer */
35
36 static ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(ENET_SOC_DESC_ADDR_ALIGNMENT)
37 __RW enet_rx_desc_t rxDescTab1[ENET_RX_BUFF_COUNT] ; /* Ethernet Rx DMA Descriptor */
38
39 static ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(ENET_SOC_DESC_ADDR_ALIGNMENT)
40 __RW enet_tx_desc_t txDescTab1[ENET_TX_BUFF_COUNT] ; /* Ethernet Tx DMA Descriptor */
41
42 static ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(ENET_SOC_BUFF_ADDR_ALIGNMENT)
43 __RW uint8_t rxBuff1[ENET_RX_BUFF_COUNT][ENET_RX_BUFF_SIZE]; /* Ethernet Receive Buffer */
44
45 static ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(ENET_SOC_BUFF_ADDR_ALIGNMENT)
46 __RW uint8_t txBuff1[ENET_TX_BUFF_COUNT][ENET_TX_BUFF_SIZE]; /* Ethernet Transmit Buffer */
47
48
49 struct HpmEnetDevice enetDev[2] = {
50 [0] = {
51 .isEnable = 1,
52 .isDefault = 1,
53 .name = "geth",
54 .base = BOARD_ENET_RGMII,
55 .irqNum = IRQn_ENET0,
56 .infType = enet_inf_rgmii,
57 .macAddr = {0x98, 0x2C, 0xBC, 0xB1, 0x9F, 0x15},
58 .ip = {192, 168, 2, 35},
59 .netmask = {255, 255, 255, 0},
60 .gw = {192, 168, 1, 1},
61 .desc = {
62 .tx_desc_list_head = txDescTab0,
63 .rx_desc_list_head = rxDescTab0,
64 .tx_buff_cfg = {
65 .buffer = (uint32_t)txBuff0,
66 .count = ENET_TX_BUFF_COUNT,
67 .size = ENET_TX_BUFF_SIZE,
68 },
69 .rx_buff_cfg = {
70 .buffer = (uint32_t)rxBuff0,
71 .count = ENET_RX_BUFF_COUNT,
72 .size = ENET_RX_BUFF_SIZE,
73 },
74
75 },
76 },
77 [1] = {
78 .isEnable = 1,
79 .isDefault = 0,
80 .name = "eth",
81 .base = BOARD_ENET_RMII,
82 .irqNum = IRQn_ENET1,
83 .infType = enet_inf_rmii,
84 .macAddr = {0x98, 0x2C, 0xBC, 0xB1, 0x9F, 0x17},
85 .ip = {192, 168, 1, 88},
86 .netmask = {255, 255, 255, 0},
87 .gw = {192, 168, 1, 1},
88 .desc = {
89 .tx_desc_list_head = txDescTab1,
90 .rx_desc_list_head = rxDescTab1,
91 .tx_buff_cfg = {
92 .buffer = (uint32_t)txBuff1,
93 .count = ENET_TX_BUFF_COUNT,
94 .size = ENET_TX_BUFF_SIZE,
95 },
96 .rx_buff_cfg = {
97 .buffer = (uint32_t)rxBuff1,
98 .count = ENET_RX_BUFF_COUNT,
99 .size = ENET_RX_BUFF_SIZE,
100 },
101 },
102 },
103 };
104
105
enetDevInit(struct HpmEnetDevice * dev)106 void enetDevInit(struct HpmEnetDevice *dev)
107 {
108 if (!dev->isEnable) {
109 return 0;
110 }
111
112 board_init_enet_pins(dev->base);
113 board_reset_enet_phy(dev->base);
114
115 if (dev->infType == enet_inf_rmii) {
116 board_init_enet_rmii_reference_clock(dev->base, BOARD_ENET_RMII_INT_REF_CLK);
117 }
118
119 if (dev->infType == enet_inf_rgmii) {
120 board_init_enet_rgmii_clock_delay(dev->base);
121 }
122
123 memset(dev->desc.rx_desc_list_head, 0x00, sizeof(enet_rx_desc_t) * dev->desc.rx_buff_cfg.count);
124 memset(dev->desc.tx_desc_list_head, 0x00, sizeof(enet_tx_desc_t) * dev->desc.tx_buff_cfg.count);
125
126 enet_mac_config_t macCfg;
127 macCfg.mac_addr_high[0] = dev->macAddr[5];
128 macCfg.mac_addr_high[0] <<= 8;
129 macCfg.mac_addr_high[0] |= dev->macAddr[4];
130
131 macCfg.mac_addr_low[0] = dev->macAddr[3];
132 macCfg.mac_addr_low[0] <<= 8;
133 macCfg.mac_addr_low[0] |= dev->macAddr[2];
134 macCfg.mac_addr_low[0] <<= 8;
135 macCfg.mac_addr_low[0] |= dev->macAddr[1];
136 macCfg.mac_addr_low[0] <<= 8;
137 macCfg.mac_addr_low[0] |= dev->macAddr[0];
138 macCfg.valid_max_count = 1;
139
140 uint32_t dmaIntEnable = ENET_DMA_INTR_EN_NIE_SET(1) /* Enable normal interrupt summary */
141 | ENET_DMA_INTR_EN_RIE_SET(1); /* Enable receive interrupt */
142 enet_controller_init(dev->base, dev->infType, &dev->desc, &macCfg, dmaIntEnable);
143 dev->base->INTR_MASK |= 0xFFFFFFFF;
144 dev->base->MMC_INTR_MASK_RX |= 0xFFFFFFFF;
145 dev->base->MMC_INTR_MASK_TX |= 0xFFFFFFFF;
146 dev->base->MMC_IPC_INTR_MASK_RX |= 0xFFFFFFFF;
147 enet_disable_lpi_interrupt(dev->base);
148
149 if (dev->infType == enet_inf_rgmii) {
150 rtl8211_config_t phyConfig;
151 rtl8211_reset(dev->base);
152 rtl8211_basic_mode_default_config(dev->base, &phyConfig);
153 rtl8211_basic_mode_init(dev->base, &phyConfig);
154 }
155
156 if (dev->infType == enet_inf_rmii) {
157 rtl8201_config_t phyConfig;
158 rtl8201_reset(dev->base);
159 rtl8201_basic_mode_default_config(dev->base, &phyConfig);
160 rtl8201_basic_mode_init(dev->base, &phyConfig);
161 }
162
163 ip_addr_t ipaddr;
164 ip_addr_t netmask;
165 ip_addr_t gw;
166
167 IP_ADDR4(&ipaddr, dev->ip[0], dev->ip[1], dev->ip[2], dev->ip[3]);
168 IP_ADDR4(&netmask, dev->netmask[0], dev->netmask[1], dev->netmask[2], dev->netmask[3]);
169 IP_ADDR4(&gw, dev->gw[0], dev->gw[1], dev->gw[2], dev->gw[3]);
170
171 netif_add(&dev->netif, &ipaddr, &netmask, &gw, dev, ethernetif_init, tcpip_input);
172
173 if (dev->isDefault) {
174 netif_set_default(&dev->netif);
175 }
176
177 netif_set_up(&dev->netif);
178 }
179
180
HpmLwipInit(void)181 void HpmLwipInit(void)
182 {
183 printf("HpmLwipInit...\n");
184
185 tcpip_init(NULL, NULL);
186
187 enetDevInit(&enetDev[0]);
188 enetDevInit(&enetDev[1]);
189 }
190
191
192 APP_SERVICE_INIT(HpmLwipInit);
193