* Patch by Dave Peverley, 30 Apr 2004:
[platform/kernel/u-boot.git] / drivers / lan91c96.c
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 "lan91c96.h"
64 #include <net.h>
65
66 #ifdef CONFIG_DRIVER_LAN91C96
67
68 #if (CONFIG_COMMANDS & CFG_CMD_NET)
69
70 /*------------------------------------------------------------------------
71  *
72  * Configuration options, for the experienced user to change.
73  *
74  -------------------------------------------------------------------------*/
75
76 /* Use power-down feature of the chip */
77 #define POWER_DOWN      0
78
79 /*
80  * Wait time for memory to be free.  This probably shouldn't be
81  * tuned that much, as waiting for this means nothing else happens
82  * in the system
83 */
84 #define MEMORY_WAIT_TIME 16
85
86 #define SMC_DEBUG 0
87
88 #if (SMC_DEBUG > 2 )
89 #define PRINTK3(args...) printf(args)
90 #else
91 #define PRINTK3(args...)
92 #endif
93
94 #if SMC_DEBUG > 1
95 #define PRINTK2(args...) printf(args)
96 #else
97 #define PRINTK2(args...)
98 #endif
99
100 #ifdef SMC_DEBUG
101 #define PRINTK(args...) printf(args)
102 #else
103 #define PRINTK(args...)
104 #endif
105
106
107 /*------------------------------------------------------------------------
108  *
109  * The internal workings of the driver.  If you are changing anything
110  * here with the SMC stuff, you should have the datasheet and know
111  * what you are doing.
112  *
113  *------------------------------------------------------------------------
114  */
115 #define CARDNAME "LAN91C96"
116
117 #define SMC_BASE_ADDRESS CONFIG_LAN91C96_BASE
118
119 #define SMC_DEV_NAME "LAN91C96"
120 #define SMC_ALLOC_MAX_TRY 5
121 #define SMC_TX_TIMEOUT 30
122
123 #define ETH_ZLEN 60
124
125 #ifdef  CONFIG_LAN91C96_USE_32_BIT
126 #define USE_32_BIT  1
127 #else
128 #undef USE_32_BIT
129 #endif
130
131 /*-----------------------------------------------------------------
132  *
133  *  The driver can be entered at any of the following entry points.
134  *
135  *-----------------------------------------------------------------
136  */
137
138 extern int eth_init (bd_t * bd);
139 extern void eth_halt (void);
140 extern int eth_rx (void);
141 extern int eth_send (volatile void *packet, int length);
142 static int smc_hw_init (void);
143
144 /*
145  * This is called by  register_netdev().  It is responsible for
146  * checking the portlist for the SMC9000 series chipset.  If it finds
147  * one, then it will initialize the device, find the hardware information,
148  * and sets up the appropriate device parameters.
149  * NOTE: Interrupts are *OFF* when this procedure is called.
150  *
151  * NB:This shouldn't be static since it is referred to externally.
152  */
153 int smc_init (void);
154
155 /*
156  * This is called by  unregister_netdev().  It is responsible for
157  * cleaning up before the driver is finally unregistered and discarded.
158  */
159 void smc_destructor (void);
160
161 /*
162  * The kernel calls this function when someone wants to use the device,
163  * typically 'ifconfig ethX up'.
164  */
165 static int smc_open (bd_t *bd);
166
167
168 /*
169  * This is called by the kernel in response to 'ifconfig ethX down'.  It
170  * is responsible for cleaning up everything that the open routine
171  * does, and maybe putting the card into a powerdown state.
172  */
173 static int smc_close (void);
174
175 /*
176  * This is a separate procedure to handle the receipt of a packet, to
177  * leave the interrupt code looking slightly cleaner
178  */
179 static int smc_rcv (void);
180
181 /* See if a MAC address is defined in the current environment. If so use it. If not
182  . print a warning and set the environment and other globals with the default.
183  . If an EEPROM is present it really should be consulted.
184 */
185 int smc_get_ethaddr(bd_t *bd);
186 int get_rom_mac(char *v_rom_mac);
187
188 /* ------------------------------------------------------------
189  * Internal routines
190  * ------------------------------------------------------------
191  */
192
193 static char smc_mac_addr[] = { 0xc0, 0x00, 0x00, 0x1b, 0x62, 0x9c };
194
195 /*
196  * This function must be called before smc_open() if you want to override
197  * the default mac address.
198  */
199
200 void smc_set_mac_addr (const char *addr)
201 {
202         int i;
203
204         for (i = 0; i < sizeof (smc_mac_addr); i++) {
205                 smc_mac_addr[i] = addr[i];
206         }
207 }
208
209 /*
210  * smc_get_macaddr is no longer used. If you want to override the default
211  * mac address, call smc_get_mac_addr as a part of the board initialisation.
212  */
213
214 #if 0
215 void smc_get_macaddr (byte * addr)
216 {
217         /* MAC ADDRESS AT FLASHBLOCK 1 / OFFSET 0x10 */
218         unsigned char *dnp1110_mac = (unsigned char *) (0xE8000000 + 0x20010);
219         int i;
220
221
222         for (i = 0; i < 6; i++) {
223                 addr[0] = *(dnp1110_mac + 0);
224                 addr[1] = *(dnp1110_mac + 1);
225                 addr[2] = *(dnp1110_mac + 2);
226                 addr[3] = *(dnp1110_mac + 3);
227                 addr[4] = *(dnp1110_mac + 4);
228                 addr[5] = *(dnp1110_mac + 5);
229         }
230 }
231 #endif /* 0 */
232
233 /***********************************************
234  * Show available memory                       *
235  ***********************************************/
236 void dump_memory_info (void)
237 {
238         word mem_info;
239         word old_bank;
240
241         old_bank = SMC_inw (LAN91C96_BANK_SELECT) & 0xF;
242
243         SMC_SELECT_BANK (0);
244         mem_info = SMC_inw (LAN91C96_MIR);
245         PRINTK2 ("Memory: %4d available\n", (mem_info >> 8) * 2048);
246
247         SMC_SELECT_BANK (old_bank);
248 }
249
250 /*
251  * A rather simple routine to print out a packet for debugging purposes.
252  */
253 #if SMC_DEBUG > 2
254 static void print_packet (byte *, int);
255 #endif
256
257 /* #define tx_done(dev) 1 */
258
259
260 /* this does a soft reset on the device */
261 static void smc_reset (void);
262
263 /* Enable Interrupts, Receive, and Transmit */
264 static void smc_enable (void);
265
266 /* this puts the device in an inactive state */
267 static void smc_shutdown (void);
268
269
270 static int poll4int (byte mask, int timeout)
271 {
272         int tmo = get_timer (0) + timeout * CFG_HZ;
273         int is_timeout = 0;
274         word old_bank = SMC_inw (LAN91C96_BANK_SELECT);
275
276         PRINTK2 ("Polling...\n");
277         SMC_SELECT_BANK (2);
278         while ((SMC_inw (LAN91C96_INT_STATS) & mask) == 0) {
279                 if (get_timer (0) >= tmo) {
280                         is_timeout = 1;
281                         break;
282                 }
283         }
284
285         /* restore old bank selection */
286         SMC_SELECT_BANK (old_bank);
287
288         if (is_timeout)
289                 return 1;
290         else
291                 return 0;
292 }
293
294 /*
295  * Function: smc_reset( void )
296  * Purpose:
297  *      This sets the SMC91111 chip to its normal state, hopefully from whatever
298  *      mess that any other DOS driver has put it in.
299  *
300  * Maybe I should reset more registers to defaults in here?  SOFTRST  should
301  * do that for me.
302  *
303  * Method:
304  *      1.  send a SOFT RESET
305  *      2.  wait for it to finish
306  *      3.  enable autorelease mode
307  *      4.  reset the memory management unit
308  *      5.  clear all interrupts
309  *
310 */
311 static void smc_reset (void)
312 {
313         PRINTK2 ("%s:smc_reset\n", SMC_DEV_NAME);
314
315         /* This resets the registers mostly to defaults, but doesn't
316            affect EEPROM.  That seems unnecessary */
317         SMC_SELECT_BANK (0);
318         SMC_outw (LAN91C96_RCR_SOFT_RST, LAN91C96_RCR);
319
320         udelay (10);
321
322         /* Disable transmit and receive functionality */
323         SMC_outw (0, LAN91C96_RCR);
324         SMC_outw (0, LAN91C96_TCR);
325
326         /* set the control register */
327         SMC_SELECT_BANK (1);
328         SMC_outw (SMC_inw (LAN91C96_CONTROL) | LAN91C96_CTR_BIT_8,
329                           LAN91C96_CONTROL);
330
331         /* Disable all interrupts */
332         SMC_outb (0, LAN91C96_INT_MASK);
333 }
334
335 /*
336  * Function: smc_enable
337  * Purpose: let the chip talk to the outside work
338  * Method:
339  *      1.  Initialize the Memory Configuration Register
340  *      2.  Enable the transmitter
341  *      3.  Enable the receiver
342 */
343 static void smc_enable ()
344 {
345         PRINTK2 ("%s:smc_enable\n", SMC_DEV_NAME);
346         SMC_SELECT_BANK (0);
347
348         /* Initialize the Memory Configuration Register. See page
349            49 of the LAN91C96 data sheet for details. */
350         SMC_outw (LAN91C96_MCR_TRANSMIT_PAGES, LAN91C96_MCR);
351
352         /* Initialize the Transmit Control Register */
353         SMC_outw (LAN91C96_TCR_TXENA, LAN91C96_TCR);
354         /* Initialize the Receive Control Register
355          * FIXME:
356          * The promiscuous bit set because I could not receive ARP reply
357          * packets from the server when I send a ARP request. It only works
358          * when I set the promiscuous bit
359          */
360         SMC_outw (LAN91C96_RCR_RXEN | LAN91C96_RCR_PRMS, LAN91C96_RCR);
361 }
362
363 /*
364  * Function: smc_shutdown
365  * Purpose:  closes down the SMC91xxx chip.
366  * Method:
367  *      1. zero the interrupt mask
368  *      2. clear the enable receive flag
369  *      3. clear the enable xmit flags
370  *
371  * TODO:
372  *   (1) maybe utilize power down mode.
373  *      Why not yet?  Because while the chip will go into power down mode,
374  *      the manual says that it will wake up in response to any I/O requests
375  *      in the register space.   Empirical results do not show this working.
376  */
377 static void smc_shutdown ()
378 {
379         PRINTK2 (CARDNAME ":smc_shutdown\n");
380
381         /* no more interrupts for me */
382         SMC_SELECT_BANK (2);
383         SMC_outb (0, LAN91C96_INT_MASK);
384
385         /* and tell the card to stay away from that nasty outside world */
386         SMC_SELECT_BANK (0);
387         SMC_outb (0, LAN91C96_RCR);
388         SMC_outb (0, LAN91C96_TCR);
389 }
390
391
392 /*
393  * Function:  smc_hardware_send_packet(struct net_device * )
394  * Purpose:
395  *      This sends the actual packet to the SMC9xxx chip.
396  *
397  * Algorithm:
398  *      First, see if a saved_skb is available.
399  *              ( this should NOT be called if there is no 'saved_skb'
400  *      Now, find the packet number that the chip allocated
401  *      Point the data pointers at it in memory
402  *      Set the length word in the chip's memory
403  *      Dump the packet to chip memory
404  *      Check if a last byte is needed ( odd length packet )
405  *              if so, set the control flag right
406  *      Tell the card to send it
407  *      Enable the transmit interrupt, so I know if it failed
408  *      Free the kernel data if I actually sent it.
409  */
410 static int smc_send_packet (volatile void *packet, int packet_length)
411 {
412         byte packet_no;
413         unsigned long ioaddr;
414         byte *buf;
415         int length;
416         int numPages;
417         int try = 0;
418         int time_out;
419         byte status;
420
421
422         PRINTK3 ("%s:smc_hardware_send_packet\n", SMC_DEV_NAME);
423
424         length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
425
426         /* allocate memory
427          ** The MMU wants the number of pages to be the number of 256 bytes
428          ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
429          **
430          ** The 91C111 ignores the size bits, but the code is left intact
431          ** for backwards and future compatibility.
432          **
433          ** Pkt size for allocating is data length +6 (for additional status
434          ** words, length and ctl!)
435          **
436          ** If odd size then last byte is included in this header.
437          */
438         numPages = ((length & 0xfffe) + 6);
439         numPages >>= 8;                         /* Divide by 256 */
440
441         if (numPages > 7) {
442                 printf ("%s: Far too big packet error. \n", SMC_DEV_NAME);
443                 return 0;
444         }
445
446         /* now, try to allocate the memory */
447
448         SMC_SELECT_BANK (2);
449         SMC_outw (LAN91C96_MMUCR_ALLOC_TX | numPages, LAN91C96_MMU);
450
451   again:
452         try++;
453         time_out = MEMORY_WAIT_TIME;
454         do {
455                 status = SMC_inb (LAN91C96_INT_STATS);
456                 if (status & LAN91C96_IST_ALLOC_INT) {
457
458                         SMC_outb (LAN91C96_IST_ALLOC_INT, LAN91C96_INT_STATS);
459                         break;
460                 }
461         } while (--time_out);
462
463         if (!time_out) {
464                 PRINTK2 ("%s: memory allocation, try %d failed ...\n",
465                                  SMC_DEV_NAME, try);
466                 if (try < SMC_ALLOC_MAX_TRY)
467                         goto again;
468                 else
469                         return 0;
470         }
471
472         PRINTK2 ("%s: memory allocation, try %d succeeded ...\n",
473                          SMC_DEV_NAME, try);
474
475         /* I can send the packet now.. */
476
477         ioaddr = SMC_BASE_ADDRESS;
478
479         buf = (byte *) packet;
480
481         /* If I get here, I _know_ there is a packet slot waiting for me */
482         packet_no = SMC_inb (LAN91C96_ARR);
483         if (packet_no & LAN91C96_ARR_FAILED) {
484                 /* or isn't there?  BAD CHIP! */
485                 printf ("%s: Memory allocation failed. \n", SMC_DEV_NAME);
486                 return 0;
487         }
488
489         /* we have a packet address, so tell the card to use it */
490         SMC_outb (packet_no, LAN91C96_PNR);
491
492         /* point to the beginning of the packet */
493         SMC_outw (LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
494
495         PRINTK3 ("%s: Trying to xmit packet of length %x\n",
496                          SMC_DEV_NAME, length);
497
498 #if SMC_DEBUG > 2
499         printf ("Transmitting Packet\n");
500         print_packet (buf, length);
501 #endif
502
503         /* send the packet length ( +6 for status, length and ctl byte )
504            and the status word ( set to zeros ) */
505 #ifdef USE_32_BIT
506         SMC_outl ((length + 6) << 16, LAN91C96_DATA_HIGH);
507 #else
508         SMC_outw (0, LAN91C96_DATA_HIGH);
509         /* send the packet length ( +6 for status words, length, and ctl */
510         SMC_outw ((length + 6), LAN91C96_DATA_HIGH);
511 #endif /* USE_32_BIT */
512
513         /* send the actual data
514          * I _think_ it's faster to send the longs first, and then
515          * mop up by sending the last word.  It depends heavily
516          * on alignment, at least on the 486.  Maybe it would be
517          * a good idea to check which is optimal?  But that could take
518          * almost as much time as is saved?
519          */
520 #ifdef USE_32_BIT
521         SMC_outsl (LAN91C96_DATA_HIGH, buf, length >> 2);
522         if (length & 0x2)
523                 SMC_outw (*((word *) (buf + (length & 0xFFFFFFFC))),
524                                   LAN91C96_DATA_HIGH);
525 #else
526         SMC_outsw (LAN91C96_DATA_HIGH, buf, (length) >> 1);
527 #endif /* USE_32_BIT */
528
529         /* Send the last byte, if there is one.   */
530         if ((length & 1) == 0) {
531                 SMC_outw (0, LAN91C96_DATA_HIGH);
532         } else {
533                 SMC_outw (buf[length - 1] | 0x2000, LAN91C96_DATA_HIGH);
534         }
535
536         /* and let the chipset deal with it */
537         SMC_outw (LAN91C96_MMUCR_ENQUEUE, LAN91C96_MMU);
538
539         /* poll for TX INT */
540         if (poll4int (LAN91C96_MSK_TX_INT, SMC_TX_TIMEOUT)) {
541                 /* sending failed */
542                 PRINTK2 ("%s: TX timeout, sending failed...\n", SMC_DEV_NAME);
543
544                 /* release packet */
545                 SMC_outw (LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
546
547                 /* wait for MMU getting ready (low) */
548                 while (SMC_inw (LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY) {
549                         udelay (10);
550                 }
551
552                 PRINTK2 ("MMU ready\n");
553
554
555                 return 0;
556         } else {
557                 /* ack. int */
558                 SMC_outw (LAN91C96_IST_TX_INT, LAN91C96_INT_STATS);
559
560                 PRINTK2 ("%s: Sent packet of length %d \n", SMC_DEV_NAME, length);
561
562                 /* release packet */
563                 SMC_outw (LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
564
565                 /* wait for MMU getting ready (low) */
566                 while (SMC_inw (LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY) {
567                         udelay (10);
568                 }
569
570                 PRINTK2 ("MMU ready\n");
571         }
572
573         return length;
574 }
575
576 /*-------------------------------------------------------------------------
577  * smc_destructor( struct net_device * dev )
578  *   Input parameters:
579  *      dev, pointer to the device structure
580  *
581  *   Output:
582  *      None.
583  *--------------------------------------------------------------------------
584  */
585 void smc_destructor ()
586 {
587         PRINTK2 (CARDNAME ":smc_destructor\n");
588 }
589
590
591 /*
592  * Open and Initialize the board
593  *
594  * Set up everything, reset the card, etc ..
595  *
596  */
597 static int smc_open (bd_t *bd)
598 {
599         int i, err;                     /* used to set hw ethernet address */
600
601         PRINTK2 ("%s:smc_open\n", SMC_DEV_NAME);
602
603         /* reset the hardware */
604
605         smc_reset ();
606         smc_enable ();
607
608         SMC_SELECT_BANK (1);
609
610         err = smc_get_ethaddr (bd);     /* set smc_mac_addr, and sync it with u-boot globals */
611         if (err < 0) {
612                 memset (bd->bi_enetaddr, 0, 6); /* hack to make error stick! upper code will abort if not set */
613                 return (-1);    /* upper code ignores this, but NOT bi_enetaddr */
614         }
615 #ifdef USE_32_BIT
616         for (i = 0; i < 6; i += 2) {
617                 word address;
618
619                 address = smc_mac_addr[i + 1] << 8;
620                 address |= smc_mac_addr[i];
621                 SMC_outw (address, LAN91C96_IA0 + i);
622         }
623 #else
624         for (i = 0; i < 6; i++)
625                 SMC_outb (smc_mac_addr[i], LAN91C96_IA0 + i);
626 #endif
627         return 0;
628 }
629
630 /*-------------------------------------------------------------
631  *
632  * smc_rcv -  receive a packet from the card
633  *
634  * There is ( at least ) a packet waiting to be read from
635  * chip-memory.
636  *
637  * o Read the status
638  * o If an error, record it
639  * o otherwise, read in the packet
640  *-------------------------------------------------------------
641  */
642 static int smc_rcv ()
643 {
644         int packet_number;
645         word status;
646         word packet_length;
647         int is_error = 0;
648
649 #ifdef USE_32_BIT
650         dword stat_len;
651 #endif
652
653
654         SMC_SELECT_BANK (2);
655         packet_number = SMC_inw (LAN91C96_FIFO);
656
657         if (packet_number & LAN91C96_FIFO_RXEMPTY) {
658                 return 0;
659         }
660
661         PRINTK3 ("%s:smc_rcv\n", SMC_DEV_NAME);
662         /*  start reading from the start of the packet */
663         SMC_outw (LAN91C96_PTR_READ | LAN91C96_PTR_RCV |
664                           LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
665
666         /* First two words are status and packet_length */
667 #ifdef USE_32_BIT
668         stat_len = SMC_inl (LAN91C96_DATA_HIGH);
669         status = stat_len & 0xffff;
670         packet_length = stat_len >> 16;
671 #else
672         status = SMC_inw (LAN91C96_DATA_HIGH);
673         packet_length = SMC_inw (LAN91C96_DATA_HIGH);
674 #endif
675
676         packet_length &= 0x07ff;        /* mask off top bits */
677
678         PRINTK2 ("RCV: STATUS %4x LENGTH %4x\n", status, packet_length);
679
680         if (!(status & FRAME_FILTER)) {
681                 /* Adjust for having already read the first two words */
682                 packet_length -= 4;             /*4; */
683
684
685                 /* set odd length for bug in LAN91C111, */
686                 /* which never sets RS_ODDFRAME */
687                 /* TODO ? */
688
689
690 #ifdef USE_32_BIT
691                 PRINTK3 (" Reading %d dwords (and %d bytes) \n",
692                          packet_length >> 2, packet_length & 3);
693                 /* QUESTION:  Like in the TX routine, do I want
694                    to send the DWORDs or the bytes first, or some
695                    mixture.  A mixture might improve already slow PIO
696                    performance  */
697                 SMC_insl (LAN91C96_DATA_HIGH, NetRxPackets[0], packet_length >> 2);
698                 /* read the left over bytes */
699                 if (packet_length & 3) {
700                         int i;
701
702                         byte *tail = (byte *) (NetRxPackets[0] + (packet_length & ~3));
703                         dword leftover = SMC_inl (LAN91C96_DATA_HIGH);
704
705                         for (i = 0; i < (packet_length & 3); i++)
706                                 *tail++ = (byte) (leftover >> (8 * i)) & 0xff;
707                 }
708 #else
709                 PRINTK3 (" Reading %d words and %d byte(s) \n",
710                                  (packet_length >> 1), packet_length & 1);
711                 SMC_insw (LAN91C96_DATA_HIGH, NetRxPackets[0], packet_length >> 1);
712
713 #endif /* USE_32_BIT */
714
715 #if     SMC_DEBUG > 2
716                 printf ("Receiving Packet\n");
717                 print_packet (NetRxPackets[0], packet_length);
718 #endif
719         } else {
720                 /* error ... */
721                 /* TODO ? */
722                 is_error = 1;
723         }
724
725         while (SMC_inw (LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
726                 udelay (1);             /* Wait until not busy */
727
728         /*  error or good, tell the card to get rid of this packet */
729         SMC_outw (LAN91C96_MMUCR_RELEASE_RX, LAN91C96_MMU);
730
731         while (SMC_inw (LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
732                 udelay (1);             /* Wait until not busy */
733
734         if (!is_error) {
735                 /* Pass the packet up to the protocol layers. */
736                 NetReceive (NetRxPackets[0], packet_length);
737                 return packet_length;
738         } else {
739                 return 0;
740         }
741
742 }
743
744 /*----------------------------------------------------
745  * smc_close
746  *
747  * this makes the board clean up everything that it can
748  * and not talk to the outside world.   Caused by
749  * an 'ifconfig ethX down'
750  *
751  -----------------------------------------------------*/
752 static int smc_close ()
753 {
754         PRINTK2 ("%s:smc_close\n", SMC_DEV_NAME);
755
756         /* clear everything */
757         smc_shutdown ();
758
759         return 0;
760 }
761
762 #if SMC_DEBUG > 2
763 static void print_packet (byte * buf, int length)
764 {
765 #if 0
766         int i;
767         int remainder;
768         int lines;
769
770         printf ("Packet of length %d \n", length);
771
772         lines = length / 16;
773         remainder = length % 16;
774
775         for (i = 0; i < lines; i++) {
776                 int cur;
777
778                 for (cur = 0; cur < 8; cur++) {
779                         byte a, b;
780
781                         a = *(buf++);
782                         b = *(buf++);
783                         printf ("%02x%02x ", a, b);
784                 }
785                 printf ("\n");
786         }
787         for (i = 0; i < remainder / 2; i++) {
788                 byte a, b;
789
790                 a = *(buf++);
791                 b = *(buf++);
792                 printf ("%02x%02x ", a, b);
793         }
794         printf ("\n");
795 #endif /* 0 */
796 }
797 #endif /* SMC_DEBUG > 2 */
798
799 int eth_init (bd_t * bd)
800 {
801         return (smc_open(bd));
802 }
803
804 void eth_halt ()
805 {
806         smc_close ();
807 }
808
809 int eth_rx ()
810 {
811         return smc_rcv ();
812 }
813
814 int eth_send (volatile void *packet, int length)
815 {
816         return smc_send_packet (packet, length);
817 }
818
819
820 /*-------------------------------------------------------------------------
821  * smc_hw_init()
822  *
823  *   Function:
824  *      Reset and enable the device, check if the I/O space location
825  *      is correct
826  *
827  *   Input parameters:
828  *      None
829  *
830  *   Output:
831  *      0 --> success
832  *      1 --> error
833  *--------------------------------------------------------------------------
834  */
835 static int smc_hw_init ()
836 {
837         unsigned short status_test;
838
839         /* The attribute register of the LAN91C96 is located at address
840            0x0e000000 on the lubbock platform */
841         volatile unsigned *attaddr = (unsigned *) (0x0e000000);
842
843         /* first reset, then enable the device. Sequence is critical */
844         attaddr[LAN91C96_ECOR] |= LAN91C96_ECOR_SRESET;
845         udelay (100);
846         attaddr[LAN91C96_ECOR] &= ~LAN91C96_ECOR_SRESET;
847         attaddr[LAN91C96_ECOR] |= LAN91C96_ECOR_ENABLE;
848
849         /* force 16-bit mode */
850         attaddr[LAN91C96_ECSR] &= ~LAN91C96_ECSR_IOIS8;
851         udelay (100);
852
853         /* check if the I/O address is correct, the upper byte of the
854            bank select register should read 0x33 */
855
856         status_test = SMC_inw (LAN91C96_BANK_SELECT);
857         if ((status_test & 0xFF00) != 0x3300) {
858                 printf ("Failed to initialize ethernetchip\n");
859                 return 1;
860         }
861         return 0;
862 }
863
864 #endif /* COMMANDS & CFG_NET */
865
866
867 /* smc_get_ethaddr (bd_t * bd)
868  *
869  * This checks both the environment and the ROM for an ethernet address. If
870  * found, the environment takes precedence.
871  */
872
873 int smc_get_ethaddr (bd_t * bd)
874 {
875         int env_size = 0;
876         int rom_valid = 0;
877         int env_present = 0;
878         int reg = 0;
879         char *s = NULL;
880         char *e = NULL;
881         char *v_mac, es[] = "11:22:33:44:55:66";
882         uchar s_env_mac[64];
883         uchar v_env_mac[6];
884         uchar v_rom_mac[6];
885
886         env_size = getenv_r ("ethaddr", s_env_mac, sizeof (s_env_mac));
887         if (env_size != sizeof(es)) {   /* Ignore if env is bad or not set */
888                 printf ("\n*** Warning: ethaddr is not set properly, ignoring!!\n");
889         } else {
890                 env_present = 1;
891                 s = s_env_mac;
892
893                 for (reg = 0; reg < 6; ++reg) { /* turn string into mac value */
894                         v_env_mac[reg] = s ? simple_strtoul (s, &e, 16) : 0;
895                         if (s)
896                                 s = (*e) ? e + 1 : e;
897                 }
898         }
899
900         rom_valid = get_rom_mac (v_rom_mac);    /* get ROM mac value if any */
901
902         if (!env_present) {     /* if NO env */
903                 if (rom_valid) {        /* but ROM is valid */
904                         v_mac = v_rom_mac;
905                         sprintf (s_env_mac, "%02X:%02X:%02X:%02X:%02X:%02X",
906                                  v_mac[0], v_mac[1], v_mac[2], v_mac[3],
907                                  v_mac[4], v_mac[5]);
908                         setenv ("ethaddr", s_env_mac);
909                 } else {        /* no env, bad ROM */
910                         printf ("\n*** ERROR: ethaddr is NOT set !!\n");
911                         return (-1);
912                 }
913         } else {                /* good env, don't care ROM */
914                 v_mac = v_env_mac;      /* always use a good env over a ROM */
915         }
916
917         if (env_present && rom_valid) { /* if both env and ROM are good */
918                 if (memcmp (v_env_mac, v_rom_mac, 6) != 0) {
919                         printf ("\nWarning: MAC addresses don't match:\n");
920                         printf ("\tHW MAC address:  "
921                                 "%02X:%02X:%02X:%02X:%02X:%02X\n",
922                                 v_rom_mac[0], v_rom_mac[1],
923                                 v_rom_mac[2], v_rom_mac[3],
924                                 v_rom_mac[4], v_rom_mac[5] );
925                         printf ("\t\"ethaddr\" value: "
926                                 "%02X:%02X:%02X:%02X:%02X:%02X\n",
927                                 v_env_mac[0], v_env_mac[1],
928                                 v_env_mac[2], v_env_mac[3],
929                                 v_env_mac[4], v_env_mac[5]) ;
930                         debug ("### Set MAC addr from environment\n");
931                 }
932         }
933         memcpy (bd->bi_enetaddr, v_mac, 6);     /* update global address to match env (allows env changing) */
934         smc_set_mac_addr (v_mac);       /* use old function to update smc default */
935         PRINTK("Using MAC Address %02X:%02X:%02X:%02X:%02X:%02X\n", v_mac[0], v_mac[1],
936                 v_mac[2], v_mac[3], v_mac[4], v_mac[5]);
937         return (0);
938 }
939
940 /*
941  * get_rom_mac()
942  * Note, this has omly been tested for the OMAP730 P2.
943  */
944
945 int get_rom_mac (char *v_rom_mac)
946 {
947 #ifdef HARDCODE_MAC     /* used for testing or to supress run time warnings */
948         char hw_mac_addr[] = { 0x02, 0x80, 0xad, 0x20, 0x31, 0xb8 };
949
950         memcpy (v_rom_mac, hw_mac_addr, 6);
951         return (1);
952 #else
953         int i;
954         SMC_SELECT_BANK (1);
955         for (i=0; i<6; i++)
956         {
957                 v_rom_mac[i] = SMC_inb (LAN91C96_IA0 + i);
958         }
959         return (1);
960 #endif
961 }
962
963 #endif /* CONFIG_DRIVER_LAN91C96 */