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 #ifdef CONFIG_DRIVER_SMC91111
70 /* Use power-down feature of the chip */
78 static const char version[] =
79 "smc91111.c:v1.0 04/25/01 by Daris A Nevil (dnevil@snmc.com)\n";
82 /* Autonegotiation timeout in seconds */
83 #ifndef CONFIG_SMC_AUTONEG_TIMEOUT
84 #define CONFIG_SMC_AUTONEG_TIMEOUT 10
87 /*------------------------------------------------------------------------
89 . Configuration options, for the experienced user to change.
91 -------------------------------------------------------------------------*/
94 . Wait time for memory to be free. This probably shouldn't be
95 . tuned that much, as waiting for this means nothing else happens
98 #define MEMORY_WAIT_TIME 16
102 #define PRINTK3(args...) printf(args)
104 #define PRINTK3(args...)
108 #define PRINTK2(args...) printf(args)
110 #define PRINTK2(args...)
114 #define PRINTK(args...) printf(args)
116 #define PRINTK(args...)
120 /*------------------------------------------------------------------------
122 . The internal workings of the driver. If you are changing anything
123 . here with the SMC stuff, you should have the datasheet and know
124 . what you are doing.
126 -------------------------------------------------------------------------*/
127 #define CARDNAME "LAN91C111"
129 /* Memory sizing constant */
130 #define LAN91C111_MEMORY_MULTIPLIER (1024*2)
132 #ifndef CONFIG_SMC91111_BASE
133 #define CONFIG_SMC91111_BASE 0x20000300
136 #define SMC_BASE_ADDRESS CONFIG_SMC91111_BASE
138 #define SMC_DEV_NAME "SMC91111"
139 #define SMC_PHY_ADDR 0x0000
140 #define SMC_ALLOC_MAX_TRY 5
141 #define SMC_TX_TIMEOUT 30
143 #define SMC_PHY_CLOCK_DELAY 1000
147 #ifdef CONFIG_SMC_USE_32_BIT
152 /*-----------------------------------------------------------------
154 . The driver can be entered at any of the following entry points.
156 .------------------------------------------------------------------ */
158 extern int eth_init(bd_t *bd);
159 extern void eth_halt(void);
160 extern int eth_rx(void);
161 extern int eth_send(volatile void *packet, int length);
165 . This is called by register_netdev(). It is responsible for
166 . checking the portlist for the SMC9000 series chipset. If it finds
167 . one, then it will initialize the device, find the hardware information,
168 . and sets up the appropriate device parameters.
169 . NOTE: Interrupts are *OFF* when this procedure is called.
171 . NB:This shouldn't be static since it is referred to externally.
176 . This is called by unregister_netdev(). It is responsible for
177 . cleaning up before the driver is finally unregistered and discarded.
179 void smc_destructor(void);
182 . The kernel calls this function when someone wants to use the device,
183 . typically 'ifconfig ethX up'.
185 static int smc_open(bd_t *bd);
189 . This is called by the kernel in response to 'ifconfig ethX down'. It
190 . is responsible for cleaning up everything that the open routine
191 . does, and maybe putting the card into a powerdown state.
193 static int smc_close(void);
196 . Configures the PHY through the MII Management interface
198 #ifndef CONFIG_SMC91111_EXT_PHY
199 static void smc_phy_configure(void);
200 #endif /* !CONFIG_SMC91111_EXT_PHY */
203 . This is a separate procedure to handle the receipt of a packet, to
204 . leave the interrupt code looking slightly cleaner
206 static int smc_rcv(void);
208 /* See if a MAC address is defined in the current environment. If so use it. If not
209 . print a warning and set the environment and other globals with the default.
210 . If an EEPROM is present it really should be consulted.
212 int smc_get_ethaddr(bd_t *bd);
213 int get_rom_mac(char *v_rom_mac);
216 ------------------------------------------------------------
220 ------------------------------------------------------------
223 static char unsigned smc_mac_addr[6] = {0x02, 0x80, 0xad, 0x20, 0x31, 0xb8};
226 * This function must be called before smc_open() if you want to override
227 * the default mac address.
230 void smc_set_mac_addr(const char *addr) {
233 for (i=0; i < sizeof(smc_mac_addr); i++){
234 smc_mac_addr[i] = addr[i];
239 * smc_get_macaddr is no longer used. If you want to override the default
240 * mac address, call smc_get_mac_addr as a part of the board initialization.
244 void smc_get_macaddr( byte *addr ) {
245 /* MAC ADDRESS AT FLASHBLOCK 1 / OFFSET 0x10 */
246 unsigned char *dnp1110_mac = (unsigned char *) (0xE8000000 + 0x20010);
250 for (i=0; i<6; i++) {
251 addr[0] = *(dnp1110_mac+0);
252 addr[1] = *(dnp1110_mac+1);
253 addr[2] = *(dnp1110_mac+2);
254 addr[3] = *(dnp1110_mac+3);
255 addr[4] = *(dnp1110_mac+4);
256 addr[5] = *(dnp1110_mac+5);
261 /***********************************************
262 * Show available memory *
263 ***********************************************/
264 void dump_memory_info(void)
269 old_bank = SMC_inw(BANK_SELECT)&0xF;
272 mem_info = SMC_inw( MIR_REG );
273 PRINTK2("Memory: %4d available\n", (mem_info >> 8)*2048);
275 SMC_SELECT_BANK(old_bank);
278 . A rather simple routine to print out a packet for debugging purposes.
281 static void print_packet( byte *, int );
284 #define tx_done(dev) 1
287 /* this does a soft reset on the device */
288 static void smc_reset( void );
290 /* Enable Interrupts, Receive, and Transmit */
291 static void smc_enable( void );
293 /* this puts the device in an inactive state */
294 static void smc_shutdown( void );
296 /* Routines to Read and Write the PHY Registers across the
297 MII Management Interface
300 #ifndef CONFIG_SMC91111_EXT_PHY
301 static word smc_read_phy_register(byte phyreg);
302 static void smc_write_phy_register(byte phyreg, word phydata);
303 #endif /* !CONFIG_SMC91111_EXT_PHY */
306 static int poll4int (byte mask, int timeout)
308 int tmo = get_timer (0) + timeout * CFG_HZ;
310 word old_bank = SMC_inw (BSR_REG);
312 PRINTK2 ("Polling...\n");
314 while ((SMC_inw (SMC91111_INT_REG) & mask) == 0) {
315 if (get_timer (0) >= tmo) {
321 /* restore old bank selection */
322 SMC_SELECT_BANK (old_bank);
330 /* Only one release command at a time, please */
331 static inline void smc_wait_mmu_release_complete (void)
335 /* assume bank 2 selected */
336 while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
337 udelay (1); /* Wait until not busy */
344 . Function: smc_reset( void )
346 . This sets the SMC91111 chip to its normal state, hopefully from whatever
347 . mess that any other DOS driver has put it in.
349 . Maybe I should reset more registers to defaults in here? SOFTRST should
353 . 1. send a SOFT RESET
354 . 2. wait for it to finish
355 . 3. enable autorelease mode
356 . 4. reset the memory management unit
357 . 5. clear all interrupts
360 static void smc_reset (void)
362 PRINTK2 ("%s: smc_reset\n", SMC_DEV_NAME);
364 /* This resets the registers mostly to defaults, but doesn't
365 affect EEPROM. That seems unnecessary */
367 SMC_outw (RCR_SOFTRST, RCR_REG);
369 /* Setup the Configuration Register */
370 /* This is necessary because the CONFIG_REG is not affected */
371 /* by a soft reset */
374 #if defined(CONFIG_SMC91111_EXT_PHY)
375 SMC_outw (CONFIG_DEFAULT | CONFIG_EXT_PHY, CONFIG_REG);
377 SMC_outw (CONFIG_DEFAULT, CONFIG_REG);
381 /* Release from possible power-down state */
382 /* Configuration register is not affected by Soft Reset */
383 SMC_outw (SMC_inw (CONFIG_REG) | CONFIG_EPH_POWER_EN, CONFIG_REG);
387 /* this should pause enough for the chip to be happy */
390 /* Disable transmit and receive functionality */
391 SMC_outw (RCR_CLEAR, RCR_REG);
392 SMC_outw (TCR_CLEAR, TCR_REG);
394 /* set the control register */
396 SMC_outw (CTL_DEFAULT, CTL_REG);
400 smc_wait_mmu_release_complete ();
401 SMC_outw (MC_RESET, MMU_CMD_REG);
402 while (SMC_inw (MMU_CMD_REG) & MC_BUSY)
403 udelay (1); /* Wait until not busy */
405 /* Note: It doesn't seem that waiting for the MMU busy is needed here,
406 but this is a place where future chipsets _COULD_ break. Be wary
407 of issuing another MMU command right after this */
409 /* Disable all interrupts */
410 SMC_outb (0, IM_REG);
414 . Function: smc_enable
415 . Purpose: let the chip talk to the outside work
417 . 1. Enable the transmitter
418 . 2. Enable the receiver
419 . 3. Enable interrupts
421 static void smc_enable()
423 PRINTK2("%s: smc_enable\n", SMC_DEV_NAME);
424 SMC_SELECT_BANK( 0 );
425 /* see the header file for options in TCR/RCR DEFAULT*/
426 SMC_outw( TCR_DEFAULT, TCR_REG );
427 SMC_outw( RCR_DEFAULT, RCR_REG );
430 /* smc_write_phy_register(PHY_CNTL_REG, 0x0000); */
434 . Function: smc_shutdown
435 . Purpose: closes down the SMC91xxx chip.
437 . 1. zero the interrupt mask
438 . 2. clear the enable receive flag
439 . 3. clear the enable xmit flags
442 . (1) maybe utilize power down mode.
443 . Why not yet? Because while the chip will go into power down mode,
444 . the manual says that it will wake up in response to any I/O requests
445 . in the register space. Empirical results do not show this working.
447 static void smc_shutdown()
449 PRINTK2(CARDNAME ": smc_shutdown\n");
451 /* no more interrupts for me */
452 SMC_SELECT_BANK( 2 );
453 SMC_outb( 0, IM_REG );
455 /* and tell the card to stay away from that nasty outside world */
456 SMC_SELECT_BANK( 0 );
457 SMC_outb( RCR_CLEAR, RCR_REG );
458 SMC_outb( TCR_CLEAR, TCR_REG );
463 . Function: smc_hardware_send_packet(struct net_device * )
465 . This sends the actual packet to the SMC9xxx chip.
468 . First, see if a saved_skb is available.
469 . ( this should NOT be called if there is no 'saved_skb'
470 . Now, find the packet number that the chip allocated
471 . Point the data pointers at it in memory
472 . Set the length word in the chip's memory
473 . Dump the packet to chip memory
474 . Check if a last byte is needed ( odd length packet )
475 . if so, set the control flag right
476 . Tell the card to send it
477 . Enable the transmit interrupt, so I know if it failed
478 . Free the kernel data if I actually sent it.
480 static int smc_send_packet (volatile void *packet, int packet_length)
483 unsigned long ioaddr;
493 /* save PTR and PNR registers before manipulation */
495 saved_pnr = SMC_inb( PN_REG );
496 saved_ptr = SMC_inw( PTR_REG );
498 PRINTK3 ("%s: smc_hardware_send_packet\n", SMC_DEV_NAME);
500 length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
503 ** The MMU wants the number of pages to be the number of 256 bytes
504 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
506 ** The 91C111 ignores the size bits, but the code is left intact
507 ** for backwards and future compatibility.
509 ** Pkt size for allocating is data length +6 (for additional status
510 ** words, length and ctl!)
512 ** If odd size then last byte is included in this header.
514 numPages = ((length & 0xfffe) + 6);
515 numPages >>= 8; /* Divide by 256 */
518 printf ("%s: Far too big packet error. \n", SMC_DEV_NAME);
522 /* now, try to allocate the memory */
524 SMC_outw (MC_ALLOC | numPages, MMU_CMD_REG);
526 /* FIXME: the ALLOC_INT bit never gets set *
527 * so the following will always give a *
528 * memory allocation error. *
529 * same code works in armboot though *
535 time_out = MEMORY_WAIT_TIME;
537 status = SMC_inb (SMC91111_INT_REG);
538 if (status & IM_ALLOC_INT) {
539 /* acknowledge the interrupt */
540 SMC_outb (IM_ALLOC_INT, SMC91111_INT_REG);
543 } while (--time_out);
546 PRINTK2 ("%s: memory allocation, try %d failed ...\n",
548 if (try < SMC_ALLOC_MAX_TRY)
554 PRINTK2 ("%s: memory allocation, try %d succeeded ...\n",
557 /* I can send the packet now.. */
559 ioaddr = SMC_BASE_ADDRESS;
561 buf = (byte *) packet;
563 /* If I get here, I _know_ there is a packet slot waiting for me */
564 packet_no = SMC_inb (AR_REG);
565 if (packet_no & AR_FAILED) {
566 /* or isn't there? BAD CHIP! */
567 printf ("%s: Memory allocation failed. \n", SMC_DEV_NAME);
571 /* we have a packet address, so tell the card to use it */
572 SMC_outb (packet_no, PN_REG);
574 /* do not write new ptr value if Write data fifo not empty */
575 while ( saved_ptr & PTR_NOTEMPTY )
576 printf ("Write data fifo not empty!\n");
578 /* point to the beginning of the packet */
579 SMC_outw (PTR_AUTOINC, PTR_REG);
581 PRINTK3 ("%s: Trying to xmit packet of length %x\n",
582 SMC_DEV_NAME, length);
585 printf ("Transmitting Packet\n");
586 print_packet (buf, length);
589 /* send the packet length ( +6 for status, length and ctl byte )
590 and the status word ( set to zeros ) */
592 SMC_outl ((length + 6) << 16, SMC91111_DATA_REG);
594 SMC_outw (0, SMC91111_DATA_REG);
595 /* send the packet length ( +6 for status words, length, and ctl */
596 SMC_outw ((length + 6), SMC91111_DATA_REG);
599 /* send the actual data
600 . I _think_ it's faster to send the longs first, and then
601 . mop up by sending the last word. It depends heavily
602 . on alignment, at least on the 486. Maybe it would be
603 . a good idea to check which is optimal? But that could take
604 . almost as much time as is saved?
607 SMC_outsl (SMC91111_DATA_REG, buf, length >> 2);
609 SMC_outw (*((word *) (buf + (length & 0xFFFFFFFC))),
612 SMC_outsw (SMC91111_DATA_REG, buf, (length) >> 1);
613 #endif /* USE_32_BIT */
615 /* Send the last byte, if there is one. */
616 if ((length & 1) == 0) {
617 SMC_outw (0, SMC91111_DATA_REG);
619 SMC_outw (buf[length - 1] | 0x2000, SMC91111_DATA_REG);
622 /* and let the chipset deal with it */
623 SMC_outw (MC_ENQUEUE, MMU_CMD_REG);
625 /* poll for TX INT */
626 /* if (poll4int (IM_TX_INT, SMC_TX_TIMEOUT)) { */
627 /* poll for TX_EMPTY INT - autorelease enabled */
628 if (poll4int(IM_TX_EMPTY_INT, SMC_TX_TIMEOUT)) {
630 PRINTK2 ("%s: TX timeout, sending failed...\n", SMC_DEV_NAME);
633 /* no need to release, MMU does that now */
634 /* SMC_outw (MC_FREEPKT, MMU_CMD_REG); */
636 /* wait for MMU getting ready (low) */
637 while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
641 PRINTK2 ("MMU ready\n");
647 SMC_outb (IM_TX_EMPTY_INT, SMC91111_INT_REG);
648 /* SMC_outb (IM_TX_INT, SMC91111_INT_REG); */
649 PRINTK2 ("%s: Sent packet of length %d \n", SMC_DEV_NAME,
653 /* no need to release, MMU does that now */
654 /* SMC_outw (MC_FREEPKT, MMU_CMD_REG); */
656 /* wait for MMU getting ready (low) */
657 while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
661 PRINTK2 ("MMU ready\n");
666 /* restore previously saved registers */
667 SMC_outb( saved_pnr, PN_REG );
668 SMC_outw( saved_ptr, PTR_REG );
673 /*-------------------------------------------------------------------------
675 | smc_destructor( struct net_device * dev )
677 | dev, pointer to the device structure
682 ---------------------------------------------------------------------------
684 void smc_destructor()
686 PRINTK2(CARDNAME ": smc_destructor\n");
691 * Open and Initialize the board
693 * Set up everything, reset the card, etc ..
696 static int smc_open (bd_t * bd)
700 PRINTK2 ("%s: smc_open\n", SMC_DEV_NAME);
702 /* reset the hardware */
706 /* Configure the PHY */
707 #ifndef CONFIG_SMC91111_EXT_PHY
708 smc_phy_configure ();
711 /* conservative setting (10Mbps, HalfDuplex, no AutoNeg.) */
712 /* SMC_SELECT_BANK(0); */
713 /* SMC_outw(0, RPC_REG); */
716 err = smc_get_ethaddr (bd); /* set smc_mac_addr, and sync it with u-boot globals */
718 memset (bd->bi_enetaddr, 0, 6); /* hack to make error stick! upper code will abort if not set */
719 return (-1); /* upper code ignores this, but NOT bi_enetaddr */
722 for (i = 0; i < 6; i += 2) {
725 address = smc_mac_addr[i + 1] << 8;
726 address |= smc_mac_addr[i];
727 SMC_outw (address, ADDR0_REG + i);
730 for (i = 0; i < 6; i++)
731 SMC_outb (smc_mac_addr[i], ADDR0_REG + i);
737 /*-------------------------------------------------------------
739 . smc_rcv - receive a packet from the card
741 . There is ( at least ) a packet waiting to be read from
745 . o If an error, record it
746 . o otherwise, read in the packet
747 --------------------------------------------------------------
762 /* save PTR and PTR registers */
763 saved_pnr = SMC_inb( PN_REG );
764 saved_ptr = SMC_inw( PTR_REG );
766 packet_number = SMC_inw( RXFIFO_REG );
768 if ( packet_number & RXFIFO_REMPTY ) {
773 PRINTK3("%s: smc_rcv\n", SMC_DEV_NAME);
774 /* start reading from the start of the packet */
775 SMC_outw( PTR_READ | PTR_RCV | PTR_AUTOINC, PTR_REG );
777 /* First two words are status and packet_length */
779 stat_len = SMC_inl(SMC91111_DATA_REG);
780 status = stat_len & 0xffff;
781 packet_length = stat_len >> 16;
783 status = SMC_inw( SMC91111_DATA_REG );
784 packet_length = SMC_inw( SMC91111_DATA_REG );
787 packet_length &= 0x07ff; /* mask off top bits */
789 PRINTK2("RCV: STATUS %4x LENGTH %4x\n", status, packet_length );
791 if ( !(status & RS_ERRORS ) ){
792 /* Adjust for having already read the first two words */
793 packet_length -= 4; /*4; */
796 /* set odd length for bug in LAN91C111, */
797 /* which never sets RS_ODDFRAME */
802 PRINTK3(" Reading %d dwords (and %d bytes) \n",
803 packet_length >> 2, packet_length & 3 );
804 /* QUESTION: Like in the TX routine, do I want
805 to send the DWORDs or the bytes first, or some
806 mixture. A mixture might improve already slow PIO
808 SMC_insl( SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 2 );
809 /* read the left over bytes */
810 if (packet_length & 3) {
813 byte *tail = (byte *)(NetRxPackets[0] + (packet_length & ~3));
814 dword leftover = SMC_inl(SMC91111_DATA_REG);
815 for (i=0; i<(packet_length & 3); i++)
816 *tail++ = (byte) (leftover >> (8*i)) & 0xff;
819 PRINTK3(" Reading %d words and %d byte(s) \n",
820 (packet_length >> 1 ), packet_length & 1 );
821 SMC_insw(SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 1);
823 #endif /* USE_32_BIT */
826 printf("Receiving Packet\n");
827 print_packet( NetRxPackets[0], packet_length );
835 while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
836 udelay(1); /* Wait until not busy */
838 /* error or good, tell the card to get rid of this packet */
839 SMC_outw( MC_RELEASE, MMU_CMD_REG );
841 while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
842 udelay(1); /* Wait until not busy */
844 /* restore saved registers */
845 SMC_outb( saved_pnr, PN_REG );
846 SMC_outw( saved_ptr, PTR_REG );
849 /* Pass the packet up to the protocol layers. */
850 NetReceive(NetRxPackets[0], packet_length);
851 return packet_length;
859 /*----------------------------------------------------
862 . this makes the board clean up everything that it can
863 . and not talk to the outside world. Caused by
864 . an 'ifconfig ethX down'
866 -----------------------------------------------------*/
867 static int smc_close()
869 PRINTK2("%s: smc_close\n", SMC_DEV_NAME);
871 /* clear everything */
879 /*------------------------------------------------------------
880 . Modify a bit in the LAN91C111 register set
881 .-------------------------------------------------------------*/
882 static word smc_modify_regbit(int bank, int ioaddr, int reg,
883 unsigned int bit, int val)
887 SMC_SELECT_BANK( bank );
889 regval = SMC_inw( reg );
895 SMC_outw( regval, 0 );
900 /*------------------------------------------------------------
901 . Retrieve a bit in the LAN91C111 register set
902 .-------------------------------------------------------------*/
903 static int smc_get_regbit(int bank, int ioaddr, int reg, unsigned int bit)
905 SMC_SELECT_BANK( bank );
906 if ( SMC_inw( reg ) & bit)
913 /*------------------------------------------------------------
914 . Modify a LAN91C111 register (word access only)
915 .-------------------------------------------------------------*/
916 static void smc_modify_reg(int bank, int ioaddr, int reg, word val)
918 SMC_SELECT_BANK( bank );
919 SMC_outw( val, reg );
923 /*------------------------------------------------------------
924 . Retrieve a LAN91C111 register (word access only)
925 .-------------------------------------------------------------*/
926 static int smc_get_reg(int bank, int ioaddr, int reg)
928 SMC_SELECT_BANK( bank );
929 return(SMC_inw( reg ));
934 /*---PHY CONTROL AND CONFIGURATION----------------------------------------- */
938 /*------------------------------------------------------------
939 . Debugging function for viewing MII Management serial bitstream
940 .-------------------------------------------------------------*/
941 static void smc_dump_mii_stream (byte * bits, int size)
946 for (i = 0; i < size; ++i) {
947 printf ("%d", i % 10);
951 for (i = 0; i < size; ++i) {
952 if (bits[i] & MII_MDOE)
959 for (i = 0; i < size; ++i) {
960 if (bits[i] & MII_MDO)
967 for (i = 0; i < size; ++i) {
968 if (bits[i] & MII_MDI)
978 /*------------------------------------------------------------
979 . Reads a register from the MII Management serial interface
980 .-------------------------------------------------------------*/
981 #ifndef CONFIG_SMC91111_EXT_PHY
982 static word smc_read_phy_register (byte phyreg)
992 byte phyaddr = SMC_PHY_ADDR;
994 /* 32 consecutive ones on MDO to establish sync */
995 for (i = 0; i < 32; ++i)
996 bits[clk_idx++] = MII_MDOE | MII_MDO;
998 /* Start code <01> */
999 bits[clk_idx++] = MII_MDOE;
1000 bits[clk_idx++] = MII_MDOE | MII_MDO;
1002 /* Read command <10> */
1003 bits[clk_idx++] = MII_MDOE | MII_MDO;
1004 bits[clk_idx++] = MII_MDOE;
1006 /* Output the PHY address, msb first */
1008 for (i = 0; i < 5; ++i) {
1010 bits[clk_idx++] = MII_MDOE | MII_MDO;
1012 bits[clk_idx++] = MII_MDOE;
1014 /* Shift to next lowest bit */
1018 /* Output the phy register number, msb first */
1020 for (i = 0; i < 5; ++i) {
1022 bits[clk_idx++] = MII_MDOE | MII_MDO;
1024 bits[clk_idx++] = MII_MDOE;
1026 /* Shift to next lowest bit */
1030 /* Tristate and turnaround (2 bit times) */
1031 bits[clk_idx++] = 0;
1032 /*bits[clk_idx++] = 0; */
1034 /* Input starts at this bit time */
1035 input_idx = clk_idx;
1037 /* Will input 16 bits */
1038 for (i = 0; i < 16; ++i)
1039 bits[clk_idx++] = 0;
1041 /* Final clock bit */
1042 bits[clk_idx++] = 0;
1044 /* Save the current bank */
1045 oldBank = SMC_inw (BANK_SELECT);
1048 SMC_SELECT_BANK (3);
1050 /* Get the current MII register value */
1051 mii_reg = SMC_inw (MII_REG);
1053 /* Turn off all MII Interface bits */
1054 mii_reg &= ~(MII_MDOE | MII_MCLK | MII_MDI | MII_MDO);
1056 /* Clock all 64 cycles */
1057 for (i = 0; i < sizeof bits; ++i) {
1058 /* Clock Low - output data */
1059 SMC_outw (mii_reg | bits[i], MII_REG);
1060 udelay (SMC_PHY_CLOCK_DELAY);
1063 /* Clock Hi - input data */
1064 SMC_outw (mii_reg | bits[i] | MII_MCLK, MII_REG);
1065 udelay (SMC_PHY_CLOCK_DELAY);
1066 bits[i] |= SMC_inw (MII_REG) & MII_MDI;
1069 /* Return to idle state */
1070 /* Set clock to low, data to low, and output tristated */
1071 SMC_outw (mii_reg, MII_REG);
1072 udelay (SMC_PHY_CLOCK_DELAY);
1074 /* Restore original bank select */
1075 SMC_SELECT_BANK (oldBank);
1077 /* Recover input data */
1079 for (i = 0; i < 16; ++i) {
1082 if (bits[input_idx++] & MII_MDI)
1086 #if (SMC_DEBUG > 2 )
1087 printf ("smc_read_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
1088 phyaddr, phyreg, phydata);
1089 smc_dump_mii_stream (bits, sizeof bits);
1096 /*------------------------------------------------------------
1097 . Writes a register to the MII Management serial interface
1098 .-------------------------------------------------------------*/
1099 static void smc_write_phy_register (byte phyreg, word phydata)
1107 byte phyaddr = SMC_PHY_ADDR;
1109 /* 32 consecutive ones on MDO to establish sync */
1110 for (i = 0; i < 32; ++i)
1111 bits[clk_idx++] = MII_MDOE | MII_MDO;
1113 /* Start code <01> */
1114 bits[clk_idx++] = MII_MDOE;
1115 bits[clk_idx++] = MII_MDOE | MII_MDO;
1117 /* Write command <01> */
1118 bits[clk_idx++] = MII_MDOE;
1119 bits[clk_idx++] = MII_MDOE | MII_MDO;
1121 /* Output the PHY address, msb first */
1123 for (i = 0; i < 5; ++i) {
1125 bits[clk_idx++] = MII_MDOE | MII_MDO;
1127 bits[clk_idx++] = MII_MDOE;
1129 /* Shift to next lowest bit */
1133 /* Output the phy register number, msb first */
1135 for (i = 0; i < 5; ++i) {
1137 bits[clk_idx++] = MII_MDOE | MII_MDO;
1139 bits[clk_idx++] = MII_MDOE;
1141 /* Shift to next lowest bit */
1145 /* Tristate and turnaround (2 bit times) */
1146 bits[clk_idx++] = 0;
1147 bits[clk_idx++] = 0;
1149 /* Write out 16 bits of data, msb first */
1151 for (i = 0; i < 16; ++i) {
1153 bits[clk_idx++] = MII_MDOE | MII_MDO;
1155 bits[clk_idx++] = MII_MDOE;
1157 /* Shift to next lowest bit */
1161 /* Final clock bit (tristate) */
1162 bits[clk_idx++] = 0;
1164 /* Save the current bank */
1165 oldBank = SMC_inw (BANK_SELECT);
1168 SMC_SELECT_BANK (3);
1170 /* Get the current MII register value */
1171 mii_reg = SMC_inw (MII_REG);
1173 /* Turn off all MII Interface bits */
1174 mii_reg &= ~(MII_MDOE | MII_MCLK | MII_MDI | MII_MDO);
1176 /* Clock all cycles */
1177 for (i = 0; i < sizeof bits; ++i) {
1178 /* Clock Low - output data */
1179 SMC_outw (mii_reg | bits[i], MII_REG);
1180 udelay (SMC_PHY_CLOCK_DELAY);
1183 /* Clock Hi - input data */
1184 SMC_outw (mii_reg | bits[i] | MII_MCLK, MII_REG);
1185 udelay (SMC_PHY_CLOCK_DELAY);
1186 bits[i] |= SMC_inw (MII_REG) & MII_MDI;
1189 /* Return to idle state */
1190 /* Set clock to low, data to low, and output tristated */
1191 SMC_outw (mii_reg, MII_REG);
1192 udelay (SMC_PHY_CLOCK_DELAY);
1194 /* Restore original bank select */
1195 SMC_SELECT_BANK (oldBank);
1197 #if (SMC_DEBUG > 2 )
1198 printf ("smc_write_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
1199 phyaddr, phyreg, phydata);
1200 smc_dump_mii_stream (bits, sizeof bits);
1203 #endif /* !CONFIG_SMC91111_EXT_PHY */
1206 /*------------------------------------------------------------
1207 . Waits the specified number of milliseconds - kernel friendly
1208 .-------------------------------------------------------------*/
1209 #ifndef CONFIG_SMC91111_EXT_PHY
1210 static void smc_wait_ms(unsigned int ms)
1214 #endif /* !CONFIG_SMC91111_EXT_PHY */
1217 /*------------------------------------------------------------
1218 . Configures the specified PHY using Autonegotiation. Calls
1219 . smc_phy_fixed() if the user has requested a certain config.
1220 .-------------------------------------------------------------*/
1221 #ifndef CONFIG_SMC91111_EXT_PHY
1222 static void smc_phy_configure ()
1226 word my_phy_caps; /* My PHY capabilities */
1227 word my_ad_caps; /* My Advertised capabilities */
1228 word status = 0; /*;my status = 0 */
1231 PRINTK3 ("%s: smc_program_phy()\n", SMC_DEV_NAME);
1234 /* Get the detected phy address */
1235 phyaddr = SMC_PHY_ADDR;
1237 /* Reset the PHY, setting all other bits to zero */
1238 smc_write_phy_register (PHY_CNTL_REG, PHY_CNTL_RST);
1240 /* Wait for the reset to complete, or time out */
1241 timeout = 6; /* Wait up to 3 seconds */
1243 if (!(smc_read_phy_register (PHY_CNTL_REG)
1245 /* reset complete */
1249 smc_wait_ms (500); /* wait 500 millisecs */
1253 printf ("%s:PHY reset timed out\n", SMC_DEV_NAME);
1254 goto smc_phy_configure_exit;
1257 /* Read PHY Register 18, Status Output */
1258 /* lp->lastPhy18 = smc_read_phy_register(PHY_INT_REG); */
1260 /* Enable PHY Interrupts (for register 18) */
1261 /* Interrupts listed here are disabled */
1262 smc_write_phy_register (PHY_MASK_REG, 0xffff);
1264 /* Configure the Receive/Phy Control register */
1265 SMC_SELECT_BANK (0);
1266 SMC_outw (RPC_DEFAULT, RPC_REG);
1268 /* Copy our capabilities from PHY_STAT_REG to PHY_AD_REG */
1269 my_phy_caps = smc_read_phy_register (PHY_STAT_REG);
1270 my_ad_caps = PHY_AD_CSMA; /* I am CSMA capable */
1272 if (my_phy_caps & PHY_STAT_CAP_T4)
1273 my_ad_caps |= PHY_AD_T4;
1275 if (my_phy_caps & PHY_STAT_CAP_TXF)
1276 my_ad_caps |= PHY_AD_TX_FDX;
1278 if (my_phy_caps & PHY_STAT_CAP_TXH)
1279 my_ad_caps |= PHY_AD_TX_HDX;
1281 if (my_phy_caps & PHY_STAT_CAP_TF)
1282 my_ad_caps |= PHY_AD_10_FDX;
1284 if (my_phy_caps & PHY_STAT_CAP_TH)
1285 my_ad_caps |= PHY_AD_10_HDX;
1287 /* Update our Auto-Neg Advertisement Register */
1288 smc_write_phy_register (PHY_AD_REG, my_ad_caps);
1290 /* Read the register back. Without this, it appears that when */
1291 /* auto-negotiation is restarted, sometimes it isn't ready and */
1292 /* the link does not come up. */
1293 smc_read_phy_register(PHY_AD_REG);
1295 PRINTK2 ("%s: phy caps=%x\n", SMC_DEV_NAME, my_phy_caps);
1296 PRINTK2 ("%s: phy advertised caps=%x\n", SMC_DEV_NAME, my_ad_caps);
1298 /* Restart auto-negotiation process in order to advertise my caps */
1299 smc_write_phy_register (PHY_CNTL_REG,
1300 PHY_CNTL_ANEG_EN | PHY_CNTL_ANEG_RST);
1302 /* Wait for the auto-negotiation to complete. This may take from */
1303 /* 2 to 3 seconds. */
1304 /* Wait for the reset to complete, or time out */
1305 timeout = CONFIG_SMC_AUTONEG_TIMEOUT * 2;
1308 status = smc_read_phy_register (PHY_STAT_REG);
1309 if (status & PHY_STAT_ANEG_ACK) {
1310 /* auto-negotiate complete */
1314 smc_wait_ms (500); /* wait 500 millisecs */
1316 /* Restart auto-negotiation if remote fault */
1317 if (status & PHY_STAT_REM_FLT) {
1318 printf ("%s: PHY remote fault detected\n",
1321 /* Restart auto-negotiation */
1322 printf ("%s: PHY restarting auto-negotiation\n",
1324 smc_write_phy_register (PHY_CNTL_REG,
1333 printf ("%s: PHY auto-negotiate timed out\n", SMC_DEV_NAME);
1337 /* Fail if we detected an auto-negotiate remote fault */
1338 if (status & PHY_STAT_REM_FLT) {
1339 printf ("%s: PHY remote fault detected\n", SMC_DEV_NAME);
1343 /* Re-Configure the Receive/Phy Control register */
1344 SMC_outw (RPC_DEFAULT, RPC_REG);
1346 smc_phy_configure_exit:
1349 #endif /* !CONFIG_SMC91111_EXT_PHY */
1353 static void print_packet( byte * buf, int length )
1359 printf("Packet of length %d \n", length );
1362 lines = length / 16;
1363 remainder = length % 16;
1365 for ( i = 0; i < lines ; i ++ ) {
1368 for ( cur = 0; cur < 8; cur ++ ) {
1373 printf("%02x%02x ", a, b );
1377 for ( i = 0; i < remainder/2 ; i++ ) {
1382 printf("%02x%02x ", a, b );
1389 int eth_init(bd_t *bd) {
1390 return (smc_open(bd));
1401 int eth_send(volatile void *packet, int length) {
1402 return smc_send_packet(packet, length);
1405 int smc_get_ethaddr (bd_t * bd)
1407 int env_size, rom_valid, env_present = 0, reg;
1408 char *s = NULL, *e, *v_mac, es[] = "11:22:33:44:55:66";
1409 uchar s_env_mac[64], v_env_mac[6], v_rom_mac[6];
1411 env_size = getenv_r ("ethaddr", s_env_mac, sizeof (s_env_mac));
1412 if ((env_size > 0) && (env_size < sizeof (es))) { /* exit if env is bad */
1413 printf ("\n*** ERROR: ethaddr is not set properly!!\n");
1422 for (reg = 0; reg < 6; ++reg) { /* turn string into mac value */
1423 v_env_mac[reg] = s ? simple_strtoul (s, &e, 16) : 0;
1425 s = (*e) ? e + 1 : e;
1428 rom_valid = get_rom_mac (v_rom_mac); /* get ROM mac value if any */
1430 if (!env_present) { /* if NO env */
1431 if (rom_valid) { /* but ROM is valid */
1433 sprintf (s_env_mac, "%02X:%02X:%02X:%02X:%02X:%02X",
1434 v_mac[0], v_mac[1], v_mac[2], v_mac[3],
1435 v_mac[4], v_mac[5]);
1436 setenv ("ethaddr", s_env_mac);
1437 } else { /* no env, bad ROM */
1438 printf ("\n*** ERROR: ethaddr is NOT set !!\n");
1441 } else { /* good env, don't care ROM */
1442 v_mac = v_env_mac; /* always use a good env over a ROM */
1445 if (env_present && rom_valid) { /* if both env and ROM are good */
1446 if (memcmp (v_env_mac, v_rom_mac, 6) != 0) {
1447 printf ("\nWarning: MAC addresses don't match:\n");
1448 printf ("\tHW MAC address: "
1449 "%02X:%02X:%02X:%02X:%02X:%02X\n",
1450 v_rom_mac[0], v_rom_mac[1],
1451 v_rom_mac[2], v_rom_mac[3],
1452 v_rom_mac[4], v_rom_mac[5] );
1453 printf ("\t\"ethaddr\" value: "
1454 "%02X:%02X:%02X:%02X:%02X:%02X\n",
1455 v_env_mac[0], v_env_mac[1],
1456 v_env_mac[2], v_env_mac[3],
1457 v_env_mac[4], v_env_mac[5]) ;
1458 debug ("### Set MAC addr from environment\n");
1461 memcpy (bd->bi_enetaddr, v_mac, 6); /* update global address to match env (allows env changing) */
1462 smc_set_mac_addr (v_mac); /* use old function to update smc default */
1463 PRINTK("Using MAC Address %02X:%02X:%02X:%02X:%02X:%02X\n", v_mac[0], v_mac[1],
1464 v_mac[2], v_mac[3], v_mac[4], v_mac[5]);
1468 int get_rom_mac (char *v_rom_mac)
1470 #ifdef HARDCODE_MAC /* used for testing or to supress run time warnings */
1471 char hw_mac_addr[] = { 0x02, 0x80, 0xad, 0x20, 0x31, 0xb8 };
1473 memcpy (v_rom_mac, hw_mac_addr, 6);
1479 SMC_SELECT_BANK (1);
1482 v_rom_mac[i] = SMC_inb (ADDR0_REG + i);
1483 valid_mac |= v_rom_mac[i];
1486 return (valid_mac ? 1 : 0);
1489 #endif /* CONFIG_DRIVER_SMC91111 */