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