1 /*------------------------------------------------------------------------
3 . This is a driver for SMSC's 91C111 single-chip Ethernet device.
6 . Sysgo Real-Time Solutions, GmbH <www.elinos.com>
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
50 . o SMSC LAN91C111 databook (www.smsc.com)
51 . o smc9194.c by Erik Stahlman
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 ----------------------------------------------------------------------------*/
67 #ifdef CONFIG_DRIVER_SMC91111
69 /* Use power-down feature of the chip */
77 static const char version[] =
81 /*------------------------------------------------------------------------
83 . Configuration options, for the experienced user to change.
85 -------------------------------------------------------------------------*/
88 . Wait time for memory to be free. This probably shouldn't be
89 . tuned that much, as waiting for this means nothing else happens
92 #define MEMORY_WAIT_TIME 16
96 #define PRINTK3(args...) printf(args)
98 #define PRINTK3(args...)
102 #define PRINTK2(args...) printf(args)
104 #define PRINTK2(args...)
108 #define PRINTK(args...) printf(args)
110 #define PRINTK(args...)
114 /*------------------------------------------------------------------------
116 . The internal workings of the driver. If you are changing anything
117 . here with the SMC stuff, you should have the datasheet and know
118 . what you are doing.
120 -------------------------------------------------------------------------*/
121 #define CARDNAME "LAN91C111"
123 /* Memory sizing constant */
124 #define LAN91C111_MEMORY_MULTIPLIER (1024*2)
126 #ifndef CONFIG_SMC91111_BASE
127 #define CONFIG_SMC91111_BASE 0x20000300
130 #define SMC_BASE_ADDRESS CONFIG_SMC91111_BASE
132 #define SMC_DEV_NAME "SMC91111"
133 #define SMC_PHY_ADDR 0x0000
134 #define SMC_ALLOC_MAX_TRY 5
135 #define SMC_TX_TIMEOUT 30
137 #define SMC_PHY_CLOCK_DELAY 1000
141 #ifdef CONFIG_SMC_USE_32_BIT
146 /*-----------------------------------------------------------------
148 . The driver can be entered at any of the following entry points.
150 .------------------------------------------------------------------ */
152 extern int eth_init(bd_t *bd);
153 extern void eth_halt(void);
154 extern int eth_rx(void);
155 extern int eth_send(volatile void *packet, int length);
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.
165 . NB:This shouldn't be static since it is referred to externally.
170 . This is called by unregister_netdev(). It is responsible for
171 . cleaning up before the driver is finally unregistered and discarded.
173 void smc_destructor(void);
176 . The kernel calls this function when someone wants to use the device,
177 . typically 'ifconfig ethX up'.
179 static int smc_open(bd_t *bd);
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.
187 static int smc_close(void);
190 . Configures the PHY through the MII Management interface
192 #ifndef CONFIG_SMC91111_EXT_PHY
193 static void smc_phy_configure(void);
194 #endif /* !CONFIG_SMC91111_EXT_PHY */
197 . This is a separate procedure to handle the receipt of a packet, to
198 . leave the interrupt code looking slightly cleaner
200 static int smc_rcv(void);
202 /* See if a MAC address is defined in the current environment. If so use it. If not
203 . print a warning and set the environment and other globals with the default.
204 . If an EEPROM is present it really should be consulted.
206 int smc_get_ethaddr(bd_t *bd);
207 int get_rom_mac(char *v_rom_mac);
210 ------------------------------------------------------------
214 ------------------------------------------------------------
217 static char unsigned smc_mac_addr[6] = {0x02, 0x80, 0xad, 0x20, 0x31, 0xb8};
220 * This function must be called before smc_open() if you want to override
221 * the default mac address.
224 void smc_set_mac_addr(const char *addr) {
227 for (i=0; i < sizeof(smc_mac_addr); i++){
228 smc_mac_addr[i] = addr[i];
233 * smc_get_macaddr is no longer used. If you want to override the default
234 * mac address, call smc_get_mac_addr as a part of the board initialization.
238 void smc_get_macaddr( byte *addr ) {
239 /* MAC ADDRESS AT FLASHBLOCK 1 / OFFSET 0x10 */
240 unsigned char *dnp1110_mac = (unsigned char *) (0xE8000000 + 0x20010);
244 for (i=0; i<6; i++) {
245 addr[0] = *(dnp1110_mac+0);
246 addr[1] = *(dnp1110_mac+1);
247 addr[2] = *(dnp1110_mac+2);
248 addr[3] = *(dnp1110_mac+3);
249 addr[4] = *(dnp1110_mac+4);
250 addr[5] = *(dnp1110_mac+5);
255 /***********************************************
256 * Show available memory *
257 ***********************************************/
258 void dump_memory_info(void)
263 old_bank = SMC_inw(BANK_SELECT)&0xF;
266 mem_info = SMC_inw( MIR_REG );
267 PRINTK2("Memory: %4d available\n", (mem_info >> 8)*2048);
269 SMC_SELECT_BANK(old_bank);
272 . A rather simple routine to print out a packet for debugging purposes.
275 static void print_packet( byte *, int );
278 #define tx_done(dev) 1
281 /* this does a soft reset on the device */
282 static void smc_reset( void );
284 /* Enable Interrupts, Receive, and Transmit */
285 static void smc_enable( void );
287 /* this puts the device in an inactive state */
288 static void smc_shutdown( void );
290 /* Routines to Read and Write the PHY Registers across the
291 MII Management Interface
294 #ifndef CONFIG_SMC91111_EXT_PHY
295 static word smc_read_phy_register(byte phyreg);
296 static void smc_write_phy_register(byte phyreg, word phydata);
297 #endif /* !CONFIG_SMC91111_EXT_PHY */
300 static int poll4int (byte mask, int timeout)
302 int tmo = get_timer (0) + timeout * CFG_HZ;
304 word old_bank = SMC_inw (BSR_REG);
306 PRINTK2 ("Polling...\n");
308 while ((SMC_inw (SMC91111_INT_REG) & mask) == 0) {
309 if (get_timer (0) >= tmo) {
315 /* restore old bank selection */
316 SMC_SELECT_BANK (old_bank);
324 /* Only one release command at a time, please */
325 static inline void smc_wait_mmu_release_complete (void)
329 /* assume bank 2 selected */
330 while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
331 udelay (1); /* Wait until not busy */
338 . Function: smc_reset( void )
340 . This sets the SMC91111 chip to its normal state, hopefully from whatever
341 . mess that any other DOS driver has put it in.
343 . Maybe I should reset more registers to defaults in here? SOFTRST should
347 . 1. send a SOFT RESET
348 . 2. wait for it to finish
349 . 3. enable autorelease mode
350 . 4. reset the memory management unit
351 . 5. clear all interrupts
354 static void smc_reset (void)
356 PRINTK2 ("%s:smc_reset\n", SMC_DEV_NAME);
358 /* This resets the registers mostly to defaults, but doesn't
359 affect EEPROM. That seems unnecessary */
361 SMC_outw (RCR_SOFTRST, RCR_REG);
363 /* Setup the Configuration Register */
364 /* This is necessary because the CONFIG_REG is not affected */
365 /* by a soft reset */
368 #if defined(CONFIG_SMC91111_EXT_PHY)
369 SMC_outw (CONFIG_DEFAULT | CONFIG_EXT_PHY, CONFIG_REG);
371 SMC_outw (CONFIG_DEFAULT, CONFIG_REG);
375 /* Release from possible power-down state */
376 /* Configuration register is not affected by Soft Reset */
377 SMC_outw (SMC_inw (CONFIG_REG) | CONFIG_EPH_POWER_EN, CONFIG_REG);
381 /* this should pause enough for the chip to be happy */
384 /* Disable transmit and receive functionality */
385 SMC_outw (RCR_CLEAR, RCR_REG);
386 SMC_outw (TCR_CLEAR, TCR_REG);
388 /* set the control register */
390 SMC_outw (CTL_DEFAULT, CTL_REG);
394 smc_wait_mmu_release_complete ();
395 SMC_outw (MC_RESET, MMU_CMD_REG);
396 while (SMC_inw (MMU_CMD_REG) & MC_BUSY)
397 udelay (1); /* Wait until not busy */
399 /* Note: It doesn't seem that waiting for the MMU busy is needed here,
400 but this is a place where future chipsets _COULD_ break. Be wary
401 of issuing another MMU command right after this */
403 /* Disable all interrupts */
404 SMC_outb (0, IM_REG);
408 . Function: smc_enable
409 . Purpose: let the chip talk to the outside work
411 . 1. Enable the transmitter
412 . 2. Enable the receiver
413 . 3. Enable interrupts
415 static void smc_enable()
417 PRINTK2("%s:smc_enable\n", SMC_DEV_NAME);
418 SMC_SELECT_BANK( 0 );
419 /* see the header file for options in TCR/RCR DEFAULT*/
420 SMC_outw( TCR_DEFAULT, TCR_REG );
421 SMC_outw( RCR_DEFAULT, RCR_REG );
424 /* smc_write_phy_register(PHY_CNTL_REG, 0x0000); */
428 . Function: smc_shutdown
429 . Purpose: closes down the SMC91xxx chip.
431 . 1. zero the interrupt mask
432 . 2. clear the enable receive flag
433 . 3. clear the enable xmit flags
436 . (1) maybe utilize power down mode.
437 . Why not yet? Because while the chip will go into power down mode,
438 . the manual says that it will wake up in response to any I/O requests
439 . in the register space. Empirical results do not show this working.
441 static void smc_shutdown()
443 PRINTK2(CARDNAME ":smc_shutdown\n");
445 /* no more interrupts for me */
446 SMC_SELECT_BANK( 2 );
447 SMC_outb( 0, IM_REG );
449 /* and tell the card to stay away from that nasty outside world */
450 SMC_SELECT_BANK( 0 );
451 SMC_outb( RCR_CLEAR, RCR_REG );
452 SMC_outb( TCR_CLEAR, TCR_REG );
457 . Function: smc_hardware_send_packet(struct net_device * )
459 . This sends the actual packet to the SMC9xxx chip.
462 . First, see if a saved_skb is available.
463 . ( this should NOT be called if there is no 'saved_skb'
464 . Now, find the packet number that the chip allocated
465 . Point the data pointers at it in memory
466 . Set the length word in the chip's memory
467 . Dump the packet to chip memory
468 . Check if a last byte is needed ( odd length packet )
469 . if so, set the control flag right
470 . Tell the card to send it
471 . Enable the transmit interrupt, so I know if it failed
472 . Free the kernel data if I actually sent it.
474 static int smc_send_packet (volatile void *packet, int packet_length)
477 unsigned long ioaddr;
486 PRINTK3 ("%s:smc_hardware_send_packet\n", SMC_DEV_NAME);
488 length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
491 ** The MMU wants the number of pages to be the number of 256 bytes
492 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
494 ** The 91C111 ignores the size bits, but the code is left intact
495 ** for backwards and future compatibility.
497 ** Pkt size for allocating is data length +6 (for additional status
498 ** words, length and ctl!)
500 ** If odd size then last byte is included in this header.
502 numPages = ((length & 0xfffe) + 6);
503 numPages >>= 8; /* Divide by 256 */
506 printf ("%s: Far too big packet error. \n", SMC_DEV_NAME);
510 /* now, try to allocate the memory */
512 SMC_outw (MC_ALLOC | numPages, MMU_CMD_REG);
514 /* FIXME: the ALLOC_INT bit never gets set *
515 * so the following will always give a *
516 * memory allocation error. *
517 * same code works in armboot though *
523 time_out = MEMORY_WAIT_TIME;
525 status = SMC_inb (SMC91111_INT_REG);
526 if (status & IM_ALLOC_INT) {
527 /* acknowledge the interrupt */
528 SMC_outb (IM_ALLOC_INT, SMC91111_INT_REG);
531 } while (--time_out);
534 PRINTK2 ("%s: memory allocation, try %d failed ...\n",
536 if (try < SMC_ALLOC_MAX_TRY)
542 PRINTK2 ("%s: memory allocation, try %d succeeded ...\n",
545 /* I can send the packet now.. */
547 ioaddr = SMC_BASE_ADDRESS;
549 buf = (byte *) packet;
551 /* If I get here, I _know_ there is a packet slot waiting for me */
552 packet_no = SMC_inb (AR_REG);
553 if (packet_no & AR_FAILED) {
554 /* or isn't there? BAD CHIP! */
555 printf ("%s: Memory allocation failed. \n", SMC_DEV_NAME);
559 /* we have a packet address, so tell the card to use it */
560 SMC_outb (packet_no, PN_REG);
562 /* point to the beginning of the packet */
563 SMC_outw (PTR_AUTOINC, PTR_REG);
565 PRINTK3 ("%s: Trying to xmit packet of length %x\n",
566 SMC_DEV_NAME, length);
569 printf ("Transmitting Packet\n");
570 print_packet (buf, length);
573 /* send the packet length ( +6 for status, length and ctl byte )
574 and the status word ( set to zeros ) */
576 SMC_outl ((length + 6) << 16, SMC91111_DATA_REG);
578 SMC_outw (0, SMC91111_DATA_REG);
579 /* send the packet length ( +6 for status words, length, and ctl */
580 SMC_outw ((length + 6), SMC91111_DATA_REG);
583 /* send the actual data
584 . I _think_ it's faster to send the longs first, and then
585 . mop up by sending the last word. It depends heavily
586 . on alignment, at least on the 486. Maybe it would be
587 . a good idea to check which is optimal? But that could take
588 . almost as much time as is saved?
591 SMC_outsl (SMC91111_DATA_REG, buf, length >> 2);
593 SMC_outw (*((word *) (buf + (length & 0xFFFFFFFC))),
596 SMC_outsw (SMC91111_DATA_REG, buf, (length) >> 1);
597 #endif /* USE_32_BIT */
599 /* Send the last byte, if there is one. */
600 if ((length & 1) == 0) {
601 SMC_outw (0, SMC91111_DATA_REG);
603 SMC_outw (buf[length - 1] | 0x2000, SMC91111_DATA_REG);
606 /* and let the chipset deal with it */
607 SMC_outw (MC_ENQUEUE, MMU_CMD_REG);
609 /* poll for TX INT */
610 if (poll4int (IM_TX_INT, SMC_TX_TIMEOUT)) {
612 PRINTK2 ("%s: TX timeout, sending failed...\n", SMC_DEV_NAME);
615 SMC_outw (MC_FREEPKT, MMU_CMD_REG);
617 /* wait for MMU getting ready (low) */
618 while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
622 PRINTK2 ("MMU ready\n");
628 SMC_outb (IM_TX_INT, SMC91111_INT_REG);
629 PRINTK2 ("%s: Sent packet of length %d \n", SMC_DEV_NAME,
633 SMC_outw (MC_FREEPKT, MMU_CMD_REG);
635 /* wait for MMU getting ready (low) */
636 while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
640 PRINTK2 ("MMU ready\n");
648 /*-------------------------------------------------------------------------
650 | smc_destructor( struct net_device * dev )
652 | dev, pointer to the device structure
657 ---------------------------------------------------------------------------
659 void smc_destructor()
661 PRINTK2(CARDNAME ":smc_destructor\n");
666 * Open and Initialize the board
668 * Set up everything, reset the card, etc ..
671 static int smc_open (bd_t * bd)
675 PRINTK2 ("%s:smc_open\n", SMC_DEV_NAME);
677 /* reset the hardware */
681 /* Configure the PHY */
682 #ifndef CONFIG_SMC91111_EXT_PHY
683 smc_phy_configure ();
686 /* conservative setting (10Mbps, HalfDuplex, no AutoNeg.) */
687 /* SMC_SELECT_BANK(0); */
688 /* SMC_outw(0, RPC_REG); */
691 err = smc_get_ethaddr (bd); /* set smc_mac_addr, and sync it with u-boot globals */
693 memset (bd->bi_enetaddr, 0, 6); /* hack to make error stick! upper code will abort if not set */
694 return (-1); /* upper code ignores this, but NOT bi_enetaddr */
697 for (i = 0; i < 6; i += 2) {
700 address = smc_mac_addr[i + 1] << 8;
701 address |= smc_mac_addr[i];
702 SMC_outw (address, ADDR0_REG + i);
705 for (i = 0; i < 6; i++)
706 SMC_outb (smc_mac_addr[i], ADDR0_REG + i);
712 /*-------------------------------------------------------------
714 . smc_rcv - receive a packet from the card
716 . There is ( at least ) a packet waiting to be read from
720 . o If an error, record it
721 . o otherwise, read in the packet
722 --------------------------------------------------------------
735 packet_number = SMC_inw( RXFIFO_REG );
737 if ( packet_number & RXFIFO_REMPTY ) {
742 PRINTK3("%s:smc_rcv\n", SMC_DEV_NAME);
743 /* start reading from the start of the packet */
744 SMC_outw( PTR_READ | PTR_RCV | PTR_AUTOINC, PTR_REG );
746 /* First two words are status and packet_length */
748 stat_len = SMC_inl(SMC91111_DATA_REG);
749 status = stat_len & 0xffff;
750 packet_length = stat_len >> 16;
752 status = SMC_inw( SMC91111_DATA_REG );
753 packet_length = SMC_inw( SMC91111_DATA_REG );
756 packet_length &= 0x07ff; /* mask off top bits */
758 PRINTK2("RCV: STATUS %4x LENGTH %4x\n", status, packet_length );
760 if ( !(status & RS_ERRORS ) ){
761 /* Adjust for having already read the first two words */
762 packet_length -= 4; /*4; */
765 /* set odd length for bug in LAN91C111, */
766 /* which never sets RS_ODDFRAME */
771 PRINTK3(" Reading %d dwords (and %d bytes) \n",
772 packet_length >> 2, packet_length & 3 );
773 /* QUESTION: Like in the TX routine, do I want
774 to send the DWORDs or the bytes first, or some
775 mixture. A mixture might improve already slow PIO
777 SMC_insl( SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 2 );
778 /* read the left over bytes */
779 if (packet_length & 3) {
782 byte *tail = (byte *)(NetRxPackets[0] + (packet_length & ~3));
783 dword leftover = SMC_inl(SMC91111_DATA_REG);
784 for (i=0; i<(packet_length & 3); i++)
785 *tail++ = (byte) (leftover >> (8*i)) & 0xff;
788 PRINTK3(" Reading %d words and %d byte(s) \n",
789 (packet_length >> 1 ), packet_length & 1 );
790 SMC_insw(SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 1);
792 #endif /* USE_32_BIT */
795 printf("Receiving Packet\n");
796 print_packet( NetRxPackets[0], packet_length );
804 while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
805 udelay(1); /* Wait until not busy */
807 /* error or good, tell the card to get rid of this packet */
808 SMC_outw( MC_RELEASE, MMU_CMD_REG );
810 while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
811 udelay(1); /* Wait until not busy */
814 /* Pass the packet up to the protocol layers. */
815 NetReceive(NetRxPackets[0], packet_length);
816 return packet_length;
824 /*----------------------------------------------------
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'
831 -----------------------------------------------------*/
832 static int smc_close()
834 PRINTK2("%s:smc_close\n", SMC_DEV_NAME);
836 /* clear everything */
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)
852 SMC_SELECT_BANK( bank );
854 regval = SMC_inw( reg );
860 SMC_outw( regval, 0 );
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)
870 SMC_SELECT_BANK( bank );
871 if ( SMC_inw( reg ) & bit)
878 /*------------------------------------------------------------
879 . Modify a LAN91C111 register (word access only)
880 .-------------------------------------------------------------*/
881 static void smc_modify_reg(int bank, int ioaddr, int reg, word val)
883 SMC_SELECT_BANK( bank );
884 SMC_outw( val, reg );
888 /*------------------------------------------------------------
889 . Retrieve a LAN91C111 register (word access only)
890 .-------------------------------------------------------------*/
891 static int smc_get_reg(int bank, int ioaddr, int reg)
893 SMC_SELECT_BANK( bank );
894 return(SMC_inw( reg ));
899 /*---PHY CONTROL AND CONFIGURATION----------------------------------------- */
903 /*------------------------------------------------------------
904 . Debugging function for viewing MII Management serial bitstream
905 .-------------------------------------------------------------*/
906 static void smc_dump_mii_stream (byte * bits, int size)
911 for (i = 0; i < size; ++i) {
912 printf ("%d", i % 10);
916 for (i = 0; i < size; ++i) {
917 if (bits[i] & MII_MDOE)
924 for (i = 0; i < size; ++i) {
925 if (bits[i] & MII_MDO)
932 for (i = 0; i < size; ++i) {
933 if (bits[i] & MII_MDI)
943 /*------------------------------------------------------------
944 . Reads a register from the MII Management serial interface
945 .-------------------------------------------------------------*/
946 #ifndef CONFIG_SMC91111_EXT_PHY
947 static word smc_read_phy_register (byte phyreg)
957 byte phyaddr = SMC_PHY_ADDR;
959 /* 32 consecutive ones on MDO to establish sync */
960 for (i = 0; i < 32; ++i)
961 bits[clk_idx++] = MII_MDOE | MII_MDO;
963 /* Start code <01> */
964 bits[clk_idx++] = MII_MDOE;
965 bits[clk_idx++] = MII_MDOE | MII_MDO;
967 /* Read command <10> */
968 bits[clk_idx++] = MII_MDOE | MII_MDO;
969 bits[clk_idx++] = MII_MDOE;
971 /* Output the PHY address, msb first */
973 for (i = 0; i < 5; ++i) {
975 bits[clk_idx++] = MII_MDOE | MII_MDO;
977 bits[clk_idx++] = MII_MDOE;
979 /* Shift to next lowest bit */
983 /* Output the phy register number, msb first */
985 for (i = 0; i < 5; ++i) {
987 bits[clk_idx++] = MII_MDOE | MII_MDO;
989 bits[clk_idx++] = MII_MDOE;
991 /* Shift to next lowest bit */
995 /* Tristate and turnaround (2 bit times) */
997 /*bits[clk_idx++] = 0; */
999 /* Input starts at this bit time */
1000 input_idx = clk_idx;
1002 /* Will input 16 bits */
1003 for (i = 0; i < 16; ++i)
1004 bits[clk_idx++] = 0;
1006 /* Final clock bit */
1007 bits[clk_idx++] = 0;
1009 /* Save the current bank */
1010 oldBank = SMC_inw (BANK_SELECT);
1013 SMC_SELECT_BANK (3);
1015 /* Get the current MII register value */
1016 mii_reg = SMC_inw (MII_REG);
1018 /* Turn off all MII Interface bits */
1019 mii_reg &= ~(MII_MDOE | MII_MCLK | MII_MDI | MII_MDO);
1021 /* Clock all 64 cycles */
1022 for (i = 0; i < sizeof bits; ++i) {
1023 /* Clock Low - output data */
1024 SMC_outw (mii_reg | bits[i], MII_REG);
1025 udelay (SMC_PHY_CLOCK_DELAY);
1028 /* Clock Hi - input data */
1029 SMC_outw (mii_reg | bits[i] | MII_MCLK, MII_REG);
1030 udelay (SMC_PHY_CLOCK_DELAY);
1031 bits[i] |= SMC_inw (MII_REG) & MII_MDI;
1034 /* Return to idle state */
1035 /* Set clock to low, data to low, and output tristated */
1036 SMC_outw (mii_reg, MII_REG);
1037 udelay (SMC_PHY_CLOCK_DELAY);
1039 /* Restore original bank select */
1040 SMC_SELECT_BANK (oldBank);
1042 /* Recover input data */
1044 for (i = 0; i < 16; ++i) {
1047 if (bits[input_idx++] & MII_MDI)
1051 #if (SMC_DEBUG > 2 )
1052 printf ("smc_read_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
1053 phyaddr, phyreg, phydata);
1054 smc_dump_mii_stream (bits, sizeof bits);
1061 /*------------------------------------------------------------
1062 . Writes a register to the MII Management serial interface
1063 .-------------------------------------------------------------*/
1064 static void smc_write_phy_register (byte phyreg, word phydata)
1072 byte phyaddr = SMC_PHY_ADDR;
1074 /* 32 consecutive ones on MDO to establish sync */
1075 for (i = 0; i < 32; ++i)
1076 bits[clk_idx++] = MII_MDOE | MII_MDO;
1078 /* Start code <01> */
1079 bits[clk_idx++] = MII_MDOE;
1080 bits[clk_idx++] = MII_MDOE | MII_MDO;
1082 /* Write command <01> */
1083 bits[clk_idx++] = MII_MDOE;
1084 bits[clk_idx++] = MII_MDOE | MII_MDO;
1086 /* Output the PHY address, msb first */
1088 for (i = 0; i < 5; ++i) {
1090 bits[clk_idx++] = MII_MDOE | MII_MDO;
1092 bits[clk_idx++] = MII_MDOE;
1094 /* Shift to next lowest bit */
1098 /* Output the phy register number, msb first */
1100 for (i = 0; i < 5; ++i) {
1102 bits[clk_idx++] = MII_MDOE | MII_MDO;
1104 bits[clk_idx++] = MII_MDOE;
1106 /* Shift to next lowest bit */
1110 /* Tristate and turnaround (2 bit times) */
1111 bits[clk_idx++] = 0;
1112 bits[clk_idx++] = 0;
1114 /* Write out 16 bits of data, msb first */
1116 for (i = 0; i < 16; ++i) {
1118 bits[clk_idx++] = MII_MDOE | MII_MDO;
1120 bits[clk_idx++] = MII_MDOE;
1122 /* Shift to next lowest bit */
1126 /* Final clock bit (tristate) */
1127 bits[clk_idx++] = 0;
1129 /* Save the current bank */
1130 oldBank = SMC_inw (BANK_SELECT);
1133 SMC_SELECT_BANK (3);
1135 /* Get the current MII register value */
1136 mii_reg = SMC_inw (MII_REG);
1138 /* Turn off all MII Interface bits */
1139 mii_reg &= ~(MII_MDOE | MII_MCLK | MII_MDI | MII_MDO);
1141 /* Clock all cycles */
1142 for (i = 0; i < sizeof bits; ++i) {
1143 /* Clock Low - output data */
1144 SMC_outw (mii_reg | bits[i], MII_REG);
1145 udelay (SMC_PHY_CLOCK_DELAY);
1148 /* Clock Hi - input data */
1149 SMC_outw (mii_reg | bits[i] | MII_MCLK, MII_REG);
1150 udelay (SMC_PHY_CLOCK_DELAY);
1151 bits[i] |= SMC_inw (MII_REG) & MII_MDI;
1154 /* Return to idle state */
1155 /* Set clock to low, data to low, and output tristated */
1156 SMC_outw (mii_reg, MII_REG);
1157 udelay (SMC_PHY_CLOCK_DELAY);
1159 /* Restore original bank select */
1160 SMC_SELECT_BANK (oldBank);
1162 #if (SMC_DEBUG > 2 )
1163 printf ("smc_write_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
1164 phyaddr, phyreg, phydata);
1165 smc_dump_mii_stream (bits, sizeof bits);
1168 #endif /* !CONFIG_SMC91111_EXT_PHY */
1171 /*------------------------------------------------------------
1172 . Waits the specified number of milliseconds - kernel friendly
1173 .-------------------------------------------------------------*/
1174 #ifndef CONFIG_SMC91111_EXT_PHY
1175 static void smc_wait_ms(unsigned int ms)
1179 #endif /* !CONFIG_SMC91111_EXT_PHY */
1182 /*------------------------------------------------------------
1183 . Configures the specified PHY using Autonegotiation. Calls
1184 . smc_phy_fixed() if the user has requested a certain config.
1185 .-------------------------------------------------------------*/
1186 #ifndef CONFIG_SMC91111_EXT_PHY
1187 static void smc_phy_configure ()
1191 word my_phy_caps; /* My PHY capabilities */
1192 word my_ad_caps; /* My Advertised capabilities */
1193 word status = 0; /*;my status = 0 */
1196 PRINTK3 ("%s:smc_program_phy()\n", SMC_DEV_NAME);
1199 /* Get the detected phy address */
1200 phyaddr = SMC_PHY_ADDR;
1202 /* Reset the PHY, setting all other bits to zero */
1203 smc_write_phy_register (PHY_CNTL_REG, PHY_CNTL_RST);
1205 /* Wait for the reset to complete, or time out */
1206 timeout = 6; /* Wait up to 3 seconds */
1208 if (!(smc_read_phy_register (PHY_CNTL_REG)
1210 /* reset complete */
1214 smc_wait_ms (500); /* wait 500 millisecs */
1218 printf ("%s:PHY reset timed out\n", SMC_DEV_NAME);
1219 goto smc_phy_configure_exit;
1222 /* Read PHY Register 18, Status Output */
1223 /* lp->lastPhy18 = smc_read_phy_register(PHY_INT_REG); */
1225 /* Enable PHY Interrupts (for register 18) */
1226 /* Interrupts listed here are disabled */
1227 smc_write_phy_register (PHY_MASK_REG, 0xffff);
1229 /* Configure the Receive/Phy Control register */
1230 SMC_SELECT_BANK (0);
1231 SMC_outw (RPC_DEFAULT, RPC_REG);
1233 /* Copy our capabilities from PHY_STAT_REG to PHY_AD_REG */
1234 my_phy_caps = smc_read_phy_register (PHY_STAT_REG);
1235 my_ad_caps = PHY_AD_CSMA; /* I am CSMA capable */
1237 if (my_phy_caps & PHY_STAT_CAP_T4)
1238 my_ad_caps |= PHY_AD_T4;
1240 if (my_phy_caps & PHY_STAT_CAP_TXF)
1241 my_ad_caps |= PHY_AD_TX_FDX;
1243 if (my_phy_caps & PHY_STAT_CAP_TXH)
1244 my_ad_caps |= PHY_AD_TX_HDX;
1246 if (my_phy_caps & PHY_STAT_CAP_TF)
1247 my_ad_caps |= PHY_AD_10_FDX;
1249 if (my_phy_caps & PHY_STAT_CAP_TH)
1250 my_ad_caps |= PHY_AD_10_HDX;
1252 /* Update our Auto-Neg Advertisement Register */
1253 smc_write_phy_register (PHY_AD_REG, my_ad_caps);
1255 PRINTK2 ("%s:phy caps=%x\n", SMC_DEV_NAME, my_phy_caps);
1256 PRINTK2 ("%s:phy advertised caps=%x\n", SMC_DEV_NAME, my_ad_caps);
1258 /* Restart auto-negotiation process in order to advertise my caps */
1259 smc_write_phy_register (PHY_CNTL_REG,
1260 PHY_CNTL_ANEG_EN | PHY_CNTL_ANEG_RST);
1262 /* Wait for the auto-negotiation to complete. This may take from */
1263 /* 2 to 3 seconds. */
1264 /* Wait for the reset to complete, or time out */
1265 timeout = 20; /* Wait up to 10 seconds */
1267 status = smc_read_phy_register (PHY_STAT_REG);
1268 if (status & PHY_STAT_ANEG_ACK) {
1269 /* auto-negotiate complete */
1273 smc_wait_ms (500); /* wait 500 millisecs */
1275 /* Restart auto-negotiation if remote fault */
1276 if (status & PHY_STAT_REM_FLT) {
1277 printf ("%s:PHY remote fault detected\n",
1280 /* Restart auto-negotiation */
1281 printf ("%s:PHY restarting auto-negotiation\n",
1283 smc_write_phy_register (PHY_CNTL_REG,
1292 printf ("%s:PHY auto-negotiate timed out\n", SMC_DEV_NAME);
1293 printf ("%s:PHY auto-negotiate timed out\n", SMC_DEV_NAME);
1297 /* Fail if we detected an auto-negotiate remote fault */
1298 if (status & PHY_STAT_REM_FLT) {
1299 printf ("%s:PHY remote fault detected\n", SMC_DEV_NAME);
1300 printf ("%s:PHY remote fault detected\n", SMC_DEV_NAME);
1304 /* Re-Configure the Receive/Phy Control register */
1305 SMC_outw (RPC_DEFAULT, RPC_REG);
1307 smc_phy_configure_exit:
1310 #endif /* !CONFIG_SMC91111_EXT_PHY */
1314 static void print_packet( byte * buf, int length )
1320 printf("Packet of length %d \n", length );
1323 lines = length / 16;
1324 remainder = length % 16;
1326 for ( i = 0; i < lines ; i ++ ) {
1329 for ( cur = 0; cur < 8; cur ++ ) {
1334 printf("%02x%02x ", a, b );
1338 for ( i = 0; i < remainder/2 ; i++ ) {
1343 printf("%02x%02x ", a, b );
1350 int eth_init(bd_t *bd) {
1351 return (smc_open(bd));
1362 int eth_send(volatile void *packet, int length) {
1363 return smc_send_packet(packet, length);
1366 int smc_get_ethaddr (bd_t * bd)
1368 int env_size, rom_valid, env_present = 0, reg;
1369 char *s = NULL, *e, *v_mac, es[] = "11:22:33:44:55:66";
1370 uchar s_env_mac[64], v_env_mac[6], v_rom_mac[6];
1372 env_size = getenv_r ("ethaddr", s_env_mac, sizeof (s_env_mac));
1373 if ((env_size > 0) && (env_size < sizeof (es))) { /* exit if env is bad */
1374 printf ("\n*** ERROR: ethaddr is not set properly!!\n");
1383 for (reg = 0; reg < 6; ++reg) { /* turn string into mac value */
1384 v_env_mac[reg] = s ? simple_strtoul (s, &e, 16) : 0;
1386 s = (*e) ? e + 1 : e;
1389 rom_valid = get_rom_mac (v_rom_mac); /* get ROM mac value if any */
1391 if (!env_present) { /* if NO env */
1392 if (rom_valid) { /* but ROM is valid */
1394 sprintf (s_env_mac, "%02X:%02X:%02X:%02X:%02X:%02X",
1395 v_mac[0], v_mac[1], v_mac[2], v_mac[3],
1396 v_mac[4], v_mac[5]);
1397 setenv ("ethaddr", s_env_mac);
1398 } else { /* no env, bad ROM */
1399 printf ("\n*** ERROR: ethaddr is NOT set !!\n");
1402 } else { /* good env, don't care ROM */
1403 v_mac = v_env_mac; /* always use a good env over a ROM */
1406 if (env_present && rom_valid) { /* if both env and ROM are good */
1407 if (memcmp (v_env_mac, v_rom_mac, 6) != 0) {
1408 printf ("\nWarning: MAC addresses don't match:\n");
1409 printf ("\tHW MAC address: "
1410 "%02X:%02X:%02X:%02X:%02X:%02X\n",
1411 v_rom_mac[0], v_rom_mac[1],
1412 v_rom_mac[2], v_rom_mac[3],
1413 v_rom_mac[4], v_rom_mac[5] );
1414 printf ("\t\"ethaddr\" value: "
1415 "%02X:%02X:%02X:%02X:%02X:%02X\n",
1416 v_env_mac[0], v_env_mac[1],
1417 v_env_mac[2], v_env_mac[3],
1418 v_env_mac[4], v_env_mac[5]) ;
1419 debug ("### Set MAC addr from environment\n");
1422 memcpy (bd->bi_enetaddr, v_mac, 6); /* update global address to match env (allows env changing) */
1423 smc_set_mac_addr (v_mac); /* use old function to update smc default */
1424 PRINTK("Using MAC Address %02X:%02X:%02X:%02X:%02X:%02X\n", v_mac[0], v_mac[1],
1425 v_mac[2], v_mac[3], v_mac[4], v_mac[5]);
1429 int get_rom_mac (char *v_rom_mac)
1431 #ifdef HARDCODE_MAC /* used for testing or to supress run time warnings */
1432 char hw_mac_addr[] = { 0x02, 0x80, 0xad, 0x20, 0x31, 0xb8 };
1434 memcpy (v_rom_mac, hw_mac_addr, 6);
1438 SMC_SELECT_BANK (1);
1441 v_rom_mac[i] = SMC_inb (ADDR0_REG + i);
1446 #endif /* CONFIG_DRIVER_SMC91111 */