]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/smc91111.c
* Patch by Rick Bronson, 16 Mar 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 /*
321  . Function: smc_reset( void )
322  . Purpose:
323  .      This sets the SMC91111 chip to its normal state, hopefully from whatever
324  .      mess that any other DOS driver has put it in.
325  .
326  . Maybe I should reset more registers to defaults in here?  SOFTRST  should
327  . do that for me.
328  .
329  . Method:
330  .      1.  send a SOFT RESET
331  .      2.  wait for it to finish
332  .      3.  enable autorelease mode
333  .      4.  reset the memory management unit
334  .      5.  clear all interrupts
335  .
336 */
337 static void smc_reset( void )
338 {
339         PRINTK2("%s:smc_reset\n", SMC_DEV_NAME);
340
341         /* This resets the registers mostly to defaults, but doesn't
342            affect EEPROM.  That seems unnecessary */
343         SMC_SELECT_BANK( 0 );
344         SMC_outw( RCR_SOFTRST, RCR_REG );
345
346         /* Setup the Configuration Register */
347         /* This is necessary because the CONFIG_REG is not affected */
348         /* by a soft reset */
349
350         SMC_SELECT_BANK( 1 );
351 #if defined(CONFIG_SMC91111_EXT_PHY)
352         SMC_outw( CONFIG_DEFAULT | CONFIG_EXT_PHY, CONFIG_REG);
353 #else
354         SMC_outw( CONFIG_DEFAULT, CONFIG_REG);
355 #endif
356
357
358         /* Release from possible power-down state */
359         /* Configuration register is not affected by Soft Reset */
360         SMC_outw( SMC_inw( CONFIG_REG ) | CONFIG_EPH_POWER_EN, CONFIG_REG  );
361
362         SMC_SELECT_BANK( 0 );
363
364         /* this should pause enough for the chip to be happy */
365         udelay(10);
366
367         /* Disable transmit and receive functionality */
368         SMC_outw( RCR_CLEAR, RCR_REG );
369         SMC_outw( TCR_CLEAR, TCR_REG );
370
371         /* set the control register */
372         SMC_SELECT_BANK( 1 );
373         SMC_outw( CTL_DEFAULT, CTL_REG );
374
375         /* Reset the MMU */
376         SMC_SELECT_BANK( 2 );
377         SMC_outw( MC_RESET, MMU_CMD_REG );
378         while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
379                 udelay(1); /* Wait until not busy */
380
381         /* Note:  It doesn't seem that waiting for the MMU busy is needed here,
382            but this is a place where future chipsets _COULD_ break.  Be wary
383            of issuing another MMU command right after this */
384
385         /* Disable all interrupts */
386         SMC_outb( 0, IM_REG );
387 }
388
389 /*
390  . Function: smc_enable
391  . Purpose: let the chip talk to the outside work
392  . Method:
393  .      1.  Enable the transmitter
394  .      2.  Enable the receiver
395  .      3.  Enable interrupts
396 */
397 static void smc_enable()
398 {
399         PRINTK2("%s:smc_enable\n", SMC_DEV_NAME);
400         SMC_SELECT_BANK( 0 );
401         /* see the header file for options in TCR/RCR DEFAULT*/
402         SMC_outw( TCR_DEFAULT, TCR_REG );
403         SMC_outw( RCR_DEFAULT, RCR_REG );
404
405         /* clear MII_DIS */
406 /*      smc_write_phy_register(PHY_CNTL_REG, 0x0000); */
407 }
408
409 /*
410  . Function: smc_shutdown
411  . Purpose:  closes down the SMC91xxx chip.
412  . Method:
413  .      1. zero the interrupt mask
414  .      2. clear the enable receive flag
415  .      3. clear the enable xmit flags
416  .
417  . TODO:
418  .   (1) maybe utilize power down mode.
419  .      Why not yet?  Because while the chip will go into power down mode,
420  .      the manual says that it will wake up in response to any I/O requests
421  .      in the register space.   Empirical results do not show this working.
422 */
423 static void smc_shutdown()
424 {
425         PRINTK2(CARDNAME ":smc_shutdown\n");
426
427         /* no more interrupts for me */
428         SMC_SELECT_BANK( 2 );
429         SMC_outb( 0, IM_REG );
430
431         /* and tell the card to stay away from that nasty outside world */
432         SMC_SELECT_BANK( 0 );
433         SMC_outb( RCR_CLEAR, RCR_REG );
434         SMC_outb( TCR_CLEAR, TCR_REG );
435 }
436
437
438 /*
439  . Function:  smc_hardware_send_packet(struct net_device * )
440  . Purpose:
441  .      This sends the actual packet to the SMC9xxx chip.
442  .
443  . Algorithm:
444  .      First, see if a saved_skb is available.
445  .              ( this should NOT be called if there is no 'saved_skb'
446  .      Now, find the packet number that the chip allocated
447  .      Point the data pointers at it in memory
448  .      Set the length word in the chip's memory
449  .      Dump the packet to chip memory
450  .      Check if a last byte is needed ( odd length packet )
451  .              if so, set the control flag right
452  .      Tell the card to send it
453  .      Enable the transmit interrupt, so I know if it failed
454  .      Free the kernel data if I actually sent it.
455 */
456 static int smc_send_packet(volatile void *packet, int packet_length)
457 {
458         byte                    packet_no;
459         unsigned long           ioaddr;
460         byte                    * buf;
461         int                     length;
462         int                     numPages;
463         int                     try = 0;
464         int                     time_out;
465         byte                    status;
466
467
468         PRINTK3("%s:smc_hardware_send_packet\n", SMC_DEV_NAME);
469
470         length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
471
472         /* allocate memory
473         ** The MMU wants the number of pages to be the number of 256 bytes
474         ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
475         **
476         ** The 91C111 ignores the size bits, but the code is left intact
477         ** for backwards and future compatibility.
478         **
479         ** Pkt size for allocating is data length +6 (for additional status
480         ** words, length and ctl!)
481         **
482         ** If odd size then last byte is included in this header.
483         */
484         numPages =   ((length & 0xfffe) + 6);
485         numPages >>= 8; /* Divide by 256 */
486
487         if (numPages > 7 ) {
488                 printf("%s: Far too big packet error. \n", SMC_DEV_NAME);
489                 return 0;
490         }
491
492         /* now, try to allocate the memory */
493         SMC_SELECT_BANK( 2 );
494         SMC_outw( MC_ALLOC | numPages, MMU_CMD_REG );
495
496         /* FIXME: the ALLOC_INT bit never gets set *
497          * so the following will always give a     *
498          * memory allocation error.                *
499          * same code works in armboot though       *
500          * -ro
501          */
502
503 again:
504         try++;
505         time_out = MEMORY_WAIT_TIME;
506         do {
507                 status = SMC_inb( SMC91111_INT_REG );
508                 if ( status & IM_ALLOC_INT ) {
509                         /* acknowledge the interrupt */
510                         SMC_outb( IM_ALLOC_INT, SMC91111_INT_REG );
511                         break;
512                 }
513         } while ( -- time_out );
514
515         if ( !time_out ) {
516                         PRINTK2("%s: memory allocation, try %d failed ...\n",
517                                 SMC_DEV_NAME, try);
518                         if (try < SMC_ALLOC_MAX_TRY)
519                                 goto again;
520                         else
521                                 return 0;
522         }
523
524         PRINTK2("%s: memory allocation, try %d succeeded ...\n",
525                 SMC_DEV_NAME,
526                 try);
527
528         /* I can send the packet now.. */
529
530         ioaddr = SMC_BASE_ADDRESS;
531
532         buf = (byte *)packet;
533
534         /* If I get here, I _know_ there is a packet slot waiting for me */
535         packet_no = SMC_inb( AR_REG );
536         if ( packet_no & AR_FAILED ) {
537                 /* or isn't there?  BAD CHIP! */
538                 printf("%s: Memory allocation failed. \n",
539                         SMC_DEV_NAME);
540                 return 0;
541         }
542
543         /* we have a packet address, so tell the card to use it */
544         SMC_outb( packet_no, PN_REG );
545
546         /* point to the beginning of the packet */
547         SMC_outw( PTR_AUTOINC , PTR_REG );
548
549         PRINTK3("%s: Trying to xmit packet of length %x\n",
550                 SMC_DEV_NAME, length);
551
552 #if SMC_DEBUG > 2
553         printf("Transmitting Packet\n");
554         print_packet( buf, length );
555 #endif
556
557         /* send the packet length ( +6 for status, length and ctl byte )
558            and the status word ( set to zeros ) */
559 #ifdef USE_32_BIT
560         SMC_outl(  (length +6 ) << 16 , SMC91111_DATA_REG );
561 #else
562         SMC_outw( 0, SMC91111_DATA_REG );
563         /* send the packet length ( +6 for status words, length, and ctl*/
564         SMC_outw( (length+6), SMC91111_DATA_REG );
565 #endif
566
567         /* send the actual data
568          . I _think_ it's faster to send the longs first, and then
569          . mop up by sending the last word.  It depends heavily
570          . on alignment, at least on the 486.  Maybe it would be
571          . a good idea to check which is optimal?  But that could take
572          . almost as much time as is saved?
573         */
574 #ifdef USE_32_BIT
575         SMC_outsl(SMC91111_DATA_REG, buf,  length >> 2 );
576         if ( length & 0x2  )
577                 SMC_outw(*((word *)(buf + (length & 0xFFFFFFFC))), SMC91111_DATA_REG);
578 #else
579         SMC_outsw(SMC91111_DATA_REG , buf, (length ) >> 1);
580 #endif /* USE_32_BIT */
581
582         /* Send the last byte, if there is one.   */
583         if ( (length & 1) == 0 ) {
584                 SMC_outw( 0, SMC91111_DATA_REG );
585         } else {
586                 SMC_outw( buf[length -1 ] | 0x2000, SMC91111_DATA_REG );
587         }
588
589         /* and let the chipset deal with it */
590         SMC_outw( MC_ENQUEUE , MMU_CMD_REG );
591
592         /* poll for TX INT */
593         if (poll4int(IM_TX_INT, SMC_TX_TIMEOUT)) {
594                 /* sending failed */
595                 PRINTK2("%s: TX timeout, sending failed...\n",
596                         SMC_DEV_NAME);
597
598                 /* release packet */
599                 SMC_outw(MC_FREEPKT, MMU_CMD_REG);
600
601                 /* wait for MMU getting ready (low) */
602                 while (SMC_inw(MMU_CMD_REG) & MC_BUSY)
603                 {
604                         udelay(10);
605                 }
606
607                 PRINTK2("MMU ready\n");
608
609
610                 return 0;
611         } else {
612                 /* ack. int */
613                 SMC_outw(IM_TX_INT, SMC91111_INT_REG);
614                 PRINTK2("%s: Sent packet of length %d \n", SMC_DEV_NAME, length);
615
616                 /* release packet */
617                 SMC_outw(MC_FREEPKT, MMU_CMD_REG);
618
619                 /* wait for MMU getting ready (low) */
620                 while (SMC_inw(MMU_CMD_REG) & MC_BUSY)
621                 {
622                         udelay(10);
623                 }
624
625                 PRINTK2("MMU ready\n");
626
627
628         }
629
630         return length;
631 }
632
633 /*-------------------------------------------------------------------------
634  |
635  | smc_destructor( struct net_device * dev )
636  |   Input parameters:
637  |      dev, pointer to the device structure
638  |
639  |   Output:
640  |      None.
641  |
642  ---------------------------------------------------------------------------
643 */
644 void smc_destructor()
645 {
646         PRINTK2(CARDNAME ":smc_destructor\n");
647 }
648
649
650 /*
651  * Open and Initialize the board
652  *
653  * Set up everything, reset the card, etc ..
654  *
655  */
656 static int smc_open()
657 {
658         int     i;      /* used to set hw ethernet address */
659
660         PRINTK2("%s:smc_open\n", SMC_DEV_NAME);
661
662         /* reset the hardware */
663
664         smc_reset();
665         smc_enable();
666
667         /* Configure the PHY */
668 #ifndef CONFIG_SMC91111_EXT_PHY
669         smc_phy_configure();
670 #endif
671
672
673         /* conservative setting (10Mbps, HalfDuplex, no AutoNeg.) */
674 /*      SMC_SELECT_BANK(0); */
675 /*      SMC_outw(0, RPC_REG); */
676
677 #ifdef USE_32_BIT
678         for ( i = 0; i < 6; i += 2 ) {
679                 word address;
680
681                 address = smc_mac_addr[ i + 1 ] << 8 ;
682                 address  |= smc_mac_addr[ i ];
683                 SMC_outw( address, ADDR0_REG + i );
684         }
685 #else
686         for ( i = 0; i < 6; i ++ )
687                 SMC_outb( smc_mac_addr[i], ADDR0_REG + i );
688 #endif
689
690         return 0;
691 }
692
693 #if 0 /* dead code? -- wd */
694 #ifdef USE_32_BIT
695 void
696 insl32(r,b,l)
697 {
698    int __i ;
699    dword *__b2;
700
701         __b2 = (dword *) b;
702         for (__i = 0; __i < l; __i++) {
703                   *(__b2 + __i) = *(dword *)(r+0x10000300);
704         }
705 }
706 #endif
707 #endif
708
709 /*-------------------------------------------------------------
710  .
711  . smc_rcv -  receive a packet from the card
712  .
713  . There is ( at least ) a packet waiting to be read from
714  . chip-memory.
715  .
716  . o Read the status
717  . o If an error, record it
718  . o otherwise, read in the packet
719  --------------------------------------------------------------
720 */
721 static int smc_rcv()
722 {
723         int     packet_number;
724         word    status;
725         word    packet_length;
726         int     is_error = 0;
727 #ifdef USE_32_BIT
728         dword stat_len;
729 #endif
730
731
732         SMC_SELECT_BANK(2);
733         packet_number = SMC_inw( RXFIFO_REG );
734
735         if ( packet_number & RXFIFO_REMPTY ) {
736
737                 return 0;
738         }
739
740         PRINTK3("%s:smc_rcv\n", SMC_DEV_NAME);
741         /*  start reading from the start of the packet */
742         SMC_outw( PTR_READ | PTR_RCV | PTR_AUTOINC, PTR_REG );
743
744         /* First two words are status and packet_length */
745 #ifdef USE_32_BIT
746         stat_len = SMC_inl(SMC91111_DATA_REG);
747         status = stat_len & 0xffff;
748         packet_length = stat_len >> 16;
749 #else
750         status          = SMC_inw( SMC91111_DATA_REG );
751         packet_length   = SMC_inw( SMC91111_DATA_REG );
752 #endif
753
754         packet_length &= 0x07ff;  /* mask off top bits */
755
756         PRINTK2("RCV: STATUS %4x LENGTH %4x\n", status, packet_length );
757
758         if ( !(status & RS_ERRORS ) ){
759                 /* Adjust for having already read the first two words */
760                 packet_length -= 4; /*4; */
761
762
763
764                 /* set odd length for bug in LAN91C111, */
765                 /* which never sets RS_ODDFRAME */
766                 /* TODO ? */
767
768
769 #ifdef USE_32_BIT
770                 PRINTK3(" Reading %d dwords (and %d bytes) \n",
771                         packet_length >> 2, packet_length & 3 );
772                 /* QUESTION:  Like in the TX routine, do I want
773                    to send the DWORDs or the bytes first, or some
774                    mixture.  A mixture might improve already slow PIO
775                    performance  */
776                 SMC_insl( SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 2 );
777                 /* read the left over bytes */
778                 if (packet_length & 3) {
779                         int i;
780
781                         byte *tail = (byte *)(NetRxPackets[0] + (packet_length & ~3));
782                         dword leftover = SMC_inl(SMC91111_DATA_REG);
783                         for (i=0; i<(packet_length & 3); i++)
784                                 *tail++ = (byte) (leftover >> (8*i)) & 0xff;
785                 }
786 #else
787                 PRINTK3(" Reading %d words and %d byte(s) \n",
788                         (packet_length >> 1 ), packet_length & 1 );
789                 SMC_insw(SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 1);
790
791 #endif /* USE_32_BIT */
792
793 #if     SMC_DEBUG > 2
794                 printf("Receiving Packet\n");
795                 print_packet( NetRxPackets[0], packet_length );
796 #endif
797         } else {
798                 /* error ... */
799                 /* TODO ? */
800                 is_error = 1;
801         }
802
803         while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
804                 udelay(1); /* Wait until not busy */
805
806         /*  error or good, tell the card to get rid of this packet */
807         SMC_outw( MC_RELEASE, MMU_CMD_REG );
808
809         while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
810                 udelay(1); /* Wait until not busy */
811
812         if (!is_error) {
813                 /* Pass the packet up to the protocol layers. */
814                 NetReceive(NetRxPackets[0], packet_length);
815                 return packet_length;
816         } else {
817                 return 0;
818         }
819
820 }
821
822
823
824 /*----------------------------------------------------
825  . smc_close
826  .
827  . this makes the board clean up everything that it can
828  . and not talk to the outside world.   Caused by
829  . an 'ifconfig ethX down'
830  .
831  -----------------------------------------------------*/
832 static int smc_close()
833 {
834         PRINTK2("%s:smc_close\n", SMC_DEV_NAME);
835
836         /* clear everything */
837         smc_shutdown();
838
839         return 0;
840 }
841
842
843 #if 0
844 /*------------------------------------------------------------
845  . Modify a bit in the LAN91C111 register set
846  .-------------------------------------------------------------*/
847 static word smc_modify_regbit(int bank, int ioaddr, int reg,
848         unsigned int bit, int val)
849 {
850         word regval;
851
852         SMC_SELECT_BANK( bank );
853
854         regval = SMC_inw( reg );
855         if (val)
856                 regval |= bit;
857         else
858                 regval &= ~bit;
859
860         SMC_outw( regval, 0 );
861         return(regval);
862 }
863
864
865 /*------------------------------------------------------------
866  . Retrieve a bit in the LAN91C111 register set
867  .-------------------------------------------------------------*/
868 static int smc_get_regbit(int bank, int ioaddr, int reg, unsigned int bit)
869 {
870         SMC_SELECT_BANK( bank );
871         if ( SMC_inw( reg ) & bit)
872                 return(1);
873         else
874                 return(0);
875 }
876
877
878 /*------------------------------------------------------------
879  . Modify a LAN91C111 register (word access only)
880  .-------------------------------------------------------------*/
881 static void smc_modify_reg(int bank, int ioaddr, int reg, word val)
882 {
883         SMC_SELECT_BANK( bank );
884         SMC_outw( val, reg );
885 }
886
887
888 /*------------------------------------------------------------
889  . Retrieve a LAN91C111 register (word access only)
890  .-------------------------------------------------------------*/
891 static int smc_get_reg(int bank, int ioaddr, int reg)
892 {
893         SMC_SELECT_BANK( bank );
894         return(SMC_inw( reg ));
895 }
896
897 #endif /* 0 */
898
899 /*---PHY CONTROL AND CONFIGURATION----------------------------------------- */
900
901 #if (SMC_DEBUG > 2 )
902
903 /*------------------------------------------------------------
904  . Debugging function for viewing MII Management serial bitstream
905  .-------------------------------------------------------------*/
906 static void smc_dump_mii_stream(byte* bits, int size)
907 {
908         int i;
909
910         printf("BIT#:");
911         for (i = 0; i < size; ++i)
912                 {
913                 printf("%d", i%10);
914                 }
915
916         printf("\nMDOE:");
917         for (i = 0; i < size; ++i)
918                 {
919                 if (bits[i] & MII_MDOE)
920                         printf("1");
921                 else
922                         printf("0");
923                 }
924
925         printf("\nMDO :");
926         for (i = 0; i < size; ++i)
927                 {
928                 if (bits[i] & MII_MDO)
929                         printf("1");
930                 else
931                         printf("0");
932                 }
933
934         printf("\nMDI :");
935         for (i = 0; i < size; ++i)
936                 {
937                 if (bits[i] & MII_MDI)
938                         printf("1");
939                 else
940                         printf("0");
941                 }
942
943         printf("\n");
944 }
945 #endif
946
947 /*------------------------------------------------------------
948  . Reads a register from the MII Management serial interface
949  .-------------------------------------------------------------*/
950 #ifndef CONFIG_SMC91111_EXT_PHY
951 static word smc_read_phy_register(byte phyreg)
952 {
953         int oldBank;
954         int i;
955         byte mask;
956         word mii_reg;
957         byte bits[64];
958         int clk_idx = 0;
959         int input_idx;
960         word phydata;
961         byte phyaddr = SMC_PHY_ADDR;
962
963         /* 32 consecutive ones on MDO to establish sync */
964         for (i = 0; i < 32; ++i)
965                 bits[clk_idx++] = MII_MDOE | MII_MDO;
966
967         /* Start code <01> */
968         bits[clk_idx++] = MII_MDOE;
969         bits[clk_idx++] = MII_MDOE | MII_MDO;
970
971         /* Read command <10> */
972         bits[clk_idx++] = MII_MDOE | MII_MDO;
973         bits[clk_idx++] = MII_MDOE;
974
975         /* Output the PHY address, msb first */
976         mask = (byte)0x10;
977         for (i = 0; i < 5; ++i)
978                 {
979                 if (phyaddr & mask)
980                         bits[clk_idx++] = MII_MDOE | MII_MDO;
981                 else
982                         bits[clk_idx++] = MII_MDOE;
983
984                 /* Shift to next lowest bit */
985                 mask >>= 1;
986                 }
987
988         /* Output the phy register number, msb first */
989         mask = (byte)0x10;
990         for (i = 0; i < 5; ++i)
991                 {
992                 if (phyreg & mask)
993                         bits[clk_idx++] = MII_MDOE | MII_MDO;
994                 else
995                         bits[clk_idx++] = MII_MDOE;
996
997                 /* Shift to next lowest bit */
998                 mask >>= 1;
999                 }
1000
1001         /* Tristate and turnaround (2 bit times) */
1002         bits[clk_idx++] = 0;
1003         /*bits[clk_idx++] = 0; */
1004
1005         /* Input starts at this bit time */
1006         input_idx = clk_idx;
1007
1008         /* Will input 16 bits */
1009         for (i = 0; i < 16; ++i)
1010                 bits[clk_idx++] = 0;
1011
1012         /* Final clock bit */
1013         bits[clk_idx++] = 0;
1014
1015         /* Save the current bank */
1016         oldBank = SMC_inw( BANK_SELECT );
1017
1018         /* Select bank 3 */
1019         SMC_SELECT_BANK( 3 );
1020
1021         /* Get the current MII register value */
1022         mii_reg = SMC_inw( MII_REG );
1023
1024         /* Turn off all MII Interface bits */
1025         mii_reg &= ~(MII_MDOE|MII_MCLK|MII_MDI|MII_MDO);
1026
1027         /* Clock all 64 cycles */
1028         for (i = 0; i < sizeof bits; ++i)
1029                 {
1030                 /* Clock Low - output data */
1031                 SMC_outw( mii_reg | bits[i], MII_REG );
1032                 udelay(SMC_PHY_CLOCK_DELAY);
1033
1034
1035                 /* Clock Hi - input data */
1036                 SMC_outw( mii_reg | bits[i] | MII_MCLK, MII_REG );
1037                 udelay(SMC_PHY_CLOCK_DELAY);
1038                 bits[i] |= SMC_inw( MII_REG ) & MII_MDI;
1039                 }
1040
1041         /* Return to idle state */
1042         /* Set clock to low, data to low, and output tristated */
1043         SMC_outw( mii_reg, MII_REG );
1044         udelay(SMC_PHY_CLOCK_DELAY);
1045
1046         /* Restore original bank select */
1047         SMC_SELECT_BANK( oldBank );
1048
1049         /* Recover input data */
1050         phydata = 0;
1051         for (i = 0; i < 16; ++i)
1052                 {
1053                 phydata <<= 1;
1054
1055                 if (bits[input_idx++] & MII_MDI)
1056                         phydata |= 0x0001;
1057                 }
1058
1059 #if (SMC_DEBUG > 2 )
1060         printf("smc_read_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
1061                 phyaddr, phyreg, phydata);
1062         smc_dump_mii_stream(bits, sizeof bits);
1063 #endif
1064
1065         return(phydata);
1066 }
1067
1068
1069 /*------------------------------------------------------------
1070  . Writes a register to the MII Management serial interface
1071  .-------------------------------------------------------------*/
1072 static void smc_write_phy_register(byte phyreg, word phydata)
1073 {
1074         int oldBank;
1075         int i;
1076         word mask;
1077         word mii_reg;
1078         byte bits[65];
1079         int clk_idx = 0;
1080         byte phyaddr = SMC_PHY_ADDR;
1081
1082         /* 32 consecutive ones on MDO to establish sync */
1083         for (i = 0; i < 32; ++i)
1084                 bits[clk_idx++] = MII_MDOE | MII_MDO;
1085
1086         /* Start code <01> */
1087         bits[clk_idx++] = MII_MDOE;
1088         bits[clk_idx++] = MII_MDOE | MII_MDO;
1089
1090         /* Write command <01> */
1091         bits[clk_idx++] = MII_MDOE;
1092         bits[clk_idx++] = MII_MDOE | MII_MDO;
1093
1094         /* Output the PHY address, msb first */
1095         mask = (byte)0x10;
1096         for (i = 0; i < 5; ++i)
1097                 {
1098                 if (phyaddr & mask)
1099                         bits[clk_idx++] = MII_MDOE | MII_MDO;
1100                 else
1101                         bits[clk_idx++] = MII_MDOE;
1102
1103                 /* Shift to next lowest bit */
1104                 mask >>= 1;
1105                 }
1106
1107         /* Output the phy register number, msb first */
1108         mask = (byte)0x10;
1109         for (i = 0; i < 5; ++i)
1110                 {
1111                 if (phyreg & mask)
1112                         bits[clk_idx++] = MII_MDOE | MII_MDO;
1113                 else
1114                         bits[clk_idx++] = MII_MDOE;
1115
1116                 /* Shift to next lowest bit */
1117                 mask >>= 1;
1118                 }
1119
1120         /* Tristate and turnaround (2 bit times) */
1121         bits[clk_idx++] = 0;
1122         bits[clk_idx++] = 0;
1123
1124         /* Write out 16 bits of data, msb first */
1125         mask = 0x8000;
1126         for (i = 0; i < 16; ++i)
1127                 {
1128                 if (phydata & mask)
1129                         bits[clk_idx++] = MII_MDOE | MII_MDO;
1130                 else
1131                         bits[clk_idx++] = MII_MDOE;
1132
1133                 /* Shift to next lowest bit */
1134                 mask >>= 1;
1135                 }
1136
1137         /* Final clock bit (tristate) */
1138         bits[clk_idx++] = 0;
1139
1140         /* Save the current bank */
1141         oldBank = SMC_inw( BANK_SELECT );
1142
1143         /* Select bank 3 */
1144         SMC_SELECT_BANK( 3 );
1145
1146         /* Get the current MII register value */
1147         mii_reg = SMC_inw( MII_REG );
1148
1149         /* Turn off all MII Interface bits */
1150         mii_reg &= ~(MII_MDOE|MII_MCLK|MII_MDI|MII_MDO);
1151
1152         /* Clock all cycles */
1153         for (i = 0; i < sizeof bits; ++i)
1154                 {
1155                 /* Clock Low - output data */
1156                 SMC_outw( mii_reg | bits[i], MII_REG );
1157                 udelay(SMC_PHY_CLOCK_DELAY);
1158
1159
1160                 /* Clock Hi - input data */
1161                 SMC_outw( mii_reg | bits[i] | MII_MCLK, MII_REG );
1162                 udelay(SMC_PHY_CLOCK_DELAY);
1163                 bits[i] |= SMC_inw( MII_REG ) & MII_MDI;
1164                 }
1165
1166         /* Return to idle state */
1167         /* Set clock to low, data to low, and output tristated */
1168         SMC_outw( mii_reg, MII_REG );
1169         udelay(SMC_PHY_CLOCK_DELAY);
1170
1171         /* Restore original bank select */
1172         SMC_SELECT_BANK( oldBank );
1173
1174 #if (SMC_DEBUG > 2 )
1175         printf("smc_write_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
1176                 phyaddr, phyreg, phydata);
1177         smc_dump_mii_stream(bits, sizeof bits);
1178 #endif
1179 }
1180 #endif /* !CONFIG_SMC91111_EXT_PHY */
1181
1182
1183
1184 /*------------------------------------------------------------
1185  . Waits the specified number of milliseconds - kernel friendly
1186  .-------------------------------------------------------------*/
1187 #ifndef CONFIG_SMC91111_EXT_PHY
1188 static void smc_wait_ms(unsigned int ms)
1189 {
1190         udelay(ms*1000);
1191 }
1192 #endif /* !CONFIG_SMC91111_EXT_PHY */
1193
1194
1195
1196 /*------------------------------------------------------------
1197  . Configures the specified PHY using Autonegotiation. Calls
1198  . smc_phy_fixed() if the user has requested a certain config.
1199  .-------------------------------------------------------------*/
1200 #ifndef CONFIG_SMC91111_EXT_PHY
1201 static void smc_phy_configure()
1202 {
1203         int timeout;
1204         byte phyaddr;
1205         word my_phy_caps; /* My PHY capabilities */
1206         word my_ad_caps;  /* My Advertised capabilities */
1207         word status = 0;  /*;my status = 0 */
1208         int failed = 0;
1209
1210         PRINTK3("%s:smc_program_phy()\n", SMC_DEV_NAME);
1211
1212
1213
1214         /* Get the detected phy address */
1215         phyaddr = SMC_PHY_ADDR;
1216
1217         /* Reset the PHY, setting all other bits to zero */
1218         smc_write_phy_register(PHY_CNTL_REG, PHY_CNTL_RST);
1219
1220         /* Wait for the reset to complete, or time out */
1221         timeout = 6; /* Wait up to 3 seconds */
1222         while (timeout--)
1223                 {
1224                 if (!(smc_read_phy_register(PHY_CNTL_REG)
1225                     & PHY_CNTL_RST))
1226                         {
1227                         /* reset complete */
1228                         break;
1229                         }
1230
1231                 smc_wait_ms(500); /* wait 500 millisecs */
1232                 }
1233
1234         if (timeout < 1)
1235                 {
1236                 printf("%s:PHY reset timed out\n", SMC_DEV_NAME);
1237                 goto smc_phy_configure_exit;
1238                 }
1239
1240         /* Read PHY Register 18, Status Output */
1241         /* lp->lastPhy18 = smc_read_phy_register(PHY_INT_REG); */
1242
1243         /* Enable PHY Interrupts (for register 18) */
1244         /* Interrupts listed here are disabled */
1245         smc_write_phy_register(PHY_INT_REG, 0xffff);
1246
1247         /* Configure the Receive/Phy Control register */
1248         SMC_SELECT_BANK( 0 );
1249         SMC_outw( RPC_DEFAULT, RPC_REG );
1250
1251         /* Copy our capabilities from PHY_STAT_REG to PHY_AD_REG */
1252         my_phy_caps = smc_read_phy_register(PHY_STAT_REG);
1253         my_ad_caps  = PHY_AD_CSMA; /* I am CSMA capable */
1254
1255         if (my_phy_caps & PHY_STAT_CAP_T4)
1256                 my_ad_caps |= PHY_AD_T4;
1257
1258         if (my_phy_caps & PHY_STAT_CAP_TXF)
1259                 my_ad_caps |= PHY_AD_TX_FDX;
1260
1261         if (my_phy_caps & PHY_STAT_CAP_TXH)
1262                 my_ad_caps |= PHY_AD_TX_HDX;
1263
1264         if (my_phy_caps & PHY_STAT_CAP_TF)
1265                 my_ad_caps |= PHY_AD_10_FDX;
1266
1267         if (my_phy_caps & PHY_STAT_CAP_TH)
1268                 my_ad_caps |= PHY_AD_10_HDX;
1269
1270         /* Update our Auto-Neg Advertisement Register */
1271         smc_write_phy_register( PHY_AD_REG, my_ad_caps);
1272
1273         PRINTK2("%s:phy caps=%x\n", SMC_DEV_NAME, my_phy_caps);
1274         PRINTK2("%s:phy advertised caps=%x\n", SMC_DEV_NAME, my_ad_caps);
1275
1276         /* Restart auto-negotiation process in order to advertise my caps */
1277         smc_write_phy_register( PHY_CNTL_REG,
1278                 PHY_CNTL_ANEG_EN | PHY_CNTL_ANEG_RST );
1279
1280         /* Wait for the auto-negotiation to complete.  This may take from */
1281         /* 2 to 3 seconds. */
1282         /* Wait for the reset to complete, or time out */
1283         timeout = 20; /* Wait up to 10 seconds */
1284         while (timeout--)
1285                 {
1286                 status = smc_read_phy_register( PHY_STAT_REG);
1287                 if (status & PHY_STAT_ANEG_ACK)
1288                         {
1289                         /* auto-negotiate complete */
1290                         break;
1291                         }
1292
1293                 smc_wait_ms(500); /* wait 500 millisecs */
1294
1295                 /* Restart auto-negotiation if remote fault */
1296                 if (status & PHY_STAT_REM_FLT)
1297                         {
1298                         printf("%s:PHY remote fault detected\n", SMC_DEV_NAME);
1299
1300                         /* Restart auto-negotiation */
1301                         printf("%s:PHY restarting auto-negotiation\n",
1302                                 SMC_DEV_NAME);
1303                         smc_write_phy_register( PHY_CNTL_REG,
1304                                 PHY_CNTL_ANEG_EN | PHY_CNTL_ANEG_RST |
1305                                 PHY_CNTL_SPEED | PHY_CNTL_DPLX);
1306                         }
1307                 }
1308
1309         if (timeout < 1)
1310                 {
1311                 printf("%s:PHY auto-negotiate timed out\n",
1312                         SMC_DEV_NAME);
1313                 printf("%s:PHY auto-negotiate timed out\n", SMC_DEV_NAME);
1314                 failed = 1;
1315                 }
1316
1317         /* Fail if we detected an auto-negotiate remote fault */
1318         if (status & PHY_STAT_REM_FLT)
1319                 {
1320                 printf( "%s:PHY remote fault detected\n", SMC_DEV_NAME);
1321                 printf("%s:PHY remote fault detected\n", SMC_DEV_NAME);
1322                 failed = 1;
1323                 }
1324
1325         /* Re-Configure the Receive/Phy Control register */
1326         SMC_outw( RPC_DEFAULT, RPC_REG );
1327
1328   smc_phy_configure_exit:
1329
1330 }
1331 #endif /* !CONFIG_SMC91111_EXT_PHY */
1332
1333
1334 #if SMC_DEBUG > 2
1335 static void print_packet( byte * buf, int length )
1336 {
1337 #if 0
1338         int i;
1339         int remainder;
1340         int lines;
1341
1342         printf("Packet of length %d \n", length );
1343
1344 #if SMC_DEBUG > 3
1345         lines = length / 16;
1346         remainder = length % 16;
1347
1348         for ( i = 0; i < lines ; i ++ ) {
1349                 int cur;
1350
1351                 for ( cur = 0; cur < 8; cur ++ ) {
1352                         byte a, b;
1353
1354                         a = *(buf ++ );
1355                         b = *(buf ++ );
1356                         printf("%02x%02x ", a, b );
1357                 }
1358                 printf("\n");
1359         }
1360         for ( i = 0; i < remainder/2 ; i++ ) {
1361                 byte a, b;
1362
1363                 a = *(buf ++ );
1364                 b = *(buf ++ );
1365                 printf("%02x%02x ", a, b );
1366         }
1367         printf("\n");
1368 #endif
1369 #endif
1370 }
1371 #endif
1372
1373 int eth_init(bd_t *bd) {
1374         smc_open();
1375         return 0;
1376 }
1377
1378 void eth_halt() {
1379         smc_close();
1380 }
1381
1382 int eth_rx() {
1383         return smc_rcv();
1384 }
1385
1386 int eth_send(volatile void *packet, int length) {
1387         return smc_send_packet(packet, length);
1388 }
1389
1390 #endif /* CONFIG_DRIVER_SMC91111 */