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