1 // SPDX-License-Identifier: GPL-2.0+
2 /*------------------------------------------------------------------------
3  * lan91c96.c
4  * This is a driver for SMSC's LAN91C96 single-chip Ethernet device, based
5  * on the SMC91111 driver from U-Boot.
6  *
7  * (C) Copyright 2002
8  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
9  * Rolf Offermanns <rof@sysgo.de>
10  *
11  * Copyright (C) 2001 Standard Microsystems Corporation (SMSC)
12  *       Developed by Simple Network Magic Corporation (SNMC)
13  * Copyright (C) 1996 by Erik Stahlman (ES)
14  *
15  * Information contained in this file was obtained from the LAN91C96
16  * manual from SMC.  To get a copy, if you really want one, you can find
17  * information under www.smsc.com.
18  *
19  * "Features" of the SMC chip:
20  *   6144 byte packet memory. ( for the 91C96 )
21  *   EEPROM for configuration
22  *   AUI/TP selection  ( mine has 10Base2/10BaseT select )
23  *
24  * Arguments:
25  *	io	= for the base address
26  *	irq	= for the IRQ
27  *
28  * author:
29  *	Erik Stahlman				( erik@vt.edu )
30  *	Daris A Nevil				( dnevil@snmc.com )
31  *
32  *
33  * Hardware multicast code from Peter Cammaert ( pc@denkart.be )
34  *
35  * Sources:
36  *    o   SMSC LAN91C96 databook (www.smsc.com)
37  *    o   smc91111.c (u-boot driver)
38  *    o   smc9194.c (linux kernel driver)
39  *    o   lan91c96.c (Intel Diagnostic Manager driver)
40  *
41  * History:
42  *	04/30/03  Mathijs Haarman	Modified smc91111.c (u-boot version)
43  *					for lan91c96
44  *---------------------------------------------------------------------------
45  */
46 
47 #include <common.h>
48 #include <command.h>
49 #include <malloc.h>
50 #include "lan91c96.h"
51 #include <net.h>
52 #include <linux/compiler.h>
53 
54 /*------------------------------------------------------------------------
55  *
56  * Configuration options, for the experienced user to change.
57  *
58  -------------------------------------------------------------------------*/
59 
60 /* Use power-down feature of the chip */
61 #define POWER_DOWN	0
62 
63 /*
64  * Wait time for memory to be free.  This probably shouldn't be
65  * tuned that much, as waiting for this means nothing else happens
66  * in the system
67 */
68 #define MEMORY_WAIT_TIME 16
69 
70 #define SMC_DEBUG 0
71 
72 #if (SMC_DEBUG > 2 )
73 #define PRINTK3(args...) printf(args)
74 #else
75 #define PRINTK3(args...)
76 #endif
77 
78 #if SMC_DEBUG > 1
79 #define PRINTK2(args...) printf(args)
80 #else
81 #define PRINTK2(args...)
82 #endif
83 
84 #ifdef SMC_DEBUG
85 #define PRINTK(args...) printf(args)
86 #else
87 #define PRINTK(args...)
88 #endif
89 
90 
91 /*------------------------------------------------------------------------
92  *
93  * The internal workings of the driver.  If you are changing anything
94  * here with the SMC stuff, you should have the datasheet and know
95  * what you are doing.
96  *
97  *------------------------------------------------------------------------
98  */
99 #define DRIVER_NAME "LAN91C96"
100 #define SMC_ALLOC_MAX_TRY 5
101 #define SMC_TX_TIMEOUT 30
102 
103 #define ETH_ZLEN 60
104 
105 #ifdef  CONFIG_LAN91C96_USE_32_BIT
106 #define USE_32_BIT  1
107 #else
108 #undef USE_32_BIT
109 #endif
110 
111 /* See if a MAC address is defined in the current environment. If so use it. If not
112  . print a warning and set the environment and other globals with the default.
113  . If an EEPROM is present it really should be consulted.
114 */
115 static int smc_get_ethaddr(bd_t *bd, struct eth_device *dev);
116 static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac);
117 
118 /* ------------------------------------------------------------
119  * Internal routines
120  * ------------------------------------------------------------
121  */
122 
123 static unsigned char smc_mac_addr[] = { 0xc0, 0x00, 0x00, 0x1b, 0x62, 0x9c };
124 
125 /*
126  * This function must be called before smc_open() if you want to override
127  * the default mac address.
128  */
129 
smc_set_mac_addr(const unsigned char * addr)130 static void smc_set_mac_addr(const unsigned char *addr)
131 {
132 	int i;
133 
134 	for (i = 0; i < sizeof (smc_mac_addr); i++) {
135 		smc_mac_addr[i] = addr[i];
136 	}
137 }
138 
139 /***********************************************
140  * Show available memory                       *
141  ***********************************************/
dump_memory_info(struct eth_device * dev)142 void dump_memory_info(struct eth_device *dev)
143 {
144 	__maybe_unused word mem_info;
145 	word old_bank;
146 
147 	old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT) & 0xF;
148 
149 	SMC_SELECT_BANK(dev, 0);
150 	mem_info = SMC_inw(dev, LAN91C96_MIR);
151 	PRINTK2 ("Memory: %4d available\n", (mem_info >> 8) * 2048);
152 
153 	SMC_SELECT_BANK(dev, old_bank);
154 }
155 
156 /*
157  * A rather simple routine to print out a packet for debugging purposes.
158  */
159 #if SMC_DEBUG > 2
160 static void print_packet (byte *, int);
161 #endif
162 
poll4int(struct eth_device * dev,byte mask,int timeout)163 static int poll4int (struct eth_device *dev, byte mask, int timeout)
164 {
165 	int tmo = get_timer (0) + timeout * CONFIG_SYS_HZ;
166 	int is_timeout = 0;
167 	word old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT);
168 
169 	PRINTK2 ("Polling...\n");
170 	SMC_SELECT_BANK(dev, 2);
171 	while ((SMC_inw(dev, LAN91C96_INT_STATS) & mask) == 0) {
172 		if (get_timer (0) >= tmo) {
173 			is_timeout = 1;
174 			break;
175 		}
176 	}
177 
178 	/* restore old bank selection */
179 	SMC_SELECT_BANK(dev, old_bank);
180 
181 	if (is_timeout)
182 		return 1;
183 	else
184 		return 0;
185 }
186 
187 /*
188  * Function: smc_reset
189  * Purpose:
190  *	This sets the SMC91111 chip to its normal state, hopefully from whatever
191  *	mess that any other DOS driver has put it in.
192  *
193  * Maybe I should reset more registers to defaults in here?  SOFTRST  should
194  * do that for me.
195  *
196  * Method:
197  *	1.  send a SOFT RESET
198  *	2.  wait for it to finish
199  *	3.  enable autorelease mode
200  *	4.  reset the memory management unit
201  *	5.  clear all interrupts
202  *
203 */
smc_reset(struct eth_device * dev)204 static void smc_reset(struct eth_device *dev)
205 {
206 	PRINTK2("%s:smc_reset\n", dev->name);
207 
208 	/* This resets the registers mostly to defaults, but doesn't
209 	   affect EEPROM.  That seems unnecessary */
210 	SMC_SELECT_BANK(dev, 0);
211 	SMC_outw(dev, LAN91C96_RCR_SOFT_RST, LAN91C96_RCR);
212 
213 	udelay (10);
214 
215 	/* Disable transmit and receive functionality */
216 	SMC_outw(dev, 0, LAN91C96_RCR);
217 	SMC_outw(dev, 0, LAN91C96_TCR);
218 
219 	/* set the control register */
220 	SMC_SELECT_BANK(dev, 1);
221 	SMC_outw(dev, SMC_inw(dev, LAN91C96_CONTROL) | LAN91C96_CTR_BIT_8,
222 			  LAN91C96_CONTROL);
223 
224 	/* Disable all interrupts */
225 	SMC_outb(dev, 0, LAN91C96_INT_MASK);
226 }
227 
228 /*
229  * Function: smc_enable
230  * Purpose: let the chip talk to the outside work
231  * Method:
232  *	1.  Initialize the Memory Configuration Register
233  *	2.  Enable the transmitter
234  *	3.  Enable the receiver
235 */
smc_enable(struct eth_device * dev)236 static void smc_enable(struct eth_device *dev)
237 {
238 	PRINTK2("%s:smc_enable\n", dev->name);
239 	SMC_SELECT_BANK(dev, 0);
240 
241 	/* Initialize the Memory Configuration Register. See page
242 	   49 of the LAN91C96 data sheet for details. */
243 	SMC_outw(dev, LAN91C96_MCR_TRANSMIT_PAGES, LAN91C96_MCR);
244 
245 	/* Initialize the Transmit Control Register */
246 	SMC_outw(dev, LAN91C96_TCR_TXENA, LAN91C96_TCR);
247 	/* Initialize the Receive Control Register
248 	 * FIXME:
249 	 * The promiscuous bit set because I could not receive ARP reply
250 	 * packets from the server when I send a ARP request. It only works
251 	 * when I set the promiscuous bit
252 	 */
253 	SMC_outw(dev, LAN91C96_RCR_RXEN | LAN91C96_RCR_PRMS, LAN91C96_RCR);
254 }
255 
256 /*
257  * Function: smc_shutdown
258  * Purpose:  closes down the SMC91xxx chip.
259  * Method:
260  *	1. zero the interrupt mask
261  *	2. clear the enable receive flag
262  *	3. clear the enable xmit flags
263  *
264  * TODO:
265  *   (1) maybe utilize power down mode.
266  *	Why not yet?  Because while the chip will go into power down mode,
267  *	the manual says that it will wake up in response to any I/O requests
268  *	in the register space.   Empirical results do not show this working.
269  */
smc_shutdown(struct eth_device * dev)270 static void smc_shutdown(struct eth_device *dev)
271 {
272 	PRINTK2("%s:smc_shutdown\n", dev->name);
273 
274 	/* no more interrupts for me */
275 	SMC_SELECT_BANK(dev, 2);
276 	SMC_outb(dev, 0, LAN91C96_INT_MASK);
277 
278 	/* and tell the card to stay away from that nasty outside world */
279 	SMC_SELECT_BANK(dev, 0);
280 	SMC_outb(dev, 0, LAN91C96_RCR);
281 	SMC_outb(dev, 0, LAN91C96_TCR);
282 }
283 
284 
285 /*
286  * Function:  smc_hardware_send_packet(struct net_device * )
287  * Purpose:
288  *	This sends the actual packet to the SMC9xxx chip.
289  *
290  * Algorithm:
291  *	First, see if a saved_skb is available.
292  *		( this should NOT be called if there is no 'saved_skb'
293  *	Now, find the packet number that the chip allocated
294  *	Point the data pointers at it in memory
295  *	Set the length word in the chip's memory
296  *	Dump the packet to chip memory
297  *	Check if a last byte is needed ( odd length packet )
298  *		if so, set the control flag right
299  *	Tell the card to send it
300  *	Enable the transmit interrupt, so I know if it failed
301  *	Free the kernel data if I actually sent it.
302  */
smc_send_packet(struct eth_device * dev,void * packet,int packet_length)303 static int smc_send_packet(struct eth_device *dev, void *packet,
304 		int packet_length)
305 {
306 	byte packet_no;
307 	byte *buf;
308 	int length;
309 	int numPages;
310 	int try = 0;
311 	int time_out;
312 	byte status;
313 
314 
315 	PRINTK3("%s:smc_hardware_send_packet\n", dev->name);
316 
317 	length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
318 
319 	/* allocate memory
320 	 ** The MMU wants the number of pages to be the number of 256 bytes
321 	 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
322 	 **
323 	 ** The 91C111 ignores the size bits, but the code is left intact
324 	 ** for backwards and future compatibility.
325 	 **
326 	 ** Pkt size for allocating is data length +6 (for additional status
327 	 ** words, length and ctl!)
328 	 **
329 	 ** If odd size then last byte is included in this header.
330 	 */
331 	numPages = ((length & 0xfffe) + 6);
332 	numPages >>= 8;				/* Divide by 256 */
333 
334 	if (numPages > 7) {
335 		printf("%s: Far too big packet error. \n", dev->name);
336 		return 0;
337 	}
338 
339 	/* now, try to allocate the memory */
340 
341 	SMC_SELECT_BANK(dev, 2);
342 	SMC_outw(dev, LAN91C96_MMUCR_ALLOC_TX | numPages, LAN91C96_MMU);
343 
344   again:
345 	try++;
346 	time_out = MEMORY_WAIT_TIME;
347 	do {
348 		status = SMC_inb(dev, LAN91C96_INT_STATS);
349 		if (status & LAN91C96_IST_ALLOC_INT) {
350 
351 			SMC_outb(dev, LAN91C96_IST_ALLOC_INT,
352 					LAN91C96_INT_STATS);
353 			break;
354 		}
355 	} while (--time_out);
356 
357 	if (!time_out) {
358 		PRINTK2 ("%s: memory allocation, try %d failed ...\n",
359 				 dev->name, try);
360 		if (try < SMC_ALLOC_MAX_TRY)
361 			goto again;
362 		else
363 			return 0;
364 	}
365 
366 	PRINTK2 ("%s: memory allocation, try %d succeeded ...\n",
367 			 dev->name, try);
368 
369 	/* I can send the packet now.. */
370 	buf = (byte *) packet;
371 
372 	/* If I get here, I _know_ there is a packet slot waiting for me */
373 	packet_no = SMC_inb(dev, LAN91C96_ARR);
374 	if (packet_no & LAN91C96_ARR_FAILED) {
375 		/* or isn't there?  BAD CHIP! */
376 		printf("%s: Memory allocation failed. \n", dev->name);
377 		return 0;
378 	}
379 
380 	/* we have a packet address, so tell the card to use it */
381 	SMC_outb(dev, packet_no, LAN91C96_PNR);
382 
383 	/* point to the beginning of the packet */
384 	SMC_outw(dev, LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
385 
386 	PRINTK3("%s: Trying to xmit packet of length %x\n",
387 			 dev->name, length);
388 
389 #if SMC_DEBUG > 2
390 	printf ("Transmitting Packet\n");
391 	print_packet (buf, length);
392 #endif
393 
394 	/* send the packet length ( +6 for status, length and ctl byte )
395 	   and the status word ( set to zeros ) */
396 #ifdef USE_32_BIT
397 	SMC_outl(dev, (length + 6) << 16, LAN91C96_DATA_HIGH);
398 #else
399 	SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
400 	/* send the packet length ( +6 for status words, length, and ctl */
401 	SMC_outw(dev, (length + 6), LAN91C96_DATA_HIGH);
402 #endif /* USE_32_BIT */
403 
404 	/* send the actual data
405 	 * I _think_ it's faster to send the longs first, and then
406 	 * mop up by sending the last word.  It depends heavily
407 	 * on alignment, at least on the 486.  Maybe it would be
408 	 * a good idea to check which is optimal?  But that could take
409 	 * almost as much time as is saved?
410 	 */
411 #ifdef USE_32_BIT
412 	SMC_outsl(dev, LAN91C96_DATA_HIGH, buf, length >> 2);
413 	if (length & 0x2)
414 		SMC_outw(dev, *((word *) (buf + (length & 0xFFFFFFFC))),
415 				  LAN91C96_DATA_HIGH);
416 #else
417 	SMC_outsw(dev, LAN91C96_DATA_HIGH, buf, (length) >> 1);
418 #endif /* USE_32_BIT */
419 
420 	/* Send the last byte, if there is one.   */
421 	if ((length & 1) == 0) {
422 		SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
423 	} else {
424 		SMC_outw(dev, buf[length - 1] | 0x2000, LAN91C96_DATA_HIGH);
425 	}
426 
427 	/* and let the chipset deal with it */
428 	SMC_outw(dev, LAN91C96_MMUCR_ENQUEUE, LAN91C96_MMU);
429 
430 	/* poll for TX INT */
431 	if (poll4int (dev, LAN91C96_MSK_TX_INT, SMC_TX_TIMEOUT)) {
432 		/* sending failed */
433 		PRINTK2("%s: TX timeout, sending failed...\n", dev->name);
434 
435 		/* release packet */
436 		SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
437 
438 		/* wait for MMU getting ready (low) */
439 		while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
440 			udelay (10);
441 
442 		PRINTK2("MMU ready\n");
443 
444 
445 		return 0;
446 	} else {
447 		/* ack. int */
448 		SMC_outw(dev, LAN91C96_IST_TX_INT, LAN91C96_INT_STATS);
449 
450 		PRINTK2("%s: Sent packet of length %d \n", dev->name, length);
451 
452 		/* release packet */
453 		SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
454 
455 		/* wait for MMU getting ready (low) */
456 		while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
457 			udelay (10);
458 
459 		PRINTK2 ("MMU ready\n");
460 	}
461 
462 	return length;
463 }
464 
465 
466 /*
467  * Open and Initialize the board
468  *
469  * Set up everything, reset the card, etc ..
470  *
471  */
smc_open(bd_t * bd,struct eth_device * dev)472 static int smc_open(bd_t *bd, struct eth_device *dev)
473 {
474 	int i, err;			/* used to set hw ethernet address */
475 
476 	PRINTK2("%s:smc_open\n", dev->name);
477 
478 	/* reset the hardware */
479 
480 	smc_reset(dev);
481 	smc_enable(dev);
482 
483 	SMC_SELECT_BANK(dev, 1);
484 	/* set smc_mac_addr, and sync it with u-boot globals */
485 	err = smc_get_ethaddr(bd, dev);
486 	if (err < 0)
487 		return -1;
488 #ifdef USE_32_BIT
489 	for (i = 0; i < 6; i += 2) {
490 		word address;
491 
492 		address = smc_mac_addr[i + 1] << 8;
493 		address |= smc_mac_addr[i];
494 		SMC_outw(dev, address, LAN91C96_IA0 + i);
495 	}
496 #else
497 	for (i = 0; i < 6; i++)
498 		SMC_outb(dev, smc_mac_addr[i], LAN91C96_IA0 + i);
499 #endif
500 	return 0;
501 }
502 
503 /*-------------------------------------------------------------
504  *
505  * smc_rcv -  receive a packet from the card
506  *
507  * There is ( at least ) a packet waiting to be read from
508  * chip-memory.
509  *
510  * o Read the status
511  * o If an error, record it
512  * o otherwise, read in the packet
513  *-------------------------------------------------------------
514  */
smc_rcv(struct eth_device * dev)515 static int smc_rcv(struct eth_device *dev)
516 {
517 	int packet_number;
518 	word status;
519 	word packet_length;
520 	int is_error = 0;
521 
522 #ifdef USE_32_BIT
523 	dword stat_len;
524 #endif
525 
526 
527 	SMC_SELECT_BANK(dev, 2);
528 	packet_number = SMC_inw(dev, LAN91C96_FIFO);
529 
530 	if (packet_number & LAN91C96_FIFO_RXEMPTY) {
531 		return 0;
532 	}
533 
534 	PRINTK3("%s:smc_rcv\n", dev->name);
535 	/*  start reading from the start of the packet */
536 	SMC_outw(dev, LAN91C96_PTR_READ | LAN91C96_PTR_RCV |
537 			  LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
538 
539 	/* First two words are status and packet_length */
540 #ifdef USE_32_BIT
541 	stat_len = SMC_inl(dev, LAN91C96_DATA_HIGH);
542 	status = stat_len & 0xffff;
543 	packet_length = stat_len >> 16;
544 #else
545 	status = SMC_inw(dev, LAN91C96_DATA_HIGH);
546 	packet_length = SMC_inw(dev, LAN91C96_DATA_HIGH);
547 #endif
548 
549 	packet_length &= 0x07ff;	/* mask off top bits */
550 
551 	PRINTK2 ("RCV: STATUS %4x LENGTH %4x\n", status, packet_length);
552 
553 	if (!(status & FRAME_FILTER)) {
554 		/* Adjust for having already read the first two words */
555 		packet_length -= 4;		/*4; */
556 
557 
558 		/* set odd length for bug in LAN91C111, */
559 		/* which never sets RS_ODDFRAME */
560 		/* TODO ? */
561 
562 
563 #ifdef USE_32_BIT
564 		PRINTK3 (" Reading %d dwords (and %d bytes) \n",
565 			 packet_length >> 2, packet_length & 3);
566 		/* QUESTION:  Like in the TX routine, do I want
567 		   to send the DWORDs or the bytes first, or some
568 		   mixture.  A mixture might improve already slow PIO
569 		   performance  */
570 		SMC_insl(dev, LAN91C96_DATA_HIGH, net_rx_packets[0],
571 			 packet_length >> 2);
572 		/* read the left over bytes */
573 		if (packet_length & 3) {
574 			int i;
575 
576 			byte *tail = (byte *)(net_rx_packets[0] +
577 				(packet_length & ~3));
578 			dword leftover = SMC_inl(dev, LAN91C96_DATA_HIGH);
579 
580 			for (i = 0; i < (packet_length & 3); i++)
581 				*tail++ = (byte) (leftover >> (8 * i)) & 0xff;
582 		}
583 #else
584 		PRINTK3(" Reading %d words and %d byte(s)\n",
585 			(packet_length >> 1), packet_length & 1);
586 		SMC_insw(dev, LAN91C96_DATA_HIGH, net_rx_packets[0],
587 			 packet_length >> 1);
588 
589 #endif /* USE_32_BIT */
590 
591 #if	SMC_DEBUG > 2
592 		printf ("Receiving Packet\n");
593 		print_packet((byte *)net_rx_packets[0], packet_length);
594 #endif
595 	} else {
596 		/* error ... */
597 		/* TODO ? */
598 		is_error = 1;
599 	}
600 
601 	while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
602 		udelay (1);		/* Wait until not busy */
603 
604 	/*  error or good, tell the card to get rid of this packet */
605 	SMC_outw(dev, LAN91C96_MMUCR_RELEASE_RX, LAN91C96_MMU);
606 
607 	while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
608 		udelay (1);		/* Wait until not busy */
609 
610 	if (!is_error) {
611 		/* Pass the packet up to the protocol layers. */
612 		net_process_received_packet(net_rx_packets[0], packet_length);
613 		return packet_length;
614 	} else {
615 		return 0;
616 	}
617 
618 }
619 
620 /*----------------------------------------------------
621  * smc_close
622  *
623  * this makes the board clean up everything that it can
624  * and not talk to the outside world.   Caused by
625  * an 'ifconfig ethX down'
626  *
627  -----------------------------------------------------*/
smc_close(struct eth_device * dev)628 static int smc_close(struct eth_device *dev)
629 {
630 	PRINTK2("%s:smc_close\n", dev->name);
631 
632 	/* clear everything */
633 	smc_shutdown(dev);
634 
635 	return 0;
636 }
637 
638 #if SMC_DEBUG > 2
print_packet(byte * buf,int length)639 static void print_packet(byte *buf, int length)
640 {
641 #if 0
642 	int i;
643 	int remainder;
644 	int lines;
645 
646 	printf ("Packet of length %d \n", length);
647 
648 	lines = length / 16;
649 	remainder = length % 16;
650 
651 	for (i = 0; i < lines; i++) {
652 		int cur;
653 
654 		for (cur = 0; cur < 8; cur++) {
655 			byte a, b;
656 
657 			a = *(buf++);
658 			b = *(buf++);
659 			printf ("%02x%02x ", a, b);
660 		}
661 		printf ("\n");
662 	}
663 	for (i = 0; i < remainder / 2; i++) {
664 		byte a, b;
665 
666 		a = *(buf++);
667 		b = *(buf++);
668 		printf ("%02x%02x ", a, b);
669 	}
670 	printf ("\n");
671 #endif /* 0 */
672 }
673 #endif /* SMC_DEBUG > 2 */
674 
lan91c96_init(struct eth_device * dev,bd_t * bd)675 static int  lan91c96_init(struct eth_device *dev, bd_t *bd)
676 {
677 	return smc_open(bd, dev);
678 }
679 
lan91c96_halt(struct eth_device * dev)680 static void lan91c96_halt(struct eth_device *dev)
681 {
682 	smc_close(dev);
683 }
684 
lan91c96_recv(struct eth_device * dev)685 static int lan91c96_recv(struct eth_device *dev)
686 {
687 	return smc_rcv(dev);
688 }
689 
lan91c96_send(struct eth_device * dev,void * packet,int length)690 static int lan91c96_send(struct eth_device *dev, void *packet,
691 		int length)
692 {
693 	return smc_send_packet(dev, packet, length);
694 }
695 
696 /* smc_get_ethaddr
697  *
698  * This checks both the environment and the ROM for an ethernet address. If
699  * found, the environment takes precedence.
700  */
701 
smc_get_ethaddr(bd_t * bd,struct eth_device * dev)702 static int smc_get_ethaddr(bd_t *bd, struct eth_device *dev)
703 {
704 	uchar v_mac[6];
705 
706 	if (!eth_env_get_enetaddr("ethaddr", v_mac)) {
707 		/* get ROM mac value if any */
708 		if (!get_rom_mac(dev, v_mac)) {
709 			printf("\n*** ERROR: ethaddr is NOT set !!\n");
710 			return -1;
711 		}
712 		eth_env_set_enetaddr("ethaddr", v_mac);
713 	}
714 
715 	smc_set_mac_addr(v_mac); /* use old function to update smc default */
716 	PRINTK("Using MAC Address %pM\n", v_mac);
717 	return 0;
718 }
719 
720 /*
721  * get_rom_mac()
722  * Note, this has omly been tested for the OMAP730 P2.
723  */
724 
get_rom_mac(struct eth_device * dev,unsigned char * v_rom_mac)725 static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac)
726 {
727 	int i;
728 	SMC_SELECT_BANK(dev, 1);
729 	for (i=0; i<6; i++)
730 	{
731 		v_rom_mac[i] = SMC_inb(dev, LAN91C96_IA0 + i);
732 	}
733 	return (1);
734 }
735 
736 /* Structure to detect the device IDs */
737 struct id_type {
738 	u8 id;
739 	char *name;
740 };
741 static struct id_type supported_chips[] = {
742 	{0, ""}, /* Dummy entry to prevent id check failure */
743 	{9, "LAN91C110"},
744 	{8, "LAN91C100FD"},
745 	{7, "LAN91C100"},
746 	{5, "LAN91C95"},
747 	{4, "LAN91C94/96"},
748 	{3, "LAN91C90/92"},
749 };
750 /* lan91c96_detect_chip
751  * See:
752  * http://www.embeddedsys.com/subpages/resources/images/documents/LAN91C96_datasheet.pdf
753  * page 71 - that is the closest we get to detect this device
754  */
lan91c96_detect_chip(struct eth_device * dev)755 static int lan91c96_detect_chip(struct eth_device *dev)
756 {
757 	u8 chip_id;
758 	int r;
759 	SMC_SELECT_BANK(dev, 3);
760 	chip_id = (SMC_inw(dev, 0xA) & LAN91C96_REV_CHIPID) >> 4;
761 	SMC_SELECT_BANK(dev, 0);
762 	for (r = 0; r < ARRAY_SIZE(supported_chips); r++)
763 		if (chip_id == supported_chips[r].id)
764 			return r;
765 	return 0;
766 }
767 
lan91c96_initialize(u8 dev_num,int base_addr)768 int lan91c96_initialize(u8 dev_num, int base_addr)
769 {
770 	struct eth_device *dev;
771 	int r = 0;
772 
773 	dev = malloc(sizeof(*dev));
774 	if (!dev) {
775 		return 0;
776 	}
777 	memset(dev, 0, sizeof(*dev));
778 
779 	dev->iobase = base_addr;
780 
781 	/* Try to detect chip. Will fail if not present. */
782 	r = lan91c96_detect_chip(dev);
783 	if (!r) {
784 		free(dev);
785 		return 0;
786 	}
787 	get_rom_mac(dev, dev->enetaddr);
788 
789 	dev->init = lan91c96_init;
790 	dev->halt = lan91c96_halt;
791 	dev->send = lan91c96_send;
792 	dev->recv = lan91c96_recv;
793 	sprintf(dev->name, "%s-%hu", supported_chips[r].name, dev_num);
794 
795 	eth_register(dev);
796 	return 0;
797 }
798