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