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