1 /* Stellaris Ethernet Controller
2 *
3 * Copyright (c) 2018 Zilogic Systems
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #define DT_DRV_COMPAT ti_stellaris_ethernet
9
10 #define LOG_MODULE_NAME eth_stellaris
11 #define LOG_LEVEL CONFIG_ETHERNET_LOG_LEVEL
12 #include <zephyr/logging/log.h>
13 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
14
15 #include <zephyr/net/ethernet.h>
16 #include <zephyr/net/net_pkt.h>
17 #include <zephyr/net/net_if.h>
18 #include <zephyr/device.h>
19 #include <soc.h>
20 #include <ethernet/eth_stats.h>
21 #include <zephyr/irq.h>
22 #include "eth_stellaris_priv.h"
23
eth_stellaris_assign_mac(const struct device * dev)24 static void eth_stellaris_assign_mac(const struct device *dev)
25 {
26 uint8_t mac_addr[6] = DT_INST_PROP(0, local_mac_address);
27 uint32_t value = 0x0;
28
29 value |= mac_addr[0];
30 value |= mac_addr[1] << 8;
31 value |= mac_addr[2] << 16;
32 value |= mac_addr[3] << 24;
33 sys_write32(value, REG_MACIA0);
34
35 value = 0x0;
36 value |= mac_addr[4];
37 value |= mac_addr[5] << 8;
38 sys_write32(value, REG_MACIA1);
39 }
40
eth_stellaris_flush(const struct device * dev)41 static void eth_stellaris_flush(const struct device *dev)
42 {
43 struct eth_stellaris_runtime *dev_data = dev->data;
44
45 if (dev_data->tx_pos != 0) {
46 sys_write32(dev_data->tx_word, REG_MACDATA);
47 dev_data->tx_pos = 0;
48 dev_data->tx_word = 0U;
49 }
50 }
51
eth_stellaris_send_byte(const struct device * dev,uint8_t byte)52 static void eth_stellaris_send_byte(const struct device *dev, uint8_t byte)
53 {
54 struct eth_stellaris_runtime *dev_data = dev->data;
55
56 dev_data->tx_word |= byte << (dev_data->tx_pos * 8);
57 dev_data->tx_pos++;
58 if (dev_data->tx_pos == 4) {
59 sys_write32(dev_data->tx_word, REG_MACDATA);
60 dev_data->tx_pos = 0;
61 dev_data->tx_word = 0U;
62 }
63 }
64
eth_stellaris_send(const struct device * dev,struct net_pkt * pkt)65 static int eth_stellaris_send(const struct device *dev, struct net_pkt *pkt)
66 {
67 struct eth_stellaris_runtime *dev_data = dev->data;
68 struct net_buf *frag;
69 uint16_t i, data_len;
70
71 /* Frame transmission
72 *
73 * First two bytes is the length of the frame, exclusive of
74 * the header length.
75 */
76 data_len = net_pkt_get_len(pkt) - sizeof(struct net_eth_hdr);
77 eth_stellaris_send_byte(dev, data_len & 0xff);
78 eth_stellaris_send_byte(dev, (data_len & 0xff00) >> 8);
79
80 /* Send the payload */
81 for (frag = pkt->frags; frag; frag = frag->frags) {
82 for (i = 0U; i < frag->len; ++i) {
83 eth_stellaris_send_byte(dev, frag->data[i]);
84 }
85 }
86
87 /* Will transmit the partial word. */
88 eth_stellaris_flush(dev);
89
90 /* Enable transmit. */
91 sys_write32(BIT_MACTR_NEWTX, REG_MACTR);
92
93 /* Wait and check if transmit successful or not. */
94 k_sem_take(&dev_data->tx_sem, K_FOREVER);
95
96 if (dev_data->tx_err) {
97 dev_data->tx_err = false;
98 return -EIO;
99 }
100
101 LOG_DBG("pkt sent %p len %d", pkt, data_len);
102
103 return 0;
104 }
105
eth_stellaris_rx_error(struct net_if * iface)106 static void eth_stellaris_rx_error(struct net_if *iface)
107 {
108 const struct device *dev = net_if_get_device(iface);
109 uint32_t val;
110
111 eth_stats_update_errors_rx(iface);
112
113 /* Clear the rx_frame buffer,
114 * otherwise it could lead to underflow errors
115 */
116 sys_write32(0x0, REG_MACRCTL);
117 sys_write32(BIT_MACRCTL_RSTFIFO, REG_MACRCTL);
118 val = BIT_MACRCTL_BADCRC | BIT_MACRCTL_RXEN;
119 sys_write32(val, REG_MACRCTL);
120 }
121
eth_stellaris_rx_pkt(const struct device * dev,struct net_if * iface)122 static struct net_pkt *eth_stellaris_rx_pkt(const struct device *dev,
123 struct net_if *iface)
124 {
125 int frame_len, bytes_left;
126 struct net_pkt *pkt;
127 uint32_t reg_val;
128 uint16_t count;
129 uint8_t *data;
130
131 /*
132 * The Ethernet frame received from the hardware has the
133 * following format. The first two bytes contains the ethernet
134 * frame length, followed by the actual ethernet frame.
135 *
136 * +---------+---- ... -------+
137 * | Length | Ethernet Frame |
138 * +---------+---- ... -------+
139 */
140
141 /*
142 * The first word contains the frame length and a portion of
143 * the ethernet frame. Extract the frame length.
144 */
145 reg_val = sys_read32(REG_MACDATA);
146 frame_len = reg_val & 0x0000ffff;
147
148 pkt = net_pkt_rx_alloc_with_buffer(iface, frame_len,
149 AF_UNSPEC, 0, K_NO_WAIT);
150 if (!pkt) {
151 return NULL;
152 }
153
154 /*
155 * The remaining 2 bytes, in the first word is appended to the
156 * ethernet frame.
157 */
158 count = 2U;
159 data = (uint8_t *)®_val + 2;
160 if (net_pkt_write(pkt, data, count)) {
161 goto error;
162 }
163
164 /* A word has been read already, thus minus 4 bytes to be read. */
165 bytes_left = frame_len - 4;
166
167 /* Read the rest of words, minus the partial word and FCS byte. */
168 for (; bytes_left > 7; bytes_left -= 4) {
169 reg_val = sys_read32(REG_MACDATA);
170 count = 4U;
171 data = (uint8_t *)®_val;
172 if (net_pkt_write(pkt, data, count)) {
173 goto error;
174 }
175 }
176
177 /* Handle the last partial word and discard the 4 Byte FCS. */
178 while (bytes_left > 0) {
179 /* Read the partial word. */
180 reg_val = sys_read32(REG_MACDATA);
181
182 /* Discard the last FCS word. */
183 if (bytes_left <= 4) {
184 bytes_left = 0;
185 break;
186 }
187
188 count = bytes_left - 4;
189 data = (uint8_t *)®_val;
190 if (net_pkt_write(pkt, data, count)) {
191 goto error;
192 }
193
194 bytes_left -= 4;
195 }
196
197 return pkt;
198
199 error:
200 net_pkt_unref(pkt);
201
202 return NULL;
203 }
204
eth_stellaris_rx(const struct device * dev)205 static int eth_stellaris_rx(const struct device *dev)
206 {
207 struct eth_stellaris_runtime *dev_data = dev->data;
208 struct net_if *iface = dev_data->iface;
209 struct net_pkt *pkt;
210
211 pkt = eth_stellaris_rx_pkt(dev, iface);
212 if (!pkt) {
213 LOG_ERR("Failed to read data");
214 goto err_mem;
215 }
216
217 if (net_recv_data(iface, pkt) < 0) {
218 LOG_ERR("Failed to place frame in RX Queue");
219 goto pkt_unref;
220 }
221
222 return 0;
223
224 pkt_unref:
225 net_pkt_unref(pkt);
226
227 err_mem:
228 eth_stellaris_rx_error(iface);
229 return -EIO;
230 }
231
eth_stellaris_isr(const struct device * dev)232 static void eth_stellaris_isr(const struct device *dev)
233 {
234 struct eth_stellaris_runtime *dev_data = dev->data;
235 int isr_val = sys_read32(REG_MACRIS);
236 int num_packets;
237 uint32_t lock;
238
239 lock = irq_lock();
240
241 /* Acknowledge the interrupt. */
242 sys_write32(isr_val, REG_MACRIS);
243
244 if (isr_val & BIT_MACRIS_RXINT) {
245 /*
246 * When multiple packets are received by the Ethernet,
247 * only one interrupt may be dispatched to the driver
248 * Therefore, it is necessary to obtain the register NP value
249 * to get how many packets are in the Ethernet.
250 */
251 num_packets = sys_read32(REG_MACNP);
252 for (int i = 0; i < num_packets; i++) {
253 if (eth_stellaris_rx(dev) != 0) {
254 break;
255 }
256 }
257 }
258
259 if (isr_val & BIT_MACRIS_TXEMP) {
260 dev_data->tx_err = false;
261 k_sem_give(&dev_data->tx_sem);
262 }
263
264 if (isr_val & BIT_MACRIS_TXER) {
265 LOG_ERR("Transmit Frame Error");
266 eth_stats_update_errors_tx(dev_data->iface);
267 dev_data->tx_err = true;
268 k_sem_give(&dev_data->tx_sem);
269 }
270
271 if (isr_val & BIT_MACRIS_RXER) {
272 LOG_ERR("Error Receiving Frame");
273 eth_stellaris_rx_error(dev_data->iface);
274 }
275
276 if (isr_val & BIT_MACRIS_FOV) {
277 LOG_ERR("Error Rx Overrun");
278 eth_stellaris_rx_error(dev_data->iface);
279 }
280
281 irq_unlock(lock);
282 }
283
eth_stellaris_init(struct net_if * iface)284 static void eth_stellaris_init(struct net_if *iface)
285 {
286 const struct device *dev = net_if_get_device(iface);
287 const struct eth_stellaris_config *dev_conf = dev->config;
288 struct eth_stellaris_runtime *dev_data = dev->data;
289
290 dev_data->iface = iface;
291
292 /* Assign link local address. */
293 net_if_set_link_addr(iface,
294 dev_data->mac_addr, 6, NET_LINK_ETHERNET);
295
296 ethernet_init(iface);
297
298 /* Initialize semaphore. */
299 k_sem_init(&dev_data->tx_sem, 0, 1);
300
301 /* Initialize Interrupts. */
302 dev_conf->config_func(dev);
303 }
304
305 #if defined(CONFIG_NET_STATISTICS_ETHERNET)
eth_stellaris_stats(const struct device * dev)306 static struct net_stats_eth *eth_stellaris_stats(const struct device *dev)
307 {
308 struct eth_stellaris_runtime *dev_data = dev->data;
309
310 return &dev_data->stats;
311 }
312 #endif
313
eth_stellaris_dev_init(const struct device * dev)314 static int eth_stellaris_dev_init(const struct device *dev)
315 {
316 uint32_t value;
317
318 /* Assign MAC address to Hardware */
319 eth_stellaris_assign_mac(dev);
320
321 /* Program MCRCTL to clear RXFIFO */
322 value = BIT_MACRCTL_RSTFIFO;
323 sys_write32(value, REG_MACRCTL);
324
325 /* Enable transmitter */
326 value = BIT_MACTCTL_DUPLEX | BIT_MACTCTL_CRC |
327 BIT_MACTCTL_PADEN | BIT_MACTCTL_TXEN;
328 sys_write32(value, REG_MACTCTL);
329
330 /* Enable Receiver */
331 value = BIT_MACRCTL_BADCRC | BIT_MACRCTL_RXEN;
332 sys_write32(value, REG_MACRCTL);
333
334 return 0;
335 }
336
eth_stellaris_irq_config(const struct device * dev)337 static void eth_stellaris_irq_config(const struct device *dev)
338 {
339 /* Enable Interrupt. */
340 IRQ_CONNECT(DT_INST_IRQN(0),
341 DT_INST_IRQ(0, priority),
342 eth_stellaris_isr, DEVICE_DT_INST_GET(0), 0);
343 irq_enable(DT_INST_IRQN(0));
344 }
345
346 struct eth_stellaris_config eth_cfg = {
347 .mac_base = DT_INST_REG_ADDR(0),
348 .config_func = eth_stellaris_irq_config,
349 };
350
351 struct eth_stellaris_runtime eth_data = {
352 .mac_addr = DT_INST_PROP(0, local_mac_address),
353 .tx_err = false,
354 .tx_word = 0,
355 .tx_pos = 0,
356 };
357
358 static const struct ethernet_api eth_stellaris_apis = {
359 .iface_api.init = eth_stellaris_init,
360 .send = eth_stellaris_send,
361 #if defined(CONFIG_NET_STATISTICS_ETHERNET)
362 .get_stats = eth_stellaris_stats,
363 #endif
364 };
365
366 NET_DEVICE_DT_INST_DEFINE(0,
367 eth_stellaris_dev_init, NULL,
368 ð_data, ð_cfg, CONFIG_ETH_INIT_PRIORITY,
369 ð_stellaris_apis, ETHERNET_L2,
370 NET_L2_GET_CTX_TYPE(ETHERNET_L2), NET_ETH_MTU);
371