1 /*------------------------------------------------------------------------
3 . This is a driver for SMC's 9000 series of Ethernet cards.
5 . Copyright (C) 1996 by Erik Stahlman
6 . This software may be used and distributed according to the terms
7 . of the GNU General Public License, incorporated herein by reference.
9 . "Features" of the SMC chip:
10 . 4608 byte packet memory. ( for the 91C92. Others have more )
11 . EEPROM for configuration
12 . AUI/TP selection ( mine has 10Base2/10BaseT select )
15 . io = for the base address
17 . ifport = 0 for autodetect, 1 for TP, 2 for AUI ( or 10base2 )
20 . Erik Stahlman ( erik@vt.edu )
22 . Arnaldo Carvalho de Melo <acme@conectiva.com.br>
24 . Hardware multicast code from Peter Cammaert ( pc@denkart.be )
28 . o skeleton.c by Donald Becker ( becker@scyld.com )
29 . o ( a LOT of advice from Becker as well )
32 . 12/07/95 Erik Stahlman written, got receive/xmit handled
33 . 01/03/96 Erik Stahlman worked out some bugs, actually usable!!! :-)
34 . 01/06/96 Erik Stahlman cleaned up some, better testing, etc
35 . 01/29/96 Erik Stahlman fixed autoirq, added multicast
36 . 02/01/96 Erik Stahlman 1. disabled all interrupts in smc_reset
37 . 2. got rid of post-decrementing bug -- UGH.
38 . 02/13/96 Erik Stahlman Tried to fix autoirq failure. Added more
39 . descriptive error messages.
40 . 02/15/96 Erik Stahlman Fixed typo that caused detection failure
41 . 02/23/96 Erik Stahlman Modified it to fit into kernel tree
42 . Added support to change hardware address
43 . Cleared stats on opens
44 . 02/26/96 Erik Stahlman Trial support for Kernel 1.2.13
45 . Kludge for automatic IRQ detection
46 . 03/04/96 Erik Stahlman Fixed kernel 1.3.70 +
47 . Fixed bug reported by Gardner Buchanan in
48 . smc_enable, with outw instead of outb
49 . 03/06/96 Erik Stahlman Added hardware multicast from Peter Cammaert
50 . 04/14/00 Heiko Pruessing (SMA Regelsysteme) Fixed bug in chip memory
52 . 08/20/00 Arnaldo Melo fix kfree(skb) in smc_hardware_send_packet
53 . 12/15/00 Christian Jullien fix "Warning: kfree_skb on hard IRQ"
54 . 11/08/01 Matt Domsch Use common crc32 function
55 ----------------------------------------------------------------------------*/
57 static const char version[] =
58 "smc9194.c:v0.14 12/15/00 by Erik Stahlman (erik@vt.edu)\n";
60 #include <linux/module.h>
61 #include <linux/kernel.h>
62 #include <linux/types.h>
63 #include <linux/fcntl.h>
64 #include <linux/interrupt.h>
65 #include <linux/ioport.h>
67 #include <linux/slab.h>
68 #include <linux/string.h>
69 #include <linux/init.h>
70 #include <linux/crc32.h>
71 #include <linux/errno.h>
72 #include <linux/netdevice.h>
73 #include <linux/etherdevice.h>
74 #include <linux/skbuff.h>
75 #include <linux/bitops.h>
81 #define DRV_NAME "smc9194"
83 /*------------------------------------------------------------------------
85 . Configuration options, for the experienced user to change.
87 -------------------------------------------------------------------------*/
90 . Do you want to use 32 bit xfers? This should work on all chips, as
91 . the chipset is designed to accommodate them.
99 #if defined(__H8300H__) || defined(__H8300S__)
103 #define insl(a,b,l) io_insl_noswap(a,b,l)
104 #define outsl(a,b,l) io_outsl_noswap(a,b,l)
108 .the SMC9194 can be at any of the following port addresses. To change,
109 .for a slightly different card, you can add it to the array. Keep in
110 .mind that the array must end in zero.
118 #if defined(CONFIG_H8S_EDOSK2674)
119 static struct devlist smc_devlist[] __initdata = {
120 {.port = 0xf80000, .irq = 16},
121 {.port = 0, .irq = 0 },
124 static struct devlist smc_devlist[] __initdata = {
125 {.port = 0x200, .irq = 0},
126 {.port = 0x220, .irq = 0},
127 {.port = 0x240, .irq = 0},
128 {.port = 0x260, .irq = 0},
129 {.port = 0x280, .irq = 0},
130 {.port = 0x2A0, .irq = 0},
131 {.port = 0x2C0, .irq = 0},
132 {.port = 0x2E0, .irq = 0},
133 {.port = 0x300, .irq = 0},
134 {.port = 0x320, .irq = 0},
135 {.port = 0x340, .irq = 0},
136 {.port = 0x360, .irq = 0},
137 {.port = 0x380, .irq = 0},
138 {.port = 0x3A0, .irq = 0},
139 {.port = 0x3C0, .irq = 0},
140 {.port = 0x3E0, .irq = 0},
141 {.port = 0, .irq = 0},
145 . Wait time for memory to be free. This probably shouldn't be
146 . tuned that much, as waiting for this means nothing else happens
149 #define MEMORY_WAIT_TIME 16
154 . 0 for normal operation
155 . 1 for slightly more details
156 . >2 for various levels of increasingly useless information
157 . 2 for interrupt tracking, status flags
158 . 3 for packet dumps, etc.
163 #define PRINTK3(x) printk x
169 #define PRINTK2(x) printk x
175 #define PRINTK(x) printk x
181 /*------------------------------------------------------------------------
183 . The internal workings of the driver. If you are changing anything
184 . here with the SMC stuff, you should have the datasheet and known
185 . what you are doing.
187 -------------------------------------------------------------------------*/
188 #define CARDNAME "SMC9194"
191 /* store this information for the driver.. */
194 If I have to wait until memory is available to send
195 a packet, I will store the skbuff here, until I get the
196 desired memory. Then, I'll send it out and free it.
198 struct sk_buff * saved_skb;
201 . This keeps track of how many packets that I have
202 . sent out. When an TX_EMPTY interrupt comes, I know
203 . that all of these have been sent.
209 /*-----------------------------------------------------------------
211 . The driver can be entered at any of the following entry points.
213 .------------------------------------------------------------------ */
216 . This is called by register_netdev(). It is responsible for
217 . checking the portlist for the SMC9000 series chipset. If it finds
218 . one, then it will initialize the device, find the hardware information,
219 . and sets up the appropriate device parameters.
220 . NOTE: Interrupts are *OFF* when this procedure is called.
222 . NB:This shouldn't be static since it is referred to externally.
224 struct net_device *smc_init(int unit);
227 . The kernel calls this function when someone wants to use the device,
228 . typically 'ifconfig ethX up'.
230 static int smc_open(struct net_device *dev);
233 . Our watchdog timed out. Called by the networking layer
235 static void smc_timeout(struct net_device *dev);
238 . This is called by the kernel in response to 'ifconfig ethX down'. It
239 . is responsible for cleaning up everything that the open routine
240 . does, and maybe putting the card into a powerdown state.
242 static int smc_close(struct net_device *dev);
245 . Finally, a call to set promiscuous mode ( for TCPDUMP and related
246 . programs ) and multicast modes.
248 static void smc_set_multicast_list(struct net_device *dev);
251 /*---------------------------------------------------------------
253 . Interrupt level calls..
255 ----------------------------------------------------------------*/
258 . Handles the actual interrupt
260 static irqreturn_t smc_interrupt(int irq, void *);
262 . This is a separate procedure to handle the receipt of a packet, to
263 . leave the interrupt code looking slightly cleaner
265 static inline void smc_rcv( struct net_device *dev );
267 . This handles a TX interrupt, which is only called when an error
268 . relating to a packet is sent.
270 static inline void smc_tx( struct net_device * dev );
273 ------------------------------------------------------------
277 ------------------------------------------------------------
281 . Test if a given location contains a chip, trying to cause as
282 . little damage as possible if it's not a SMC chip.
284 static int smc_probe(struct net_device *dev, int ioaddr);
287 . A rather simple routine to print out a packet for debugging purposes.
290 static void print_packet( byte *, int );
293 #define tx_done(dev) 1
295 /* this is called to actually send the packet to the chip */
296 static void smc_hardware_send_packet( struct net_device * dev );
298 /* Since I am not sure if I will have enough room in the chip's ram
299 . to store the packet, I call this routine, which either sends it
300 . now, or generates an interrupt when the card is ready for the
302 static int smc_wait_to_send_packet( struct sk_buff * skb, struct net_device *dev );
304 /* this does a soft reset on the device */
305 static void smc_reset( int ioaddr );
307 /* Enable Interrupts, Receive, and Transmit */
308 static void smc_enable( int ioaddr );
310 /* this puts the device in an inactive state */
311 static void smc_shutdown( int ioaddr );
313 /* This routine will find the IRQ of the driver if one is not
314 . specified in the input to the device. */
315 static int smc_findirq( int ioaddr );
318 . Function: smc_reset( int ioaddr )
320 . This sets the SMC91xx chip to its normal state, hopefully from whatever
321 . mess that any other DOS driver has put it in.
323 . Maybe I should reset more registers to defaults in here? SOFTRESET should
327 . 1. send a SOFT RESET
328 . 2. wait for it to finish
329 . 3. enable autorelease mode
330 . 4. reset the memory management unit
331 . 5. clear all interrupts
334 static void smc_reset( int ioaddr )
336 /* This resets the registers mostly to defaults, but doesn't
337 affect EEPROM. That seems unnecessary */
338 SMC_SELECT_BANK( 0 );
339 outw( RCR_SOFTRESET, ioaddr + RCR );
341 /* this should pause enough for the chip to be happy */
344 /* Set the transmit and receive configuration registers to
346 outw( RCR_CLEAR, ioaddr + RCR );
347 outw( TCR_CLEAR, ioaddr + TCR );
349 /* set the control register to automatically
350 release successfully transmitted packets, to make the best
351 use out of our limited memory */
352 SMC_SELECT_BANK( 1 );
353 outw( inw( ioaddr + CONTROL ) | CTL_AUTO_RELEASE , ioaddr + CONTROL );
356 SMC_SELECT_BANK( 2 );
357 outw( MC_RESET, ioaddr + MMU_CMD );
359 /* Note: It doesn't seem that waiting for the MMU busy is needed here,
360 but this is a place where future chipsets _COULD_ break. Be wary
361 of issuing another MMU command right after this */
363 outb( 0, ioaddr + INT_MASK );
367 . Function: smc_enable
368 . Purpose: let the chip talk to the outside work
370 . 1. Enable the transmitter
371 . 2. Enable the receiver
372 . 3. Enable interrupts
374 static void smc_enable( int ioaddr )
376 SMC_SELECT_BANK( 0 );
377 /* see the header file for options in TCR/RCR NORMAL*/
378 outw( TCR_NORMAL, ioaddr + TCR );
379 outw( RCR_NORMAL, ioaddr + RCR );
381 /* now, enable interrupts */
382 SMC_SELECT_BANK( 2 );
383 outb( SMC_INTERRUPT_MASK, ioaddr + INT_MASK );
387 . Function: smc_shutdown
388 . Purpose: closes down the SMC91xxx chip.
390 . 1. zero the interrupt mask
391 . 2. clear the enable receive flag
392 . 3. clear the enable xmit flags
395 . (1) maybe utilize power down mode.
396 . Why not yet? Because while the chip will go into power down mode,
397 . the manual says that it will wake up in response to any I/O requests
398 . in the register space. Empirical results do not show this working.
400 static void smc_shutdown( int ioaddr )
402 /* no more interrupts for me */
403 SMC_SELECT_BANK( 2 );
404 outb( 0, ioaddr + INT_MASK );
406 /* and tell the card to stay away from that nasty outside world */
407 SMC_SELECT_BANK( 0 );
408 outb( RCR_CLEAR, ioaddr + RCR );
409 outb( TCR_CLEAR, ioaddr + TCR );
411 /* finally, shut the chip down */
412 SMC_SELECT_BANK( 1 );
413 outw( inw( ioaddr + CONTROL ), CTL_POWERDOWN, ioaddr + CONTROL );
419 . Function: smc_setmulticast( int ioaddr, int count, dev_mc_list * adds )
421 . This sets the internal hardware table to filter out unwanted multicast
422 . packets before they take up memory.
424 . The SMC chip uses a hash table where the high 6 bits of the CRC of
425 . address are the offset into the table. If that bit is 1, then the
426 . multicast packet is accepted. Otherwise, it's dropped silently.
428 . To use the 6 bits as an offset into the table, the high 3 bits are the
429 . number of the 8 bit register, while the low 3 bits are the bit within
432 . This routine is based very heavily on the one provided by Peter Cammaert.
436 static void smc_setmulticast( int ioaddr, int count, struct dev_mc_list * addrs ) {
438 unsigned char multicast_table[ 8 ];
439 struct dev_mc_list * cur_addr;
440 /* table for flipping the order of 3 bits */
441 unsigned char invert3[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
443 /* start with a table of all zeros: reject all */
444 memset( multicast_table, 0, sizeof( multicast_table ) );
447 for ( i = 0; i < count ; i ++, cur_addr = cur_addr->next ) {
450 /* do we have a pointer here? */
453 /* make sure this is a multicast address - shouldn't this
454 be a given if we have it here ? */
455 if ( !( *cur_addr->dmi_addr & 1 ) )
458 /* only use the low order bits */
459 position = ether_crc_le(6, cur_addr->dmi_addr) & 0x3f;
461 /* do some messy swapping to put the bit in the right spot */
462 multicast_table[invert3[position&7]] |=
463 (1<<invert3[(position>>3)&7]);
466 /* now, the table can be loaded into the chipset */
467 SMC_SELECT_BANK( 3 );
469 for ( i = 0; i < 8 ; i++ ) {
470 outb( multicast_table[i], ioaddr + MULTICAST1 + i );
475 . Function: smc_wait_to_send_packet( struct sk_buff * skb, struct net_device * )
477 . Attempt to allocate memory for a packet, if chip-memory is not
478 . available, then tell the card to generate an interrupt when it
483 . o if the saved_skb is not currently null, then drop this packet
484 . on the floor. This should never happen, because of TBUSY.
485 . o if the saved_skb is null, then replace it with the current packet,
486 . o See if I can sending it now.
487 . o (NO): Enable interrupts and let the interrupt handler deal with it.
488 . o (YES):Send it now.
490 static int smc_wait_to_send_packet( struct sk_buff * skb, struct net_device * dev )
492 struct smc_local *lp = netdev_priv(dev);
493 unsigned int ioaddr = dev->base_addr;
495 unsigned short numPages;
498 netif_stop_queue(dev);
499 /* Well, I want to send the packet.. but I don't know
500 if I can send it right now... */
502 if ( lp->saved_skb) {
503 /* THIS SHOULD NEVER HAPPEN. */
504 dev->stats.tx_aborted_errors++;
505 printk(CARDNAME": Bad Craziness - sent packet while busy.\n" );
512 if (length < ETH_ZLEN) {
513 if (skb_padto(skb, ETH_ZLEN)) {
514 netif_wake_queue(dev);
521 ** The MMU wants the number of pages to be the number of 256 bytes
522 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
524 ** Pkt size for allocating is data length +6 (for additional status words,
525 ** length and ctl!) If odd size last byte is included in this header.
527 numPages = ((length & 0xfffe) + 6) / 256;
530 printk(CARDNAME": Far too big packet error. \n");
531 /* freeing the packet is a good thing here... but should
532 . any packets of this size get down here? */
534 lp->saved_skb = NULL;
535 /* this IS an error, but, i don't want the skb saved */
536 netif_wake_queue(dev);
539 /* either way, a packet is waiting now */
540 lp->packets_waiting++;
542 /* now, try to allocate the memory */
543 SMC_SELECT_BANK( 2 );
544 outw( MC_ALLOC | numPages, ioaddr + MMU_CMD );
548 . wait a short amount of time.. if I can send a packet now, I send
549 . it now. Otherwise, I enable an interrupt and wait for one to be
552 . I could have handled this a slightly different way, by checking to
553 . see if any memory was available in the FREE MEMORY register. However,
554 . either way, I need to generate an allocation, and the allocation works
555 . no matter what, so I saw no point in checking free memory.
557 time_out = MEMORY_WAIT_TIME;
561 status = inb( ioaddr + INTERRUPT );
562 if ( status & IM_ALLOC_INT ) {
563 /* acknowledge the interrupt */
564 outb( IM_ALLOC_INT, ioaddr + INTERRUPT );
567 } while ( -- time_out );
570 /* oh well, wait until the chip finds memory later */
571 SMC_ENABLE_INT( IM_ALLOC_INT );
572 PRINTK2((CARDNAME": memory allocation deferred. \n"));
573 /* it's deferred, but I'll handle it later */
576 /* or YES! I can send the packet now.. */
577 smc_hardware_send_packet(dev);
578 netif_wake_queue(dev);
583 . Function: smc_hardware_send_packet(struct net_device * )
585 . This sends the actual packet to the SMC9xxx chip.
588 . First, see if a saved_skb is available.
589 . ( this should NOT be called if there is no 'saved_skb'
590 . Now, find the packet number that the chip allocated
591 . Point the data pointers at it in memory
592 . Set the length word in the chip's memory
593 . Dump the packet to chip memory
594 . Check if a last byte is needed ( odd length packet )
595 . if so, set the control flag right
596 . Tell the card to send it
597 . Enable the transmit interrupt, so I know if it failed
598 . Free the kernel data if I actually sent it.
600 static void smc_hardware_send_packet( struct net_device * dev )
602 struct smc_local *lp = netdev_priv(dev);
604 struct sk_buff * skb = lp->saved_skb;
609 ioaddr = dev->base_addr;
612 PRINTK((CARDNAME": In XMIT with no packet to send \n"));
615 length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
618 /* If I get here, I _know_ there is a packet slot waiting for me */
619 packet_no = inb( ioaddr + PNR_ARR + 1 );
620 if ( packet_no & 0x80 ) {
621 /* or isn't there? BAD CHIP! */
622 printk(KERN_DEBUG CARDNAME": Memory allocation failed. \n");
623 dev_kfree_skb_any(skb);
624 lp->saved_skb = NULL;
625 netif_wake_queue(dev);
629 /* we have a packet address, so tell the card to use it */
630 outb( packet_no, ioaddr + PNR_ARR );
632 /* point to the beginning of the packet */
633 outw( PTR_AUTOINC , ioaddr + POINTER );
635 PRINTK3((CARDNAME": Trying to xmit packet of length %x\n", length ));
637 print_packet( buf, length );
640 /* send the packet length ( +6 for status, length and ctl byte )
641 and the status word ( set to zeros ) */
643 outl( (length +6 ) << 16 , ioaddr + DATA_1 );
645 outw( 0, ioaddr + DATA_1 );
646 /* send the packet length ( +6 for status words, length, and ctl*/
647 outb( (length+6) & 0xFF,ioaddr + DATA_1 );
648 outb( (length+6) >> 8 , ioaddr + DATA_1 );
651 /* send the actual data
652 . I _think_ it's faster to send the longs first, and then
653 . mop up by sending the last word. It depends heavily
654 . on alignment, at least on the 486. Maybe it would be
655 . a good idea to check which is optimal? But that could take
656 . almost as much time as is saved?
659 if ( length & 0x2 ) {
660 outsl(ioaddr + DATA_1, buf, length >> 2 );
661 #if !defined(__H8300H__) && !defined(__H8300S__)
662 outw( *((word *)(buf + (length & 0xFFFFFFFC))),ioaddr +DATA_1);
664 ctrl_outw( *((word *)(buf + (length & 0xFFFFFFFC))),ioaddr +DATA_1);
668 outsl(ioaddr + DATA_1, buf, length >> 2 );
670 outsw(ioaddr + DATA_1 , buf, (length ) >> 1);
672 /* Send the last byte, if there is one. */
674 if ( (length & 1) == 0 ) {
675 outw( 0, ioaddr + DATA_1 );
677 outb( buf[length -1 ], ioaddr + DATA_1 );
678 outb( 0x20, ioaddr + DATA_1);
681 /* enable the interrupts */
682 SMC_ENABLE_INT( (IM_TX_INT | IM_TX_EMPTY_INT) );
684 /* and let the chipset deal with it */
685 outw( MC_ENQUEUE , ioaddr + MMU_CMD );
687 PRINTK2((CARDNAME": Sent packet of length %d \n",length));
689 lp->saved_skb = NULL;
690 dev_kfree_skb_any (skb);
692 dev->trans_start = jiffies;
694 /* we can send another packet */
695 netif_wake_queue(dev);
700 /*-------------------------------------------------------------------------
704 | dev->base_addr == 0, try to find all possible locations
705 | dev->base_addr == 1, return failure code
706 | dev->base_addr == 2, always allocate space, and return success
707 | dev->base_addr == <anything else> this is the address to check
710 | pointer to net_device or ERR_PTR(error)
712 ---------------------------------------------------------------------------
718 struct net_device * __init smc_init(int unit)
720 struct net_device *dev = alloc_etherdev(sizeof(struct smc_local));
721 struct devlist *smcdev = smc_devlist;
725 return ERR_PTR(-ENODEV);
728 sprintf(dev->name, "eth%d", unit);
729 netdev_boot_setup_check(dev);
734 if (io > 0x1ff) { /* Check a single specified location. */
735 err = smc_probe(dev, io);
736 } else if (io != 0) { /* Don't probe at all. */
739 for (;smcdev->port; smcdev++) {
740 if (smc_probe(dev, smcdev->port) == 0)
748 err = register_netdev(dev);
753 free_irq(dev->irq, dev);
754 release_region(dev->base_addr, SMC_IO_EXTENT);
760 /*----------------------------------------------------------------------
763 . This routine has a simple purpose -- make the SMC chip generate an
764 . interrupt, so an auto-detect routine can detect it, and find the IRQ,
765 ------------------------------------------------------------------------
767 int __init smc_findirq( int ioaddr )
771 unsigned long cookie;
774 cookie = probe_irq_on();
777 * What I try to do here is trigger an ALLOC_INT. This is done
778 * by allocating a small chunk of memory, which will give an interrupt
784 /* enable ALLOCation interrupts ONLY */
785 outb( IM_ALLOC_INT, ioaddr + INT_MASK );
788 . Allocate 512 bytes of memory. Note that the chip was just
789 . reset so all the memory is available
791 outw( MC_ALLOC | 1, ioaddr + MMU_CMD );
794 . Wait until positive that the interrupt has been generated
799 int_status = inb( ioaddr + INTERRUPT );
801 if ( int_status & IM_ALLOC_INT )
802 break; /* got the interrupt */
805 /* there is really nothing that I can do here if timeout fails,
806 as probe_irq_off will return a 0 anyway, which is what I
807 want in this case. Plus, the clean up is needed in both
811 On a fast machine, the status might change before the interrupt
812 is given to the processor. This means that the interrupt was
813 never detected, and probe_irq_off fails to report anything.
814 This should fix probe_irq_* problems.
819 /* and disable all interrupts again */
820 outb( 0, ioaddr + INT_MASK );
822 /* and return what I found */
823 return probe_irq_off(cookie);
824 #else /* NO_AUTOPROBE */
825 struct devlist *smcdev;
826 for (smcdev = smc_devlist; smcdev->port; smcdev++) {
827 if (smcdev->port == ioaddr)
834 /*----------------------------------------------------------------------
835 . Function: smc_probe( int ioaddr )
838 . Tests to see if a given ioaddr points to an SMC9xxx chip.
839 . Returns a 0 on success
842 . (1) see if the high byte of BANK_SELECT is 0x33
843 . (2) compare the ioaddr with the base register's address
844 . (3) see if I recognize the chip ID in the appropriate register
846 .---------------------------------------------------------------------
849 /*---------------------------------------------------------------
850 . Here I do typical initialization tasks.
852 . o Initialize the structure if needed
853 . o print out my vanity message if not done so already
854 . o print out what type of hardware is detected
855 . o print out the ethernet address
857 . o set up my private data
858 . o configure the dev structure with my subroutines
859 . o actually GRAB the irq.
861 .-----------------------------------------------------------------
863 static int __init smc_probe(struct net_device *dev, int ioaddr)
865 int i, memory, retval;
866 static unsigned version_printed;
869 const char *version_string;
870 const char *if_string;
873 word revision_register;
874 word base_address_register;
875 word configuration_register;
876 word memory_info_register;
877 word memory_cfg_register;
879 DECLARE_MAC_BUF(mac);
881 /* Grab the region so that no one else tries to probe our ioports. */
882 if (!request_region(ioaddr, SMC_IO_EXTENT, DRV_NAME))
886 dev->if_port = ifport;
888 /* First, see if the high byte is 0x33 */
889 bank = inw( ioaddr + BANK_SELECT );
890 if ( (bank & 0xFF00) != 0x3300 ) {
894 /* The above MIGHT indicate a device, but I need to write to further
896 outw( 0x0, ioaddr + BANK_SELECT );
897 bank = inw( ioaddr + BANK_SELECT );
898 if ( (bank & 0xFF00 ) != 0x3300 ) {
902 #if !defined(CONFIG_H8S_EDOSK2674)
903 /* well, we've already written once, so hopefully another time won't
904 hurt. This time, I need to switch the bank register to bank 1,
905 so I can access the base address register */
907 base_address_register = inw( ioaddr + BASE );
908 if ( ioaddr != ( base_address_register >> 3 & 0x3E0 ) ) {
909 printk(CARDNAME ": IOADDR %x doesn't match configuration (%x)."
910 "Probably not a SMC chip\n",
911 ioaddr, base_address_register >> 3 & 0x3E0 );
912 /* well, the base address register didn't match. Must not have
913 been a SMC chip after all. */
918 (void)base_address_register; /* Warning suppression */
922 /* check if the revision register is something that I recognize.
923 These might need to be added to later, as future revisions
926 revision_register = inw( ioaddr + REVISION );
927 if ( !chip_ids[ ( revision_register >> 4 ) & 0xF ] ) {
928 /* I don't recognize this chip, so... */
929 printk(CARDNAME ": IO %x: Unrecognized revision register:"
930 " %x, Contact author. \n", ioaddr, revision_register );
936 /* at this point I'll assume that the chip is an SMC9xxx.
937 It might be prudent to check a listing of MAC addresses
938 against the hardware address, or do some other tests. */
940 if (version_printed++ == 0)
941 printk("%s", version);
943 /* fill in some of the fields */
944 dev->base_addr = ioaddr;
947 . Get the MAC address ( bank 1, regs 4 - 9 )
949 SMC_SELECT_BANK( 1 );
950 for ( i = 0; i < 6; i += 2 ) {
953 address = inw( ioaddr + ADDR0 + i );
954 dev->dev_addr[ i + 1] = address >> 8;
955 dev->dev_addr[ i ] = address & 0xFF;
958 /* get the memory information */
960 SMC_SELECT_BANK( 0 );
961 memory_info_register = inw( ioaddr + MIR );
962 memory_cfg_register = inw( ioaddr + MCR );
963 memory = ( memory_cfg_register >> 9 ) & 0x7; /* multiplier */
964 memory *= 256 * ( memory_info_register & 0xFF );
967 Now, I want to find out more about the chip. This is sort of
968 redundant, but it's cleaner to have it in both, rather than having
969 one VERY long probe procedure.
972 revision_register = inw( ioaddr + REVISION );
973 version_string = chip_ids[ ( revision_register >> 4 ) & 0xF ];
974 if ( !version_string ) {
975 /* I shouldn't get here because this call was done before.... */
980 /* is it using AUI or 10BaseT ? */
981 if ( dev->if_port == 0 ) {
983 configuration_register = inw( ioaddr + CONFIG );
984 if ( configuration_register & CFG_AUI_SELECT )
989 if_string = interfaces[ dev->if_port - 1 ];
991 /* now, reset the chip, and put it into a known state */
995 . If dev->irq is 0, then the device has to be banged on to see
998 . This banging doesn't always detect the IRQ, for unknown reasons.
999 . a workaround is to reset the chip and try again.
1001 . Interestingly, the DOS packet driver *SETS* the IRQ on the card to
1002 . be what is requested on the command line. I don't do that, mostly
1003 . because the card that I have uses a non-standard method of accessing
1004 . the IRQs, and because this _should_ work in most configurations.
1006 . Specifying an IRQ is done with the assumption that the user knows
1007 . what (s)he is doing. No checking is done!!!!
1010 if ( dev->irq < 2 ) {
1014 while ( trials-- ) {
1015 dev->irq = smc_findirq( ioaddr );
1018 /* kick the card and try again */
1019 smc_reset( ioaddr );
1022 if (dev->irq == 0 ) {
1023 printk(CARDNAME": Couldn't autodetect your IRQ. Use irq=xx.\n");
1028 /* now, print out the card info, in a short format.. */
1030 printk("%s: %s(r:%d) at %#3x IRQ:%d INTF:%s MEM:%db ", dev->name,
1031 version_string, revision_register & 0xF, ioaddr, dev->irq,
1032 if_string, memory );
1034 . Print the Ethernet address
1036 printk("ADDR: %s\n", print_mac(mac, dev->dev_addr));
1038 /* set the private data to zero by default */
1039 memset(dev->priv, 0, sizeof(struct smc_local));
1042 retval = request_irq(dev->irq, &smc_interrupt, 0, DRV_NAME, dev);
1044 printk("%s: unable to get IRQ %d (irqval=%d).\n", DRV_NAME,
1049 dev->open = smc_open;
1050 dev->stop = smc_close;
1051 dev->hard_start_xmit = smc_wait_to_send_packet;
1052 dev->tx_timeout = smc_timeout;
1053 dev->watchdog_timeo = HZ/20;
1054 dev->set_multicast_list = smc_set_multicast_list;
1059 release_region(ioaddr, SMC_IO_EXTENT);
1064 static void print_packet( byte * buf, int length )
1071 printk("Packet of length %d \n", length );
1072 lines = length / 16;
1073 remainder = length % 16;
1075 for ( i = 0; i < lines ; i ++ ) {
1078 for ( cur = 0; cur < 8; cur ++ ) {
1083 printk("%02x%02x ", a, b );
1087 for ( i = 0; i < remainder/2 ; i++ ) {
1092 printk("%02x%02x ", a, b );
1101 * Open and Initialize the board
1103 * Set up everything, reset the card, etc ..
1106 static int smc_open(struct net_device *dev)
1108 int ioaddr = dev->base_addr;
1110 int i; /* used to set hw ethernet address */
1112 /* clear out all the junk that was put here before... */
1113 memset(dev->priv, 0, sizeof(struct smc_local));
1115 /* reset the hardware */
1117 smc_reset( ioaddr );
1118 smc_enable( ioaddr );
1120 /* Select which interface to use */
1122 SMC_SELECT_BANK( 1 );
1123 if ( dev->if_port == 1 ) {
1124 outw( inw( ioaddr + CONFIG ) & ~CFG_AUI_SELECT,
1127 else if ( dev->if_port == 2 ) {
1128 outw( inw( ioaddr + CONFIG ) | CFG_AUI_SELECT,
1133 According to Becker, I have to set the hardware address
1134 at this point, because the (l)user can set it with an
1135 ioctl. Easily done...
1137 SMC_SELECT_BANK( 1 );
1138 for ( i = 0; i < 6; i += 2 ) {
1141 address = dev->dev_addr[ i + 1 ] << 8 ;
1142 address |= dev->dev_addr[ i ];
1143 outw( address, ioaddr + ADDR0 + i );
1146 netif_start_queue(dev);
1150 /*--------------------------------------------------------
1151 . Called by the kernel to send a packet out into the void
1152 . of the net. This routine is largely based on
1153 . skeleton.c, from Becker.
1154 .--------------------------------------------------------
1157 static void smc_timeout(struct net_device *dev)
1159 /* If we get here, some higher level has decided we are broken.
1160 There should really be a "kick me" function call instead. */
1161 printk(KERN_WARNING CARDNAME": transmit timed out, %s?\n",
1162 tx_done(dev) ? "IRQ conflict" :
1163 "network cable problem");
1164 /* "kick" the adaptor */
1165 smc_reset( dev->base_addr );
1166 smc_enable( dev->base_addr );
1167 dev->trans_start = jiffies;
1168 /* clear anything saved */
1169 ((struct smc_local *)dev->priv)->saved_skb = NULL;
1170 netif_wake_queue(dev);
1173 /*-------------------------------------------------------------
1175 . smc_rcv - receive a packet from the card
1177 . There is ( at least ) a packet waiting to be read from
1181 . o If an error, record it
1182 . o otherwise, read in the packet
1183 --------------------------------------------------------------
1185 static void smc_rcv(struct net_device *dev)
1187 int ioaddr = dev->base_addr;
1194 packet_number = inw( ioaddr + FIFO_PORTS );
1196 if ( packet_number & FP_RXEMPTY ) {
1197 /* we got called , but nothing was on the FIFO */
1198 PRINTK((CARDNAME ": WARNING: smc_rcv with nothing on FIFO. \n"));
1199 /* don't need to restore anything */
1203 /* start reading from the start of the packet */
1204 outw( PTR_READ | PTR_RCV | PTR_AUTOINC, ioaddr + POINTER );
1206 /* First two words are status and packet_length */
1207 status = inw( ioaddr + DATA_1 );
1208 packet_length = inw( ioaddr + DATA_1 );
1210 packet_length &= 0x07ff; /* mask off top bits */
1212 PRINTK2(("RCV: STATUS %4x LENGTH %4x\n", status, packet_length ));
1214 . the packet length contains 3 extra words :
1215 . status, length, and an extra word with an odd byte .
1219 if ( !(status & RS_ERRORS ) ){
1220 /* do stuff to make a new packet */
1221 struct sk_buff * skb;
1224 /* read one extra byte */
1225 if ( status & RS_ODDFRAME )
1228 /* set multicast stats */
1229 if ( status & RS_MULTICAST )
1230 dev->stats.multicast++;
1232 skb = dev_alloc_skb( packet_length + 5);
1234 if ( skb == NULL ) {
1235 printk(KERN_NOTICE CARDNAME ": Low memory, packet dropped.\n");
1236 dev->stats.rx_dropped++;
1241 ! This should work without alignment, but it could be
1245 skb_reserve( skb, 2 ); /* 16 bit alignment */
1247 data = skb_put( skb, packet_length);
1250 /* QUESTION: Like in the TX routine, do I want
1251 to send the DWORDs or the bytes first, or some
1252 mixture. A mixture might improve already slow PIO
1254 PRINTK3((" Reading %d dwords (and %d bytes) \n",
1255 packet_length >> 2, packet_length & 3 ));
1256 insl(ioaddr + DATA_1 , data, packet_length >> 2 );
1257 /* read the left over bytes */
1258 insb( ioaddr + DATA_1, data + (packet_length & 0xFFFFFC),
1259 packet_length & 0x3 );
1261 PRINTK3((" Reading %d words and %d byte(s) \n",
1262 (packet_length >> 1 ), packet_length & 1 ));
1263 insw(ioaddr + DATA_1 , data, packet_length >> 1);
1264 if ( packet_length & 1 ) {
1265 data += packet_length & ~1;
1266 *(data++) = inb( ioaddr + DATA_1 );
1270 print_packet( data, packet_length );
1273 skb->protocol = eth_type_trans(skb, dev );
1275 dev->last_rx = jiffies;
1276 dev->stats.rx_packets++;
1277 dev->stats.rx_bytes += packet_length;
1280 dev->stats.rx_errors++;
1282 if ( status & RS_ALGNERR ) dev->stats.rx_frame_errors++;
1283 if ( status & (RS_TOOSHORT | RS_TOOLONG ) )
1284 dev->stats.rx_length_errors++;
1285 if ( status & RS_BADCRC) dev->stats.rx_crc_errors++;
1289 /* error or good, tell the card to get rid of this packet */
1290 outw( MC_RELEASE, ioaddr + MMU_CMD );
1294 /*************************************************************************
1297 . Purpose: Handle a transmit error message. This will only be called
1298 . when an error, because of the AUTO_RELEASE mode.
1301 . Save pointer and packet no
1302 . Get the packet no from the top of the queue
1303 . check if it's valid ( if not, is this an error??? )
1304 . read the status word
1306 . ( resend? Not really, since we don't want old packets around )
1307 . Restore saved values
1308 ************************************************************************/
1309 static void smc_tx( struct net_device * dev )
1311 int ioaddr = dev->base_addr;
1312 struct smc_local *lp = netdev_priv(dev);
1320 saved_packet = inb( ioaddr + PNR_ARR );
1321 packet_no = inw( ioaddr + FIFO_PORTS );
1324 /* select this as the packet to read from */
1325 outb( packet_no, ioaddr + PNR_ARR );
1327 /* read the first word from this packet */
1328 outw( PTR_AUTOINC | PTR_READ, ioaddr + POINTER );
1330 tx_status = inw( ioaddr + DATA_1 );
1331 PRINTK3((CARDNAME": TX DONE STATUS: %4x \n", tx_status ));
1333 dev->stats.tx_errors++;
1334 if ( tx_status & TS_LOSTCAR ) dev->stats.tx_carrier_errors++;
1335 if ( tx_status & TS_LATCOL ) {
1336 printk(KERN_DEBUG CARDNAME
1337 ": Late collision occurred on last xmit.\n");
1338 dev->stats.tx_window_errors++;
1341 if ( tx_status & TS_16COL ) { ... }
1344 if ( tx_status & TS_SUCCESS ) {
1345 printk(CARDNAME": Successful packet caused interrupt \n");
1347 /* re-enable transmit */
1348 SMC_SELECT_BANK( 0 );
1349 outw( inw( ioaddr + TCR ) | TCR_ENABLE, ioaddr + TCR );
1351 /* kill the packet */
1352 SMC_SELECT_BANK( 2 );
1353 outw( MC_FREEPKT, ioaddr + MMU_CMD );
1355 /* one less packet waiting for me */
1356 lp->packets_waiting--;
1358 outb( saved_packet, ioaddr + PNR_ARR );
1362 /*--------------------------------------------------------------------
1364 . This is the main routine of the driver, to handle the device when
1365 . it needs some attention.
1368 . first, save state of the chipset
1369 . branch off into routines to handle each case, and acknowledge
1370 . each to the interrupt register
1371 . and finally restore state.
1373 ---------------------------------------------------------------------*/
1375 static irqreturn_t smc_interrupt(int irq, void * dev_id)
1377 struct net_device *dev = dev_id;
1378 int ioaddr = dev->base_addr;
1379 struct smc_local *lp = netdev_priv(dev);
1385 /* state registers */
1391 PRINTK3((CARDNAME": SMC interrupt started \n"));
1393 saved_bank = inw( ioaddr + BANK_SELECT );
1396 saved_pointer = inw( ioaddr + POINTER );
1398 mask = inb( ioaddr + INT_MASK );
1399 /* clear all interrupts */
1400 outb( 0, ioaddr + INT_MASK );
1403 /* set a timeout value, so I don't stay here forever */
1406 PRINTK2((KERN_WARNING CARDNAME ": MASK IS %x \n", mask ));
1408 /* read the status flag, and mask it */
1409 status = inb( ioaddr + INTERRUPT ) & mask;
1415 PRINTK3((KERN_WARNING CARDNAME
1416 ": Handling interrupt status %x \n", status ));
1418 if (status & IM_RCV_INT) {
1419 /* Got a packet(s). */
1420 PRINTK2((KERN_WARNING CARDNAME
1421 ": Receive Interrupt\n"));
1423 } else if (status & IM_TX_INT ) {
1424 PRINTK2((KERN_WARNING CARDNAME
1425 ": TX ERROR handled\n"));
1427 outb(IM_TX_INT, ioaddr + INTERRUPT );
1428 } else if (status & IM_TX_EMPTY_INT ) {
1430 SMC_SELECT_BANK( 0 );
1431 card_stats = inw( ioaddr + COUNTER );
1432 /* single collisions */
1433 dev->stats.collisions += card_stats & 0xF;
1435 /* multiple collisions */
1436 dev->stats.collisions += card_stats & 0xF;
1438 /* these are for when linux supports these statistics */
1440 SMC_SELECT_BANK( 2 );
1441 PRINTK2((KERN_WARNING CARDNAME
1442 ": TX_BUFFER_EMPTY handled\n"));
1443 outb( IM_TX_EMPTY_INT, ioaddr + INTERRUPT );
1444 mask &= ~IM_TX_EMPTY_INT;
1445 dev->stats.tx_packets += lp->packets_waiting;
1446 lp->packets_waiting = 0;
1448 } else if (status & IM_ALLOC_INT ) {
1449 PRINTK2((KERN_DEBUG CARDNAME
1450 ": Allocation interrupt \n"));
1451 /* clear this interrupt so it doesn't happen again */
1452 mask &= ~IM_ALLOC_INT;
1454 smc_hardware_send_packet( dev );
1456 /* enable xmit interrupts based on this */
1457 mask |= ( IM_TX_EMPTY_INT | IM_TX_INT );
1459 /* and let the card send more packets to me */
1460 netif_wake_queue(dev);
1462 PRINTK2((CARDNAME": Handoff done successfully.\n"));
1463 } else if (status & IM_RX_OVRN_INT ) {
1464 dev->stats.rx_errors++;
1465 dev->stats.rx_fifo_errors++;
1466 outb( IM_RX_OVRN_INT, ioaddr + INTERRUPT );
1467 } else if (status & IM_EPH_INT ) {
1468 PRINTK((CARDNAME ": UNSUPPORTED: EPH INTERRUPT \n"));
1469 } else if (status & IM_ERCV_INT ) {
1470 PRINTK((CARDNAME ": UNSUPPORTED: ERCV INTERRUPT \n"));
1471 outb( IM_ERCV_INT, ioaddr + INTERRUPT );
1473 } while ( timeout -- );
1476 /* restore state register */
1477 SMC_SELECT_BANK( 2 );
1478 outb( mask, ioaddr + INT_MASK );
1480 PRINTK3(( KERN_WARNING CARDNAME ": MASK is now %x \n", mask ));
1481 outw( saved_pointer, ioaddr + POINTER );
1483 SMC_SELECT_BANK( saved_bank );
1485 PRINTK3((CARDNAME ": Interrupt done\n"));
1486 return IRQ_RETVAL(handled);
1490 /*----------------------------------------------------
1493 . this makes the board clean up everything that it can
1494 . and not talk to the outside world. Caused by
1495 . an 'ifconfig ethX down'
1497 -----------------------------------------------------*/
1498 static int smc_close(struct net_device *dev)
1500 netif_stop_queue(dev);
1501 /* clear everything */
1502 smc_shutdown( dev->base_addr );
1504 /* Update the statistics here. */
1508 /*-----------------------------------------------------------
1509 . smc_set_multicast_list
1511 . This routine will, depending on the values passed to it,
1512 . either make it accept multicast packets, go into
1513 . promiscuous mode ( for TCPDUMP and cousins ) or accept
1514 . a select set of multicast packets
1516 static void smc_set_multicast_list(struct net_device *dev)
1518 short ioaddr = dev->base_addr;
1521 if ( dev->flags & IFF_PROMISC )
1522 outw( inw(ioaddr + RCR ) | RCR_PROMISC, ioaddr + RCR );
1524 /* BUG? I never disable promiscuous mode if multicasting was turned on.
1525 Now, I turn off promiscuous mode, but I don't do anything to multicasting
1526 when promiscuous mode is turned on.
1529 /* Here, I am setting this to accept all multicast packets.
1530 I don't need to zero the multicast table, because the flag is
1531 checked before the table is
1533 else if (dev->flags & IFF_ALLMULTI)
1534 outw( inw(ioaddr + RCR ) | RCR_ALMUL, ioaddr + RCR );
1536 /* We just get all multicast packets even if we only want them
1537 . from one source. This will be changed at some future
1539 else if (dev->mc_count ) {
1540 /* support hardware multicasting */
1542 /* be sure I get rid of flags I might have set */
1543 outw( inw( ioaddr + RCR ) & ~(RCR_PROMISC | RCR_ALMUL),
1545 /* NOTE: this has to set the bank, so make sure it is the
1546 last thing called. The bank is set to zero at the top */
1547 smc_setmulticast( ioaddr, dev->mc_count, dev->mc_list );
1550 outw( inw( ioaddr + RCR ) & ~(RCR_PROMISC | RCR_ALMUL),
1554 since I'm disabling all multicast entirely, I need to
1555 clear the multicast list
1557 SMC_SELECT_BANK( 3 );
1558 outw( 0, ioaddr + MULTICAST1 );
1559 outw( 0, ioaddr + MULTICAST2 );
1560 outw( 0, ioaddr + MULTICAST3 );
1561 outw( 0, ioaddr + MULTICAST4 );
1567 static struct net_device *devSMC9194;
1568 MODULE_LICENSE("GPL");
1570 module_param(io, int, 0);
1571 module_param(irq, int, 0);
1572 module_param(ifport, int, 0);
1573 MODULE_PARM_DESC(io, "SMC 99194 I/O base address");
1574 MODULE_PARM_DESC(irq, "SMC 99194 IRQ number");
1575 MODULE_PARM_DESC(ifport, "SMC 99194 interface port (0-default, 1-TP, 2-AUI)");
1577 int __init init_module(void)
1581 CARDNAME": You shouldn't use auto-probing with insmod!\n" );
1583 /* copy the parameters from insmod into the device structure */
1584 devSMC9194 = smc_init(-1);
1585 if (IS_ERR(devSMC9194))
1586 return PTR_ERR(devSMC9194);
1590 void __exit cleanup_module(void)
1592 unregister_netdev(devSMC9194);
1593 free_irq(devSMC9194->irq, devSMC9194);
1594 release_region(devSMC9194->base_addr, SMC_IO_EXTENT);
1595 free_netdev(devSMC9194);