1 /*------------------------------------------------------------------------
3 * This is a driver for SMSC's LAN91C96 single-chip Ethernet device, based
4 * on the SMC91111 driver from U-boot.
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)
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Information contained in this file was obtained from the LAN91C96
29 * manual from SMC. To get a copy, if you really want one, you can find
30 * information under www.smsc.com.
33 * "Features" of the SMC chip:
34 * 6144 byte packet memory. ( for the 91C96 )
35 * EEPROM for configuration
36 * AUI/TP selection ( mine has 10Base2/10BaseT select )
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 LAN91C96 databook (www.smsc.com)
51 * o smc91111.c (u-boot driver)
52 * o smc9194.c (linux kernel driver)
53 * o lan91c96.c (Intel Diagnostic Manager driver)
56 * 04/30/03 Mathijs Haarman Modified smc91111.c (u-boot version)
58 *---------------------------------------------------------------------------
66 #ifdef CONFIG_DRIVER_LAN91C96
68 #if (CONFIG_COMMANDS & CFG_CMD_NET)
70 /*------------------------------------------------------------------------
72 * Configuration options, for the experienced user to change.
74 -------------------------------------------------------------------------*/
76 /* Use power-down feature of the chip */
80 * Wait time for memory to be free. This probably shouldn't be
81 * tuned that much, as waiting for this means nothing else happens
84 #define MEMORY_WAIT_TIME 16
89 #define PRINTK3(args...) printf(args)
91 #define PRINTK3(args...)
95 #define PRINTK2(args...) printf(args)
97 #define PRINTK2(args...)
101 #define PRINTK(args...) printf(args)
103 #define PRINTK(args...)
107 /*------------------------------------------------------------------------
109 * The internal workings of the driver. If you are changing anything
110 * here with the SMC stuff, you should have the datasheet and know
111 * what you are doing.
113 *------------------------------------------------------------------------
115 #define CARDNAME "LAN91C96"
117 #define SMC_BASE_ADDRESS CONFIG_LAN91C96_BASE
119 #define SMC_DEV_NAME "LAN91C96"
120 #define SMC_ALLOC_MAX_TRY 5
121 #define SMC_TX_TIMEOUT 30
125 #ifdef CONFIG_LAN91C96_USE_32_BIT
131 /*-----------------------------------------------------------------
133 * The driver can be entered at any of the following entry points.
135 *-----------------------------------------------------------------
138 extern int eth_init (bd_t * bd);
139 extern void eth_halt (void);
140 extern int eth_rx (void);
141 extern int eth_send (volatile void *packet, int length);
142 static int smc_hw_init (void);
145 * This is called by register_netdev(). It is responsible for
146 * checking the portlist for the SMC9000 series chipset. If it finds
147 * one, then it will initialize the device, find the hardware information,
148 * and sets up the appropriate device parameters.
149 * NOTE: Interrupts are *OFF* when this procedure is called.
151 * NB:This shouldn't be static since it is referred to externally.
156 * This is called by unregister_netdev(). It is responsible for
157 * cleaning up before the driver is finally unregistered and discarded.
159 void smc_destructor (void);
162 * The kernel calls this function when someone wants to use the device,
163 * typically 'ifconfig ethX up'.
165 static int smc_open (void);
169 * This is called by the kernel in response to 'ifconfig ethX down'. It
170 * is responsible for cleaning up everything that the open routine
171 * does, and maybe putting the card into a powerdown state.
173 static int smc_close (void);
176 * This is a separate procedure to handle the receipt of a packet, to
177 * leave the interrupt code looking slightly cleaner
179 static int smc_rcv (void);
183 /* ------------------------------------------------------------
185 * ------------------------------------------------------------
188 static char smc_mac_addr[] = { 0xc0, 0x00, 0x00, 0x1b, 0x62, 0x9c };
191 * This function must be called before smc_open() if you want to override
192 * the default mac address.
195 void smc_set_mac_addr (const char *addr)
199 for (i = 0; i < sizeof (smc_mac_addr); i++) {
200 smc_mac_addr[i] = addr[i];
205 * smc_get_macaddr is no longer used. If you want to override the default
206 * mac address, call smc_get_mac_addr as a part of the board initialisation.
210 void smc_get_macaddr (byte * addr)
212 /* MAC ADDRESS AT FLASHBLOCK 1 / OFFSET 0x10 */
213 unsigned char *dnp1110_mac = (unsigned char *) (0xE8000000 + 0x20010);
217 for (i = 0; i < 6; i++) {
218 addr[0] = *(dnp1110_mac + 0);
219 addr[1] = *(dnp1110_mac + 1);
220 addr[2] = *(dnp1110_mac + 2);
221 addr[3] = *(dnp1110_mac + 3);
222 addr[4] = *(dnp1110_mac + 4);
223 addr[5] = *(dnp1110_mac + 5);
228 /***********************************************
229 * Show available memory *
230 ***********************************************/
231 void dump_memory_info (void)
236 old_bank = SMC_inw (LAN91C96_BANK_SELECT) & 0xF;
239 mem_info = SMC_inw (LAN91C96_MIR);
240 PRINTK2 ("Memory: %4d available\n", (mem_info >> 8) * 2048);
242 SMC_SELECT_BANK (old_bank);
246 * A rather simple routine to print out a packet for debugging purposes.
249 static void print_packet (byte *, int);
252 /* #define tx_done(dev) 1 */
256 /* this does a soft reset on the device */
257 static void smc_reset (void);
259 /* Enable Interrupts, Receive, and Transmit */
260 static void smc_enable (void);
262 /* this puts the device in an inactive state */
263 static void smc_shutdown (void);
266 static int poll4int (byte mask, int timeout)
268 int tmo = get_timer (0) + timeout * CFG_HZ;
270 word old_bank = SMC_inw (LAN91C96_BANK_SELECT);
272 PRINTK2 ("Polling...\n");
274 while ((SMC_inw (LAN91C96_INT_STATS) & mask) == 0) {
275 if (get_timer (0) >= tmo) {
281 /* restore old bank selection */
282 SMC_SELECT_BANK (old_bank);
291 * Function: smc_reset( void )
293 * This sets the SMC91111 chip to its normal state, hopefully from whatever
294 * mess that any other DOS driver has put it in.
296 * Maybe I should reset more registers to defaults in here? SOFTRST should
300 * 1. send a SOFT RESET
301 * 2. wait for it to finish
302 * 3. enable autorelease mode
303 * 4. reset the memory management unit
304 * 5. clear all interrupts
307 static void smc_reset (void)
309 PRINTK2 ("%s:smc_reset\n", SMC_DEV_NAME);
311 /* This resets the registers mostly to defaults, but doesn't
312 affect EEPROM. That seems unnecessary */
314 SMC_outw (LAN91C96_RCR_SOFT_RST, LAN91C96_RCR);
318 /* Disable transmit and receive functionality */
319 SMC_outw (0, LAN91C96_RCR);
320 SMC_outw (0, LAN91C96_TCR);
322 /* set the control register */
324 SMC_outw (SMC_inw (LAN91C96_CONTROL) | LAN91C96_CTR_BIT_8,
327 /* Disable all interrupts */
328 SMC_outb (0, LAN91C96_INT_MASK);
332 * Function: smc_enable
333 * Purpose: let the chip talk to the outside work
335 * 1. Initialize the Memory Configuration Register
336 * 2. Enable the transmitter
337 * 3. Enable the receiver
339 static void smc_enable ()
341 PRINTK2 ("%s:smc_enable\n", SMC_DEV_NAME);
344 /* Initialize the Memory Configuration Register. See page
345 49 of the LAN91C96 data sheet for details. */
346 SMC_outw (LAN91C96_MCR_TRANSMIT_PAGES, LAN91C96_MCR);
348 /* Initialize the Transmit Control Register */
349 SMC_outw (LAN91C96_TCR_TXENA, LAN91C96_TCR);
350 /* Initialize the Receive Control Register
352 * The promiscuous bit set because I could not receive ARP reply
353 * packets from the server when I send a ARP request. It only works
354 * when I set the promiscuous bit
356 SMC_outw (LAN91C96_RCR_RXEN | LAN91C96_RCR_PRMS, LAN91C96_RCR);
360 * Function: smc_shutdown
361 * Purpose: closes down the SMC91xxx chip.
363 * 1. zero the interrupt mask
364 * 2. clear the enable receive flag
365 * 3. clear the enable xmit flags
368 * (1) maybe utilize power down mode.
369 * Why not yet? Because while the chip will go into power down mode,
370 * the manual says that it will wake up in response to any I/O requests
371 * in the register space. Empirical results do not show this working.
373 static void smc_shutdown ()
375 PRINTK2 (CARDNAME ":smc_shutdown\n");
377 /* no more interrupts for me */
379 SMC_outb (0, LAN91C96_INT_MASK);
381 /* and tell the card to stay away from that nasty outside world */
383 SMC_outb (0, LAN91C96_RCR);
384 SMC_outb (0, LAN91C96_TCR);
389 * Function: smc_hardware_send_packet(struct net_device * )
391 * This sends the actual packet to the SMC9xxx chip.
394 * First, see if a saved_skb is available.
395 * ( this should NOT be called if there is no 'saved_skb'
396 * Now, find the packet number that the chip allocated
397 * Point the data pointers at it in memory
398 * Set the length word in the chip's memory
399 * Dump the packet to chip memory
400 * Check if a last byte is needed ( odd length packet )
401 * if so, set the control flag right
402 * Tell the card to send it
403 * Enable the transmit interrupt, so I know if it failed
404 * Free the kernel data if I actually sent it.
406 static int smc_send_packet (volatile void *packet, int packet_length)
409 unsigned long ioaddr;
418 PRINTK3 ("%s:smc_hardware_send_packet\n", SMC_DEV_NAME);
420 length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
423 ** The MMU wants the number of pages to be the number of 256 bytes
424 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
426 ** The 91C111 ignores the size bits, but the code is left intact
427 ** for backwards and future compatibility.
429 ** Pkt size for allocating is data length +6 (for additional status
430 ** words, length and ctl!)
432 ** If odd size then last byte is included in this header.
434 numPages = ((length & 0xfffe) + 6);
435 numPages >>= 8; /* Divide by 256 */
438 printf ("%s: Far too big packet error. \n", SMC_DEV_NAME);
442 /* now, try to allocate the memory */
445 SMC_outw (LAN91C96_MMUCR_ALLOC_TX | numPages, LAN91C96_MMU);
449 time_out = MEMORY_WAIT_TIME;
451 status = SMC_inb (LAN91C96_INT_STATS);
452 if (status & LAN91C96_IST_ALLOC_INT) {
454 SMC_outb (LAN91C96_IST_ALLOC_INT, LAN91C96_INT_STATS);
457 } while (--time_out);
460 PRINTK2 ("%s: memory allocation, try %d failed ...\n",
462 if (try < SMC_ALLOC_MAX_TRY)
468 PRINTK2 ("%s: memory allocation, try %d succeeded ...\n",
471 /* I can send the packet now.. */
473 ioaddr = SMC_BASE_ADDRESS;
475 buf = (byte *) packet;
477 /* If I get here, I _know_ there is a packet slot waiting for me */
478 packet_no = SMC_inb (LAN91C96_ARR);
479 if (packet_no & LAN91C96_ARR_FAILED) {
480 /* or isn't there? BAD CHIP! */
481 printf ("%s: Memory allocation failed. \n", SMC_DEV_NAME);
485 /* we have a packet address, so tell the card to use it */
486 SMC_outb (packet_no, LAN91C96_PNR);
488 /* point to the beginning of the packet */
489 SMC_outw (LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
491 PRINTK3 ("%s: Trying to xmit packet of length %x\n",
492 SMC_DEV_NAME, length);
495 printf ("Transmitting Packet\n");
496 print_packet (buf, length);
499 /* send the packet length ( +6 for status, length and ctl byte )
500 and the status word ( set to zeros ) */
502 SMC_outl ((length + 6) << 16, LAN91C96_DATA_HIGH);
504 SMC_outw (0, LAN91C96_DATA_HIGH);
505 /* send the packet length ( +6 for status words, length, and ctl */
506 SMC_outw ((length + 6), LAN91C96_DATA_HIGH);
507 #endif /* USE_32_BIT */
509 /* send the actual data
510 * I _think_ it's faster to send the longs first, and then
511 * mop up by sending the last word. It depends heavily
512 * on alignment, at least on the 486. Maybe it would be
513 * a good idea to check which is optimal? But that could take
514 * almost as much time as is saved?
517 SMC_outsl (LAN91C96_DATA_HIGH, buf, length >> 2);
519 SMC_outw (*((word *) (buf + (length & 0xFFFFFFFC))),
522 SMC_outsw (LAN91C96_DATA_HIGH, buf, (length) >> 1);
523 #endif /* USE_32_BIT */
525 /* Send the last byte, if there is one. */
526 if ((length & 1) == 0) {
527 SMC_outw (0, LAN91C96_DATA_HIGH);
529 SMC_outw (buf[length - 1] | 0x2000, LAN91C96_DATA_HIGH);
532 /* and let the chipset deal with it */
533 SMC_outw (LAN91C96_MMUCR_ENQUEUE, LAN91C96_MMU);
535 /* poll for TX INT */
536 if (poll4int (LAN91C96_MSK_TX_INT, SMC_TX_TIMEOUT)) {
538 PRINTK2 ("%s: TX timeout, sending failed...\n", SMC_DEV_NAME);
541 SMC_outw (LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
543 /* wait for MMU getting ready (low) */
544 while (SMC_inw (LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY) {
548 PRINTK2 ("MMU ready\n");
554 SMC_outw (LAN91C96_IST_TX_INT, LAN91C96_INT_STATS);
556 PRINTK2 ("%s: Sent packet of length %d \n", SMC_DEV_NAME, length);
559 SMC_outw (LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
561 /* wait for MMU getting ready (low) */
562 while (SMC_inw (LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY) {
566 PRINTK2 ("MMU ready\n");
572 /*-------------------------------------------------------------------------
573 * smc_destructor( struct net_device * dev )
575 * dev, pointer to the device structure
579 *--------------------------------------------------------------------------
581 void smc_destructor ()
583 PRINTK2 (CARDNAME ":smc_destructor\n");
588 * Open and Initialize the board
590 * Set up everything, reset the card, etc ..
593 static int smc_open ()
595 int i; /* used to set hw ethernet address */
597 PRINTK2 ("%s:smc_open\n", SMC_DEV_NAME);
599 /* reset the hardware */
606 for (i = 0; i < 6; i += 2) {
609 address = smc_mac_addr[i + 1] << 8;
610 address |= smc_mac_addr[i];
611 SMC_outw (address, LAN91C96_IA0 + i);
616 /*-------------------------------------------------------------
618 * smc_rcv - receive a packet from the card
620 * There is ( at least ) a packet waiting to be read from
624 * o If an error, record it
625 * o otherwise, read in the packet
626 *-------------------------------------------------------------
628 static int smc_rcv ()
641 packet_number = SMC_inw (LAN91C96_FIFO);
643 if (packet_number & LAN91C96_FIFO_RXEMPTY) {
647 PRINTK3 ("%s:smc_rcv\n", SMC_DEV_NAME);
648 /* start reading from the start of the packet */
649 SMC_outw (LAN91C96_PTR_READ | LAN91C96_PTR_RCV |
650 LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
652 /* First two words are status and packet_length */
654 stat_len = SMC_inl (LAN91C96_DATA_HIGH);
655 status = stat_len & 0xffff;
656 packet_length = stat_len >> 16;
658 status = SMC_inw (LAN91C96_DATA_HIGH);
659 packet_length = SMC_inw (LAN91C96_DATA_HIGH);
662 packet_length &= 0x07ff; /* mask off top bits */
664 PRINTK2 ("RCV: STATUS %4x LENGTH %4x\n", status, packet_length);
666 if (!(status & FRAME_FILTER)) {
667 /* Adjust for having already read the first two words */
668 packet_length -= 4; /*4; */
672 /* set odd length for bug in LAN91C111, */
673 /* which never sets RS_ODDFRAME */
678 PRINTK3 (" Reading %d dwords (and %d bytes) \n",
679 packet_length >> 2, packet_length & 3);
680 /* QUESTION: Like in the TX routine, do I want
681 to send the DWORDs or the bytes first, or some
682 mixture. A mixture might improve already slow PIO
684 SMC_insl (LAN91C96_DATA_HIGH, NetRxPackets[0], packet_length >> 2);
685 /* read the left over bytes */
686 if (packet_length & 3) {
689 byte *tail = (byte *) (NetRxPackets[0] + (packet_length & ~3));
690 dword leftover = SMC_inl (LAN91C96_DATA_HIGH);
692 for (i = 0; i < (packet_length & 3); i++)
693 *tail++ = (byte) (leftover >> (8 * i)) & 0xff;
696 PRINTK3 (" Reading %d words and %d byte(s) \n",
697 (packet_length >> 1), packet_length & 1);
698 SMC_insw (LAN91C96_DATA_HIGH, NetRxPackets[0], packet_length >> 1);
700 #endif /* USE_32_BIT */
703 printf ("Receiving Packet\n");
704 print_packet (NetRxPackets[0], packet_length);
712 while (SMC_inw (LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
713 udelay (1); /* Wait until not busy */
715 /* error or good, tell the card to get rid of this packet */
716 SMC_outw (LAN91C96_MMUCR_RELEASE_RX, LAN91C96_MMU);
718 while (SMC_inw (LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
719 udelay (1); /* Wait until not busy */
722 /* Pass the packet up to the protocol layers. */
723 NetReceive (NetRxPackets[0], packet_length);
724 return packet_length;
731 /*----------------------------------------------------
734 * this makes the board clean up everything that it can
735 * and not talk to the outside world. Caused by
736 * an 'ifconfig ethX down'
738 -----------------------------------------------------*/
739 static int smc_close ()
741 PRINTK2 ("%s:smc_close\n", SMC_DEV_NAME);
743 /* clear everything */
750 static void print_packet (byte * buf, int length)
757 printf ("Packet of length %d \n", length);
760 remainder = length % 16;
762 for (i = 0; i < lines; i++) {
765 for (cur = 0; cur < 8; cur++) {
770 printf ("%02x%02x ", a, b);
774 for (i = 0; i < remainder / 2; i++) {
779 printf ("%02x%02x ", a, b);
784 #endif /* SMC_DEBUG > 2 */
786 int eth_init (bd_t * bd)
802 int eth_send (volatile void *packet, int length)
804 return smc_send_packet (packet, length);
809 return smc_hw_init ();
812 /*-------------------------------------------------------------------------
816 * Reset and enable the device, check if the I/O space location
825 *--------------------------------------------------------------------------
827 static int smc_hw_init ()
829 unsigned short status_test;
831 /* The attribute register of the LAN91C96 is located at address
832 0x0e000000 on the lubbock platform */
833 volatile unsigned *attaddr = (unsigned *) (0x0e000000);
835 /* first reset, then enable the device. Sequence is critical */
836 attaddr[LAN91C96_ECOR] |= LAN91C96_ECOR_SRESET;
838 attaddr[LAN91C96_ECOR] &= ~LAN91C96_ECOR_SRESET;
839 attaddr[LAN91C96_ECOR] |= LAN91C96_ECOR_ENABLE;
841 /* force 16-bit mode */
842 attaddr[LAN91C96_ECSR] &= ~LAN91C96_ECSR_IOIS8;
845 /* check if the I/O address is correct, the upper byte of the
846 bank select register should read 0x33 */
848 status_test = SMC_inw (LAN91C96_BANK_SELECT);
849 if ((status_test & 0xFF00) != 0x3300) {
850 printf ("Failed to initialize ethernetchip\n");
856 #endif /* COMMANDS & CFG_NET */
858 #endif /* CONFIG_DRIVER_LAN91C96 */