1 /*------------------------------------------------------------------------
3 . This is a driver for SMSC's 91C111 single-chip Ethernet device.
6 . Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 . Rolf Offermanns <rof@sysgo.de>
9 . Copyright (C) 2001 Standard Microsystems Corporation (SMSC)
10 . Developed by Simple Network Magic Corporation (SNMC)
11 . Copyright (C) 1996 by Erik Stahlman (ES)
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.
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.
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
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.
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
39 . io = for the base address
43 . Erik Stahlman ( erik@vt.edu )
44 . Daris A Nevil ( dnevil@snmc.com )
47 . Hardware multicast code from Peter Cammaert ( pc@denkart.be )
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 )
55 . 06/19/03 Richard Woodruff Made u-boot environment aware and added mac addr checks.
56 . 10/17/01 Marco Hasewinkel Modify for DNP/1110
57 . 07/25/01 Woojung Huh Modify for ADS Bitsy
58 . 04/25/01 Daris A Nevil Initial public release through SMSC
59 . 03/16/01 Daris A Nevil Modified smc9194.c for use with LAN91C111
60 ----------------------------------------------------------------------------*/
68 /* Use power-down feature of the chip */
76 static const char version[] =
77 "smc91111.c:v1.0 04/25/01 by Daris A Nevil (dnevil@snmc.com)\n";
80 /* Autonegotiation timeout in seconds */
81 #ifndef CONFIG_SMC_AUTONEG_TIMEOUT
82 #define CONFIG_SMC_AUTONEG_TIMEOUT 10
85 /*------------------------------------------------------------------------
87 . Configuration options, for the experienced user to change.
89 -------------------------------------------------------------------------*/
92 . Wait time for memory to be free. This probably shouldn't be
93 . tuned that much, as waiting for this means nothing else happens
96 #define MEMORY_WAIT_TIME 16
100 #define PRINTK3(args...) printf(args)
102 #define PRINTK3(args...)
106 #define PRINTK2(args...) printf(args)
108 #define PRINTK2(args...)
112 #define PRINTK(args...) printf(args)
114 #define PRINTK(args...)
118 /*------------------------------------------------------------------------
120 . The internal workings of the driver. If you are changing anything
121 . here with the SMC stuff, you should have the datasheet and know
122 . what you are doing.
124 -------------------------------------------------------------------------*/
125 #define CARDNAME "LAN91C111"
127 /* Memory sizing constant */
128 #define LAN91C111_MEMORY_MULTIPLIER (1024*2)
130 #ifndef CONFIG_SMC91111_BASE
131 #define CONFIG_SMC91111_BASE 0x20000300
134 #define SMC_BASE_ADDRESS CONFIG_SMC91111_BASE
136 #define SMC_DEV_NAME "SMC91111"
137 #define SMC_PHY_ADDR 0x0000
138 #define SMC_ALLOC_MAX_TRY 5
139 #define SMC_TX_TIMEOUT 30
141 #define SMC_PHY_CLOCK_DELAY 1000
145 #ifdef CONFIG_SMC_USE_32_BIT
150 /*-----------------------------------------------------------------
152 . The driver can be entered at any of the following entry points.
154 .------------------------------------------------------------------ */
156 extern int eth_init(bd_t *bd);
157 extern void eth_halt(void);
158 extern int eth_rx(void);
159 extern int eth_send(volatile void *packet, int length);
161 #ifdef SHARED_RESOURCES
162 extern void swap_to(int device_id);
166 . This is called by register_netdev(). It is responsible for
167 . checking the portlist for the SMC9000 series chipset. If it finds
168 . one, then it will initialize the device, find the hardware information,
169 . and sets up the appropriate device parameters.
170 . NOTE: Interrupts are *OFF* when this procedure is called.
172 . NB:This shouldn't be static since it is referred to externally.
177 . This is called by unregister_netdev(). It is responsible for
178 . cleaning up before the driver is finally unregistered and discarded.
180 void smc_destructor(void);
183 . The kernel calls this function when someone wants to use the device,
184 . typically 'ifconfig ethX up'.
186 static int smc_open(bd_t *bd);
190 . This is called by the kernel in response to 'ifconfig ethX down'. It
191 . is responsible for cleaning up everything that the open routine
192 . does, and maybe putting the card into a powerdown state.
194 static int smc_close(void);
197 . Configures the PHY through the MII Management interface
199 #ifndef CONFIG_SMC91111_EXT_PHY
200 static void smc_phy_configure(void);
201 #endif /* !CONFIG_SMC91111_EXT_PHY */
204 . This is a separate procedure to handle the receipt of a packet, to
205 . leave the interrupt code looking slightly cleaner
207 static int smc_rcv(void);
209 /* See if a MAC address is defined in the current environment. If so use it. If not
210 . print a warning and set the environment and other globals with the default.
211 . If an EEPROM is present it really should be consulted.
213 int smc_get_ethaddr(bd_t *bd);
214 int get_rom_mac(uchar *v_rom_mac);
217 ------------------------------------------------------------
221 ------------------------------------------------------------
224 #ifdef CONFIG_SMC_USE_IOFUNCS
226 * input and output functions
228 * Implemented due to inx,outx macros accessing the device improperly
229 * and putting the device into an unkown state.
231 * For instance, on Sharp LPD7A400 SDK, affects were chip memory
232 * could not be free'd (hence the alloc failures), duplicate packets,
233 * packets being corrupt (shifted) on the wire, etc. Switching to the
234 * inx,outx functions fixed this problem.
236 static inline word SMC_inw(dword offset);
237 static inline void SMC_outw(word value, dword offset);
238 static inline byte SMC_inb(dword offset);
239 static inline void SMC_outb(byte value, dword offset);
240 static inline void SMC_insw(dword offset, volatile uchar* buf, dword len);
241 static inline void SMC_outsw(dword offset, uchar* buf, dword len);
243 #define barrier() __asm__ __volatile__("": : :"memory")
245 static inline word SMC_inw(dword offset)
248 v = *((volatile word*)(SMC_BASE_ADDRESS+offset));
249 barrier(); *(volatile u32*)(0xc0000000);
253 static inline void SMC_outw(word value, dword offset)
255 *((volatile word*)(SMC_BASE_ADDRESS+offset)) = value;
256 barrier(); *(volatile u32*)(0xc0000000);
259 static inline byte SMC_inb(dword offset)
263 _w = SMC_inw(offset & ~((dword)1));
264 return (offset & 1) ? (byte)(_w >> 8) : (byte)(_w);
267 static inline void SMC_outb(byte value, dword offset)
271 _w = SMC_inw(offset & ~((dword)1));
273 *((volatile word*)(SMC_BASE_ADDRESS+(offset & ~((dword)1)))) = (value<<8) | (_w & 0x00ff);
275 *((volatile word*)(SMC_BASE_ADDRESS+offset)) = value | (_w & 0xff00);
278 static inline void SMC_insw(dword offset, volatile uchar* buf, dword len)
280 volatile word *p = (volatile word *)buf;
283 *p++ = SMC_inw(offset);
285 *((volatile u32*)(0xc0000000));
289 static inline void SMC_outsw(dword offset, uchar* buf, dword len)
291 volatile word *p = (volatile word *)buf;
294 SMC_outw(*p++, offset);
296 *(volatile u32*)(0xc0000000);
299 #endif /* CONFIG_SMC_USE_IOFUNCS */
301 static char unsigned smc_mac_addr[6] = {0x02, 0x80, 0xad, 0x20, 0x31, 0xb8};
304 * This function must be called before smc_open() if you want to override
305 * the default mac address.
308 void smc_set_mac_addr(const unsigned char *addr) {
311 for (i=0; i < sizeof(smc_mac_addr); i++){
312 smc_mac_addr[i] = addr[i];
317 * smc_get_macaddr is no longer used. If you want to override the default
318 * mac address, call smc_get_mac_addr as a part of the board initialization.
322 void smc_get_macaddr( byte *addr ) {
323 /* MAC ADDRESS AT FLASHBLOCK 1 / OFFSET 0x10 */
324 unsigned char *dnp1110_mac = (unsigned char *) (0xE8000000 + 0x20010);
328 for (i=0; i<6; i++) {
329 addr[0] = *(dnp1110_mac+0);
330 addr[1] = *(dnp1110_mac+1);
331 addr[2] = *(dnp1110_mac+2);
332 addr[3] = *(dnp1110_mac+3);
333 addr[4] = *(dnp1110_mac+4);
334 addr[5] = *(dnp1110_mac+5);
339 /***********************************************
340 * Show available memory *
341 ***********************************************/
342 void dump_memory_info(void)
347 old_bank = SMC_inw(BANK_SELECT)&0xF;
350 mem_info = SMC_inw( MIR_REG );
351 PRINTK2("Memory: %4d available\n", (mem_info >> 8)*2048);
353 SMC_SELECT_BANK(old_bank);
356 . A rather simple routine to print out a packet for debugging purposes.
359 static void print_packet( byte *, int );
362 #define tx_done(dev) 1
365 /* this does a soft reset on the device */
366 static void smc_reset( void );
368 /* Enable Interrupts, Receive, and Transmit */
369 static void smc_enable( void );
371 /* this puts the device in an inactive state */
372 static void smc_shutdown( void );
374 /* Routines to Read and Write the PHY Registers across the
375 MII Management Interface
378 #ifndef CONFIG_SMC91111_EXT_PHY
379 static word smc_read_phy_register(byte phyreg);
380 static void smc_write_phy_register(byte phyreg, word phydata);
381 #endif /* !CONFIG_SMC91111_EXT_PHY */
384 static int poll4int (byte mask, int timeout)
386 int tmo = get_timer (0) + timeout * CFG_HZ;
388 word old_bank = SMC_inw (BSR_REG);
390 PRINTK2 ("Polling...\n");
392 while ((SMC_inw (SMC91111_INT_REG) & mask) == 0) {
393 if (get_timer (0) >= tmo) {
399 /* restore old bank selection */
400 SMC_SELECT_BANK (old_bank);
408 /* Only one release command at a time, please */
409 static inline void smc_wait_mmu_release_complete (void)
413 /* assume bank 2 selected */
414 while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
415 udelay (1); /* Wait until not busy */
422 . Function: smc_reset( void )
424 . This sets the SMC91111 chip to its normal state, hopefully from whatever
425 . mess that any other DOS driver has put it in.
427 . Maybe I should reset more registers to defaults in here? SOFTRST should
431 . 1. send a SOFT RESET
432 . 2. wait for it to finish
433 . 3. enable autorelease mode
434 . 4. reset the memory management unit
435 . 5. clear all interrupts
438 static void smc_reset (void)
440 PRINTK2 ("%s: smc_reset\n", SMC_DEV_NAME);
442 /* This resets the registers mostly to defaults, but doesn't
443 affect EEPROM. That seems unnecessary */
445 SMC_outw (RCR_SOFTRST, RCR_REG);
447 /* Setup the Configuration Register */
448 /* This is necessary because the CONFIG_REG is not affected */
449 /* by a soft reset */
452 #if defined(CONFIG_SMC91111_EXT_PHY)
453 SMC_outw (CONFIG_DEFAULT | CONFIG_EXT_PHY, CONFIG_REG);
455 SMC_outw (CONFIG_DEFAULT, CONFIG_REG);
459 /* Release from possible power-down state */
460 /* Configuration register is not affected by Soft Reset */
461 SMC_outw (SMC_inw (CONFIG_REG) | CONFIG_EPH_POWER_EN, CONFIG_REG);
465 /* this should pause enough for the chip to be happy */
468 /* Disable transmit and receive functionality */
469 SMC_outw (RCR_CLEAR, RCR_REG);
470 SMC_outw (TCR_CLEAR, TCR_REG);
472 /* set the control register */
474 SMC_outw (CTL_DEFAULT, CTL_REG);
478 smc_wait_mmu_release_complete ();
479 SMC_outw (MC_RESET, MMU_CMD_REG);
480 while (SMC_inw (MMU_CMD_REG) & MC_BUSY)
481 udelay (1); /* Wait until not busy */
483 /* Note: It doesn't seem that waiting for the MMU busy is needed here,
484 but this is a place where future chipsets _COULD_ break. Be wary
485 of issuing another MMU command right after this */
487 /* Disable all interrupts */
488 SMC_outb (0, IM_REG);
492 . Function: smc_enable
493 . Purpose: let the chip talk to the outside work
495 . 1. Enable the transmitter
496 . 2. Enable the receiver
497 . 3. Enable interrupts
499 static void smc_enable()
501 PRINTK2("%s: smc_enable\n", SMC_DEV_NAME);
502 SMC_SELECT_BANK( 0 );
503 /* see the header file for options in TCR/RCR DEFAULT*/
504 SMC_outw( TCR_DEFAULT, TCR_REG );
505 SMC_outw( RCR_DEFAULT, RCR_REG );
508 /* smc_write_phy_register(PHY_CNTL_REG, 0x0000); */
512 . Function: smc_shutdown
513 . Purpose: closes down the SMC91xxx chip.
515 . 1. zero the interrupt mask
516 . 2. clear the enable receive flag
517 . 3. clear the enable xmit flags
520 . (1) maybe utilize power down mode.
521 . Why not yet? Because while the chip will go into power down mode,
522 . the manual says that it will wake up in response to any I/O requests
523 . in the register space. Empirical results do not show this working.
525 static void smc_shutdown()
527 PRINTK2(CARDNAME ": smc_shutdown\n");
529 /* no more interrupts for me */
530 SMC_SELECT_BANK( 2 );
531 SMC_outb( 0, IM_REG );
533 /* and tell the card to stay away from that nasty outside world */
534 SMC_SELECT_BANK( 0 );
535 SMC_outb( RCR_CLEAR, RCR_REG );
536 SMC_outb( TCR_CLEAR, TCR_REG );
537 #ifdef SHARED_RESOURCES
544 . Function: smc_hardware_send_packet(struct net_device * )
546 . This sends the actual packet to the SMC9xxx chip.
549 . First, see if a saved_skb is available.
550 . ( this should NOT be called if there is no 'saved_skb'
551 . Now, find the packet number that the chip allocated
552 . Point the data pointers at it in memory
553 . Set the length word in the chip's memory
554 . Dump the packet to chip memory
555 . Check if a last byte is needed ( odd length packet )
556 . if so, set the control flag right
557 . Tell the card to send it
558 . Enable the transmit interrupt, so I know if it failed
559 . Free the kernel data if I actually sent it.
561 static int smc_send_packet (volatile void *packet, int packet_length)
564 unsigned long ioaddr;
574 /* save PTR and PNR registers before manipulation */
576 saved_pnr = SMC_inb( PN_REG );
577 saved_ptr = SMC_inw( PTR_REG );
579 PRINTK3 ("%s: smc_hardware_send_packet\n", SMC_DEV_NAME);
581 length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
584 ** The MMU wants the number of pages to be the number of 256 bytes
585 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
587 ** The 91C111 ignores the size bits, but the code is left intact
588 ** for backwards and future compatibility.
590 ** Pkt size for allocating is data length +6 (for additional status
591 ** words, length and ctl!)
593 ** If odd size then last byte is included in this header.
595 numPages = ((length & 0xfffe) + 6);
596 numPages >>= 8; /* Divide by 256 */
599 printf ("%s: Far too big packet error. \n", SMC_DEV_NAME);
603 /* now, try to allocate the memory */
605 SMC_outw (MC_ALLOC | numPages, MMU_CMD_REG);
607 /* FIXME: the ALLOC_INT bit never gets set *
608 * so the following will always give a *
609 * memory allocation error. *
610 * same code works in armboot though *
616 time_out = MEMORY_WAIT_TIME;
618 status = SMC_inb (SMC91111_INT_REG);
619 if (status & IM_ALLOC_INT) {
620 /* acknowledge the interrupt */
621 SMC_outb (IM_ALLOC_INT, SMC91111_INT_REG);
624 } while (--time_out);
627 PRINTK2 ("%s: memory allocation, try %d failed ...\n",
629 if (try < SMC_ALLOC_MAX_TRY)
635 PRINTK2 ("%s: memory allocation, try %d succeeded ...\n",
638 /* I can send the packet now.. */
640 ioaddr = SMC_BASE_ADDRESS;
642 buf = (byte *) packet;
644 /* If I get here, I _know_ there is a packet slot waiting for me */
645 packet_no = SMC_inb (AR_REG);
646 if (packet_no & AR_FAILED) {
647 /* or isn't there? BAD CHIP! */
648 printf ("%s: Memory allocation failed. \n", SMC_DEV_NAME);
652 /* we have a packet address, so tell the card to use it */
653 #ifndef CONFIG_XAENIAX
654 SMC_outb (packet_no, PN_REG);
656 /* On Xaeniax board, we can't use SMC_outb here because that way
657 * the Allocate MMU command will end up written to the command register
658 * as well, which will lead to a problem.
660 SMC_outl (packet_no << 16, 0);
662 /* do not write new ptr value if Write data fifo not empty */
663 while ( saved_ptr & PTR_NOTEMPTY )
664 printf ("Write data fifo not empty!\n");
666 /* point to the beginning of the packet */
667 SMC_outw (PTR_AUTOINC, PTR_REG);
669 PRINTK3 ("%s: Trying to xmit packet of length %x\n",
670 SMC_DEV_NAME, length);
673 printf ("Transmitting Packet\n");
674 print_packet (buf, length);
677 /* send the packet length ( +6 for status, length and ctl byte )
678 and the status word ( set to zeros ) */
680 SMC_outl ((length + 6) << 16, SMC91111_DATA_REG);
682 SMC_outw (0, SMC91111_DATA_REG);
683 /* send the packet length ( +6 for status words, length, and ctl */
684 SMC_outw ((length + 6), SMC91111_DATA_REG);
687 /* send the actual data
688 . I _think_ it's faster to send the longs first, and then
689 . mop up by sending the last word. It depends heavily
690 . on alignment, at least on the 486. Maybe it would be
691 . a good idea to check which is optimal? But that could take
692 . almost as much time as is saved?
695 SMC_outsl (SMC91111_DATA_REG, buf, length >> 2);
696 #ifndef CONFIG_XAENIAX
698 SMC_outw (*((word *) (buf + (length & 0xFFFFFFFC))),
701 /* On XANEIAX, we can only use 32-bit writes, so we need to handle
702 * unaligned tail part specially. The standard code doesn't work.
704 if ((length & 3) == 3) {
705 u16 * ptr = (u16*) &buf[length-3];
706 SMC_outl((*ptr) | ((0x2000 | buf[length-1]) << 16),
708 } else if ((length & 2) == 2) {
709 u16 * ptr = (u16*) &buf[length-2];
710 SMC_outl(*ptr, SMC91111_DATA_REG);
711 } else if (length & 1) {
712 SMC_outl((0x2000 | buf[length-1]), SMC91111_DATA_REG);
714 SMC_outl(0, SMC91111_DATA_REG);
718 SMC_outsw (SMC91111_DATA_REG, buf, (length) >> 1);
719 #endif /* USE_32_BIT */
721 #ifndef CONFIG_XAENIAX
722 /* Send the last byte, if there is one. */
723 if ((length & 1) == 0) {
724 SMC_outw (0, SMC91111_DATA_REG);
726 SMC_outw (buf[length - 1] | 0x2000, SMC91111_DATA_REG);
730 /* and let the chipset deal with it */
731 SMC_outw (MC_ENQUEUE, MMU_CMD_REG);
733 /* poll for TX INT */
734 /* if (poll4int (IM_TX_INT, SMC_TX_TIMEOUT)) { */
735 /* poll for TX_EMPTY INT - autorelease enabled */
736 if (poll4int(IM_TX_EMPTY_INT, SMC_TX_TIMEOUT)) {
738 PRINTK2 ("%s: TX timeout, sending failed...\n", SMC_DEV_NAME);
741 /* no need to release, MMU does that now */
742 #ifdef CONFIG_XAENIAX
743 SMC_outw (MC_FREEPKT, MMU_CMD_REG);
746 /* wait for MMU getting ready (low) */
747 while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
751 PRINTK2 ("MMU ready\n");
757 SMC_outb (IM_TX_EMPTY_INT, SMC91111_INT_REG);
758 /* SMC_outb (IM_TX_INT, SMC91111_INT_REG); */
759 PRINTK2 ("%s: Sent packet of length %d \n", SMC_DEV_NAME,
763 /* no need to release, MMU does that now */
764 #ifdef CONFIG_XAENIAX
765 SMC_outw (MC_FREEPKT, MMU_CMD_REG);
768 /* wait for MMU getting ready (low) */
769 while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
773 PRINTK2 ("MMU ready\n");
778 /* restore previously saved registers */
779 #ifndef CONFIG_XAENIAX
780 SMC_outb( saved_pnr, PN_REG );
782 /* On Xaeniax board, we can't use SMC_outb here because that way
783 * the Allocate MMU command will end up written to the command register
784 * as well, which will lead to a problem.
786 SMC_outl(saved_pnr << 16, 0);
788 SMC_outw( saved_ptr, PTR_REG );
793 /*-------------------------------------------------------------------------
795 | smc_destructor( struct net_device * dev )
797 | dev, pointer to the device structure
802 ---------------------------------------------------------------------------
804 void smc_destructor()
806 PRINTK2(CARDNAME ": smc_destructor\n");
811 * Open and Initialize the board
813 * Set up everything, reset the card, etc ..
816 static int smc_open (bd_t * bd)
820 PRINTK2 ("%s: smc_open\n", SMC_DEV_NAME);
822 /* reset the hardware */
826 /* Configure the PHY */
827 #ifndef CONFIG_SMC91111_EXT_PHY
828 smc_phy_configure ();
831 /* conservative setting (10Mbps, HalfDuplex, no AutoNeg.) */
832 /* SMC_SELECT_BANK(0); */
833 /* SMC_outw(0, RPC_REG); */
836 err = smc_get_ethaddr (bd); /* set smc_mac_addr, and sync it with u-boot globals */
838 memset (bd->bi_enetaddr, 0, 6); /* hack to make error stick! upper code will abort if not set */
839 return (-1); /* upper code ignores this, but NOT bi_enetaddr */
842 for (i = 0; i < 6; i += 2) {
845 address = smc_mac_addr[i + 1] << 8;
846 address |= smc_mac_addr[i];
847 SMC_outw (address, (ADDR0_REG + i));
850 for (i = 0; i < 6; i++)
851 SMC_outb (smc_mac_addr[i], (ADDR0_REG + i));
857 /*-------------------------------------------------------------
859 . smc_rcv - receive a packet from the card
861 . There is ( at least ) a packet waiting to be read from
865 . o If an error, record it
866 . o otherwise, read in the packet
867 --------------------------------------------------------------
882 /* save PTR and PTR registers */
883 saved_pnr = SMC_inb( PN_REG );
884 saved_ptr = SMC_inw( PTR_REG );
886 packet_number = SMC_inw( RXFIFO_REG );
888 if ( packet_number & RXFIFO_REMPTY ) {
893 PRINTK3("%s: smc_rcv\n", SMC_DEV_NAME);
894 /* start reading from the start of the packet */
895 SMC_outw( PTR_READ | PTR_RCV | PTR_AUTOINC, PTR_REG );
897 /* First two words are status and packet_length */
899 stat_len = SMC_inl(SMC91111_DATA_REG);
900 status = stat_len & 0xffff;
901 packet_length = stat_len >> 16;
903 status = SMC_inw( SMC91111_DATA_REG );
904 packet_length = SMC_inw( SMC91111_DATA_REG );
907 packet_length &= 0x07ff; /* mask off top bits */
909 PRINTK2("RCV: STATUS %4x LENGTH %4x\n", status, packet_length );
911 if ( !(status & RS_ERRORS ) ){
912 /* Adjust for having already read the first two words */
913 packet_length -= 4; /*4; */
916 /* set odd length for bug in LAN91C111, */
917 /* which never sets RS_ODDFRAME */
922 PRINTK3(" Reading %d dwords (and %d bytes) \n",
923 packet_length >> 2, packet_length & 3 );
924 /* QUESTION: Like in the TX routine, do I want
925 to send the DWORDs or the bytes first, or some
926 mixture. A mixture might improve already slow PIO
928 SMC_insl( SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 2 );
929 /* read the left over bytes */
930 if (packet_length & 3) {
933 byte *tail = (byte *)(NetRxPackets[0] + (packet_length & ~3));
934 dword leftover = SMC_inl(SMC91111_DATA_REG);
935 for (i=0; i<(packet_length & 3); i++)
936 *tail++ = (byte) (leftover >> (8*i)) & 0xff;
939 PRINTK3(" Reading %d words and %d byte(s) \n",
940 (packet_length >> 1 ), packet_length & 1 );
941 SMC_insw(SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 1);
943 #endif /* USE_32_BIT */
946 printf("Receiving Packet\n");
947 print_packet( NetRxPackets[0], packet_length );
955 while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
956 udelay(1); /* Wait until not busy */
958 /* error or good, tell the card to get rid of this packet */
959 SMC_outw( MC_RELEASE, MMU_CMD_REG );
961 while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
962 udelay(1); /* Wait until not busy */
964 /* restore saved registers */
965 #ifndef CONFIG_XAENIAX
966 SMC_outb( saved_pnr, PN_REG );
968 /* On Xaeniax board, we can't use SMC_outb here because that way
969 * the Allocate MMU command will end up written to the command register
970 * as well, which will lead to a problem.
972 SMC_outl( saved_pnr << 16, 0);
974 SMC_outw( saved_ptr, PTR_REG );
977 /* Pass the packet up to the protocol layers. */
978 NetReceive(NetRxPackets[0], packet_length);
979 return packet_length;
987 /*----------------------------------------------------
990 . this makes the board clean up everything that it can
991 . and not talk to the outside world. Caused by
992 . an 'ifconfig ethX down'
994 -----------------------------------------------------*/
995 static int smc_close()
997 PRINTK2("%s: smc_close\n", SMC_DEV_NAME);
999 /* clear everything */
1007 /*------------------------------------------------------------
1008 . Modify a bit in the LAN91C111 register set
1009 .-------------------------------------------------------------*/
1010 static word smc_modify_regbit(int bank, int ioaddr, int reg,
1011 unsigned int bit, int val)
1015 SMC_SELECT_BANK( bank );
1017 regval = SMC_inw( reg );
1023 SMC_outw( regval, 0 );
1028 /*------------------------------------------------------------
1029 . Retrieve a bit in the LAN91C111 register set
1030 .-------------------------------------------------------------*/
1031 static int smc_get_regbit(int bank, int ioaddr, int reg, unsigned int bit)
1033 SMC_SELECT_BANK( bank );
1034 if ( SMC_inw( reg ) & bit)
1041 /*------------------------------------------------------------
1042 . Modify a LAN91C111 register (word access only)
1043 .-------------------------------------------------------------*/
1044 static void smc_modify_reg(int bank, int ioaddr, int reg, word val)
1046 SMC_SELECT_BANK( bank );
1047 SMC_outw( val, reg );
1051 /*------------------------------------------------------------
1052 . Retrieve a LAN91C111 register (word access only)
1053 .-------------------------------------------------------------*/
1054 static int smc_get_reg(int bank, int ioaddr, int reg)
1056 SMC_SELECT_BANK( bank );
1057 return(SMC_inw( reg ));
1062 /*---PHY CONTROL AND CONFIGURATION----------------------------------------- */
1064 #if (SMC_DEBUG > 2 )
1066 /*------------------------------------------------------------
1067 . Debugging function for viewing MII Management serial bitstream
1068 .-------------------------------------------------------------*/
1069 static void smc_dump_mii_stream (byte * bits, int size)
1074 for (i = 0; i < size; ++i) {
1075 printf ("%d", i % 10);
1079 for (i = 0; i < size; ++i) {
1080 if (bits[i] & MII_MDOE)
1087 for (i = 0; i < size; ++i) {
1088 if (bits[i] & MII_MDO)
1095 for (i = 0; i < size; ++i) {
1096 if (bits[i] & MII_MDI)
1106 /*------------------------------------------------------------
1107 . Reads a register from the MII Management serial interface
1108 .-------------------------------------------------------------*/
1109 #ifndef CONFIG_SMC91111_EXT_PHY
1110 static word smc_read_phy_register (byte phyreg)
1120 byte phyaddr = SMC_PHY_ADDR;
1122 /* 32 consecutive ones on MDO to establish sync */
1123 for (i = 0; i < 32; ++i)
1124 bits[clk_idx++] = MII_MDOE | MII_MDO;
1126 /* Start code <01> */
1127 bits[clk_idx++] = MII_MDOE;
1128 bits[clk_idx++] = MII_MDOE | MII_MDO;
1130 /* Read command <10> */
1131 bits[clk_idx++] = MII_MDOE | MII_MDO;
1132 bits[clk_idx++] = MII_MDOE;
1134 /* Output the PHY address, msb first */
1136 for (i = 0; i < 5; ++i) {
1138 bits[clk_idx++] = MII_MDOE | MII_MDO;
1140 bits[clk_idx++] = MII_MDOE;
1142 /* Shift to next lowest bit */
1146 /* Output the phy register number, msb first */
1148 for (i = 0; i < 5; ++i) {
1150 bits[clk_idx++] = MII_MDOE | MII_MDO;
1152 bits[clk_idx++] = MII_MDOE;
1154 /* Shift to next lowest bit */
1158 /* Tristate and turnaround (2 bit times) */
1159 bits[clk_idx++] = 0;
1160 /*bits[clk_idx++] = 0; */
1162 /* Input starts at this bit time */
1163 input_idx = clk_idx;
1165 /* Will input 16 bits */
1166 for (i = 0; i < 16; ++i)
1167 bits[clk_idx++] = 0;
1169 /* Final clock bit */
1170 bits[clk_idx++] = 0;
1172 /* Save the current bank */
1173 oldBank = SMC_inw (BANK_SELECT);
1176 SMC_SELECT_BANK (3);
1178 /* Get the current MII register value */
1179 mii_reg = SMC_inw (MII_REG);
1181 /* Turn off all MII Interface bits */
1182 mii_reg &= ~(MII_MDOE | MII_MCLK | MII_MDI | MII_MDO);
1184 /* Clock all 64 cycles */
1185 for (i = 0; i < sizeof bits; ++i) {
1186 /* Clock Low - output data */
1187 SMC_outw (mii_reg | bits[i], MII_REG);
1188 udelay (SMC_PHY_CLOCK_DELAY);
1191 /* Clock Hi - input data */
1192 SMC_outw (mii_reg | bits[i] | MII_MCLK, MII_REG);
1193 udelay (SMC_PHY_CLOCK_DELAY);
1194 bits[i] |= SMC_inw (MII_REG) & MII_MDI;
1197 /* Return to idle state */
1198 /* Set clock to low, data to low, and output tristated */
1199 SMC_outw (mii_reg, MII_REG);
1200 udelay (SMC_PHY_CLOCK_DELAY);
1202 /* Restore original bank select */
1203 SMC_SELECT_BANK (oldBank);
1205 /* Recover input data */
1207 for (i = 0; i < 16; ++i) {
1210 if (bits[input_idx++] & MII_MDI)
1214 #if (SMC_DEBUG > 2 )
1215 printf ("smc_read_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
1216 phyaddr, phyreg, phydata);
1217 smc_dump_mii_stream (bits, sizeof bits);
1224 /*------------------------------------------------------------
1225 . Writes a register to the MII Management serial interface
1226 .-------------------------------------------------------------*/
1227 static void smc_write_phy_register (byte phyreg, word phydata)
1235 byte phyaddr = SMC_PHY_ADDR;
1237 /* 32 consecutive ones on MDO to establish sync */
1238 for (i = 0; i < 32; ++i)
1239 bits[clk_idx++] = MII_MDOE | MII_MDO;
1241 /* Start code <01> */
1242 bits[clk_idx++] = MII_MDOE;
1243 bits[clk_idx++] = MII_MDOE | MII_MDO;
1245 /* Write command <01> */
1246 bits[clk_idx++] = MII_MDOE;
1247 bits[clk_idx++] = MII_MDOE | MII_MDO;
1249 /* Output the PHY address, msb first */
1251 for (i = 0; i < 5; ++i) {
1253 bits[clk_idx++] = MII_MDOE | MII_MDO;
1255 bits[clk_idx++] = MII_MDOE;
1257 /* Shift to next lowest bit */
1261 /* Output the phy register number, msb first */
1263 for (i = 0; i < 5; ++i) {
1265 bits[clk_idx++] = MII_MDOE | MII_MDO;
1267 bits[clk_idx++] = MII_MDOE;
1269 /* Shift to next lowest bit */
1273 /* Tristate and turnaround (2 bit times) */
1274 bits[clk_idx++] = 0;
1275 bits[clk_idx++] = 0;
1277 /* Write out 16 bits of data, msb first */
1279 for (i = 0; i < 16; ++i) {
1281 bits[clk_idx++] = MII_MDOE | MII_MDO;
1283 bits[clk_idx++] = MII_MDOE;
1285 /* Shift to next lowest bit */
1289 /* Final clock bit (tristate) */
1290 bits[clk_idx++] = 0;
1292 /* Save the current bank */
1293 oldBank = SMC_inw (BANK_SELECT);
1296 SMC_SELECT_BANK (3);
1298 /* Get the current MII register value */
1299 mii_reg = SMC_inw (MII_REG);
1301 /* Turn off all MII Interface bits */
1302 mii_reg &= ~(MII_MDOE | MII_MCLK | MII_MDI | MII_MDO);
1304 /* Clock all cycles */
1305 for (i = 0; i < sizeof bits; ++i) {
1306 /* Clock Low - output data */
1307 SMC_outw (mii_reg | bits[i], MII_REG);
1308 udelay (SMC_PHY_CLOCK_DELAY);
1311 /* Clock Hi - input data */
1312 SMC_outw (mii_reg | bits[i] | MII_MCLK, MII_REG);
1313 udelay (SMC_PHY_CLOCK_DELAY);
1314 bits[i] |= SMC_inw (MII_REG) & MII_MDI;
1317 /* Return to idle state */
1318 /* Set clock to low, data to low, and output tristated */
1319 SMC_outw (mii_reg, MII_REG);
1320 udelay (SMC_PHY_CLOCK_DELAY);
1322 /* Restore original bank select */
1323 SMC_SELECT_BANK (oldBank);
1325 #if (SMC_DEBUG > 2 )
1326 printf ("smc_write_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
1327 phyaddr, phyreg, phydata);
1328 smc_dump_mii_stream (bits, sizeof bits);
1331 #endif /* !CONFIG_SMC91111_EXT_PHY */
1334 /*------------------------------------------------------------
1335 . Waits the specified number of milliseconds - kernel friendly
1336 .-------------------------------------------------------------*/
1337 #ifndef CONFIG_SMC91111_EXT_PHY
1338 static void smc_wait_ms(unsigned int ms)
1342 #endif /* !CONFIG_SMC91111_EXT_PHY */
1345 /*------------------------------------------------------------
1346 . Configures the specified PHY using Autonegotiation. Calls
1347 . smc_phy_fixed() if the user has requested a certain config.
1348 .-------------------------------------------------------------*/
1349 #ifndef CONFIG_SMC91111_EXT_PHY
1350 static void smc_phy_configure ()
1354 word my_phy_caps; /* My PHY capabilities */
1355 word my_ad_caps; /* My Advertised capabilities */
1356 word status = 0; /*;my status = 0 */
1359 PRINTK3 ("%s: smc_program_phy()\n", SMC_DEV_NAME);
1362 /* Get the detected phy address */
1363 phyaddr = SMC_PHY_ADDR;
1365 /* Reset the PHY, setting all other bits to zero */
1366 smc_write_phy_register (PHY_CNTL_REG, PHY_CNTL_RST);
1368 /* Wait for the reset to complete, or time out */
1369 timeout = 6; /* Wait up to 3 seconds */
1371 if (!(smc_read_phy_register (PHY_CNTL_REG)
1373 /* reset complete */
1377 smc_wait_ms (500); /* wait 500 millisecs */
1381 printf ("%s:PHY reset timed out\n", SMC_DEV_NAME);
1382 goto smc_phy_configure_exit;
1385 /* Read PHY Register 18, Status Output */
1386 /* lp->lastPhy18 = smc_read_phy_register(PHY_INT_REG); */
1388 /* Enable PHY Interrupts (for register 18) */
1389 /* Interrupts listed here are disabled */
1390 smc_write_phy_register (PHY_MASK_REG, 0xffff);
1392 /* Configure the Receive/Phy Control register */
1393 SMC_SELECT_BANK (0);
1394 SMC_outw (RPC_DEFAULT, RPC_REG);
1396 /* Copy our capabilities from PHY_STAT_REG to PHY_AD_REG */
1397 my_phy_caps = smc_read_phy_register (PHY_STAT_REG);
1398 my_ad_caps = PHY_AD_CSMA; /* I am CSMA capable */
1400 if (my_phy_caps & PHY_STAT_CAP_T4)
1401 my_ad_caps |= PHY_AD_T4;
1403 if (my_phy_caps & PHY_STAT_CAP_TXF)
1404 my_ad_caps |= PHY_AD_TX_FDX;
1406 if (my_phy_caps & PHY_STAT_CAP_TXH)
1407 my_ad_caps |= PHY_AD_TX_HDX;
1409 if (my_phy_caps & PHY_STAT_CAP_TF)
1410 my_ad_caps |= PHY_AD_10_FDX;
1412 if (my_phy_caps & PHY_STAT_CAP_TH)
1413 my_ad_caps |= PHY_AD_10_HDX;
1415 /* Update our Auto-Neg Advertisement Register */
1416 smc_write_phy_register (PHY_AD_REG, my_ad_caps);
1418 /* Read the register back. Without this, it appears that when */
1419 /* auto-negotiation is restarted, sometimes it isn't ready and */
1420 /* the link does not come up. */
1421 smc_read_phy_register(PHY_AD_REG);
1423 PRINTK2 ("%s: phy caps=%x\n", SMC_DEV_NAME, my_phy_caps);
1424 PRINTK2 ("%s: phy advertised caps=%x\n", SMC_DEV_NAME, my_ad_caps);
1426 /* Restart auto-negotiation process in order to advertise my caps */
1427 smc_write_phy_register (PHY_CNTL_REG,
1428 PHY_CNTL_ANEG_EN | PHY_CNTL_ANEG_RST);
1430 /* Wait for the auto-negotiation to complete. This may take from */
1431 /* 2 to 3 seconds. */
1432 /* Wait for the reset to complete, or time out */
1433 timeout = CONFIG_SMC_AUTONEG_TIMEOUT * 2;
1436 status = smc_read_phy_register (PHY_STAT_REG);
1437 if (status & PHY_STAT_ANEG_ACK) {
1438 /* auto-negotiate complete */
1442 smc_wait_ms (500); /* wait 500 millisecs */
1444 /* Restart auto-negotiation if remote fault */
1445 if (status & PHY_STAT_REM_FLT) {
1446 printf ("%s: PHY remote fault detected\n",
1449 /* Restart auto-negotiation */
1450 printf ("%s: PHY restarting auto-negotiation\n",
1452 smc_write_phy_register (PHY_CNTL_REG,
1461 printf ("%s: PHY auto-negotiate timed out\n", SMC_DEV_NAME);
1465 /* Fail if we detected an auto-negotiate remote fault */
1466 if (status & PHY_STAT_REM_FLT) {
1467 printf ("%s: PHY remote fault detected\n", SMC_DEV_NAME);
1471 /* Re-Configure the Receive/Phy Control register */
1472 SMC_outw (RPC_DEFAULT, RPC_REG);
1474 smc_phy_configure_exit: ;
1477 #endif /* !CONFIG_SMC91111_EXT_PHY */
1481 static void print_packet( byte * buf, int length )
1487 printf("Packet of length %d \n", length );
1490 lines = length / 16;
1491 remainder = length % 16;
1493 for ( i = 0; i < lines ; i ++ ) {
1496 for ( cur = 0; cur < 8; cur ++ ) {
1501 printf("%02x%02x ", a, b );
1505 for ( i = 0; i < remainder/2 ; i++ ) {
1510 printf("%02x%02x ", a, b );
1517 int eth_init(bd_t *bd) {
1518 #ifdef SHARED_RESOURCES
1521 return (smc_open(bd));
1532 int eth_send(volatile void *packet, int length) {
1533 return smc_send_packet(packet, length);
1536 int smc_get_ethaddr (bd_t * bd)
1538 int env_size, rom_valid, env_present = 0, reg;
1539 char *s = NULL, *e, es[] = "11:22:33:44:55:66";
1541 uchar v_env_mac[6], v_rom_mac[6], *v_mac;
1543 env_size = getenv_r ("ethaddr", s_env_mac, sizeof (s_env_mac));
1544 if ((env_size > 0) && (env_size < sizeof (es))) { /* exit if env is bad */
1545 printf ("\n*** ERROR: ethaddr is not set properly!!\n");
1554 for (reg = 0; reg < 6; ++reg) { /* turn string into mac value */
1555 v_env_mac[reg] = s ? simple_strtoul (s, &e, 16) : 0;
1557 s = (*e) ? e + 1 : e;
1560 rom_valid = get_rom_mac (v_rom_mac); /* get ROM mac value if any */
1562 if (!env_present) { /* if NO env */
1563 if (rom_valid) { /* but ROM is valid */
1565 sprintf (s_env_mac, "%02X:%02X:%02X:%02X:%02X:%02X",
1566 v_mac[0], v_mac[1], v_mac[2], v_mac[3],
1567 v_mac[4], v_mac[5]);
1568 setenv ("ethaddr", s_env_mac);
1569 } else { /* no env, bad ROM */
1570 printf ("\n*** ERROR: ethaddr is NOT set !!\n");
1573 } else { /* good env, don't care ROM */
1574 v_mac = v_env_mac; /* always use a good env over a ROM */
1577 if (env_present && rom_valid) { /* if both env and ROM are good */
1578 if (memcmp (v_env_mac, v_rom_mac, 6) != 0) {
1579 printf ("\nWarning: MAC addresses don't match:\n");
1580 printf ("\tHW MAC address: "
1581 "%02X:%02X:%02X:%02X:%02X:%02X\n",
1582 v_rom_mac[0], v_rom_mac[1],
1583 v_rom_mac[2], v_rom_mac[3],
1584 v_rom_mac[4], v_rom_mac[5] );
1585 printf ("\t\"ethaddr\" value: "
1586 "%02X:%02X:%02X:%02X:%02X:%02X\n",
1587 v_env_mac[0], v_env_mac[1],
1588 v_env_mac[2], v_env_mac[3],
1589 v_env_mac[4], v_env_mac[5]) ;
1590 debug ("### Set MAC addr from environment\n");
1593 memcpy (bd->bi_enetaddr, v_mac, 6); /* update global address to match env (allows env changing) */
1594 smc_set_mac_addr ((uchar *)v_mac); /* use old function to update smc default */
1595 PRINTK("Using MAC Address %02X:%02X:%02X:%02X:%02X:%02X\n", v_mac[0], v_mac[1],
1596 v_mac[2], v_mac[3], v_mac[4], v_mac[5]);
1600 int get_rom_mac (uchar *v_rom_mac)
1602 #ifdef HARDCODE_MAC /* used for testing or to supress run time warnings */
1603 char hw_mac_addr[] = { 0x02, 0x80, 0xad, 0x20, 0x31, 0xb8 };
1605 memcpy (v_rom_mac, hw_mac_addr, 6);
1611 SMC_SELECT_BANK (1);
1614 v_rom_mac[i] = SMC_inb ((ADDR0_REG + i));
1615 valid_mac |= v_rom_mac[i];
1618 return (valid_mac ? 1 : 0);