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 *)&reg_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 *)&reg_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 *)&reg_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 void 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;
223 
224 pkt_unref:
225 	net_pkt_unref(pkt);
226 
227 err_mem:
228 	eth_stellaris_rx_error(iface);
229 }
230 
eth_stellaris_isr(const struct device * dev)231 static void eth_stellaris_isr(const struct device *dev)
232 {
233 	struct eth_stellaris_runtime *dev_data = dev->data;
234 	int isr_val = sys_read32(REG_MACRIS);
235 	uint32_t lock;
236 
237 	lock = irq_lock();
238 
239 	/* Acknowledge the interrupt. */
240 	sys_write32(isr_val, REG_MACRIS);
241 
242 	if (isr_val & BIT_MACRIS_RXINT) {
243 		eth_stellaris_rx(dev);
244 	}
245 
246 	if (isr_val & BIT_MACRIS_TXEMP) {
247 		dev_data->tx_err = false;
248 		k_sem_give(&dev_data->tx_sem);
249 	}
250 
251 	if (isr_val & BIT_MACRIS_TXER) {
252 		LOG_ERR("Transmit Frame Error");
253 		eth_stats_update_errors_tx(dev_data->iface);
254 		dev_data->tx_err = true;
255 		k_sem_give(&dev_data->tx_sem);
256 	}
257 
258 	if (isr_val & BIT_MACRIS_RXER) {
259 		LOG_ERR("Error Receiving Frame");
260 		eth_stellaris_rx_error(dev_data->iface);
261 	}
262 
263 	if (isr_val & BIT_MACRIS_FOV) {
264 		LOG_ERR("Error Rx Overrun");
265 		eth_stellaris_rx_error(dev_data->iface);
266 	}
267 
268 	irq_unlock(lock);
269 }
270 
eth_stellaris_init(struct net_if * iface)271 static void eth_stellaris_init(struct net_if *iface)
272 {
273 	const struct device *dev = net_if_get_device(iface);
274 	const struct eth_stellaris_config *dev_conf = dev->config;
275 	struct eth_stellaris_runtime *dev_data = dev->data;
276 
277 	dev_data->iface = iface;
278 
279 	/* Assign link local address. */
280 	net_if_set_link_addr(iface,
281 			     dev_data->mac_addr, 6, NET_LINK_ETHERNET);
282 
283 	ethernet_init(iface);
284 
285 	/* Initialize semaphore. */
286 	k_sem_init(&dev_data->tx_sem, 0, 1);
287 
288 	/* Initialize Interrupts. */
289 	dev_conf->config_func(dev);
290 }
291 
292 #if defined(CONFIG_NET_STATISTICS_ETHERNET)
eth_stellaris_stats(const struct device * dev)293 static struct net_stats_eth *eth_stellaris_stats(const struct device *dev)
294 {
295 	struct eth_stellaris_runtime *dev_data = dev->data;
296 
297 	return &dev_data->stats;
298 }
299 #endif
300 
eth_stellaris_dev_init(const struct device * dev)301 static int eth_stellaris_dev_init(const struct device *dev)
302 {
303 	uint32_t value;
304 
305 	/* Assign MAC address to Hardware */
306 	eth_stellaris_assign_mac(dev);
307 
308 	/* Program MCRCTL to clear RXFIFO */
309 	value = BIT_MACRCTL_RSTFIFO;
310 	sys_write32(value, REG_MACRCTL);
311 
312 	/* Enable transmitter */
313 	value = BIT_MACTCTL_DUPLEX | BIT_MACTCTL_CRC |
314 		BIT_MACTCTL_PADEN | BIT_MACTCTL_TXEN;
315 	sys_write32(value, REG_MACTCTL);
316 
317 	/* Enable Receiver */
318 	value = BIT_MACRCTL_BADCRC | BIT_MACRCTL_RXEN;
319 	sys_write32(value, REG_MACRCTL);
320 
321 	return 0;
322 }
323 
eth_stellaris_irq_config(const struct device * dev)324 static void eth_stellaris_irq_config(const struct device *dev)
325 {
326 	/* Enable Interrupt. */
327 	IRQ_CONNECT(DT_INST_IRQN(0),
328 		    DT_INST_IRQ(0, priority),
329 		    eth_stellaris_isr, DEVICE_DT_INST_GET(0), 0);
330 	irq_enable(DT_INST_IRQN(0));
331 }
332 
333 struct eth_stellaris_config eth_cfg = {
334 	.mac_base = DT_INST_REG_ADDR(0),
335 	.config_func = eth_stellaris_irq_config,
336 };
337 
338 struct eth_stellaris_runtime eth_data = {
339 	.mac_addr = DT_INST_PROP(0, local_mac_address),
340 	.tx_err = false,
341 	.tx_word = 0,
342 	.tx_pos = 0,
343 };
344 
345 static const struct ethernet_api eth_stellaris_apis = {
346 	.iface_api.init	= eth_stellaris_init,
347 	.send =  eth_stellaris_send,
348 #if defined(CONFIG_NET_STATISTICS_ETHERNET)
349 	.get_stats = eth_stellaris_stats,
350 #endif
351 };
352 
353 NET_DEVICE_DT_INST_DEFINE(0,
354 		eth_stellaris_dev_init, NULL,
355 		&eth_data, &eth_cfg, CONFIG_ETH_INIT_PRIORITY,
356 		&eth_stellaris_apis, ETHERNET_L2,
357 		NET_L2_GET_CTX_TYPE(ETHERNET_L2), NET_ETH_MTU);
358