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