1 // SPDX-License-Identifier: GPL-2.0+
2 /*------------------------------------------------------------------------
4 . This is a driver for SMSC's 91C111 single-chip Ethernet device.
7 . Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8 . Rolf Offermanns <rof@sysgo.de>
10 . Copyright (C) 2001 Standard Microsystems Corporation (SMSC)
11 . Developed by Simple Network Magic Corporation (SNMC)
12 . Copyright (C) 1996 by Erik Stahlman (ES)
15 . Information contained in this file was obtained from the LAN91C111
16 . manual from SMC. To get a copy, if you really want one, you can find
17 . information under www.smsc.com.
20 . "Features" of the SMC chip:
21 . Integrated PHY/MAC for 10/100BaseT Operation
22 . Supports internal and external MII
23 . Integrated 8K packet memory
24 . EEPROM interface for configuration
27 . io = for the base address
31 . Erik Stahlman ( erik@vt.edu )
32 . Daris A Nevil ( dnevil@snmc.com )
35 . Hardware multicast code from Peter Cammaert ( pc@denkart.be )
38 . o SMSC LAN91C111 databook (www.smsc.com)
39 . o smc9194.c by Erik Stahlman
40 . o skeleton.c by Donald Becker ( becker@cesdis.gsfc.nasa.gov )
43 . 06/19/03 Richard Woodruff Made u-boot environment aware and added mac addr checks.
44 . 10/17/01 Marco Hasewinkel Modify for DNP/1110
45 . 07/25/01 Woojung Huh Modify for ADS Bitsy
46 . 04/25/01 Daris A Nevil Initial public release through SMSC
47 . 03/16/01 Daris A Nevil Modified smc9194.c for use with LAN91C111
48 ----------------------------------------------------------------------------*/
54 #include <linux/delay.h>
58 /* Use power-down feature of the chip */
66 static const char version[] =
67 "smc91111.c:v1.0 04/25/01 by Daris A Nevil (dnevil@snmc.com)\n";
70 /* Autonegotiation timeout in seconds */
71 #ifndef CONFIG_SMC_AUTONEG_TIMEOUT
72 #define CONFIG_SMC_AUTONEG_TIMEOUT 10
75 /*------------------------------------------------------------------------
77 . Configuration options, for the experienced user to change.
79 -------------------------------------------------------------------------*/
82 . Wait time for memory to be free. This probably shouldn't be
83 . tuned that much, as waiting for this means nothing else happens
86 #define MEMORY_WAIT_TIME 16
90 #define PRINTK3(args...) printf(args)
92 #define PRINTK3(args...)
96 #define PRINTK2(args...) printf(args)
98 #define PRINTK2(args...)
102 #define PRINTK(args...) printf(args)
104 #define PRINTK(args...)
108 /*------------------------------------------------------------------------
110 . The internal workings of the driver. If you are changing anything
111 . here with the SMC stuff, you should have the datasheet and know
112 . what you are doing.
114 -------------------------------------------------------------------------*/
116 /* Memory sizing constant */
117 #define LAN91C111_MEMORY_MULTIPLIER (1024*2)
119 #ifndef CONFIG_SMC91111_BASE
120 #error "SMC91111 Base address must be passed to initialization funciton"
121 /* #define CONFIG_SMC91111_BASE 0x20000300 */
124 #define SMC_DEV_NAME "SMC91111"
125 #define SMC_PHY_ADDR 0x0000
126 #define SMC_ALLOC_MAX_TRY 5
127 #define SMC_TX_TIMEOUT 30
129 #define SMC_PHY_CLOCK_DELAY 1000
133 #ifdef CONFIG_SMC_USE_32_BIT
139 #ifdef SHARED_RESOURCES
140 extern void swap_to(int device_id);
145 #ifndef CONFIG_SMC91111_EXT_PHY
146 static void smc_phy_configure(struct eth_device *dev);
147 #endif /* !CONFIG_SMC91111_EXT_PHY */
150 ------------------------------------------------------------
154 ------------------------------------------------------------
157 #ifdef CONFIG_SMC_USE_IOFUNCS
159 * input and output functions
161 * Implemented due to inx,outx macros accessing the device improperly
162 * and putting the device into an unkown state.
164 * For instance, on Sharp LPD7A400 SDK, affects were chip memory
165 * could not be free'd (hence the alloc failures), duplicate packets,
166 * packets being corrupt (shifted) on the wire, etc. Switching to the
167 * inx,outx functions fixed this problem.
170 static inline word SMC_inw(struct eth_device *dev, dword offset)
173 v = *((volatile word*)(dev->iobase + offset));
174 barrier(); *(volatile u32*)(0xc0000000);
178 static inline void SMC_outw(struct eth_device *dev, word value, dword offset)
180 *((volatile word*)(dev->iobase + offset)) = value;
181 barrier(); *(volatile u32*)(0xc0000000);
184 static inline byte SMC_inb(struct eth_device *dev, dword offset)
188 _w = SMC_inw(dev, offset & ~((dword)1));
189 return (offset & 1) ? (byte)(_w >> 8) : (byte)(_w);
192 static inline void SMC_outb(struct eth_device *dev, byte value, dword offset)
196 _w = SMC_inw(dev, offset & ~((dword)1));
198 *((volatile word*)(dev->iobase + (offset & ~((dword)1)))) =
199 (value<<8) | (_w & 0x00ff);
201 *((volatile word*)(dev->iobase + offset)) =
202 value | (_w & 0xff00);
205 static inline void SMC_insw(struct eth_device *dev, dword offset,
206 volatile uchar* buf, dword len)
208 volatile word *p = (volatile word *)buf;
211 *p++ = SMC_inw(dev, offset);
213 *((volatile u32*)(0xc0000000));
217 static inline void SMC_outsw(struct eth_device *dev, dword offset,
218 uchar* buf, dword len)
220 volatile word *p = (volatile word *)buf;
223 SMC_outw(dev, *p++, offset);
225 *(volatile u32*)(0xc0000000);
228 #endif /* CONFIG_SMC_USE_IOFUNCS */
231 . A rather simple routine to print out a packet for debugging purposes.
234 static void print_packet( byte *, int );
237 #define tx_done(dev) 1
239 static int poll4int (struct eth_device *dev, byte mask, int timeout)
241 int tmo = get_timer (0) + timeout * CONFIG_SYS_HZ;
243 word old_bank = SMC_inw (dev, BSR_REG);
245 PRINTK2 ("Polling...\n");
246 SMC_SELECT_BANK (dev, 2);
247 while ((SMC_inw (dev, SMC91111_INT_REG) & mask) == 0) {
248 if (get_timer (0) >= tmo) {
254 /* restore old bank selection */
255 SMC_SELECT_BANK (dev, old_bank);
263 /* Only one release command at a time, please */
264 static inline void smc_wait_mmu_release_complete (struct eth_device *dev)
268 /* assume bank 2 selected */
269 while (SMC_inw (dev, MMU_CMD_REG) & MC_BUSY) {
270 udelay(1); /* Wait until not busy */
277 . Function: smc_reset( void )
279 . This sets the SMC91111 chip to its normal state, hopefully from whatever
280 . mess that any other DOS driver has put it in.
282 . Maybe I should reset more registers to defaults in here? SOFTRST should
286 . 1. send a SOFT RESET
287 . 2. wait for it to finish
288 . 3. enable autorelease mode
289 . 4. reset the memory management unit
290 . 5. clear all interrupts
293 static void smc_reset (struct eth_device *dev)
295 PRINTK2 ("%s: smc_reset\n", SMC_DEV_NAME);
297 /* This resets the registers mostly to defaults, but doesn't
298 affect EEPROM. That seems unnecessary */
299 SMC_SELECT_BANK (dev, 0);
300 SMC_outw (dev, RCR_SOFTRST, RCR_REG);
302 /* Setup the Configuration Register */
303 /* This is necessary because the CONFIG_REG is not affected */
304 /* by a soft reset */
306 SMC_SELECT_BANK (dev, 1);
307 #if defined(CONFIG_SMC91111_EXT_PHY)
308 SMC_outw (dev, CONFIG_DEFAULT | CONFIG_EXT_PHY, CONFIG_REG);
310 SMC_outw (dev, CONFIG_DEFAULT, CONFIG_REG);
314 /* Release from possible power-down state */
315 /* Configuration register is not affected by Soft Reset */
316 SMC_outw (dev, SMC_inw (dev, CONFIG_REG) | CONFIG_EPH_POWER_EN,
319 SMC_SELECT_BANK (dev, 0);
321 /* this should pause enough for the chip to be happy */
324 /* Disable transmit and receive functionality */
325 SMC_outw (dev, RCR_CLEAR, RCR_REG);
326 SMC_outw (dev, TCR_CLEAR, TCR_REG);
328 /* set the control register */
329 SMC_SELECT_BANK (dev, 1);
330 SMC_outw (dev, CTL_DEFAULT, CTL_REG);
333 SMC_SELECT_BANK (dev, 2);
334 smc_wait_mmu_release_complete (dev);
335 SMC_outw (dev, MC_RESET, MMU_CMD_REG);
336 while (SMC_inw (dev, MMU_CMD_REG) & MC_BUSY)
337 udelay(1); /* Wait until not busy */
339 /* Note: It doesn't seem that waiting for the MMU busy is needed here,
340 but this is a place where future chipsets _COULD_ break. Be wary
341 of issuing another MMU command right after this */
343 /* Disable all interrupts */
344 SMC_outb (dev, 0, IM_REG);
348 . Function: smc_enable
349 . Purpose: let the chip talk to the outside work
351 . 1. Enable the transmitter
352 . 2. Enable the receiver
353 . 3. Enable interrupts
355 static void smc_enable(struct eth_device *dev)
357 PRINTK2("%s: smc_enable\n", SMC_DEV_NAME);
358 SMC_SELECT_BANK( dev, 0 );
359 /* see the header file for options in TCR/RCR DEFAULT*/
360 SMC_outw( dev, TCR_DEFAULT, TCR_REG );
361 SMC_outw( dev, RCR_DEFAULT, RCR_REG );
364 /* smc_write_phy_register(PHY_CNTL_REG, 0x0000); */
369 . Purpose: closes down the SMC91xxx chip.
371 . 1. zero the interrupt mask
372 . 2. clear the enable receive flag
373 . 3. clear the enable xmit flags
376 . (1) maybe utilize power down mode.
377 . Why not yet? Because while the chip will go into power down mode,
378 . the manual says that it will wake up in response to any I/O requests
379 . in the register space. Empirical results do not show this working.
381 static void smc_halt(struct eth_device *dev)
383 PRINTK2("%s: smc_halt\n", SMC_DEV_NAME);
385 /* no more interrupts for me */
386 SMC_SELECT_BANK( dev, 2 );
387 SMC_outb( dev, 0, IM_REG );
389 /* and tell the card to stay away from that nasty outside world */
390 SMC_SELECT_BANK( dev, 0 );
391 SMC_outb( dev, RCR_CLEAR, RCR_REG );
392 SMC_outb( dev, TCR_CLEAR, TCR_REG );
399 . Function: smc_send(struct net_device * )
401 . This sends the actual packet to the SMC9xxx chip.
404 . First, see if a saved_skb is available.
405 . ( this should NOT be called if there is no 'saved_skb'
406 . Now, find the packet number that the chip allocated
407 . Point the data pointers at it in memory
408 . Set the length word in the chip's memory
409 . Dump the packet to chip memory
410 . Check if a last byte is needed ( odd length packet )
411 . if so, set the control flag right
412 . Tell the card to send it
413 . Enable the transmit interrupt, so I know if it failed
414 . Free the kernel data if I actually sent it.
416 static int smc_send(struct eth_device *dev, void *packet, int packet_length)
428 /* save PTR and PNR registers before manipulation */
429 SMC_SELECT_BANK (dev, 2);
430 saved_pnr = SMC_inb( dev, PN_REG );
431 saved_ptr = SMC_inw( dev, PTR_REG );
433 PRINTK3 ("%s: smc_hardware_send_packet\n", SMC_DEV_NAME);
435 length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
438 ** The MMU wants the number of pages to be the number of 256 bytes
439 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
441 ** The 91C111 ignores the size bits, but the code is left intact
442 ** for backwards and future compatibility.
444 ** Pkt size for allocating is data length +6 (for additional status
445 ** words, length and ctl!)
447 ** If odd size then last byte is included in this header.
449 numPages = ((length & 0xfffe) + 6);
450 numPages >>= 8; /* Divide by 256 */
453 printf ("%s: Far too big packet error. \n", SMC_DEV_NAME);
457 /* now, try to allocate the memory */
458 SMC_SELECT_BANK (dev, 2);
459 SMC_outw (dev, MC_ALLOC | numPages, MMU_CMD_REG);
461 /* FIXME: the ALLOC_INT bit never gets set *
462 * so the following will always give a *
463 * memory allocation error. *
464 * same code works in armboot though *
470 time_out = MEMORY_WAIT_TIME;
472 status = SMC_inb (dev, SMC91111_INT_REG);
473 if (status & IM_ALLOC_INT) {
474 /* acknowledge the interrupt */
475 SMC_outb (dev, IM_ALLOC_INT, SMC91111_INT_REG);
478 } while (--time_out);
481 PRINTK2 ("%s: memory allocation, try %d failed ...\n",
483 if (try < SMC_ALLOC_MAX_TRY)
489 PRINTK2 ("%s: memory allocation, try %d succeeded ...\n",
492 buf = (byte *) packet;
494 /* If I get here, I _know_ there is a packet slot waiting for me */
495 packet_no = SMC_inb (dev, AR_REG);
496 if (packet_no & AR_FAILED) {
497 /* or isn't there? BAD CHIP! */
498 printf ("%s: Memory allocation failed. \n", SMC_DEV_NAME);
502 /* we have a packet address, so tell the card to use it */
503 SMC_outb (dev, packet_no, PN_REG);
505 /* do not write new ptr value if Write data fifo not empty */
506 while ( saved_ptr & PTR_NOTEMPTY )
507 printf ("Write data fifo not empty!\n");
509 /* point to the beginning of the packet */
510 SMC_outw (dev, PTR_AUTOINC, PTR_REG);
512 PRINTK3 ("%s: Trying to xmit packet of length %x\n",
513 SMC_DEV_NAME, length);
516 printf ("Transmitting Packet\n");
517 print_packet (buf, length);
520 /* send the packet length ( +6 for status, length and ctl byte )
521 and the status word ( set to zeros ) */
523 SMC_outl (dev, (length + 6) << 16, SMC91111_DATA_REG);
525 SMC_outw (dev, 0, SMC91111_DATA_REG);
526 /* send the packet length ( +6 for status words, length, and ctl */
527 SMC_outw (dev, (length + 6), SMC91111_DATA_REG);
530 /* send the actual data
531 . I _think_ it's faster to send the longs first, and then
532 . mop up by sending the last word. It depends heavily
533 . on alignment, at least on the 486. Maybe it would be
534 . a good idea to check which is optimal? But that could take
535 . almost as much time as is saved?
538 SMC_outsl (dev, SMC91111_DATA_REG, buf, length >> 2);
540 SMC_outw (dev, *((word *) (buf + (length & 0xFFFFFFFC))),
543 SMC_outsw (dev, SMC91111_DATA_REG, buf, (length) >> 1);
544 #endif /* USE_32_BIT */
546 /* Send the last byte, if there is one. */
547 if ((length & 1) == 0) {
548 SMC_outw (dev, 0, SMC91111_DATA_REG);
550 SMC_outw (dev, buf[length - 1] | 0x2000, SMC91111_DATA_REG);
553 /* and let the chipset deal with it */
554 SMC_outw (dev, MC_ENQUEUE, MMU_CMD_REG);
556 /* poll for TX INT */
557 /* if (poll4int (dev, IM_TX_INT, SMC_TX_TIMEOUT)) { */
558 /* poll for TX_EMPTY INT - autorelease enabled */
559 if (poll4int(dev, IM_TX_EMPTY_INT, SMC_TX_TIMEOUT)) {
561 PRINTK2 ("%s: TX timeout, sending failed...\n", SMC_DEV_NAME);
564 /* no need to release, MMU does that now */
566 /* wait for MMU getting ready (low) */
567 while (SMC_inw (dev, MMU_CMD_REG) & MC_BUSY) {
571 PRINTK2 ("MMU ready\n");
577 SMC_outb (dev, IM_TX_EMPTY_INT, SMC91111_INT_REG);
578 /* SMC_outb (IM_TX_INT, SMC91111_INT_REG); */
579 PRINTK2 ("%s: Sent packet of length %d \n", SMC_DEV_NAME,
583 /* no need to release, MMU does that now */
585 /* wait for MMU getting ready (low) */
586 while (SMC_inw (dev, MMU_CMD_REG) & MC_BUSY) {
590 PRINTK2 ("MMU ready\n");
595 /* restore previously saved registers */
596 SMC_outb( dev, saved_pnr, PN_REG );
597 SMC_outw( dev, saved_ptr, PTR_REG );
602 static int smc_write_hwaddr(struct eth_device *dev)
607 SMC_SELECT_BANK (dev, 1);
609 for (i = 0; i < 6; i += 2) {
612 address = dev->enetaddr[i + 1] << 8;
613 address |= dev->enetaddr[i];
614 SMC_outw(dev, address, (ADDR0_REG + i));
617 for (i = 0; i < 6; i++)
618 SMC_outb(dev, dev->enetaddr[i], (ADDR0_REG + i));
625 * Open and Initialize the board
627 * Set up everything, reset the card, etc ..
630 static int smc_init(struct eth_device *dev, bd_t *bd)
634 PRINTK2 ("%s: smc_init\n", SMC_DEV_NAME);
636 /* reset the hardware */
640 /* Configure the PHY */
641 #ifndef CONFIG_SMC91111_EXT_PHY
642 smc_phy_configure (dev);
645 /* conservative setting (10Mbps, HalfDuplex, no AutoNeg.) */
646 /* SMC_SELECT_BANK(dev, 0); */
647 /* SMC_outw(dev, 0, RPC_REG); */
649 printf(SMC_DEV_NAME ": MAC %pM\n", dev->enetaddr);
654 /*-------------------------------------------------------------
656 . smc_rcv - receive a packet from the card
658 . There is ( at least ) a packet waiting to be read from
662 . o If an error, record it
663 . o otherwise, read in the packet
664 --------------------------------------------------------------
666 static int smc_rcv(struct eth_device *dev)
678 SMC_SELECT_BANK(dev, 2);
679 /* save PTR and PTR registers */
680 saved_pnr = SMC_inb( dev, PN_REG );
681 saved_ptr = SMC_inw( dev, PTR_REG );
683 packet_number = SMC_inw( dev, RXFIFO_REG );
685 if ( packet_number & RXFIFO_REMPTY ) {
690 PRINTK3("%s: smc_rcv\n", SMC_DEV_NAME);
691 /* start reading from the start of the packet */
692 SMC_outw( dev, PTR_READ | PTR_RCV | PTR_AUTOINC, PTR_REG );
694 /* First two words are status and packet_length */
696 stat_len = SMC_inl(dev, SMC91111_DATA_REG);
697 status = stat_len & 0xffff;
698 packet_length = stat_len >> 16;
700 status = SMC_inw( dev, SMC91111_DATA_REG );
701 packet_length = SMC_inw( dev, SMC91111_DATA_REG );
704 packet_length &= 0x07ff; /* mask off top bits */
706 PRINTK2("RCV: STATUS %4x LENGTH %4x\n", status, packet_length );
708 if ( !(status & RS_ERRORS ) ){
709 /* Adjust for having already read the first two words */
710 packet_length -= 4; /*4; */
713 /* set odd length for bug in LAN91C111, */
714 /* which never sets RS_ODDFRAME */
719 PRINTK3(" Reading %d dwords (and %d bytes)\n",
720 packet_length >> 2, packet_length & 3 );
721 /* QUESTION: Like in the TX routine, do I want
722 to send the DWORDs or the bytes first, or some
723 mixture. A mixture might improve already slow PIO
725 SMC_insl(dev, SMC91111_DATA_REG, net_rx_packets[0],
727 /* read the left over bytes */
728 if (packet_length & 3) {
731 byte *tail = (byte *)(net_rx_packets[0] +
732 (packet_length & ~3));
733 dword leftover = SMC_inl(dev, SMC91111_DATA_REG);
734 for (i=0; i<(packet_length & 3); i++)
735 *tail++ = (byte) (leftover >> (8*i)) & 0xff;
738 PRINTK3(" Reading %d words and %d byte(s)\n",
739 (packet_length >> 1 ), packet_length & 1 );
740 SMC_insw(dev, SMC91111_DATA_REG , net_rx_packets[0],
743 #endif /* USE_32_BIT */
746 printf("Receiving Packet\n");
747 print_packet(net_rx_packets[0], packet_length);
755 while ( SMC_inw( dev, MMU_CMD_REG ) & MC_BUSY )
756 udelay(1); /* Wait until not busy */
758 /* error or good, tell the card to get rid of this packet */
759 SMC_outw( dev, MC_RELEASE, MMU_CMD_REG );
761 while ( SMC_inw( dev, MMU_CMD_REG ) & MC_BUSY )
762 udelay(1); /* Wait until not busy */
764 /* restore saved registers */
765 SMC_outb( dev, saved_pnr, PN_REG );
766 SMC_outw( dev, saved_ptr, PTR_REG );
769 /* Pass the packet up to the protocol layers. */
770 net_process_received_packet(net_rx_packets[0], packet_length);
771 return packet_length;
780 /*------------------------------------------------------------
781 . Modify a bit in the LAN91C111 register set
782 .-------------------------------------------------------------*/
783 static word smc_modify_regbit(struct eth_device *dev, int bank, int ioaddr, int reg,
784 unsigned int bit, int val)
788 SMC_SELECT_BANK( dev, bank );
790 regval = SMC_inw( dev, reg );
796 SMC_outw( dev, regval, 0 );
801 /*------------------------------------------------------------
802 . Retrieve a bit in the LAN91C111 register set
803 .-------------------------------------------------------------*/
804 static int smc_get_regbit(struct eth_device *dev, int bank, int ioaddr, int reg, unsigned int bit)
806 SMC_SELECT_BANK( dev, bank );
807 if ( SMC_inw( dev, reg ) & bit)
814 /*------------------------------------------------------------
815 . Modify a LAN91C111 register (word access only)
816 .-------------------------------------------------------------*/
817 static void smc_modify_reg(struct eth_device *dev, int bank, int ioaddr, int reg, word val)
819 SMC_SELECT_BANK( dev, bank );
820 SMC_outw( dev, val, reg );
824 /*------------------------------------------------------------
825 . Retrieve a LAN91C111 register (word access only)
826 .-------------------------------------------------------------*/
827 static int smc_get_reg(struct eth_device *dev, int bank, int ioaddr, int reg)
829 SMC_SELECT_BANK( dev, bank );
830 return(SMC_inw( dev, reg ));
835 /*---PHY CONTROL AND CONFIGURATION----------------------------------------- */
839 /*------------------------------------------------------------
840 . Debugging function for viewing MII Management serial bitstream
841 .-------------------------------------------------------------*/
842 static void smc_dump_mii_stream (byte * bits, int size)
847 for (i = 0; i < size; ++i) {
848 printf ("%d", i % 10);
852 for (i = 0; i < size; ++i) {
853 if (bits[i] & MII_MDOE)
860 for (i = 0; i < size; ++i) {
861 if (bits[i] & MII_MDO)
868 for (i = 0; i < size; ++i) {
869 if (bits[i] & MII_MDI)
879 /*------------------------------------------------------------
880 . Reads a register from the MII Management serial interface
881 .-------------------------------------------------------------*/
882 #ifndef CONFIG_SMC91111_EXT_PHY
883 static word smc_read_phy_register (struct eth_device *dev, byte phyreg)
893 byte phyaddr = SMC_PHY_ADDR;
895 /* 32 consecutive ones on MDO to establish sync */
896 for (i = 0; i < 32; ++i)
897 bits[clk_idx++] = MII_MDOE | MII_MDO;
899 /* Start code <01> */
900 bits[clk_idx++] = MII_MDOE;
901 bits[clk_idx++] = MII_MDOE | MII_MDO;
903 /* Read command <10> */
904 bits[clk_idx++] = MII_MDOE | MII_MDO;
905 bits[clk_idx++] = MII_MDOE;
907 /* Output the PHY address, msb first */
909 for (i = 0; i < 5; ++i) {
911 bits[clk_idx++] = MII_MDOE | MII_MDO;
913 bits[clk_idx++] = MII_MDOE;
915 /* Shift to next lowest bit */
919 /* Output the phy register number, msb first */
921 for (i = 0; i < 5; ++i) {
923 bits[clk_idx++] = MII_MDOE | MII_MDO;
925 bits[clk_idx++] = MII_MDOE;
927 /* Shift to next lowest bit */
931 /* Tristate and turnaround (2 bit times) */
933 /*bits[clk_idx++] = 0; */
935 /* Input starts at this bit time */
938 /* Will input 16 bits */
939 for (i = 0; i < 16; ++i)
942 /* Final clock bit */
945 /* Save the current bank */
946 oldBank = SMC_inw (dev, BANK_SELECT);
949 SMC_SELECT_BANK (dev, 3);
951 /* Get the current MII register value */
952 mii_reg = SMC_inw (dev, MII_REG);
954 /* Turn off all MII Interface bits */
955 mii_reg &= ~(MII_MDOE | MII_MCLK | MII_MDI | MII_MDO);
957 /* Clock all 64 cycles */
958 for (i = 0; i < sizeof bits; ++i) {
959 /* Clock Low - output data */
960 SMC_outw (dev, mii_reg | bits[i], MII_REG);
961 udelay(SMC_PHY_CLOCK_DELAY);
964 /* Clock Hi - input data */
965 SMC_outw (dev, mii_reg | bits[i] | MII_MCLK, MII_REG);
966 udelay(SMC_PHY_CLOCK_DELAY);
967 bits[i] |= SMC_inw (dev, MII_REG) & MII_MDI;
970 /* Return to idle state */
971 /* Set clock to low, data to low, and output tristated */
972 SMC_outw (dev, mii_reg, MII_REG);
973 udelay(SMC_PHY_CLOCK_DELAY);
975 /* Restore original bank select */
976 SMC_SELECT_BANK (dev, oldBank);
978 /* Recover input data */
980 for (i = 0; i < 16; ++i) {
983 if (bits[input_idx++] & MII_MDI)
988 printf ("smc_read_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
989 phyaddr, phyreg, phydata);
990 smc_dump_mii_stream (bits, sizeof bits);
997 /*------------------------------------------------------------
998 . Writes a register to the MII Management serial interface
999 .-------------------------------------------------------------*/
1000 static void smc_write_phy_register (struct eth_device *dev, byte phyreg,
1009 byte phyaddr = SMC_PHY_ADDR;
1011 /* 32 consecutive ones on MDO to establish sync */
1012 for (i = 0; i < 32; ++i)
1013 bits[clk_idx++] = MII_MDOE | MII_MDO;
1015 /* Start code <01> */
1016 bits[clk_idx++] = MII_MDOE;
1017 bits[clk_idx++] = MII_MDOE | MII_MDO;
1019 /* Write command <01> */
1020 bits[clk_idx++] = MII_MDOE;
1021 bits[clk_idx++] = MII_MDOE | MII_MDO;
1023 /* Output the PHY address, msb first */
1025 for (i = 0; i < 5; ++i) {
1027 bits[clk_idx++] = MII_MDOE | MII_MDO;
1029 bits[clk_idx++] = MII_MDOE;
1031 /* Shift to next lowest bit */
1035 /* Output the phy register number, msb first */
1037 for (i = 0; i < 5; ++i) {
1039 bits[clk_idx++] = MII_MDOE | MII_MDO;
1041 bits[clk_idx++] = MII_MDOE;
1043 /* Shift to next lowest bit */
1047 /* Tristate and turnaround (2 bit times) */
1048 bits[clk_idx++] = 0;
1049 bits[clk_idx++] = 0;
1051 /* Write out 16 bits of data, msb first */
1053 for (i = 0; i < 16; ++i) {
1055 bits[clk_idx++] = MII_MDOE | MII_MDO;
1057 bits[clk_idx++] = MII_MDOE;
1059 /* Shift to next lowest bit */
1063 /* Final clock bit (tristate) */
1064 bits[clk_idx++] = 0;
1066 /* Save the current bank */
1067 oldBank = SMC_inw (dev, BANK_SELECT);
1070 SMC_SELECT_BANK (dev, 3);
1072 /* Get the current MII register value */
1073 mii_reg = SMC_inw (dev, MII_REG);
1075 /* Turn off all MII Interface bits */
1076 mii_reg &= ~(MII_MDOE | MII_MCLK | MII_MDI | MII_MDO);
1078 /* Clock all cycles */
1079 for (i = 0; i < sizeof bits; ++i) {
1080 /* Clock Low - output data */
1081 SMC_outw (dev, mii_reg | bits[i], MII_REG);
1082 udelay(SMC_PHY_CLOCK_DELAY);
1085 /* Clock Hi - input data */
1086 SMC_outw (dev, mii_reg | bits[i] | MII_MCLK, MII_REG);
1087 udelay(SMC_PHY_CLOCK_DELAY);
1088 bits[i] |= SMC_inw (dev, MII_REG) & MII_MDI;
1091 /* Return to idle state */
1092 /* Set clock to low, data to low, and output tristated */
1093 SMC_outw (dev, mii_reg, MII_REG);
1094 udelay(SMC_PHY_CLOCK_DELAY);
1096 /* Restore original bank select */
1097 SMC_SELECT_BANK (dev, oldBank);
1099 #if (SMC_DEBUG > 2 )
1100 printf ("smc_write_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
1101 phyaddr, phyreg, phydata);
1102 smc_dump_mii_stream (bits, sizeof bits);
1105 #endif /* !CONFIG_SMC91111_EXT_PHY */
1108 /*------------------------------------------------------------
1109 . Configures the specified PHY using Autonegotiation. Calls
1110 . smc_phy_fixed() if the user has requested a certain config.
1111 .-------------------------------------------------------------*/
1112 #ifndef CONFIG_SMC91111_EXT_PHY
1113 static void smc_phy_configure (struct eth_device *dev)
1116 word my_phy_caps; /* My PHY capabilities */
1117 word my_ad_caps; /* My Advertised capabilities */
1118 word status = 0; /*;my status = 0 */
1120 PRINTK3 ("%s: smc_program_phy()\n", SMC_DEV_NAME);
1122 /* Reset the PHY, setting all other bits to zero */
1123 smc_write_phy_register (dev, PHY_CNTL_REG, PHY_CNTL_RST);
1125 /* Wait for the reset to complete, or time out */
1126 timeout = 6; /* Wait up to 3 seconds */
1128 if (!(smc_read_phy_register (dev, PHY_CNTL_REG)
1130 /* reset complete */
1134 mdelay(500); /* wait 500 millisecs */
1138 printf ("%s:PHY reset timed out\n", SMC_DEV_NAME);
1139 goto smc_phy_configure_exit;
1142 /* Read PHY Register 18, Status Output */
1143 /* lp->lastPhy18 = smc_read_phy_register(PHY_INT_REG); */
1145 /* Enable PHY Interrupts (for register 18) */
1146 /* Interrupts listed here are disabled */
1147 smc_write_phy_register (dev, PHY_MASK_REG, 0xffff);
1149 /* Configure the Receive/Phy Control register */
1150 SMC_SELECT_BANK (dev, 0);
1151 SMC_outw (dev, RPC_DEFAULT, RPC_REG);
1153 /* Copy our capabilities from PHY_STAT_REG to PHY_AD_REG */
1154 my_phy_caps = smc_read_phy_register (dev, PHY_STAT_REG);
1155 my_ad_caps = PHY_AD_CSMA; /* I am CSMA capable */
1157 if (my_phy_caps & PHY_STAT_CAP_T4)
1158 my_ad_caps |= PHY_AD_T4;
1160 if (my_phy_caps & PHY_STAT_CAP_TXF)
1161 my_ad_caps |= PHY_AD_TX_FDX;
1163 if (my_phy_caps & PHY_STAT_CAP_TXH)
1164 my_ad_caps |= PHY_AD_TX_HDX;
1166 if (my_phy_caps & PHY_STAT_CAP_TF)
1167 my_ad_caps |= PHY_AD_10_FDX;
1169 if (my_phy_caps & PHY_STAT_CAP_TH)
1170 my_ad_caps |= PHY_AD_10_HDX;
1172 /* Update our Auto-Neg Advertisement Register */
1173 smc_write_phy_register (dev, PHY_AD_REG, my_ad_caps);
1175 /* Read the register back. Without this, it appears that when */
1176 /* auto-negotiation is restarted, sometimes it isn't ready and */
1177 /* the link does not come up. */
1178 smc_read_phy_register(dev, PHY_AD_REG);
1180 PRINTK2 ("%s: phy caps=%x\n", SMC_DEV_NAME, my_phy_caps);
1181 PRINTK2 ("%s: phy advertised caps=%x\n", SMC_DEV_NAME, my_ad_caps);
1183 /* Restart auto-negotiation process in order to advertise my caps */
1184 smc_write_phy_register (dev, PHY_CNTL_REG,
1185 PHY_CNTL_ANEG_EN | PHY_CNTL_ANEG_RST);
1187 /* Wait for the auto-negotiation to complete. This may take from */
1188 /* 2 to 3 seconds. */
1189 /* Wait for the reset to complete, or time out */
1190 timeout = CONFIG_SMC_AUTONEG_TIMEOUT * 2;
1193 status = smc_read_phy_register (dev, PHY_STAT_REG);
1194 if (status & PHY_STAT_ANEG_ACK) {
1195 /* auto-negotiate complete */
1199 mdelay(500); /* wait 500 millisecs */
1201 /* Restart auto-negotiation if remote fault */
1202 if (status & PHY_STAT_REM_FLT) {
1203 printf ("%s: PHY remote fault detected\n",
1206 /* Restart auto-negotiation */
1207 printf ("%s: PHY restarting auto-negotiation\n",
1209 smc_write_phy_register (dev, PHY_CNTL_REG,
1218 printf ("%s: PHY auto-negotiate timed out\n", SMC_DEV_NAME);
1221 /* Fail if we detected an auto-negotiate remote fault */
1222 if (status & PHY_STAT_REM_FLT) {
1223 printf ("%s: PHY remote fault detected\n", SMC_DEV_NAME);
1226 /* Re-Configure the Receive/Phy Control register */
1227 SMC_outw (dev, RPC_DEFAULT, RPC_REG);
1229 smc_phy_configure_exit: ;
1232 #endif /* !CONFIG_SMC91111_EXT_PHY */
1236 static void print_packet( byte * buf, int length )
1242 printf("Packet of length %d \n", length );
1245 lines = length / 16;
1246 remainder = length % 16;
1248 for ( i = 0; i < lines ; i ++ ) {
1251 for ( cur = 0; cur < 8; cur ++ ) {
1256 printf("%02x%02x ", a, b );
1260 for ( i = 0; i < remainder/2 ; i++ ) {
1265 printf("%02x%02x ", a, b );
1272 int smc91111_initialize(u8 dev_num, int base_addr)
1274 struct smc91111_priv *priv;
1275 struct eth_device *dev;
1278 priv = malloc(sizeof(*priv));
1281 dev = malloc(sizeof(*dev));
1287 memset(dev, 0, sizeof(*dev));
1288 priv->dev_num = dev_num;
1290 dev->iobase = base_addr;
1293 SMC_SELECT_BANK(dev, 1);
1294 for (i = 0; i < 6; ++i)
1295 dev->enetaddr[i] = SMC_inb(dev, (ADDR0_REG + i));
1298 dev->init = smc_init;
1299 dev->halt = smc_halt;
1300 dev->send = smc_send;
1301 dev->recv = smc_rcv;
1302 dev->write_hwaddr = smc_write_hwaddr;
1303 sprintf(dev->name, "%s-%hu", SMC_DEV_NAME, dev_num);