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);
182 /* ------------------------------------------------------------
184 * ------------------------------------------------------------
187 static char smc_mac_addr[] = { 0xc0, 0x00, 0x00, 0x1b, 0x62, 0x9c };
190 * This function must be called before smc_open() if you want to override
191 * the default mac address.
194 void smc_set_mac_addr (const char *addr)
198 for (i = 0; i < sizeof (smc_mac_addr); i++) {
199 smc_mac_addr[i] = addr[i];
204 * smc_get_macaddr is no longer used. If you want to override the default
205 * mac address, call smc_get_mac_addr as a part of the board initialisation.
209 void smc_get_macaddr (byte * addr)
211 /* MAC ADDRESS AT FLASHBLOCK 1 / OFFSET 0x10 */
212 unsigned char *dnp1110_mac = (unsigned char *) (0xE8000000 + 0x20010);
216 for (i = 0; i < 6; i++) {
217 addr[0] = *(dnp1110_mac + 0);
218 addr[1] = *(dnp1110_mac + 1);
219 addr[2] = *(dnp1110_mac + 2);
220 addr[3] = *(dnp1110_mac + 3);
221 addr[4] = *(dnp1110_mac + 4);
222 addr[5] = *(dnp1110_mac + 5);
227 /***********************************************
228 * Show available memory *
229 ***********************************************/
230 void dump_memory_info (void)
235 old_bank = SMC_inw (LAN91C96_BANK_SELECT) & 0xF;
238 mem_info = SMC_inw (LAN91C96_MIR);
239 PRINTK2 ("Memory: %4d available\n", (mem_info >> 8) * 2048);
241 SMC_SELECT_BANK (old_bank);
245 * A rather simple routine to print out a packet for debugging purposes.
248 static void print_packet (byte *, int);
251 /* #define tx_done(dev) 1 */
254 /* this does a soft reset on the device */
255 static void smc_reset (void);
257 /* Enable Interrupts, Receive, and Transmit */
258 static void smc_enable (void);
260 /* this puts the device in an inactive state */
261 static void smc_shutdown (void);
264 static int poll4int (byte mask, int timeout)
266 int tmo = get_timer (0) + timeout * CFG_HZ;
268 word old_bank = SMC_inw (LAN91C96_BANK_SELECT);
270 PRINTK2 ("Polling...\n");
272 while ((SMC_inw (LAN91C96_INT_STATS) & mask) == 0) {
273 if (get_timer (0) >= tmo) {
279 /* restore old bank selection */
280 SMC_SELECT_BANK (old_bank);
289 * Function: smc_reset( void )
291 * This sets the SMC91111 chip to its normal state, hopefully from whatever
292 * mess that any other DOS driver has put it in.
294 * Maybe I should reset more registers to defaults in here? SOFTRST should
298 * 1. send a SOFT RESET
299 * 2. wait for it to finish
300 * 3. enable autorelease mode
301 * 4. reset the memory management unit
302 * 5. clear all interrupts
305 static void smc_reset (void)
307 PRINTK2 ("%s:smc_reset\n", SMC_DEV_NAME);
309 /* This resets the registers mostly to defaults, but doesn't
310 affect EEPROM. That seems unnecessary */
312 SMC_outw (LAN91C96_RCR_SOFT_RST, LAN91C96_RCR);
316 /* Disable transmit and receive functionality */
317 SMC_outw (0, LAN91C96_RCR);
318 SMC_outw (0, LAN91C96_TCR);
320 /* set the control register */
322 SMC_outw (SMC_inw (LAN91C96_CONTROL) | LAN91C96_CTR_BIT_8,
325 /* Disable all interrupts */
326 SMC_outb (0, LAN91C96_INT_MASK);
330 * Function: smc_enable
331 * Purpose: let the chip talk to the outside work
333 * 1. Initialize the Memory Configuration Register
334 * 2. Enable the transmitter
335 * 3. Enable the receiver
337 static void smc_enable ()
339 PRINTK2 ("%s:smc_enable\n", SMC_DEV_NAME);
342 /* Initialize the Memory Configuration Register. See page
343 49 of the LAN91C96 data sheet for details. */
344 SMC_outw (LAN91C96_MCR_TRANSMIT_PAGES, LAN91C96_MCR);
346 /* Initialize the Transmit Control Register */
347 SMC_outw (LAN91C96_TCR_TXENA, LAN91C96_TCR);
348 /* Initialize the Receive Control Register
350 * The promiscuous bit set because I could not receive ARP reply
351 * packets from the server when I send a ARP request. It only works
352 * when I set the promiscuous bit
354 SMC_outw (LAN91C96_RCR_RXEN | LAN91C96_RCR_PRMS, LAN91C96_RCR);
358 * Function: smc_shutdown
359 * Purpose: closes down the SMC91xxx chip.
361 * 1. zero the interrupt mask
362 * 2. clear the enable receive flag
363 * 3. clear the enable xmit flags
366 * (1) maybe utilize power down mode.
367 * Why not yet? Because while the chip will go into power down mode,
368 * the manual says that it will wake up in response to any I/O requests
369 * in the register space. Empirical results do not show this working.
371 static void smc_shutdown ()
373 PRINTK2 (CARDNAME ":smc_shutdown\n");
375 /* no more interrupts for me */
377 SMC_outb (0, LAN91C96_INT_MASK);
379 /* and tell the card to stay away from that nasty outside world */
381 SMC_outb (0, LAN91C96_RCR);
382 SMC_outb (0, LAN91C96_TCR);
387 * Function: smc_hardware_send_packet(struct net_device * )
389 * This sends the actual packet to the SMC9xxx chip.
392 * First, see if a saved_skb is available.
393 * ( this should NOT be called if there is no 'saved_skb'
394 * Now, find the packet number that the chip allocated
395 * Point the data pointers at it in memory
396 * Set the length word in the chip's memory
397 * Dump the packet to chip memory
398 * Check if a last byte is needed ( odd length packet )
399 * if so, set the control flag right
400 * Tell the card to send it
401 * Enable the transmit interrupt, so I know if it failed
402 * Free the kernel data if I actually sent it.
404 static int smc_send_packet (volatile void *packet, int packet_length)
407 unsigned long ioaddr;
416 PRINTK3 ("%s:smc_hardware_send_packet\n", SMC_DEV_NAME);
418 length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
421 ** The MMU wants the number of pages to be the number of 256 bytes
422 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
424 ** The 91C111 ignores the size bits, but the code is left intact
425 ** for backwards and future compatibility.
427 ** Pkt size for allocating is data length +6 (for additional status
428 ** words, length and ctl!)
430 ** If odd size then last byte is included in this header.
432 numPages = ((length & 0xfffe) + 6);
433 numPages >>= 8; /* Divide by 256 */
436 printf ("%s: Far too big packet error. \n", SMC_DEV_NAME);
440 /* now, try to allocate the memory */
443 SMC_outw (LAN91C96_MMUCR_ALLOC_TX | numPages, LAN91C96_MMU);
447 time_out = MEMORY_WAIT_TIME;
449 status = SMC_inb (LAN91C96_INT_STATS);
450 if (status & LAN91C96_IST_ALLOC_INT) {
452 SMC_outb (LAN91C96_IST_ALLOC_INT, LAN91C96_INT_STATS);
455 } while (--time_out);
458 PRINTK2 ("%s: memory allocation, try %d failed ...\n",
460 if (try < SMC_ALLOC_MAX_TRY)
466 PRINTK2 ("%s: memory allocation, try %d succeeded ...\n",
469 /* I can send the packet now.. */
471 ioaddr = SMC_BASE_ADDRESS;
473 buf = (byte *) packet;
475 /* If I get here, I _know_ there is a packet slot waiting for me */
476 packet_no = SMC_inb (LAN91C96_ARR);
477 if (packet_no & LAN91C96_ARR_FAILED) {
478 /* or isn't there? BAD CHIP! */
479 printf ("%s: Memory allocation failed. \n", SMC_DEV_NAME);
483 /* we have a packet address, so tell the card to use it */
484 SMC_outb (packet_no, LAN91C96_PNR);
486 /* point to the beginning of the packet */
487 SMC_outw (LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
489 PRINTK3 ("%s: Trying to xmit packet of length %x\n",
490 SMC_DEV_NAME, length);
493 printf ("Transmitting Packet\n");
494 print_packet (buf, length);
497 /* send the packet length ( +6 for status, length and ctl byte )
498 and the status word ( set to zeros ) */
500 SMC_outl ((length + 6) << 16, LAN91C96_DATA_HIGH);
502 SMC_outw (0, LAN91C96_DATA_HIGH);
503 /* send the packet length ( +6 for status words, length, and ctl */
504 SMC_outw ((length + 6), LAN91C96_DATA_HIGH);
505 #endif /* USE_32_BIT */
507 /* send the actual data
508 * I _think_ it's faster to send the longs first, and then
509 * mop up by sending the last word. It depends heavily
510 * on alignment, at least on the 486. Maybe it would be
511 * a good idea to check which is optimal? But that could take
512 * almost as much time as is saved?
515 SMC_outsl (LAN91C96_DATA_HIGH, buf, length >> 2);
517 SMC_outw (*((word *) (buf + (length & 0xFFFFFFFC))),
520 SMC_outsw (LAN91C96_DATA_HIGH, buf, (length) >> 1);
521 #endif /* USE_32_BIT */
523 /* Send the last byte, if there is one. */
524 if ((length & 1) == 0) {
525 SMC_outw (0, LAN91C96_DATA_HIGH);
527 SMC_outw (buf[length - 1] | 0x2000, LAN91C96_DATA_HIGH);
530 /* and let the chipset deal with it */
531 SMC_outw (LAN91C96_MMUCR_ENQUEUE, LAN91C96_MMU);
533 /* poll for TX INT */
534 if (poll4int (LAN91C96_MSK_TX_INT, SMC_TX_TIMEOUT)) {
536 PRINTK2 ("%s: TX timeout, sending failed...\n", SMC_DEV_NAME);
539 SMC_outw (LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
541 /* wait for MMU getting ready (low) */
542 while (SMC_inw (LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY) {
546 PRINTK2 ("MMU ready\n");
552 SMC_outw (LAN91C96_IST_TX_INT, LAN91C96_INT_STATS);
554 PRINTK2 ("%s: Sent packet of length %d \n", SMC_DEV_NAME, length);
557 SMC_outw (LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
559 /* wait for MMU getting ready (low) */
560 while (SMC_inw (LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY) {
564 PRINTK2 ("MMU ready\n");
570 /*-------------------------------------------------------------------------
571 * smc_destructor( struct net_device * dev )
573 * dev, pointer to the device structure
577 *--------------------------------------------------------------------------
579 void smc_destructor ()
581 PRINTK2 (CARDNAME ":smc_destructor\n");
586 * Open and Initialize the board
588 * Set up everything, reset the card, etc ..
591 static int smc_open ()
593 int i; /* used to set hw ethernet address */
595 PRINTK2 ("%s:smc_open\n", SMC_DEV_NAME);
597 /* reset the hardware */
604 for (i = 0; i < 6; i += 2) {
607 address = smc_mac_addr[i + 1] << 8;
608 address |= smc_mac_addr[i];
609 SMC_outw (address, LAN91C96_IA0 + i);
614 /*-------------------------------------------------------------
616 * smc_rcv - receive a packet from the card
618 * There is ( at least ) a packet waiting to be read from
622 * o If an error, record it
623 * o otherwise, read in the packet
624 *-------------------------------------------------------------
626 static int smc_rcv ()
639 packet_number = SMC_inw (LAN91C96_FIFO);
641 if (packet_number & LAN91C96_FIFO_RXEMPTY) {
645 PRINTK3 ("%s:smc_rcv\n", SMC_DEV_NAME);
646 /* start reading from the start of the packet */
647 SMC_outw (LAN91C96_PTR_READ | LAN91C96_PTR_RCV |
648 LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
650 /* First two words are status and packet_length */
652 stat_len = SMC_inl (LAN91C96_DATA_HIGH);
653 status = stat_len & 0xffff;
654 packet_length = stat_len >> 16;
656 status = SMC_inw (LAN91C96_DATA_HIGH);
657 packet_length = SMC_inw (LAN91C96_DATA_HIGH);
660 packet_length &= 0x07ff; /* mask off top bits */
662 PRINTK2 ("RCV: STATUS %4x LENGTH %4x\n", status, packet_length);
664 if (!(status & FRAME_FILTER)) {
665 /* Adjust for having already read the first two words */
666 packet_length -= 4; /*4; */
669 /* set odd length for bug in LAN91C111, */
670 /* which never sets RS_ODDFRAME */
675 PRINTK3 (" Reading %d dwords (and %d bytes) \n",
676 packet_length >> 2, packet_length & 3);
677 /* QUESTION: Like in the TX routine, do I want
678 to send the DWORDs or the bytes first, or some
679 mixture. A mixture might improve already slow PIO
681 SMC_insl (LAN91C96_DATA_HIGH, NetRxPackets[0], packet_length >> 2);
682 /* read the left over bytes */
683 if (packet_length & 3) {
686 byte *tail = (byte *) (NetRxPackets[0] + (packet_length & ~3));
687 dword leftover = SMC_inl (LAN91C96_DATA_HIGH);
689 for (i = 0; i < (packet_length & 3); i++)
690 *tail++ = (byte) (leftover >> (8 * i)) & 0xff;
693 PRINTK3 (" Reading %d words and %d byte(s) \n",
694 (packet_length >> 1), packet_length & 1);
695 SMC_insw (LAN91C96_DATA_HIGH, NetRxPackets[0], packet_length >> 1);
697 #endif /* USE_32_BIT */
700 printf ("Receiving Packet\n");
701 print_packet (NetRxPackets[0], packet_length);
709 while (SMC_inw (LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
710 udelay (1); /* Wait until not busy */
712 /* error or good, tell the card to get rid of this packet */
713 SMC_outw (LAN91C96_MMUCR_RELEASE_RX, LAN91C96_MMU);
715 while (SMC_inw (LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
716 udelay (1); /* Wait until not busy */
719 /* Pass the packet up to the protocol layers. */
720 NetReceive (NetRxPackets[0], packet_length);
721 return packet_length;
728 /*----------------------------------------------------
731 * this makes the board clean up everything that it can
732 * and not talk to the outside world. Caused by
733 * an 'ifconfig ethX down'
735 -----------------------------------------------------*/
736 static int smc_close ()
738 PRINTK2 ("%s:smc_close\n", SMC_DEV_NAME);
740 /* clear everything */
747 static void print_packet (byte * buf, int length)
754 printf ("Packet of length %d \n", length);
757 remainder = length % 16;
759 for (i = 0; i < lines; i++) {
762 for (cur = 0; cur < 8; cur++) {
767 printf ("%02x%02x ", a, b);
771 for (i = 0; i < remainder / 2; i++) {
776 printf ("%02x%02x ", a, b);
781 #endif /* SMC_DEBUG > 2 */
783 int eth_init (bd_t * bd)
799 int eth_send (volatile void *packet, int length)
801 return smc_send_packet (packet, length);
806 return smc_hw_init ();
809 /*-------------------------------------------------------------------------
813 * Reset and enable the device, check if the I/O space location
822 *--------------------------------------------------------------------------
824 static int smc_hw_init ()
826 unsigned short status_test;
828 /* The attribute register of the LAN91C96 is located at address
829 0x0e000000 on the lubbock platform */
830 volatile unsigned *attaddr = (unsigned *) (0x0e000000);
832 /* first reset, then enable the device. Sequence is critical */
833 attaddr[LAN91C96_ECOR] |= LAN91C96_ECOR_SRESET;
835 attaddr[LAN91C96_ECOR] &= ~LAN91C96_ECOR_SRESET;
836 attaddr[LAN91C96_ECOR] |= LAN91C96_ECOR_ENABLE;
838 /* force 16-bit mode */
839 attaddr[LAN91C96_ECSR] &= ~LAN91C96_ECSR_IOIS8;
842 /* check if the I/O address is correct, the upper byte of the
843 bank select register should read 0x33 */
845 status_test = SMC_inw (LAN91C96_BANK_SELECT);
846 if ((status_test & 0xFF00) != 0x3300) {
847 printf ("Failed to initialize ethernetchip\n");
853 #endif /* COMMANDS & CFG_NET */
855 #endif /* CONFIG_DRIVER_LAN91C96 */