1 /*
2  * Copyright (c) 2023 Arm Limited (or its affiliates). All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #include <zephyr/net/ethernet.h>
7 #include <zephyr/net/phy.h>
8 #include <zephyr/arch/cpu.h>
9 #include <zephyr/sys_clock.h>
10 #include <zephyr/drivers/mdio.h>
11 #include <zephyr/logging/log.h>
12 #include "eth_smsc91x_priv.h"
13 
14 #define DT_DRV_COMPAT smsc_lan91c111
15 
16 LOG_MODULE_REGISTER(eth_smsc91x, CONFIG_ETHERNET_LOG_LEVEL);
17 
18 #define SMSC_LOCK(sc)	   k_mutex_lock(&(sc)->lock, K_FOREVER)
19 #define SMSC_UNLOCK(sc)	   k_mutex_unlock(&(sc)->lock)
20 #define HW_CYCLE_PER_US	   (sys_clock_hw_cycles_per_sec() / 1000000UL)
21 #define TX_ALLOC_WAIT_TIME 100
22 #define MAX_IRQ_LOOPS	   8
23 
24 /*
25  * MII
26  */
27 #define MDO	 MGMT_MDO
28 #define MDI	 MGMT_MDI
29 #define MDC	 MGMT_MCLK
30 #define MDIRPHY	 MGMT_MDOE
31 #define MDIRHOST 0
32 
33 #define MII_IDLE_DETECT_CYCLES 32
34 
35 #define MII_COMMAND_START 0x01
36 #define MII_COMMAND_READ  0x02
37 #define MII_COMMAND_WRITE 0x01
38 #define MII_COMMAND_ACK	  0x02
39 
40 static const char *smsc_chip_ids[16] = {
41 	NULL,
42 	NULL,
43 	NULL,
44 	NULL,
45 	NULL,
46 	NULL,
47 	NULL,
48 	NULL,
49 	NULL,
50 	/* 9 */ "SMSC LAN91C11",
51 	NULL,
52 	NULL,
53 	NULL,
54 	NULL,
55 	NULL,
56 	NULL,
57 };
58 
59 struct smsc_data {
60 	mm_reg_t smsc_reg;
61 	unsigned int irq;
62 	unsigned int smsc_chip;
63 	unsigned int smsc_rev;
64 	unsigned int smsc_mask;
65 	uint8_t mac[6];
66 	struct k_mutex lock;
67 	struct k_work isr_work;
68 };
69 
70 struct eth_config {
71 	DEVICE_MMIO_ROM;
72 	const struct device *phy_dev;
73 };
74 
75 struct eth_context {
76 	DEVICE_MMIO_RAM;
77 	struct net_if *iface;
78 	struct smsc_data sc;
79 };
80 
81 static uint8_t tx_buffer[NET_ETH_MAX_FRAME_SIZE];
82 static uint8_t rx_buffer[NET_ETH_MAX_FRAME_SIZE];
83 
delay(int us)84 static ALWAYS_INLINE void delay(int us)
85 {
86 	k_busy_wait(us);
87 }
88 
smsc_select_bank(struct smsc_data * sc,uint16_t bank)89 static ALWAYS_INLINE void smsc_select_bank(struct smsc_data *sc, uint16_t bank)
90 {
91 	sys_write16(bank & BSR_BANK_MASK, sc->smsc_reg + BSR);
92 }
93 
smsc_current_bank(struct smsc_data * sc)94 static ALWAYS_INLINE unsigned int smsc_current_bank(struct smsc_data *sc)
95 {
96 	return FIELD_GET(BSR_BANK_MASK, sys_read16(sc->smsc_reg + BSR));
97 }
98 
smsc_mmu_wait(struct smsc_data * sc)99 static void smsc_mmu_wait(struct smsc_data *sc)
100 {
101 	__ASSERT((smsc_current_bank(sc) == 2), "%s called when not in bank 2", __func__);
102 	while (sys_read16(sc->smsc_reg + MMUCR) & MMUCR_BUSY)
103 		;
104 }
105 
smsc_read_1(struct smsc_data * sc,int offset)106 static ALWAYS_INLINE uint8_t smsc_read_1(struct smsc_data *sc, int offset)
107 {
108 	return sys_read8(sc->smsc_reg + offset);
109 }
110 
smsc_read_2(struct smsc_data * sc,int offset)111 static ALWAYS_INLINE uint16_t smsc_read_2(struct smsc_data *sc, int offset)
112 {
113 	return sys_read16(sc->smsc_reg + offset);
114 }
115 
smsc_read_multi_2(struct smsc_data * sc,int offset,uint16_t * datap,uint16_t count)116 static ALWAYS_INLINE void smsc_read_multi_2(struct smsc_data *sc, int offset, uint16_t *datap,
117 					    uint16_t count)
118 {
119 	while (count--) {
120 		*datap++ = sys_read16(sc->smsc_reg + offset);
121 	}
122 }
123 
smsc_write_1(struct smsc_data * sc,int offset,uint8_t val)124 static ALWAYS_INLINE void smsc_write_1(struct smsc_data *sc, int offset, uint8_t val)
125 {
126 	sys_write8(val, sc->smsc_reg + offset);
127 }
128 
smsc_write_2(struct smsc_data * sc,int offset,uint16_t val)129 static ALWAYS_INLINE void smsc_write_2(struct smsc_data *sc, int offset, uint16_t val)
130 {
131 	sys_write16(val, sc->smsc_reg + offset);
132 }
133 
smsc_write_multi_2(struct smsc_data * sc,int offset,uint16_t * datap,uint16_t count)134 static ALWAYS_INLINE void smsc_write_multi_2(struct smsc_data *sc, int offset, uint16_t *datap,
135 					     uint16_t count)
136 {
137 	while (count--) {
138 		sys_write16(*datap++, sc->smsc_reg + offset);
139 	}
140 }
141 
smsc_mii_bitbang_read(struct smsc_data * sc)142 static uint32_t smsc_mii_bitbang_read(struct smsc_data *sc)
143 {
144 	uint16_t val;
145 
146 	__ASSERT(FIELD_GET(BSR_BANK_MASK, smsc_read_2(sc, BSR)) == 3,
147 		 "%s called with bank %d (!=3)", __func__,
148 		 FIELD_GET(BSR_BANK_MASK, smsc_read_2(sc, BSR)));
149 
150 	val = smsc_read_2(sc, MGMT);
151 	delay(1); /* Simulate a timing sequence */
152 
153 	return val;
154 }
155 
smsc_mii_bitbang_write(struct smsc_data * sc,uint16_t val)156 static void smsc_mii_bitbang_write(struct smsc_data *sc, uint16_t val)
157 {
158 	__ASSERT(FIELD_GET(BSR_BANK_MASK, smsc_read_2(sc, BSR)) == 3,
159 		 "%s called with bank %d (!=3)", __func__,
160 		 FIELD_GET(BSR_BANK_MASK, smsc_read_2(sc, BSR)));
161 
162 	smsc_write_2(sc, MGMT, val);
163 	delay(1); /* Simulate a timing sequence */
164 }
165 
smsc_miibus_sync(struct smsc_data * sc)166 static void smsc_miibus_sync(struct smsc_data *sc)
167 {
168 	int i;
169 	uint32_t v;
170 
171 	v = MDIRPHY | MDO;
172 
173 	smsc_mii_bitbang_write(sc, v);
174 	for (i = 0; i < MII_IDLE_DETECT_CYCLES; i++) {
175 		smsc_mii_bitbang_write(sc, v | MDC);
176 		smsc_mii_bitbang_write(sc, v);
177 	}
178 }
179 
smsc_miibus_sendbits(struct smsc_data * sc,uint32_t data,int nbits)180 static void smsc_miibus_sendbits(struct smsc_data *sc, uint32_t data, int nbits)
181 {
182 	int i;
183 	uint32_t v;
184 
185 	v = MDIRPHY;
186 	smsc_mii_bitbang_write(sc, v);
187 
188 	for (i = 1 << (nbits - 1); i != 0; i >>= 1) {
189 		if (data & i) {
190 			v |= MDO;
191 		} else {
192 			v &= ~MDO;
193 		}
194 
195 		smsc_mii_bitbang_write(sc, v);
196 		smsc_mii_bitbang_write(sc, v | MDC);
197 		smsc_mii_bitbang_write(sc, v);
198 	}
199 }
200 
smsc_miibus_readreg(struct smsc_data * sc,int phy,int reg)201 static int smsc_miibus_readreg(struct smsc_data *sc, int phy, int reg)
202 {
203 	int i, err, val;
204 
205 	irq_disable(sc->irq);
206 	SMSC_LOCK(sc);
207 
208 	smsc_select_bank(sc, 3);
209 
210 	smsc_miibus_sync(sc);
211 
212 	smsc_miibus_sendbits(sc, MII_COMMAND_START, 2);
213 	smsc_miibus_sendbits(sc, MII_COMMAND_READ, 2);
214 	smsc_miibus_sendbits(sc, phy, 5);
215 	smsc_miibus_sendbits(sc, reg, 5);
216 
217 	/* Switch direction to PHY -> host */
218 	smsc_mii_bitbang_write(sc, MDIRHOST);
219 	smsc_mii_bitbang_write(sc, MDIRHOST | MDC);
220 	smsc_mii_bitbang_write(sc, MDIRHOST);
221 
222 	/* Check for error. */
223 	err = smsc_mii_bitbang_read(sc) & MDI;
224 
225 	/* Idle clock. */
226 	smsc_mii_bitbang_write(sc, MDIRHOST | MDC);
227 	smsc_mii_bitbang_write(sc, MDIRHOST);
228 
229 	val = 0;
230 	for (i = 0; i < 16; i++) {
231 		val <<= 1;
232 		/* Read data prior to clock low-high transition. */
233 		if (err == 0 && (smsc_mii_bitbang_read(sc) & MDI) != 0) {
234 			val |= 1;
235 		}
236 
237 		smsc_mii_bitbang_write(sc, MDIRHOST | MDC);
238 		smsc_mii_bitbang_write(sc, MDIRHOST);
239 	}
240 
241 	/* Set direction to host -> PHY, without a clock transition. */
242 	smsc_mii_bitbang_write(sc, MDIRPHY);
243 
244 	SMSC_UNLOCK(sc);
245 	irq_enable(sc->irq);
246 
247 	return (err == 0 ? val : 0);
248 }
249 
smsc_miibus_writereg(struct smsc_data * sc,int phy,int reg,uint16_t val)250 static void smsc_miibus_writereg(struct smsc_data *sc, int phy, int reg, uint16_t val)
251 {
252 	irq_disable(sc->irq);
253 	SMSC_LOCK(sc);
254 
255 	smsc_select_bank(sc, 3);
256 
257 	smsc_miibus_sync(sc);
258 
259 	smsc_miibus_sendbits(sc, MII_COMMAND_START, 2);
260 	smsc_miibus_sendbits(sc, MII_COMMAND_WRITE, 2);
261 	smsc_miibus_sendbits(sc, phy, 5);
262 	smsc_miibus_sendbits(sc, reg, 5);
263 	smsc_miibus_sendbits(sc, MII_COMMAND_ACK, 2);
264 	smsc_miibus_sendbits(sc, val, 16);
265 
266 	smsc_mii_bitbang_write(sc, MDIRPHY);
267 
268 	SMSC_UNLOCK(sc);
269 	irq_enable(sc->irq);
270 }
271 
smsc_reset(struct smsc_data * sc)272 static void smsc_reset(struct smsc_data *sc)
273 {
274 	uint16_t ctr;
275 
276 	/*
277 	 * Mask all interrupts
278 	 */
279 	smsc_select_bank(sc, 2);
280 	smsc_write_1(sc, MSK, 0);
281 
282 	/*
283 	 * Tell the device to reset
284 	 */
285 	smsc_select_bank(sc, 0);
286 	smsc_write_2(sc, RCR, RCR_SOFT_RST);
287 
288 	/*
289 	 * Set up the configuration register
290 	 */
291 	smsc_select_bank(sc, 1);
292 	smsc_write_2(sc, CR, CR_EPH_POWER_EN);
293 	delay(1);
294 
295 	/*
296 	 * Turn off transmit and receive.
297 	 */
298 	smsc_select_bank(sc, 0);
299 	smsc_write_2(sc, TCR, 0);
300 	smsc_write_2(sc, RCR, 0);
301 
302 	/*
303 	 * Set up the control register
304 	 */
305 	smsc_select_bank(sc, 1);
306 	ctr = smsc_read_2(sc, CTR);
307 	ctr |= CTR_LE_ENABLE | CTR_AUTO_RELEASE;
308 	smsc_write_2(sc, CTR, ctr);
309 
310 	/*
311 	 * Reset the MMU
312 	 */
313 	smsc_select_bank(sc, 2);
314 	smsc_mmu_wait(sc);
315 	smsc_write_2(sc, MMUCR, FIELD_PREP(MMUCR_CMD_MASK, MMUCR_CMD_MMU_RESET));
316 	smsc_mmu_wait(sc);
317 }
318 
smsc_enable(struct smsc_data * sc)319 static void smsc_enable(struct smsc_data *sc)
320 {
321 	/*
322 	 * Set up the receive/PHY control register.
323 	 */
324 	smsc_select_bank(sc, 0);
325 	smsc_write_2(sc, RPCR,
326 		     RPCR_ANEG | RPCR_DPLX | RPCR_SPEED |
327 		     FIELD_PREP(RPCR_LSA_MASK, RPCR_LED_LINK_ANY) |
328 		     FIELD_PREP(RPCR_LSB_MASK, RPCR_LED_ACT_ANY));
329 
330 	/*
331 	 * Set up the transmit and receive control registers.
332 	 */
333 	smsc_write_2(sc, TCR, TCR_TXENA | TCR_PAD_EN);
334 	smsc_write_2(sc, RCR, RCR_RXEN | RCR_STRIP_CRC);
335 
336 	/*
337 	 * Clear all interrupt status
338 	 */
339 	smsc_select_bank(sc, 2);
340 	smsc_write_1(sc, ACK, 0);
341 
342 	/*
343 	 * Set up the interrupt mask
344 	 */
345 	smsc_select_bank(sc, 2);
346 	sc->smsc_mask = RCV_INT;
347 	smsc_write_1(sc, MSK, sc->smsc_mask);
348 }
349 
smsc_check(struct smsc_data * sc)350 static int smsc_check(struct smsc_data *sc)
351 {
352 	uint16_t val;
353 
354 	val = smsc_read_2(sc, BSR);
355 	if (FIELD_GET(BSR_IDENTIFY_MASK, val) != BSR_IDENTIFY) {
356 		LOG_ERR("Identification value not in BSR");
357 		return -ENODEV;
358 	}
359 
360 	smsc_write_2(sc, BSR, 0);
361 	val = smsc_read_2(sc, BSR);
362 	if (FIELD_GET(BSR_IDENTIFY_MASK, val) != BSR_IDENTIFY) {
363 		LOG_ERR("Identification value not in BSR after write");
364 		return -ENODEV;
365 	}
366 
367 	smsc_select_bank(sc, 3);
368 	val = smsc_read_2(sc, REV);
369 	val = FIELD_GET(REV_CHIP_MASK, val);
370 	if (smsc_chip_ids[val] == NULL) {
371 		LOG_ERR("Unknown chip revision: %d", val);
372 		return -ENODEV;
373 	}
374 
375 	return 0;
376 }
377 
smsc_recv_pkt(struct eth_context * data)378 static void smsc_recv_pkt(struct eth_context *data)
379 {
380 	struct net_pkt *pkt;
381 	unsigned int packet, status, len;
382 	struct smsc_data *sc = &data->sc;
383 	uint16_t val16;
384 	int ret;
385 
386 	smsc_select_bank(sc, 2);
387 	packet = smsc_read_1(sc, FIFO_RX);
388 	while ((packet & FIFO_EMPTY) == 0) {
389 		/*
390 		 * Point to the start of the packet.
391 		 */
392 		smsc_select_bank(sc, 2);
393 		smsc_write_1(sc, PNR, packet);
394 		smsc_write_2(sc, PTR, PTR_READ | PTR_RCV | PTR_AUTO_INCR);
395 
396 		/*
397 		 * Grab status and packet length.
398 		 */
399 		status = smsc_read_2(sc, DATA0);
400 		val16 = smsc_read_2(sc, DATA0);
401 		len = FIELD_GET(RX_LEN_MASK, val16);
402 		if (len < PKT_CTRL_DATA_LEN) {
403 			LOG_WRN("rxlen(%d) too short", len);
404 		} else {
405 			len -= PKT_CTRL_DATA_LEN;
406 			if (status & RX_ODDFRM) {
407 				len += 1;
408 			}
409 
410 			if (len > NET_ETH_MAX_FRAME_SIZE) {
411 				LOG_WRN("rxlen(%d) too large", len);
412 				goto _mmu_release;
413 			}
414 
415 			/*
416 			 * Check for errors.
417 			 */
418 			if (status & (RX_TOOSHORT | RX_TOOLNG | RX_BADCRC | RX_ALIGNERR)) {
419 				LOG_WRN("status word (0x%04x) indicate some error", status);
420 				goto _mmu_release;
421 			}
422 
423 			/*
424 			 * Pull the packet out of the device.
425 			 */
426 			smsc_select_bank(sc, 2);
427 			smsc_write_1(sc, PNR, packet);
428 
429 			/*
430 			 * Pointer start from 4 because we have already read status and len from
431 			 * RX_FIFO
432 			 */
433 			smsc_write_2(sc, PTR, 4 | PTR_READ | PTR_RCV | PTR_AUTO_INCR);
434 			smsc_read_multi_2(sc, DATA0, (uint16_t *)rx_buffer, len / 2);
435 			if (len & 1) {
436 				rx_buffer[len - 1] = smsc_read_1(sc, DATA0);
437 			}
438 
439 			pkt = net_pkt_rx_alloc_with_buffer(data->iface, len, AF_UNSPEC, 0,
440 							   K_NO_WAIT);
441 			if (!pkt) {
442 				LOG_ERR("Failed to obtain RX buffer");
443 				goto _mmu_release;
444 			}
445 
446 			ret = net_pkt_write(pkt, rx_buffer, len);
447 			if (ret) {
448 				net_pkt_unref(pkt);
449 				LOG_WRN("net_pkt_write return %d", ret);
450 				goto _mmu_release;
451 			}
452 
453 			ret = net_recv_data(data->iface, pkt);
454 			if (ret) {
455 				LOG_WRN("net_recv_data return %d", ret);
456 				net_pkt_unref(pkt);
457 			}
458 		}
459 
460 _mmu_release:
461 		/*
462 		 * Tell the device we're done
463 		 */
464 		smsc_mmu_wait(sc);
465 		smsc_write_2(sc, MMUCR, FIELD_PREP(MMUCR_CMD_MASK, MMUCR_CMD_RELEASE));
466 		smsc_mmu_wait(sc);
467 
468 		packet = smsc_read_1(sc, FIFO_RX);
469 	}
470 
471 	sc->smsc_mask |= RCV_INT;
472 	smsc_write_1(sc, MSK, sc->smsc_mask);
473 }
474 
smsc_send_pkt(struct smsc_data * sc,uint8_t * buf,uint16_t len)475 static int smsc_send_pkt(struct smsc_data *sc, uint8_t *buf, uint16_t len)
476 {
477 	unsigned int polling_count;
478 	uint8_t packet;
479 
480 	SMSC_LOCK(sc);
481 	/*
482 	 * Request memory
483 	 */
484 	smsc_select_bank(sc, 2);
485 	smsc_mmu_wait(sc);
486 	smsc_write_2(sc, MMUCR, FIELD_PREP(MMUCR_CMD_MASK, MMUCR_CMD_TX_ALLOC));
487 
488 	/*
489 	 * Polling if the allocation succeeds.
490 	 */
491 	for (polling_count = TX_ALLOC_WAIT_TIME; polling_count > 0; polling_count--) {
492 		if (smsc_read_1(sc, IST) & ALLOC_INT) {
493 			break;
494 		}
495 
496 		delay(1);
497 	}
498 
499 	if (polling_count == 0) {
500 		SMSC_UNLOCK(sc);
501 		LOG_WRN("Alloc TX mem timeout");
502 		return -1;
503 	}
504 
505 	packet = smsc_read_1(sc, ARR);
506 	if (packet & ARR_FAILED) {
507 		SMSC_UNLOCK(sc);
508 		LOG_WRN("Alloc TX mem failed");
509 		return -1;
510 	}
511 
512 	/*
513 	 * Tell the device to write to our packet number.
514 	 */
515 	smsc_write_1(sc, PNR, packet);
516 	smsc_write_2(sc, PTR, PTR_AUTO_INCR);
517 
518 	/*
519 	 * Tell the device how long the packet is (include control data).
520 	 */
521 	smsc_write_2(sc, DATA0, 0);
522 	smsc_write_2(sc, DATA0, len + PKT_CTRL_DATA_LEN);
523 	smsc_write_multi_2(sc, DATA0, (uint16_t *)buf, len / 2);
524 
525 	/* Push out the control byte and and the odd byte if needed. */
526 	if (len & 1) {
527 		smsc_write_2(sc, DATA0, (CTRL_ODD << 8) | buf[len - 1]);
528 	} else {
529 		smsc_write_2(sc, DATA0, 0);
530 	}
531 
532 	/*
533 	 * Enqueue the packet.
534 	 */
535 	smsc_mmu_wait(sc);
536 	smsc_write_2(sc, MMUCR, FIELD_PREP(MMUCR_CMD_MASK, MMUCR_CMD_ENQUEUE));
537 
538 	/*
539 	 * Unmask the TX empty interrupt
540 	 */
541 	sc->smsc_mask |= (TX_EMPTY_INT | TX_INT);
542 	smsc_write_1(sc, MSK, sc->smsc_mask);
543 
544 	SMSC_UNLOCK(sc);
545 
546 	/*
547 	 * Finish up
548 	 */
549 	return 0;
550 }
551 
smsc_isr_task(struct k_work * item)552 static void smsc_isr_task(struct k_work *item)
553 {
554 	struct smsc_data *sc = CONTAINER_OF(item, struct smsc_data, isr_work);
555 	struct eth_context *data = CONTAINER_OF(sc, struct eth_context, sc);
556 	uint8_t status;
557 	unsigned int mem_info, ephsr, packet, tcr;
558 
559 	SMSC_LOCK(sc);
560 
561 	for (int loop_count = 0; loop_count < MAX_IRQ_LOOPS; loop_count++) {
562 		smsc_select_bank(sc, 0);
563 		mem_info = smsc_read_2(sc, MIR);
564 
565 		smsc_select_bank(sc, 2);
566 		status = smsc_read_1(sc, IST);
567 		LOG_DBG("INT 0x%02x MASK 0x%02x MEM 0x%04x FIFO 0x%04x", status,
568 			smsc_read_1(sc, MSK), mem_info, smsc_read_2(sc, FIFO));
569 
570 		status &= sc->smsc_mask;
571 		if (!status) {
572 			break;
573 		}
574 
575 		/*
576 		 * Transmit error
577 		 */
578 		if (status & TX_INT) {
579 			/*
580 			 * Kill off the packet if there is one.
581 			 */
582 			packet = smsc_read_1(sc, FIFO_TX);
583 			if ((packet & FIFO_EMPTY) == 0) {
584 				smsc_select_bank(sc, 2);
585 				smsc_write_1(sc, PNR, packet);
586 				smsc_write_2(sc, PTR, PTR_READ | PTR_AUTO_INCR);
587 
588 				smsc_select_bank(sc, 0);
589 				ephsr = smsc_read_2(sc, EPHSR);
590 
591 				if ((ephsr & EPHSR_TX_SUC) == 0) {
592 					LOG_WRN("bad packet, EPHSR: 0x%04x", ephsr);
593 				}
594 
595 				smsc_select_bank(sc, 2);
596 				smsc_mmu_wait(sc);
597 				smsc_write_2(sc, MMUCR,
598 					     FIELD_PREP(MMUCR_CMD_MASK, MMUCR_CMD_RELEASE_PKT));
599 
600 				smsc_select_bank(sc, 0);
601 				tcr = smsc_read_2(sc, TCR);
602 				tcr |= TCR_TXENA | TCR_PAD_EN;
603 				smsc_write_2(sc, TCR, tcr);
604 			}
605 
606 			/*
607 			 * Ack the interrupt
608 			 */
609 			smsc_select_bank(sc, 2);
610 			smsc_write_1(sc, ACK, TX_INT);
611 		}
612 
613 		/*
614 		 * Receive
615 		 */
616 		if (status & RCV_INT) {
617 			smsc_write_1(sc, ACK, RCV_INT);
618 			smsc_recv_pkt(data);
619 		}
620 
621 		/*
622 		 * Transmit empty
623 		 */
624 		if (status & TX_EMPTY_INT) {
625 			smsc_write_1(sc, ACK, TX_EMPTY_INT);
626 			sc->smsc_mask &= ~TX_EMPTY_INT;
627 		}
628 	}
629 
630 	smsc_select_bank(sc, 2);
631 	smsc_write_1(sc, MSK, sc->smsc_mask);
632 
633 	SMSC_UNLOCK(sc);
634 }
635 
smsc_init(struct smsc_data * sc)636 static int smsc_init(struct smsc_data *sc)
637 {
638 	int ret;
639 	unsigned int val;
640 
641 	ret = smsc_check(sc);
642 	if (ret) {
643 		return ret;
644 	}
645 
646 	SMSC_LOCK(sc);
647 	smsc_reset(sc);
648 	SMSC_UNLOCK(sc);
649 
650 	smsc_select_bank(sc, 3);
651 	val = smsc_read_2(sc, REV);
652 	sc->smsc_chip = FIELD_GET(REV_CHIP_MASK, val);
653 	sc->smsc_rev = FIELD_GET(REV_REV_MASK, val);
654 
655 	smsc_select_bank(sc, 1);
656 	sc->mac[0] = smsc_read_1(sc, IAR0);
657 	sc->mac[1] = smsc_read_1(sc, IAR1);
658 	sc->mac[2] = smsc_read_1(sc, IAR2);
659 	sc->mac[3] = smsc_read_1(sc, IAR3);
660 	sc->mac[4] = smsc_read_1(sc, IAR4);
661 	sc->mac[5] = smsc_read_1(sc, IAR5);
662 
663 	return 0;
664 }
665 
phy_link_state_changed(const struct device * phy_dev,struct phy_link_state * state,void * user_data)666 static void phy_link_state_changed(const struct device *phy_dev, struct phy_link_state *state,
667 				   void *user_data)
668 {
669 	const struct device *dev = user_data;
670 	struct eth_context *data = dev->data;
671 
672 	if (state->is_up) {
673 		net_eth_carrier_on(data->iface);
674 	} else {
675 		net_eth_carrier_off(data->iface);
676 	}
677 }
678 
eth_smsc_get_caps(const struct device * dev)679 static enum ethernet_hw_caps eth_smsc_get_caps(const struct device *dev)
680 {
681 	ARG_UNUSED(dev);
682 
683 	return (ETHERNET_LINK_10BASE_T
684 		| ETHERNET_LINK_100BASE_T
685 #if defined(CONFIG_NET_PROMISCUOUS_MODE)
686 		| ETHERNET_PROMISC_MODE
687 #endif
688 	);
689 }
690 
eth_tx(const struct device * dev,struct net_pkt * pkt)691 static int eth_tx(const struct device *dev, struct net_pkt *pkt)
692 {
693 	struct eth_context *data = dev->data;
694 	struct smsc_data *sc = &data->sc;
695 	uint16_t len;
696 
697 	len = net_pkt_get_len(pkt);
698 	if (net_pkt_read(pkt, tx_buffer, len)) {
699 		LOG_WRN("read pkt failed");
700 		return -1;
701 	}
702 
703 	return smsc_send_pkt(sc, tx_buffer, len);
704 }
705 
eth_smsc_set_config(const struct device * dev,enum ethernet_config_type type,const struct ethernet_config * config)706 static int eth_smsc_set_config(const struct device *dev,
707 			       enum ethernet_config_type type,
708 			       const struct ethernet_config *config)
709 {
710 	int ret = 0;
711 
712 	switch (type) {
713 #if defined(CONFIG_NET_PROMISCUOUS_MODE)
714 	case ETHERNET_CONFIG_TYPE_PROMISC_MODE:
715 		struct eth_context *data = dev->data;
716 		struct smsc_data *sc = &data->sc;
717 		uint8_t reg_val;
718 
719 		SMSC_LOCK(sc);
720 		smsc_select_bank(sc, 0);
721 		reg_val = smsc_read_1(sc, RCR);
722 		if (config->promisc_mode && !(reg_val & RCR_PRMS)) {
723 			smsc_write_1(sc, RCR, reg_val | RCR_PRMS);
724 		} else if (!config->promisc_mode && (reg_val & RCR_PRMS)) {
725 			smsc_write_1(sc, RCR, reg_val & ~RCR_PRMS);
726 		} else {
727 			ret = -EALREADY;
728 		}
729 		SMSC_UNLOCK(sc);
730 		break;
731 #endif
732 
733 	default:
734 		ret = -ENOTSUP;
735 		break;
736 	}
737 
738 	return ret;
739 }
740 
eth_initialize(struct net_if * iface)741 static void eth_initialize(struct net_if *iface)
742 {
743 	const struct device *dev = net_if_get_device(iface);
744 	struct eth_context *data = dev->data;
745 	const struct eth_config *cfg = dev->config;
746 	const struct device *phy_dev = cfg->phy_dev;
747 	struct smsc_data *sc = &data->sc;
748 
749 	ethernet_init(iface);
750 
751 	net_if_carrier_off(iface);
752 
753 	smsc_reset(sc);
754 	smsc_enable(sc);
755 
756 	LOG_INF("MAC %02x:%02x:%02x:%02x:%02x:%02x", sc->mac[0], sc->mac[1], sc->mac[2], sc->mac[3],
757 		sc->mac[4], sc->mac[5]);
758 
759 	net_if_set_link_addr(iface, sc->mac, sizeof(sc->mac), NET_LINK_ETHERNET);
760 	data->iface = iface;
761 
762 	if (device_is_ready(phy_dev)) {
763 		phy_link_callback_set(phy_dev, phy_link_state_changed, (void *)dev);
764 	} else {
765 		LOG_ERR("PHY device not ready");
766 	}
767 }
768 
769 static const struct ethernet_api api_funcs = {
770 	.iface_api.init   = eth_initialize,
771 	.get_capabilities = eth_smsc_get_caps,
772 	.set_config       = eth_smsc_set_config,
773 	.send             = eth_tx,
774 };
775 
eth_smsc_isr(const struct device * dev)776 static void eth_smsc_isr(const struct device *dev)
777 {
778 	struct eth_context *data = dev->data;
779 	struct smsc_data *sc = &data->sc;
780 	uint32_t curbank;
781 
782 	curbank = smsc_current_bank(sc);
783 
784 	/*
785 	 * Block interrupts in order to let smsc91x_isr_task to kick in
786 	 */
787 	smsc_select_bank(sc, 2);
788 	smsc_write_1(sc, MSK, 0);
789 
790 	smsc_select_bank(sc, curbank);
791 	k_work_submit(&(sc->isr_work));
792 }
793 
eth_init(const struct device * dev)794 int eth_init(const struct device *dev)
795 {
796 	struct eth_context *data = (struct eth_context *)dev->data;
797 	struct smsc_data *sc = &data->sc;
798 	int ret;
799 
800 	ret = k_mutex_init(&sc->lock);
801 	if (ret) {
802 		return ret;
803 	}
804 
805 	k_work_init(&sc->isr_work, smsc_isr_task);
806 
807 	IRQ_CONNECT(DT_INST_IRQN(0), DT_INST_IRQ(0, priority), eth_smsc_isr, DEVICE_DT_INST_GET(0),
808 		    0);
809 
810 	DEVICE_MMIO_MAP(dev, K_MEM_CACHE_NONE);
811 	sc->smsc_reg = DEVICE_MMIO_GET(dev);
812 	sc->irq = DT_INST_IRQN(0);
813 
814 	smsc_init(sc);
815 
816 	irq_enable(DT_INST_IRQN(0));
817 
818 	return 0;
819 }
820 
821 static struct eth_context eth_0_context;
822 
823 static struct eth_config eth_0_config = {
824 	DEVICE_MMIO_ROM_INIT(DT_PARENT(DT_DRV_INST(0))),
825 	.phy_dev = DEVICE_DT_GET(DT_INST_PHANDLE(0, phy_handle)),
826 };
827 
828 ETH_NET_DEVICE_DT_INST_DEFINE(0,
829 	eth_init, NULL, &eth_0_context,
830 	&eth_0_config, CONFIG_ETH_INIT_PRIORITY,
831 	&api_funcs, NET_ETH_MTU);
832 
833 #undef DT_DRV_COMPAT
834 #define DT_DRV_COMPAT smsc_lan91c111_mdio
835 
836 struct mdio_smsc_config {
837 	const struct device *eth_dev;
838 };
839 
mdio_smsc_bus_disable(const struct device * dev)840 static void mdio_smsc_bus_disable(const struct device *dev)
841 {
842 	ARG_UNUSED(dev);
843 }
844 
mdio_smsc_bus_enable(const struct device * dev)845 static void mdio_smsc_bus_enable(const struct device *dev)
846 {
847 	ARG_UNUSED(dev);
848 }
849 
mdio_smsc_read(const struct device * dev,uint8_t prtad,uint8_t devad,uint16_t * data)850 static int mdio_smsc_read(const struct device *dev, uint8_t prtad, uint8_t devad, uint16_t *data)
851 {
852 	const struct mdio_smsc_config *cfg = dev->config;
853 	const struct device *eth_dev = cfg->eth_dev;
854 	struct eth_context *eth_data = eth_dev->data;
855 	struct smsc_data *sc = &eth_data->sc;
856 
857 	*data = smsc_miibus_readreg(sc, prtad, devad);
858 
859 	return 0;
860 }
861 
mdio_smsc_write(const struct device * dev,uint8_t prtad,uint8_t devad,uint16_t data)862 static int mdio_smsc_write(const struct device *dev, uint8_t prtad, uint8_t devad, uint16_t data)
863 {
864 	const struct mdio_smsc_config *cfg = dev->config;
865 	const struct device *eth_dev = cfg->eth_dev;
866 	struct eth_context *eth_data = eth_dev->data;
867 	struct smsc_data *sc = &eth_data->sc;
868 
869 	smsc_miibus_writereg(sc, prtad, devad, data);
870 
871 	return 0;
872 }
873 
874 static const struct mdio_driver_api mdio_smsc_api = {
875 	.bus_disable = mdio_smsc_bus_disable,
876 	.bus_enable = mdio_smsc_bus_enable,
877 	.read = mdio_smsc_read,
878 	.write = mdio_smsc_write,
879 };
880 
881 const struct mdio_smsc_config mdio_smsc_config_0 = {
882 	.eth_dev = DEVICE_DT_GET(DT_CHILD(DT_INST_PARENT(0), ethernet)),
883 };
884 
885 DEVICE_DT_INST_DEFINE(0, NULL, NULL, NULL, &mdio_smsc_config_0, POST_KERNEL,
886 		      CONFIG_MDIO_INIT_PRIORITY, &mdio_smsc_api);
887