]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/smc91111.c
Patch by Robert Schwebel, April 02, 2003:
[karo-tx-uboot.git] / drivers / smc91111.c
1 /*------------------------------------------------------------------------
2  . smc91111.c
3  . This is a driver for SMSC's 91C111 single-chip Ethernet device.
4  .
5  . (C) Copyright 2002
6  . Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7  . Rolf Offermanns <rof@sysgo.de>
8  .
9  . Copyright (C) 2001 Standard Microsystems Corporation (SMSC)
10  .       Developed by Simple Network Magic Corporation (SNMC)
11  . Copyright (C) 1996 by Erik Stahlman (ES)
12  .
13  . This program is free software; you can redistribute it and/or modify
14  . it under the terms of the GNU General Public License as published by
15  . the Free Software Foundation; either version 2 of the License, or
16  . (at your option) any later version.
17  .
18  . This program is distributed in the hope that it will be useful,
19  . but WITHOUT ANY WARRANTY; without even the implied warranty of
20  . MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  . GNU General Public License for more details.
22  .
23  . You should have received a copy of the GNU General Public License
24  . along with this program; if not, write to the Free Software
25  . Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  .
27  . Information contained in this file was obtained from the LAN91C111
28  . manual from SMC.  To get a copy, if you really want one, you can find
29  . information under www.smsc.com.
30  .
31  .
32  . "Features" of the SMC chip:
33  .   Integrated PHY/MAC for 10/100BaseT Operation
34  .   Supports internal and external MII
35  .   Integrated 8K packet memory
36  .   EEPROM interface for configuration
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 LAN91C111 databook (www.smsc.com)
51  .    o   smc9194.c by Erik Stahlman
52  .    o   skeleton.c by Donald Becker ( becker@cesdis.gsfc.nasa.gov )
53  .
54  . History:
55  .      10/17/01  Marco Hasewinkel Modify for DNP/1110
56  .      07/25/01  Woojung Huh      Modify for ADS Bitsy
57  .      04/25/01  Daris A Nevil    Initial public release through SMSC
58  .      03/16/01  Daris A Nevil    Modified smc9194.c for use with LAN91C111
59  ----------------------------------------------------------------------------*/
60
61 #include <common.h>
62 #include <command.h>
63 #include "smc91111.h"
64 #include <net.h>
65
66 #ifdef CONFIG_DRIVER_SMC91111
67
68 /* Use power-down feature of the chip */
69 #define POWER_DOWN      0
70
71 #define NO_AUTOPROBE
72
73 static const char version[] =
74         "smc91111.c:v1.0 04/25/01 by Daris A Nevil (dnevil@snmc.com)\n";
75
76 #define SMC_DEBUG 0
77
78 /*------------------------------------------------------------------------
79  .
80  . Configuration options, for the experienced user to change.
81  .
82  -------------------------------------------------------------------------*/
83
84 /*
85  . Wait time for memory to be free.  This probably shouldn't be
86  . tuned that much, as waiting for this means nothing else happens
87  . in the system
88 */
89 #define MEMORY_WAIT_TIME 16
90
91
92 #if (SMC_DEBUG > 2 )
93 #define PRINTK3(args...) printf(args)
94 #else
95 #define PRINTK3(args...)
96 #endif
97
98 #if SMC_DEBUG > 1
99 #define PRINTK2(args...) printf(args)
100 #else
101 #define PRINTK2(args...)
102 #endif
103
104 #ifdef SMC_DEBUG
105 #define PRINTK(args...) printf(args)
106 #else
107 #define PRINTK(args...)
108 #endif
109
110
111 /*------------------------------------------------------------------------
112  .
113  . The internal workings of the driver.  If you are changing anything
114  . here with the SMC stuff, you should have the datasheet and know
115  . what you are doing.
116  .
117  -------------------------------------------------------------------------*/
118 #define CARDNAME "LAN91C111"
119
120 /* Memory sizing constant */
121 #define LAN91C111_MEMORY_MULTIPLIER     (1024*2)
122
123 #ifndef CONFIG_SMC91111_BASE
124 #define CONFIG_SMC91111_BASE 0x20000300
125 #endif
126
127 #define SMC_BASE_ADDRESS CONFIG_SMC91111_BASE
128
129 #define SMC_DEV_NAME "SMC91111"
130 #define SMC_PHY_ADDR 0x0000
131 #define SMC_ALLOC_MAX_TRY 5
132 #define SMC_TX_TIMEOUT 30
133
134 #define SMC_PHY_CLOCK_DELAY 1000
135
136 #define ETH_ZLEN 60
137
138 #ifdef  CONFIG_SMC_USE_32_BIT
139 #define USE_32_BIT  1
140 #else
141 #undef USE_32_BIT
142 #endif
143 /*-----------------------------------------------------------------
144  .
145  .  The driver can be entered at any of the following entry points.
146  .
147  .------------------------------------------------------------------  */
148
149 extern int eth_init(bd_t *bd);
150 extern void eth_halt(void);
151 extern int eth_rx(void);
152 extern int eth_send(volatile void *packet, int length);
153
154
155
156
157
158 /*
159  . This is called by  register_netdev().  It is responsible for
160  . checking the portlist for the SMC9000 series chipset.  If it finds
161  . one, then it will initialize the device, find the hardware information,
162  . and sets up the appropriate device parameters.
163  . NOTE: Interrupts are *OFF* when this procedure is called.
164  .
165  . NB:This shouldn't be static since it is referred to externally.
166 */
167 int smc_init(void);
168
169 /*
170  . This is called by  unregister_netdev().  It is responsible for
171  . cleaning up before the driver is finally unregistered and discarded.
172 */
173 void smc_destructor(void);
174
175 /*
176  . The kernel calls this function when someone wants to use the device,
177  . typically 'ifconfig ethX up'.
178 */
179 static int smc_open(void);
180
181
182 /*
183  . This is called by the kernel in response to 'ifconfig ethX down'.  It
184  . is responsible for cleaning up everything that the open routine
185  . does, and maybe putting the card into a powerdown state.
186 */
187 static int smc_close(void);
188
189 /*
190  . Configures the PHY through the MII Management interface
191 */
192 #ifndef CONFIG_SMC91111_EXT_PHY
193 static void smc_phy_configure(void);
194 #endif /* !CONFIG_SMC91111_EXT_PHY */
195
196 /*
197  . This is a separate procedure to handle the receipt of a packet, to
198  . leave the interrupt code looking slightly cleaner
199 */
200 static int smc_rcv(void);
201
202
203
204 /*
205  ------------------------------------------------------------
206  .
207  . Internal routines
208  .
209  ------------------------------------------------------------
210 */
211
212 static char smc_mac_addr[] = {0x02, 0x80, 0xad, 0x20, 0x31, 0xb8};
213
214 /*
215  * This function must be called before smc_open() if you want to override
216  * the default mac address.
217  */
218
219 void smc_set_mac_addr(const char *addr) {
220         int i;
221
222         for (i=0; i < sizeof(smc_mac_addr); i++){
223                 smc_mac_addr[i] = addr[i];
224         }
225 }
226
227 /*
228  * smc_get_macaddr is no longer used. If you want to override the default
229  * mac address, call smc_get_mac_addr as a part of the board initialisation.
230  */
231
232 #if 0
233 void smc_get_macaddr( byte *addr ) {
234         /* MAC ADDRESS AT FLASHBLOCK 1 / OFFSET 0x10 */
235         unsigned char *dnp1110_mac = (unsigned char *) (0xE8000000 + 0x20010);
236         int i;
237
238
239         for (i=0; i<6; i++) {
240             addr[0] = *(dnp1110_mac+0);
241             addr[1] = *(dnp1110_mac+1);
242             addr[2] = *(dnp1110_mac+2);
243             addr[3] = *(dnp1110_mac+3);
244             addr[4] = *(dnp1110_mac+4);
245             addr[5] = *(dnp1110_mac+5);
246         }
247 }
248 #endif /* 0 */
249
250 /***********************************************
251  * Show available memory                       *
252  ***********************************************/
253 void dump_memory_info(void)
254 {
255         word mem_info;
256         word old_bank;
257
258         old_bank = SMC_inw(BANK_SELECT)&0xF;
259
260         SMC_SELECT_BANK(0);
261         mem_info = SMC_inw( MIR_REG );
262         PRINTK2("Memory: %4d available\n", (mem_info >> 8)*2048);
263
264         SMC_SELECT_BANK(old_bank);
265 }
266 /*
267  . A rather simple routine to print out a packet for debugging purposes.
268 */
269 #if SMC_DEBUG > 2
270 static void print_packet( byte *, int );
271 #endif
272
273 #define tx_done(dev) 1
274
275
276
277 /* this does a soft reset on the device */
278 static void smc_reset( void );
279
280 /* Enable Interrupts, Receive, and Transmit */
281 static void smc_enable( void );
282
283 /* this puts the device in an inactive state */
284 static void smc_shutdown( void );
285
286 /* Routines to Read and Write the PHY Registers across the
287    MII Management Interface
288 */
289
290 #ifndef CONFIG_SMC91111_EXT_PHY
291 static word smc_read_phy_register(byte phyreg);
292 static void smc_write_phy_register(byte phyreg, word phydata);
293 #endif /* !CONFIG_SMC91111_EXT_PHY */
294
295
296 static int poll4int( byte mask, int timeout ) {
297     int tmo = get_timer(0) + timeout * CFG_HZ;
298     int is_timeout = 0;
299     word old_bank = SMC_inw(BSR_REG);
300
301     PRINTK2("Polling...\n");
302     SMC_SELECT_BANK(2);
303     while((SMC_inw(SMC91111_INT_REG) & mask) == 0)
304     {
305         if (get_timer(0) >= tmo) {
306           is_timeout = 1;
307           break;
308         }
309     }
310
311     /* restore old bank selection */
312     SMC_SELECT_BANK(old_bank);
313
314     if (is_timeout)
315         return 1;
316     else
317         return 0;
318 }
319
320 /* Only one release command at a time, please */
321 static inline void smc_wait_mmu_release_complete(void)
322 {
323         int count = 0;
324         /* assume bank 2 selected */
325         while ( SMC_inw(MMU_CMD_REG) & MC_BUSY ) {
326                 udelay(1); // Wait until not busy
327                 if( ++count > 200) break;
328         }
329 }
330
331 /*
332  . Function: smc_reset( void )
333  . Purpose:
334  .      This sets the SMC91111 chip to its normal state, hopefully from whatever
335  .      mess that any other DOS driver has put it in.
336  .
337  . Maybe I should reset more registers to defaults in here?  SOFTRST  should
338  . do that for me.
339  .
340  . Method:
341  .      1.  send a SOFT RESET
342  .      2.  wait for it to finish
343  .      3.  enable autorelease mode
344  .      4.  reset the memory management unit
345  .      5.  clear all interrupts
346  .
347 */
348 static void smc_reset( void )
349 {
350         PRINTK2("%s:smc_reset\n", SMC_DEV_NAME);
351
352         /* This resets the registers mostly to defaults, but doesn't
353            affect EEPROM.  That seems unnecessary */
354         SMC_SELECT_BANK( 0 );
355         SMC_outw( RCR_SOFTRST, RCR_REG );
356
357         /* Setup the Configuration Register */
358         /* This is necessary because the CONFIG_REG is not affected */
359         /* by a soft reset */
360
361         SMC_SELECT_BANK( 1 );
362 #if defined(CONFIG_SMC91111_EXT_PHY)
363         SMC_outw( CONFIG_DEFAULT | CONFIG_EXT_PHY, CONFIG_REG);
364 #else
365         SMC_outw( CONFIG_DEFAULT, CONFIG_REG);
366 #endif
367
368
369         /* Release from possible power-down state */
370         /* Configuration register is not affected by Soft Reset */
371         SMC_outw( SMC_inw( CONFIG_REG ) | CONFIG_EPH_POWER_EN, CONFIG_REG  );
372
373         SMC_SELECT_BANK( 0 );
374
375         /* this should pause enough for the chip to be happy */
376         udelay(10);
377
378         /* Disable transmit and receive functionality */
379         SMC_outw( RCR_CLEAR, RCR_REG );
380         SMC_outw( TCR_CLEAR, TCR_REG );
381
382         /* set the control register */
383         SMC_SELECT_BANK( 1 );
384         SMC_outw( CTL_DEFAULT, CTL_REG );
385
386         /* Reset the MMU */
387         SMC_SELECT_BANK( 2 );
388         smc_wait_mmu_release_complete();
389         SMC_outw( MC_RESET, MMU_CMD_REG );
390         while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
391                 udelay(1); /* Wait until not busy */
392
393         /* Note:  It doesn't seem that waiting for the MMU busy is needed here,
394            but this is a place where future chipsets _COULD_ break.  Be wary
395            of issuing another MMU command right after this */
396
397         /* Disable all interrupts */
398         SMC_outb( 0, IM_REG );
399 }
400
401 /*
402  . Function: smc_enable
403  . Purpose: let the chip talk to the outside work
404  . Method:
405  .      1.  Enable the transmitter
406  .      2.  Enable the receiver
407  .      3.  Enable interrupts
408 */
409 static void smc_enable()
410 {
411         PRINTK2("%s:smc_enable\n", SMC_DEV_NAME);
412         SMC_SELECT_BANK( 0 );
413         /* see the header file for options in TCR/RCR DEFAULT*/
414         SMC_outw( TCR_DEFAULT, TCR_REG );
415         SMC_outw( RCR_DEFAULT, RCR_REG );
416
417         /* clear MII_DIS */
418 /*      smc_write_phy_register(PHY_CNTL_REG, 0x0000); */
419 }
420
421 /*
422  . Function: smc_shutdown
423  . Purpose:  closes down the SMC91xxx chip.
424  . Method:
425  .      1. zero the interrupt mask
426  .      2. clear the enable receive flag
427  .      3. clear the enable xmit flags
428  .
429  . TODO:
430  .   (1) maybe utilize power down mode.
431  .      Why not yet?  Because while the chip will go into power down mode,
432  .      the manual says that it will wake up in response to any I/O requests
433  .      in the register space.   Empirical results do not show this working.
434 */
435 static void smc_shutdown()
436 {
437         PRINTK2(CARDNAME ":smc_shutdown\n");
438
439         /* no more interrupts for me */
440         SMC_SELECT_BANK( 2 );
441         SMC_outb( 0, IM_REG );
442
443         /* and tell the card to stay away from that nasty outside world */
444         SMC_SELECT_BANK( 0 );
445         SMC_outb( RCR_CLEAR, RCR_REG );
446         SMC_outb( TCR_CLEAR, TCR_REG );
447 }
448
449
450 /*
451  . Function:  smc_hardware_send_packet(struct net_device * )
452  . Purpose:
453  .      This sends the actual packet to the SMC9xxx chip.
454  .
455  . Algorithm:
456  .      First, see if a saved_skb is available.
457  .              ( this should NOT be called if there is no 'saved_skb'
458  .      Now, find the packet number that the chip allocated
459  .      Point the data pointers at it in memory
460  .      Set the length word in the chip's memory
461  .      Dump the packet to chip memory
462  .      Check if a last byte is needed ( odd length packet )
463  .              if so, set the control flag right
464  .      Tell the card to send it
465  .      Enable the transmit interrupt, so I know if it failed
466  .      Free the kernel data if I actually sent it.
467 */
468 static int smc_send_packet(volatile void *packet, int packet_length)
469 {
470         byte                    packet_no;
471         unsigned long           ioaddr;
472         byte                    * buf;
473         int                     length;
474         int                     numPages;
475         int                     try = 0;
476         int                     time_out;
477         byte                    status;
478
479
480         PRINTK3("%s:smc_hardware_send_packet\n", SMC_DEV_NAME);
481
482         length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
483
484         /* allocate memory
485         ** The MMU wants the number of pages to be the number of 256 bytes
486         ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
487         **
488         ** The 91C111 ignores the size bits, but the code is left intact
489         ** for backwards and future compatibility.
490         **
491         ** Pkt size for allocating is data length +6 (for additional status
492         ** words, length and ctl!)
493         **
494         ** If odd size then last byte is included in this header.
495         */
496         numPages =   ((length & 0xfffe) + 6);
497         numPages >>= 8; /* Divide by 256 */
498
499         if (numPages > 7 ) {
500                 printf("%s: Far too big packet error. \n", SMC_DEV_NAME);
501                 return 0;
502         }
503
504         /* now, try to allocate the memory */
505         SMC_SELECT_BANK( 2 );
506         SMC_outw( MC_ALLOC | numPages, MMU_CMD_REG );
507
508         /* FIXME: the ALLOC_INT bit never gets set *
509          * so the following will always give a     *
510          * memory allocation error.                *
511          * same code works in armboot though       *
512          * -ro
513          */
514
515 again:
516         try++;
517         time_out = MEMORY_WAIT_TIME;
518         do {
519                 status = SMC_inb( SMC91111_INT_REG );
520                 if ( status & IM_ALLOC_INT ) {
521                         /* acknowledge the interrupt */
522                         SMC_outb( IM_ALLOC_INT, SMC91111_INT_REG );
523                         break;
524                 }
525         } while ( -- time_out );
526
527         if ( !time_out ) {
528                         PRINTK2("%s: memory allocation, try %d failed ...\n",
529                                 SMC_DEV_NAME, try);
530                         if (try < SMC_ALLOC_MAX_TRY)
531                                 goto again;
532                         else
533                                 return 0;
534         }
535
536         PRINTK2("%s: memory allocation, try %d succeeded ...\n",
537                 SMC_DEV_NAME,
538                 try);
539
540         /* I can send the packet now.. */
541
542         ioaddr = SMC_BASE_ADDRESS;
543
544         buf = (byte *)packet;
545
546         /* If I get here, I _know_ there is a packet slot waiting for me */
547         packet_no = SMC_inb( AR_REG );
548         if ( packet_no & AR_FAILED ) {
549                 /* or isn't there?  BAD CHIP! */
550                 printf("%s: Memory allocation failed. \n",
551                         SMC_DEV_NAME);
552                 return 0;
553         }
554
555         /* we have a packet address, so tell the card to use it */
556         SMC_outb( packet_no, PN_REG );
557
558         /* point to the beginning of the packet */
559         SMC_outw( PTR_AUTOINC , PTR_REG );
560
561         PRINTK3("%s: Trying to xmit packet of length %x\n",
562                 SMC_DEV_NAME, length);
563
564 #if SMC_DEBUG > 2
565         printf("Transmitting Packet\n");
566         print_packet( buf, length );
567 #endif
568
569         /* send the packet length ( +6 for status, length and ctl byte )
570            and the status word ( set to zeros ) */
571 #ifdef USE_32_BIT
572         SMC_outl(  (length +6 ) << 16 , SMC91111_DATA_REG );
573 #else
574         SMC_outw( 0, SMC91111_DATA_REG );
575         /* send the packet length ( +6 for status words, length, and ctl*/
576         SMC_outw( (length+6), SMC91111_DATA_REG );
577 #endif
578
579         /* send the actual data
580          . I _think_ it's faster to send the longs first, and then
581          . mop up by sending the last word.  It depends heavily
582          . on alignment, at least on the 486.  Maybe it would be
583          . a good idea to check which is optimal?  But that could take
584          . almost as much time as is saved?
585         */
586 #ifdef USE_32_BIT
587         SMC_outsl(SMC91111_DATA_REG, buf,  length >> 2 );
588         if ( length & 0x2  )
589                 SMC_outw(*((word *)(buf + (length & 0xFFFFFFFC))), SMC91111_DATA_REG);
590 #else
591         SMC_outsw(SMC91111_DATA_REG , buf, (length ) >> 1);
592 #endif /* USE_32_BIT */
593
594         /* Send the last byte, if there is one.   */
595         if ( (length & 1) == 0 ) {
596                 SMC_outw( 0, SMC91111_DATA_REG );
597         } else {
598                 SMC_outw( buf[length -1 ] | 0x2000, SMC91111_DATA_REG );
599         }
600
601         /* and let the chipset deal with it */
602         SMC_outw( MC_ENQUEUE , MMU_CMD_REG );
603
604         /* poll for TX INT */
605         if (poll4int(IM_TX_INT, SMC_TX_TIMEOUT)) {
606                 /* sending failed */
607                 PRINTK2("%s: TX timeout, sending failed...\n",
608                         SMC_DEV_NAME);
609
610                 /* release packet */
611                 SMC_outw(MC_FREEPKT, MMU_CMD_REG);
612
613                 /* wait for MMU getting ready (low) */
614                 while (SMC_inw(MMU_CMD_REG) & MC_BUSY)
615                 {
616                         udelay(10);
617                 }
618
619                 PRINTK2("MMU ready\n");
620
621
622                 return 0;
623         } else {
624                 /* ack. int */
625                 SMC_outw(IM_TX_INT, SMC91111_INT_REG);
626                 PRINTK2("%s: Sent packet of length %d \n", SMC_DEV_NAME, length);
627
628                 /* release packet */
629                 SMC_outw(MC_FREEPKT, MMU_CMD_REG);
630
631                 /* wait for MMU getting ready (low) */
632                 while (SMC_inw(MMU_CMD_REG) & MC_BUSY)
633                 {
634                         udelay(10);
635                 }
636
637                 PRINTK2("MMU ready\n");
638
639
640         }
641
642         return length;
643 }
644
645 /*-------------------------------------------------------------------------
646  |
647  | smc_destructor( struct net_device * dev )
648  |   Input parameters:
649  |      dev, pointer to the device structure
650  |
651  |   Output:
652  |      None.
653  |
654  ---------------------------------------------------------------------------
655 */
656 void smc_destructor()
657 {
658         PRINTK2(CARDNAME ":smc_destructor\n");
659 }
660
661
662 /*
663  * Open and Initialize the board
664  *
665  * Set up everything, reset the card, etc ..
666  *
667  */
668 static int smc_open()
669 {
670         int     i;      /* used to set hw ethernet address */
671
672         PRINTK2("%s:smc_open\n", SMC_DEV_NAME);
673
674         /* reset the hardware */
675
676         smc_reset();
677         smc_enable();
678
679         /* Configure the PHY */
680 #ifndef CONFIG_SMC91111_EXT_PHY
681         smc_phy_configure();
682 #endif
683
684
685         /* conservative setting (10Mbps, HalfDuplex, no AutoNeg.) */
686 /*      SMC_SELECT_BANK(0); */
687 /*      SMC_outw(0, RPC_REG); */
688
689         SMC_SELECT_BANK(1);
690
691 #ifdef USE_32_BIT
692         for ( i = 0; i < 6; i += 2 ) {
693                 word address;
694
695                 address = smc_mac_addr[ i + 1 ] << 8 ;
696                 address  |= smc_mac_addr[ i ];
697                 SMC_outw( address, ADDR0_REG + i );
698         }
699 #else
700         for ( i = 0; i < 6; i ++ )
701                 SMC_outb( smc_mac_addr[i], ADDR0_REG + i );
702 #endif
703
704         return 0;
705 }
706
707 #if 0 /* dead code? -- wd */
708 #ifdef USE_32_BIT
709 void
710 insl32(r,b,l)
711 {
712    int __i ;
713    dword *__b2;
714
715         __b2 = (dword *) b;
716         for (__i = 0; __i < l; __i++) {
717                   *(__b2 + __i) = *(dword *)(r+0x10000300);
718         }
719 }
720 #endif
721 #endif
722
723 /*-------------------------------------------------------------
724  .
725  . smc_rcv -  receive a packet from the card
726  .
727  . There is ( at least ) a packet waiting to be read from
728  . chip-memory.
729  .
730  . o Read the status
731  . o If an error, record it
732  . o otherwise, read in the packet
733  --------------------------------------------------------------
734 */
735 static int smc_rcv()
736 {
737         int     packet_number;
738         word    status;
739         word    packet_length;
740         int     is_error = 0;
741 #ifdef USE_32_BIT
742         dword stat_len;
743 #endif
744
745
746         SMC_SELECT_BANK(2);
747         packet_number = SMC_inw( RXFIFO_REG );
748
749         if ( packet_number & RXFIFO_REMPTY ) {
750
751                 return 0;
752         }
753
754         PRINTK3("%s:smc_rcv\n", SMC_DEV_NAME);
755         /*  start reading from the start of the packet */
756         SMC_outw( PTR_READ | PTR_RCV | PTR_AUTOINC, PTR_REG );
757
758         /* First two words are status and packet_length */
759 #ifdef USE_32_BIT
760         stat_len = SMC_inl(SMC91111_DATA_REG);
761         status = stat_len & 0xffff;
762         packet_length = stat_len >> 16;
763 #else
764         status          = SMC_inw( SMC91111_DATA_REG );
765         packet_length   = SMC_inw( SMC91111_DATA_REG );
766 #endif
767
768         packet_length &= 0x07ff;  /* mask off top bits */
769
770         PRINTK2("RCV: STATUS %4x LENGTH %4x\n", status, packet_length );
771
772         if ( !(status & RS_ERRORS ) ){
773                 /* Adjust for having already read the first two words */
774                 packet_length -= 4; /*4; */
775
776
777
778                 /* set odd length for bug in LAN91C111, */
779                 /* which never sets RS_ODDFRAME */
780                 /* TODO ? */
781
782
783 #ifdef USE_32_BIT
784                 PRINTK3(" Reading %d dwords (and %d bytes) \n",
785                         packet_length >> 2, packet_length & 3 );
786                 /* QUESTION:  Like in the TX routine, do I want
787                    to send the DWORDs or the bytes first, or some
788                    mixture.  A mixture might improve already slow PIO
789                    performance  */
790                 SMC_insl( SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 2 );
791                 /* read the left over bytes */
792                 if (packet_length & 3) {
793                         int i;
794
795                         byte *tail = (byte *)(NetRxPackets[0] + (packet_length & ~3));
796                         dword leftover = SMC_inl(SMC91111_DATA_REG);
797                         for (i=0; i<(packet_length & 3); i++)
798                                 *tail++ = (byte) (leftover >> (8*i)) & 0xff;
799                 }
800 #else
801                 PRINTK3(" Reading %d words and %d byte(s) \n",
802                         (packet_length >> 1 ), packet_length & 1 );
803                 SMC_insw(SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 1);
804
805 #endif /* USE_32_BIT */
806
807 #if     SMC_DEBUG > 2
808                 printf("Receiving Packet\n");
809                 print_packet( NetRxPackets[0], packet_length );
810 #endif
811         } else {
812                 /* error ... */
813                 /* TODO ? */
814                 is_error = 1;
815         }
816
817         while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
818                 udelay(1); /* Wait until not busy */
819
820         /*  error or good, tell the card to get rid of this packet */
821         SMC_outw( MC_RELEASE, MMU_CMD_REG );
822
823         while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
824                 udelay(1); /* Wait until not busy */
825
826         if (!is_error) {
827                 /* Pass the packet up to the protocol layers. */
828                 NetReceive(NetRxPackets[0], packet_length);
829                 return packet_length;
830         } else {
831                 return 0;
832         }
833
834 }
835
836
837
838 /*----------------------------------------------------
839  . smc_close
840  .
841  . this makes the board clean up everything that it can
842  . and not talk to the outside world.   Caused by
843  . an 'ifconfig ethX down'
844  .
845  -----------------------------------------------------*/
846 static int smc_close()
847 {
848         PRINTK2("%s:smc_close\n", SMC_DEV_NAME);
849
850         /* clear everything */
851         smc_shutdown();
852
853         return 0;
854 }
855
856
857 #if 0
858 /*------------------------------------------------------------
859  . Modify a bit in the LAN91C111 register set
860  .-------------------------------------------------------------*/
861 static word smc_modify_regbit(int bank, int ioaddr, int reg,
862         unsigned int bit, int val)
863 {
864         word regval;
865
866         SMC_SELECT_BANK( bank );
867
868         regval = SMC_inw( reg );
869         if (val)
870                 regval |= bit;
871         else
872                 regval &= ~bit;
873
874         SMC_outw( regval, 0 );
875         return(regval);
876 }
877
878
879 /*------------------------------------------------------------
880  . Retrieve a bit in the LAN91C111 register set
881  .-------------------------------------------------------------*/
882 static int smc_get_regbit(int bank, int ioaddr, int reg, unsigned int bit)
883 {
884         SMC_SELECT_BANK( bank );
885         if ( SMC_inw( reg ) & bit)
886                 return(1);
887         else
888                 return(0);
889 }
890
891
892 /*------------------------------------------------------------
893  . Modify a LAN91C111 register (word access only)
894  .-------------------------------------------------------------*/
895 static void smc_modify_reg(int bank, int ioaddr, int reg, word val)
896 {
897         SMC_SELECT_BANK( bank );
898         SMC_outw( val, reg );
899 }
900
901
902 /*------------------------------------------------------------
903  . Retrieve a LAN91C111 register (word access only)
904  .-------------------------------------------------------------*/
905 static int smc_get_reg(int bank, int ioaddr, int reg)
906 {
907         SMC_SELECT_BANK( bank );
908         return(SMC_inw( reg ));
909 }
910
911 #endif /* 0 */
912
913 /*---PHY CONTROL AND CONFIGURATION----------------------------------------- */
914
915 #if (SMC_DEBUG > 2 )
916
917 /*------------------------------------------------------------
918  . Debugging function for viewing MII Management serial bitstream
919  .-------------------------------------------------------------*/
920 static void smc_dump_mii_stream(byte* bits, int size)
921 {
922         int i;
923
924         printf("BIT#:");
925         for (i = 0; i < size; ++i)
926                 {
927                 printf("%d", i%10);
928                 }
929
930         printf("\nMDOE:");
931         for (i = 0; i < size; ++i)
932                 {
933                 if (bits[i] & MII_MDOE)
934                         printf("1");
935                 else
936                         printf("0");
937                 }
938
939         printf("\nMDO :");
940         for (i = 0; i < size; ++i)
941                 {
942                 if (bits[i] & MII_MDO)
943                         printf("1");
944                 else
945                         printf("0");
946                 }
947
948         printf("\nMDI :");
949         for (i = 0; i < size; ++i)
950                 {
951                 if (bits[i] & MII_MDI)
952                         printf("1");
953                 else
954                         printf("0");
955                 }
956
957         printf("\n");
958 }
959 #endif
960
961 /*------------------------------------------------------------
962  . Reads a register from the MII Management serial interface
963  .-------------------------------------------------------------*/
964 #ifndef CONFIG_SMC91111_EXT_PHY
965 static word smc_read_phy_register(byte phyreg)
966 {
967         int oldBank;
968         int i;
969         byte mask;
970         word mii_reg;
971         byte bits[64];
972         int clk_idx = 0;
973         int input_idx;
974         word phydata;
975         byte phyaddr = SMC_PHY_ADDR;
976
977         /* 32 consecutive ones on MDO to establish sync */
978         for (i = 0; i < 32; ++i)
979                 bits[clk_idx++] = MII_MDOE | MII_MDO;
980
981         /* Start code <01> */
982         bits[clk_idx++] = MII_MDOE;
983         bits[clk_idx++] = MII_MDOE | MII_MDO;
984
985         /* Read command <10> */
986         bits[clk_idx++] = MII_MDOE | MII_MDO;
987         bits[clk_idx++] = MII_MDOE;
988
989         /* Output the PHY address, msb first */
990         mask = (byte)0x10;
991         for (i = 0; i < 5; ++i)
992                 {
993                 if (phyaddr & mask)
994                         bits[clk_idx++] = MII_MDOE | MII_MDO;
995                 else
996                         bits[clk_idx++] = MII_MDOE;
997
998                 /* Shift to next lowest bit */
999                 mask >>= 1;
1000                 }
1001
1002         /* Output the phy register number, msb first */
1003         mask = (byte)0x10;
1004         for (i = 0; i < 5; ++i)
1005                 {
1006                 if (phyreg & mask)
1007                         bits[clk_idx++] = MII_MDOE | MII_MDO;
1008                 else
1009                         bits[clk_idx++] = MII_MDOE;
1010
1011                 /* Shift to next lowest bit */
1012                 mask >>= 1;
1013                 }
1014
1015         /* Tristate and turnaround (2 bit times) */
1016         bits[clk_idx++] = 0;
1017         /*bits[clk_idx++] = 0; */
1018
1019         /* Input starts at this bit time */
1020         input_idx = clk_idx;
1021
1022         /* Will input 16 bits */
1023         for (i = 0; i < 16; ++i)
1024                 bits[clk_idx++] = 0;
1025
1026         /* Final clock bit */
1027         bits[clk_idx++] = 0;
1028
1029         /* Save the current bank */
1030         oldBank = SMC_inw( BANK_SELECT );
1031
1032         /* Select bank 3 */
1033         SMC_SELECT_BANK( 3 );
1034
1035         /* Get the current MII register value */
1036         mii_reg = SMC_inw( MII_REG );
1037
1038         /* Turn off all MII Interface bits */
1039         mii_reg &= ~(MII_MDOE|MII_MCLK|MII_MDI|MII_MDO);
1040
1041         /* Clock all 64 cycles */
1042         for (i = 0; i < sizeof bits; ++i)
1043                 {
1044                 /* Clock Low - output data */
1045                 SMC_outw( mii_reg | bits[i], MII_REG );
1046                 udelay(SMC_PHY_CLOCK_DELAY);
1047
1048
1049                 /* Clock Hi - input data */
1050                 SMC_outw( mii_reg | bits[i] | MII_MCLK, MII_REG );
1051                 udelay(SMC_PHY_CLOCK_DELAY);
1052                 bits[i] |= SMC_inw( MII_REG ) & MII_MDI;
1053                 }
1054
1055         /* Return to idle state */
1056         /* Set clock to low, data to low, and output tristated */
1057         SMC_outw( mii_reg, MII_REG );
1058         udelay(SMC_PHY_CLOCK_DELAY);
1059
1060         /* Restore original bank select */
1061         SMC_SELECT_BANK( oldBank );
1062
1063         /* Recover input data */
1064         phydata = 0;
1065         for (i = 0; i < 16; ++i)
1066                 {
1067                 phydata <<= 1;
1068
1069                 if (bits[input_idx++] & MII_MDI)
1070                         phydata |= 0x0001;
1071                 }
1072
1073 #if (SMC_DEBUG > 2 )
1074         printf("smc_read_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
1075                 phyaddr, phyreg, phydata);
1076         smc_dump_mii_stream(bits, sizeof bits);
1077 #endif
1078
1079         return(phydata);
1080 }
1081
1082
1083 /*------------------------------------------------------------
1084  . Writes a register to the MII Management serial interface
1085  .-------------------------------------------------------------*/
1086 static void smc_write_phy_register(byte phyreg, word phydata)
1087 {
1088         int oldBank;
1089         int i;
1090         word mask;
1091         word mii_reg;
1092         byte bits[65];
1093         int clk_idx = 0;
1094         byte phyaddr = SMC_PHY_ADDR;
1095
1096         /* 32 consecutive ones on MDO to establish sync */
1097         for (i = 0; i < 32; ++i)
1098                 bits[clk_idx++] = MII_MDOE | MII_MDO;
1099
1100         /* Start code <01> */
1101         bits[clk_idx++] = MII_MDOE;
1102         bits[clk_idx++] = MII_MDOE | MII_MDO;
1103
1104         /* Write command <01> */
1105         bits[clk_idx++] = MII_MDOE;
1106         bits[clk_idx++] = MII_MDOE | MII_MDO;
1107
1108         /* Output the PHY address, msb first */
1109         mask = (byte)0x10;
1110         for (i = 0; i < 5; ++i)
1111                 {
1112                 if (phyaddr & mask)
1113                         bits[clk_idx++] = MII_MDOE | MII_MDO;
1114                 else
1115                         bits[clk_idx++] = MII_MDOE;
1116
1117                 /* Shift to next lowest bit */
1118                 mask >>= 1;
1119                 }
1120
1121         /* Output the phy register number, msb first */
1122         mask = (byte)0x10;
1123         for (i = 0; i < 5; ++i)
1124                 {
1125                 if (phyreg & mask)
1126                         bits[clk_idx++] = MII_MDOE | MII_MDO;
1127                 else
1128                         bits[clk_idx++] = MII_MDOE;
1129
1130                 /* Shift to next lowest bit */
1131                 mask >>= 1;
1132                 }
1133
1134         /* Tristate and turnaround (2 bit times) */
1135         bits[clk_idx++] = 0;
1136         bits[clk_idx++] = 0;
1137
1138         /* Write out 16 bits of data, msb first */
1139         mask = 0x8000;
1140         for (i = 0; i < 16; ++i)
1141                 {
1142                 if (phydata & mask)
1143                         bits[clk_idx++] = MII_MDOE | MII_MDO;
1144                 else
1145                         bits[clk_idx++] = MII_MDOE;
1146
1147                 /* Shift to next lowest bit */
1148                 mask >>= 1;
1149                 }
1150
1151         /* Final clock bit (tristate) */
1152         bits[clk_idx++] = 0;
1153
1154         /* Save the current bank */
1155         oldBank = SMC_inw( BANK_SELECT );
1156
1157         /* Select bank 3 */
1158         SMC_SELECT_BANK( 3 );
1159
1160         /* Get the current MII register value */
1161         mii_reg = SMC_inw( MII_REG );
1162
1163         /* Turn off all MII Interface bits */
1164         mii_reg &= ~(MII_MDOE|MII_MCLK|MII_MDI|MII_MDO);
1165
1166         /* Clock all cycles */
1167         for (i = 0; i < sizeof bits; ++i)
1168                 {
1169                 /* Clock Low - output data */
1170                 SMC_outw( mii_reg | bits[i], MII_REG );
1171                 udelay(SMC_PHY_CLOCK_DELAY);
1172
1173
1174                 /* Clock Hi - input data */
1175                 SMC_outw( mii_reg | bits[i] | MII_MCLK, MII_REG );
1176                 udelay(SMC_PHY_CLOCK_DELAY);
1177                 bits[i] |= SMC_inw( MII_REG ) & MII_MDI;
1178                 }
1179
1180         /* Return to idle state */
1181         /* Set clock to low, data to low, and output tristated */
1182         SMC_outw( mii_reg, MII_REG );
1183         udelay(SMC_PHY_CLOCK_DELAY);
1184
1185         /* Restore original bank select */
1186         SMC_SELECT_BANK( oldBank );
1187
1188 #if (SMC_DEBUG > 2 )
1189         printf("smc_write_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
1190                 phyaddr, phyreg, phydata);
1191         smc_dump_mii_stream(bits, sizeof bits);
1192 #endif
1193 }
1194 #endif /* !CONFIG_SMC91111_EXT_PHY */
1195
1196
1197
1198 /*------------------------------------------------------------
1199  . Waits the specified number of milliseconds - kernel friendly
1200  .-------------------------------------------------------------*/
1201 #ifndef CONFIG_SMC91111_EXT_PHY
1202 static void smc_wait_ms(unsigned int ms)
1203 {
1204         udelay(ms*1000);
1205 }
1206 #endif /* !CONFIG_SMC91111_EXT_PHY */
1207
1208
1209
1210 /*------------------------------------------------------------
1211  . Configures the specified PHY using Autonegotiation. Calls
1212  . smc_phy_fixed() if the user has requested a certain config.
1213  .-------------------------------------------------------------*/
1214 #ifndef CONFIG_SMC91111_EXT_PHY
1215 static void smc_phy_configure()
1216 {
1217         int timeout;
1218         byte phyaddr;
1219         word my_phy_caps; /* My PHY capabilities */
1220         word my_ad_caps;  /* My Advertised capabilities */
1221         word status = 0;  /*;my status = 0 */
1222         int failed = 0;
1223
1224         PRINTK3("%s:smc_program_phy()\n", SMC_DEV_NAME);
1225
1226
1227
1228         /* Get the detected phy address */
1229         phyaddr = SMC_PHY_ADDR;
1230
1231         /* Reset the PHY, setting all other bits to zero */
1232         smc_write_phy_register(PHY_CNTL_REG, PHY_CNTL_RST);
1233
1234         /* Wait for the reset to complete, or time out */
1235         timeout = 6; /* Wait up to 3 seconds */
1236         while (timeout--)
1237                 {
1238                 if (!(smc_read_phy_register(PHY_CNTL_REG)
1239                     & PHY_CNTL_RST))
1240                         {
1241                         /* reset complete */
1242                         break;
1243                         }
1244
1245                 smc_wait_ms(500); /* wait 500 millisecs */
1246                 }
1247
1248         if (timeout < 1)
1249                 {
1250                 printf("%s:PHY reset timed out\n", SMC_DEV_NAME);
1251                 goto smc_phy_configure_exit;
1252                 }
1253
1254         /* Read PHY Register 18, Status Output */
1255         /* lp->lastPhy18 = smc_read_phy_register(PHY_INT_REG); */
1256
1257         /* Enable PHY Interrupts (for register 18) */
1258         /* Interrupts listed here are disabled */
1259         smc_write_phy_register(PHY_INT_REG, 0xffff);
1260
1261         /* Configure the Receive/Phy Control register */
1262         SMC_SELECT_BANK( 0 );
1263         SMC_outw( RPC_DEFAULT, RPC_REG );
1264
1265         /* Copy our capabilities from PHY_STAT_REG to PHY_AD_REG */
1266         my_phy_caps = smc_read_phy_register(PHY_STAT_REG);
1267         my_ad_caps  = PHY_AD_CSMA; /* I am CSMA capable */
1268
1269         if (my_phy_caps & PHY_STAT_CAP_T4)
1270                 my_ad_caps |= PHY_AD_T4;
1271
1272         if (my_phy_caps & PHY_STAT_CAP_TXF)
1273                 my_ad_caps |= PHY_AD_TX_FDX;
1274
1275         if (my_phy_caps & PHY_STAT_CAP_TXH)
1276                 my_ad_caps |= PHY_AD_TX_HDX;
1277
1278         if (my_phy_caps & PHY_STAT_CAP_TF)
1279                 my_ad_caps |= PHY_AD_10_FDX;
1280
1281         if (my_phy_caps & PHY_STAT_CAP_TH)
1282                 my_ad_caps |= PHY_AD_10_HDX;
1283
1284         /* Update our Auto-Neg Advertisement Register */
1285         smc_write_phy_register( PHY_AD_REG, my_ad_caps);
1286
1287         PRINTK2("%s:phy caps=%x\n", SMC_DEV_NAME, my_phy_caps);
1288         PRINTK2("%s:phy advertised caps=%x\n", SMC_DEV_NAME, my_ad_caps);
1289
1290         /* Restart auto-negotiation process in order to advertise my caps */
1291         smc_write_phy_register( PHY_CNTL_REG,
1292                 PHY_CNTL_ANEG_EN | PHY_CNTL_ANEG_RST );
1293
1294         /* Wait for the auto-negotiation to complete.  This may take from */
1295         /* 2 to 3 seconds. */
1296         /* Wait for the reset to complete, or time out */
1297         timeout = 20; /* Wait up to 10 seconds */
1298         while (timeout--)
1299                 {
1300                 status = smc_read_phy_register( PHY_STAT_REG);
1301                 if (status & PHY_STAT_ANEG_ACK)
1302                         {
1303                         /* auto-negotiate complete */
1304                         break;
1305                         }
1306
1307                 smc_wait_ms(500); /* wait 500 millisecs */
1308
1309                 /* Restart auto-negotiation if remote fault */
1310                 if (status & PHY_STAT_REM_FLT)
1311                         {
1312                         printf("%s:PHY remote fault detected\n", SMC_DEV_NAME);
1313
1314                         /* Restart auto-negotiation */
1315                         printf("%s:PHY restarting auto-negotiation\n",
1316                                 SMC_DEV_NAME);
1317                         smc_write_phy_register( PHY_CNTL_REG,
1318                                 PHY_CNTL_ANEG_EN | PHY_CNTL_ANEG_RST |
1319                                 PHY_CNTL_SPEED | PHY_CNTL_DPLX);
1320                         }
1321                 }
1322
1323         if (timeout < 1)
1324                 {
1325                 printf("%s:PHY auto-negotiate timed out\n",
1326                         SMC_DEV_NAME);
1327                 printf("%s:PHY auto-negotiate timed out\n", SMC_DEV_NAME);
1328                 failed = 1;
1329                 }
1330
1331         /* Fail if we detected an auto-negotiate remote fault */
1332         if (status & PHY_STAT_REM_FLT)
1333                 {
1334                 printf( "%s:PHY remote fault detected\n", SMC_DEV_NAME);
1335                 printf("%s:PHY remote fault detected\n", SMC_DEV_NAME);
1336                 failed = 1;
1337                 }
1338
1339         /* Re-Configure the Receive/Phy Control register */
1340         SMC_outw( RPC_DEFAULT, RPC_REG );
1341
1342   smc_phy_configure_exit:
1343
1344 }
1345 #endif /* !CONFIG_SMC91111_EXT_PHY */
1346
1347
1348 #if SMC_DEBUG > 2
1349 static void print_packet( byte * buf, int length )
1350 {
1351 #if 0
1352         int i;
1353         int remainder;
1354         int lines;
1355
1356         printf("Packet of length %d \n", length );
1357
1358 #if SMC_DEBUG > 3
1359         lines = length / 16;
1360         remainder = length % 16;
1361
1362         for ( i = 0; i < lines ; i ++ ) {
1363                 int cur;
1364
1365                 for ( cur = 0; cur < 8; cur ++ ) {
1366                         byte a, b;
1367
1368                         a = *(buf ++ );
1369                         b = *(buf ++ );
1370                         printf("%02x%02x ", a, b );
1371                 }
1372                 printf("\n");
1373         }
1374         for ( i = 0; i < remainder/2 ; i++ ) {
1375                 byte a, b;
1376
1377                 a = *(buf ++ );
1378                 b = *(buf ++ );
1379                 printf("%02x%02x ", a, b );
1380         }
1381         printf("\n");
1382 #endif
1383 #endif
1384 }
1385 #endif
1386
1387 int eth_init(bd_t *bd) {
1388         smc_open();
1389         return 0;
1390 }
1391
1392 void eth_halt() {
1393         smc_close();
1394 }
1395
1396 int eth_rx() {
1397         return smc_rcv();
1398 }
1399
1400 int eth_send(volatile void *packet, int length) {
1401         return smc_send_packet(packet, length);
1402 }
1403
1404 #endif /* CONFIG_DRIVER_SMC91111 */