1 /* fmv18x.c: A network device driver for the Fujitsu FMV-181/182/183/184.
3 Original: at1700.c (1993-94 by Donald Becker).
4 Copyright 1993 United States Government as represented by the
5 Director, National Security Agency.
6 The author may be reached as becker@scyld.com, or C/O
7 Scyld Computing Corporation
8 410 Severn Ave., Suite 210
11 Modified by Yutaka TAMIYA (tamy@flab.fujitsu.co.jp)
12 Copyright 1994 Fujitsu Laboratories Ltd.
14 Masayoshi UTAKA (utaka@ace.yk.fujitsu.co.jp)
15 for testing this driver.
16 H. NEGISHI (agy, negishi@sun45.psd.cs.fujitsu.co.jp)
17 for suggestion of some program modification.
18 Masahiro SEKIGUCHI <seki@sysrap.cs.fujitsu.co.jp>
19 for suggestion of some program modification.
20 Kazutoshi MORIOKA (morioka@aurora.oaks.cs.fujitsu.co.jp)
21 for testing this driver.
23 This software may be used and distributed according to the terms
24 of the GNU General Public License, incorporated herein by reference.
26 This is a device driver for the Fujitsu FMV-181/182/183/184, which
27 is a straight-forward Fujitsu MB86965 implementation.
31 The Fujitsu MB86965 datasheet.
32 The Fujitsu FMV-181/182 user's guide
35 static const char version[] =
36 "fmv18x.c:v2.2.0 09/24/98 Yutaka TAMIYA (tamy@flab.fujitsu.co.jp)\n";
38 #include <linux/module.h>
40 #include <linux/kernel.h>
41 #include <linux/sched.h>
42 #include <linux/types.h>
43 #include <linux/fcntl.h>
44 #include <linux/interrupt.h>
45 #include <linux/ptrace.h>
46 #include <linux/ioport.h>
48 #include <linux/slab.h>
49 #include <linux/string.h>
50 #include <linux/init.h>
51 #include <asm/system.h>
52 #include <asm/bitops.h>
55 #include <linux/errno.h>
56 #include <linux/spinlock.h>
58 #include <linux/netdevice.h>
59 #include <linux/etherdevice.h>
60 #include <linux/skbuff.h>
61 #include <linux/delay.h>
63 static int fmv18x_probe_list[] __initdata = {
64 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x300, 0x340, 0
67 /* use 0 for production, 1 for verification, >2 for debug */
71 static unsigned int net_debug = NET_DEBUG;
73 typedef unsigned char uchar;
75 /* Information that need to be kept for each board. */
77 struct net_device_stats stats;
78 long open_time; /* Useless example local info. */
79 uint tx_started:1; /* Number of packet on the Tx queue. */
80 uint tx_queue_ready:1; /* Tx queue is ready to be sent. */
81 uint rx_started:1; /* Packets are Rxing. */
82 uchar tx_queue; /* Number of packet on the Tx queue. */
83 ushort tx_queue_len; /* Current length of the Tx queue. */
88 /* Offsets from the base address. */
92 #define TX_INTR 2 /* Bit-mapped interrupt enable registers. */
96 #define CONFIG_0 6 /* Misc. configuration settings. */
98 /* Run-time register bank 2 definitions. */
99 #define DATAPORT 8 /* Word-wide DMA or programmed-I/O dataport. */
101 #define COL16CNTL 11 /* Controll Reg for 16 collisions */
103 /* Fujitsu FMV-18x Card Configuration */
104 #define FJ_STATUS0 0x10
105 #define FJ_STATUS1 0x11
106 #define FJ_CONFIG0 0x12
107 #define FJ_CONFIG1 0x13
108 #define FJ_MACADDR 0x14 /* 0x14 - 0x19 */
109 #define FJ_BUFCNTL 0x1A
110 #define FJ_BUFDATA 0x1C
111 #define FMV18X_IO_EXTENT 32
113 /* Index to functions, as function prototypes. */
115 extern int fmv18x_probe(struct net_device *dev);
117 static int fmv18x_probe1(struct net_device *dev, short ioaddr);
118 static int net_open(struct net_device *dev);
119 static int net_send_packet(struct sk_buff *skb, struct net_device *dev);
120 static void net_interrupt(int irq, void *dev_id, struct pt_regs *regs);
121 static void net_rx(struct net_device *dev);
122 static void net_timeout(struct net_device *dev);
123 static int net_close(struct net_device *dev);
124 static struct net_device_stats *net_get_stats(struct net_device *dev);
125 static void set_multicast_list(struct net_device *dev);
128 /* Check for a network adaptor of this type, and return '0' iff one exists.
129 If dev->base_addr == 0, probe all likely locations.
130 If dev->base_addr == 1, always return failure.
131 If dev->base_addr == 2, allocate space for the device and return success
132 (detachable devices only).
135 int __init fmv18x_probe(struct net_device *dev)
138 int base_addr = dev->base_addr;
140 SET_MODULE_OWNER(dev);
142 if (base_addr > 0x1ff) /* Check a single specified location. */
143 return fmv18x_probe1(dev, base_addr);
144 else if (base_addr != 0) /* Don't probe at all. */
147 for (i = 0; fmv18x_probe_list[i]; i++)
148 if (fmv18x_probe1(dev, fmv18x_probe_list[i]) == 0)
154 /* The Fujitsu datasheet suggests that the NIC be probed for by checking its
155 "signature", the default bit pattern after a reset. This *doesn't* work --
156 there is no way to reset the bus interface without a complete power-cycle!
158 It turns out that ATI came to the same conclusion I did: the only thing
159 that can be done is checking a few bits and then diving right into MAC
162 static int __init fmv18x_probe1(struct net_device *dev, short ioaddr)
164 char irqmap[4] = {3, 7, 10, 15};
165 char irqmap_pnp[8] = {3, 4, 5, 7, 9, 10, 11, 15};
166 unsigned int i, irq, retval;
167 struct net_local *lp;
169 /* Resetting the chip doesn't reset the ISA interface, so don't bother.
170 That means we have to be careful with the register values we probe for.
173 if (!request_region(ioaddr, FMV18X_IO_EXTENT, dev->name))
176 /* Check I/O address configuration and Fujitsu vendor code */
177 if (inb(ioaddr+FJ_MACADDR ) != 0x00
178 || inb(ioaddr+FJ_MACADDR+1) != 0x00
179 || inb(ioaddr+FJ_MACADDR+2) != 0x0e) {
184 /* Check PnP mode for FMV-183/184/183A/184A. */
185 /* This PnP routine is very poor. IO and IRQ should be known. */
186 if (inb(ioaddr + FJ_STATUS1) & 0x20) {
188 for (i = 0; i < 8; i++) {
189 if (irq == irqmap_pnp[i])
197 if (fmv18x_probe_list[inb(ioaddr + FJ_CONFIG0) & 0x07] != ioaddr)
199 irq = irqmap[(inb(ioaddr + FJ_CONFIG0)>>6) & 0x03];
202 /* Snarf the interrupt vector now. */
203 retval = request_irq(irq, &net_interrupt, 0, dev->name, dev);
205 printk ("FMV-18x found at %#3x, but it's unusable due to a conflict on"
206 "IRQ %d.\n", ioaddr, irq);
210 printk("%s: FMV-18x found at %#3x, IRQ %d, address ", dev->name,
213 dev->base_addr = ioaddr;
216 for(i = 0; i < 6; i++) {
217 unsigned char val = inb(ioaddr + FJ_MACADDR + i);
219 dev->dev_addr[i] = val;
222 /* "FJ_STATUS0" 12 bit 0x0400 means use regular 100 ohm 10baseT signals,
223 rather than 150 ohm shielded twisted pair compensation.
224 0x0000 == auto-sense the interface
225 0x0800 == use TP interface
226 0x1800 == use coax interface
229 const char *porttype[] = {"auto-sense", "10baseT", "auto-sense", "10base2/5"};
230 ushort setup_value = inb(ioaddr + FJ_STATUS0);
232 switch( setup_value & 0x07 ){
233 case 0x01 /* 10base5 */:
234 case 0x02 /* 10base2 */: dev->if_port = 0x18; break;
235 case 0x04 /* 10baseT */: dev->if_port = 0x08; break;
236 default /* auto-sense*/: dev->if_port = 0x00; break;
238 printk(" %s interface.\n", porttype[(dev->if_port>>3) & 3]);
241 /* Initialize LAN Controller and LAN Card */
242 outb(0xda, ioaddr + CONFIG_0); /* Initialize LAN Controller */
243 outb(0x00, ioaddr + CONFIG_1); /* Stand by mode */
244 outb(0x00, ioaddr + FJ_CONFIG1); /* Disable IRQ of LAN Card */
245 outb(0x00, ioaddr + FJ_BUFCNTL); /* Reset ? I'm not sure (TAMIYA) */
247 /* wait for a while */
250 /* Set the station address in bank zero. */
251 outb(0x00, ioaddr + CONFIG_1);
252 for (i = 0; i < 6; i++)
253 outb(dev->dev_addr[i], ioaddr + 8 + i);
255 /* Switch to bank 1 and set the multicast table to accept none. */
256 outb(0x04, ioaddr + CONFIG_1);
257 for (i = 0; i < 8; i++)
258 outb(0x00, ioaddr + 8 + i);
260 /* Switch to bank 2 and lock our I/O address. */
261 outb(0x08, ioaddr + CONFIG_1);
262 outb(dev->if_port, ioaddr + MODE13);
263 outb(0x00, ioaddr + COL16CNTL);
268 /* Initialize the device structure. */
269 dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
274 memset(dev->priv, 0, sizeof(struct net_local));
276 spin_lock_init(&lp->lock);
278 dev->open = net_open;
279 dev->stop = net_close;
280 dev->hard_start_xmit = net_send_packet;
281 dev->tx_timeout = net_timeout;
282 dev->watchdog_timeo = HZ/10;
283 dev->get_stats = net_get_stats;
284 dev->set_multicast_list = set_multicast_list;
286 /* Fill in the fields of 'dev' with ethernet-generic values. */
294 release_region(ioaddr, FMV18X_IO_EXTENT);
299 static int net_open(struct net_device *dev)
301 struct net_local *lp = dev->priv;
302 int ioaddr = dev->base_addr;
304 /* Set the configuration register 0 to 32K 100ns. byte-wide memory,
305 16 bit bus access, and two 4K Tx, enable the Rx and Tx. */
306 outb(0x5a, ioaddr + CONFIG_0);
308 /* Powerup and switch to register bank 2 for the run-time registers. */
309 outb(0xe8, ioaddr + CONFIG_1);
312 lp->tx_queue_ready = 1;
315 lp->tx_queue_len = 0;
317 /* Clear Tx and Rx Status */
318 outb(0xff, ioaddr + TX_STATUS);
319 outb(0xff, ioaddr + RX_STATUS);
320 lp->open_time = jiffies;
322 netif_start_queue(dev);
324 /* Enable the IRQ of the LAN Card */
325 outb(0x80, ioaddr + FJ_CONFIG1);
327 /* Enable both Tx and Rx interrupts */
328 outw(0x8182, ioaddr+TX_INTR);
333 static void net_timeout(struct net_device *dev)
335 struct net_local *lp = dev->priv;
336 int ioaddr = dev->base_addr;
340 printk(KERN_WARNING "%s: transmit timed out with status %04x, %s?\n", dev->name,
341 htons(inw(ioaddr + TX_STATUS)),
342 inb(ioaddr + TX_STATUS) & 0x80
343 ? "IRQ conflict" : "network cable problem");
344 printk(KERN_WARNING "%s: timeout registers: %04x %04x %04x %04x %04x %04x %04x %04x.\n",
345 dev->name, htons(inw(ioaddr + 0)),
346 htons(inw(ioaddr + 2)), htons(inw(ioaddr + 4)),
347 htons(inw(ioaddr + 6)), htons(inw(ioaddr + 8)),
348 htons(inw(ioaddr +10)), htons(inw(ioaddr +12)),
349 htons(inw(ioaddr +14)));
350 printk(KERN_WARNING "eth card: %04x %04x\n",
351 htons(inw(ioaddr+FJ_STATUS0)),
352 htons(inw(ioaddr+FJ_CONFIG0)));
353 lp->stats.tx_errors++;
354 /* ToDo: We should try to restart the adaptor... */
355 spin_lock_irqsave(&lp->lock, flags);
357 /* Initialize LAN Controller and LAN Card */
358 outb(0xda, ioaddr + CONFIG_0); /* Initialize LAN Controller */
359 outb(0x00, ioaddr + CONFIG_1); /* Stand by mode */
360 outb(0x00, ioaddr + FJ_CONFIG1); /* Disable IRQ of LAN Card */
361 outb(0x00, ioaddr + FJ_BUFCNTL); /* Reset ? I'm not sure */
363 spin_unlock_irqrestore(&lp->lock, flags);
365 netif_wake_queue(dev);
368 static int net_send_packet(struct sk_buff *skb, struct net_device *dev)
370 struct net_local *lp = dev->priv;
371 int ioaddr = dev->base_addr;
372 short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
373 unsigned char *buf = skb->data;
376 /* Block a transmit from overlapping. */
378 if (length > ETH_FRAME_LEN) {
380 printk("%s: Attempting to send a large packet (%d bytes).\n",
385 printk("%s: Transmitting a packet of length %lu.\n", dev->name,
386 (unsigned long)skb->len);
387 /* We may not start transmitting unless we finish transferring
388 a packet into the Tx queue. During executing the following
389 codes we possibly catch a Tx interrupt. Thus we flag off
390 tx_queue_ready, so that we prevent the interrupt routine
391 (net_interrupt) to start transmitting. */
392 spin_lock_irqsave(&lp->lock, flags);
393 lp->tx_queue_ready = 0;
395 outw(length, ioaddr + DATAPORT);
396 outsw(ioaddr + DATAPORT, buf, (length + 1) >> 1);
398 lp->tx_queue_len += length + 2;
400 lp->tx_queue_ready = 1;
401 spin_unlock_irqrestore(&lp->lock, flags);
403 if (lp->tx_started == 0) {
404 /* If the Tx is idle, always trigger a transmit. */
405 outb(0x80 | lp->tx_queue, ioaddr + TX_START);
407 lp->tx_queue_len = 0;
408 dev->trans_start = jiffies;
410 } else if (lp->tx_queue_len < 4096 - 1502)
411 /* Yes, there is room for one more packet. */
413 netif_stop_queue(dev);
419 /* The typical workload of the driver:
420 Handle the network interface interrupts. */
422 net_interrupt(int irq, void *dev_id, struct pt_regs *regs)
424 struct net_device *dev = dev_id;
425 struct net_local *lp;
428 ioaddr = dev->base_addr;
430 status = inw(ioaddr + TX_STATUS);
431 outw(status, ioaddr + TX_STATUS);
434 printk("%s: Interrupt with status %04x.\n", dev->name, status);
435 if (lp->rx_started == 0 &&
436 (status & 0xff00 || (inb(ioaddr + RX_MODE) & 0x40) == 0)) {
438 We cannot execute net_rx more than once at the same time for
439 the same device. During executing net_rx, we possibly catch a
440 Tx interrupt. Thus we flag on rx_started, so that we prevent
441 the interrupt routine (net_interrupt) to dive into net_rx
444 outb(0x00, ioaddr + RX_INTR); /* Disable RX intr. */
446 outb(0x81, ioaddr + RX_INTR); /* Enable RX intr. */
449 if (status & 0x00ff) {
451 /* More than 16 collisions occurred */
453 printk("%s: 16 Collision occur during Txing.\n", dev->name);
454 /* Cancel sending a packet. */
455 outb(0x03, ioaddr + COL16CNTL);
456 lp->stats.collisions++;
459 spin_lock(&lp->lock);
460 lp->stats.tx_packets++;
461 if (lp->tx_queue && lp->tx_queue_ready) {
462 outb(0x80 | lp->tx_queue, ioaddr + TX_START);
464 lp->tx_queue_len = 0;
465 dev->trans_start = jiffies;
466 netif_wake_queue(dev); /* Inform upper layers. */
469 netif_wake_queue(dev); /* Inform upper layers. */
471 spin_unlock(&lp->lock);
477 /* We have a good packet(s), get it/them out of the buffers. */
478 static void net_rx(struct net_device *dev)
480 struct net_local *lp = dev->priv;
481 int ioaddr = dev->base_addr;
484 while ((inb(ioaddr + RX_MODE) & 0x40) == 0) {
485 /* Clear PKT_RDY bit: by agy 19940922 */
486 /* outb(0x80, ioaddr + RX_STATUS); */
487 ushort status = inw(ioaddr + DATAPORT);
490 printk("%s: Rxing packet mode %02x status %04x.\n",
491 dev->name, inb(ioaddr + RX_MODE), status);
492 #ifndef final_version
494 outb(0x05, ioaddr + 14);
499 if ((status & 0xF0) != 0x20) { /* There was an error. */
500 lp->stats.rx_errors++;
501 if (status & 0x08) lp->stats.rx_length_errors++;
502 if (status & 0x04) lp->stats.rx_frame_errors++;
503 if (status & 0x02) lp->stats.rx_crc_errors++;
504 if (status & 0x01) lp->stats.rx_over_errors++;
506 ushort pkt_len = inw(ioaddr + DATAPORT);
507 /* Malloc up new buffer. */
510 if (pkt_len > 1550) {
511 printk("%s: The FMV-18x claimed a very large packet, size %d.\n",
513 outb(0x05, ioaddr + 14);
514 lp->stats.rx_errors++;
517 skb = dev_alloc_skb(pkt_len+3);
519 printk("%s: Memory squeeze, dropping packet (len %d).\n",
521 outb(0x05, ioaddr + 14);
522 lp->stats.rx_dropped++;
528 insw(ioaddr + DATAPORT, skb_put(skb,pkt_len), (pkt_len + 1) >> 1);
532 printk("%s: Rxed packet of length %d: ", dev->name, pkt_len);
533 for (i = 0; i < 14; i++)
534 printk(" %02x", skb->data[i]);
538 skb->protocol=eth_type_trans(skb, dev);
540 dev->last_rx = jiffies;
541 lp->stats.rx_packets++;
542 lp->stats.rx_bytes += pkt_len;
544 if (--boguscount <= 0)
548 /* If any worth-while packets have been received, dev_rint()
549 has done a mark_bh(NET_BH) for us and will work on them
550 when we get to the bottom-half routine. */
553 for (i = 0; i < 20; i++) {
554 if ((inb(ioaddr + RX_MODE) & 0x40) == 0x40)
556 (void)inw(ioaddr + DATAPORT); /* dummy status read */
557 outb(0x05, ioaddr + 14);
560 if (net_debug > 5 && i > 0)
561 printk("%s: Exint Rx packet with mode %02x after %d ticks.\n",
562 dev->name, inb(ioaddr + RX_MODE), i);
568 /* The inverse routine to net_open(). */
569 static int net_close(struct net_device *dev)
571 int ioaddr = dev->base_addr;
573 ((struct net_local *)dev->priv)->open_time = 0;
575 netif_stop_queue(dev);
577 /* Set configuration register 0 to disable Tx and Rx. */
578 outb(0xda, ioaddr + CONFIG_0);
580 /* Update the statistics -- ToDo. */
582 /* Power-down the chip. Green, green, green! */
583 outb(0x00, ioaddr + CONFIG_1);
585 /* Set the ethernet adaptor disable IRQ */
586 outb(0x00, ioaddr + FJ_CONFIG1);
591 /* Get the current statistics. This may be called with the card open or
593 static struct net_device_stats *net_get_stats(struct net_device *dev)
595 struct net_local *lp = dev->priv;
599 /* Set or clear the multicast filter for this adaptor.
600 num_addrs == -1 Promiscuous mode, receive all packets
601 num_addrs == 0 Normal mode, clear multicast list
602 num_addrs > 0 Multicast mode, receive normal and MC packets, and do
603 best-effort filtering.
606 static void set_multicast_list(struct net_device *dev)
608 short ioaddr = dev->base_addr;
609 if (dev->mc_count || dev->flags&(IFF_PROMISC|IFF_ALLMULTI))
612 * We must make the kernel realise we had to move
613 * into promisc mode or we start all out war on
616 dev->flags|=IFF_PROMISC;
618 outb(3, ioaddr + RX_MODE); /* Enable promiscuous mode */
621 outb(2, ioaddr + RX_MODE); /* Disable promiscuous, use normal mode */
625 static struct net_device dev_fmv18x;
626 static int io = 0x220;
629 MODULE_PARM(io, "i");
630 MODULE_PARM(irq, "i");
631 MODULE_PARM(net_debug, "i");
632 MODULE_PARM_DESC(io, "FMV-18X I/O address");
633 MODULE_PARM_DESC(irq, "FMV-18X IRQ number");
634 MODULE_PARM_DESC(net_debug, "FMV-18X debug level (0-1,5-6)");
635 MODULE_LICENSE("GPL");
637 int init_module(void)
640 printk("fmv18x: You should not use auto-probing with insmod!\n");
641 dev_fmv18x.base_addr = io;
642 dev_fmv18x.irq = irq;
643 dev_fmv18x.init = fmv18x_probe;
644 if (register_netdev(&dev_fmv18x) != 0) {
645 printk("fmv18x: register_netdev() returned non-zero.\n");
654 unregister_netdev(&dev_fmv18x);
655 kfree(dev_fmv18x.priv);
656 dev_fmv18x.priv = NULL;
658 /* If we don't do this, we can't re-insmod it later. */
659 free_irq(dev_fmv18x.irq, &dev_fmv18x);
660 release_region(dev_fmv18x.base_addr, FMV18X_IO_EXTENT);
666 * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c fmv18x.c"
668 * kept-new-versions: 5