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>
39 #include <linux/kernel.h>
40 #include <linux/types.h>
41 #include <linux/fcntl.h>
42 #include <linux/interrupt.h>
43 #include <linux/ioport.h>
45 #include <linux/slab.h>
46 #include <linux/string.h>
47 #include <linux/init.h>
48 #include <linux/errno.h>
49 #include <linux/spinlock.h>
50 #include <linux/netdevice.h>
51 #include <linux/etherdevice.h>
52 #include <linux/skbuff.h>
53 #include <linux/delay.h>
55 #include <asm/system.h>
56 #include <asm/bitops.h>
60 static unsigned fmv18x_probe_list[] __initdata = {
61 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x300, 0x340, 0
64 /* use 0 for production, 1 for verification, >2 for debug */
68 static unsigned int net_debug = NET_DEBUG;
70 typedef unsigned char uchar;
72 /* Information that need to be kept for each board. */
74 struct net_device_stats stats;
75 long open_time; /* Useless example local info. */
76 uint tx_started:1; /* Number of packet on the Tx queue. */
77 uint tx_queue_ready:1; /* Tx queue is ready to be sent. */
78 uint rx_started:1; /* Packets are Rxing. */
79 uchar tx_queue; /* Number of packet on the Tx queue. */
80 ushort tx_queue_len; /* Current length of the Tx queue. */
85 /* Offsets from the base address. */
89 #define TX_INTR 2 /* Bit-mapped interrupt enable registers. */
93 #define CONFIG_0 6 /* Misc. configuration settings. */
95 /* Run-time register bank 2 definitions. */
96 #define DATAPORT 8 /* Word-wide DMA or programmed-I/O dataport. */
98 #define COL16CNTL 11 /* Controll Reg for 16 collisions */
100 /* Fujitsu FMV-18x Card Configuration */
101 #define FJ_STATUS0 0x10
102 #define FJ_STATUS1 0x11
103 #define FJ_CONFIG0 0x12
104 #define FJ_CONFIG1 0x13
105 #define FJ_MACADDR 0x14 /* 0x14 - 0x19 */
106 #define FJ_BUFCNTL 0x1A
107 #define FJ_BUFDATA 0x1C
108 #define FMV18X_IO_EXTENT 32
110 /* Index to functions, as function prototypes. */
112 static int fmv18x_probe1(struct net_device *dev, short ioaddr);
113 static int net_open(struct net_device *dev);
114 static int net_send_packet(struct sk_buff *skb, struct net_device *dev);
115 static irqreturn_t net_interrupt(int irq, void *dev_id, struct pt_regs *regs);
116 static void net_rx(struct net_device *dev);
117 static void net_timeout(struct net_device *dev);
118 static int net_close(struct net_device *dev);
119 static struct net_device_stats *net_get_stats(struct net_device *dev);
120 static void set_multicast_list(struct net_device *dev);
123 /* Check for a network adaptor of this type, and return '0' iff one exists.
124 If dev->base_addr == 0, probe all likely locations.
125 If dev->base_addr == 1, always return failure.
126 If dev->base_addr == 2, allocate space for the device and return success
127 (detachable devices only).
130 static int io = 0x220;
133 struct net_device * __init fmv18x_probe(int unit)
135 struct net_device *dev = alloc_etherdev(sizeof(struct net_local));
140 return ERR_PTR(-ENODEV);
143 sprintf(dev->name, "eth%d", unit);
144 netdev_boot_setup_check(dev);
149 SET_MODULE_OWNER(dev);
151 if (io > 0x1ff) { /* Check a single specified location. */
152 err = fmv18x_probe1(dev, io);
153 } else if (io != 0) { /* Don't probe at all. */
156 for (port = fmv18x_probe_list; *port; port++)
157 if (fmv18x_probe1(dev, *port) == 0)
164 err = register_netdev(dev);
169 free_irq(dev->irq, dev);
170 release_region(dev->base_addr, FMV18X_IO_EXTENT);
176 /* The Fujitsu datasheet suggests that the NIC be probed for by checking its
177 "signature", the default bit pattern after a reset. This *doesn't* work --
178 there is no way to reset the bus interface without a complete power-cycle!
180 It turns out that ATI came to the same conclusion I did: the only thing
181 that can be done is checking a few bits and then diving right into MAC
184 static int __init fmv18x_probe1(struct net_device *dev, short ioaddr)
186 char irqmap[4] = {3, 7, 10, 15};
187 char irqmap_pnp[8] = {3, 4, 5, 7, 9, 10, 11, 15};
188 unsigned int i, retval;
189 struct net_local *lp;
191 /* Resetting the chip doesn't reset the ISA interface, so don't bother.
192 That means we have to be careful with the register values we probe for.
195 if (!request_region(ioaddr, FMV18X_IO_EXTENT, dev->name))
199 dev->base_addr = ioaddr;
201 /* Check I/O address configuration and Fujitsu vendor code */
202 if (inb(ioaddr+FJ_MACADDR ) != 0x00
203 || inb(ioaddr+FJ_MACADDR+1) != 0x00
204 || inb(ioaddr+FJ_MACADDR+2) != 0x0e) {
209 /* Check PnP mode for FMV-183/184/183A/184A. */
210 /* This PnP routine is very poor. IO and IRQ should be known. */
211 if (inb(ioaddr + FJ_STATUS1) & 0x20) {
212 for (i = 0; i < 8; i++) {
213 if (dev->irq == irqmap_pnp[i])
221 if (fmv18x_probe_list[inb(ioaddr + FJ_CONFIG0) & 0x07] != ioaddr)
223 dev->irq = irqmap[(inb(ioaddr + FJ_CONFIG0)>>6) & 0x03];
226 /* Snarf the interrupt vector now. */
227 retval = request_irq(dev->irq, &net_interrupt, 0, dev->name, dev);
229 printk ("FMV-18x found at %#3x, but it's unusable due to a conflict on"
230 "IRQ %d.\n", ioaddr, dev->irq);
234 printk("%s: FMV-18x found at %#3x, IRQ %d, address ", dev->name,
237 for(i = 0; i < 6; i++) {
238 unsigned char val = inb(ioaddr + FJ_MACADDR + i);
240 dev->dev_addr[i] = val;
243 /* "FJ_STATUS0" 12 bit 0x0400 means use regular 100 ohm 10baseT signals,
244 rather than 150 ohm shielded twisted pair compensation.
245 0x0000 == auto-sense the interface
246 0x0800 == use TP interface
247 0x1800 == use coax interface
250 const char *porttype[] = {"auto-sense", "10baseT", "auto-sense", "10base2/5"};
251 ushort setup_value = inb(ioaddr + FJ_STATUS0);
253 switch( setup_value & 0x07 ){
254 case 0x01 /* 10base5 */:
255 case 0x02 /* 10base2 */: dev->if_port = 0x18; break;
256 case 0x04 /* 10baseT */: dev->if_port = 0x08; break;
257 default /* auto-sense*/: dev->if_port = 0x00; break;
259 printk(" %s interface.\n", porttype[(dev->if_port>>3) & 3]);
262 /* Initialize LAN Controller and LAN Card */
263 outb(0xda, ioaddr + CONFIG_0); /* Initialize LAN Controller */
264 outb(0x00, ioaddr + CONFIG_1); /* Stand by mode */
265 outb(0x00, ioaddr + FJ_CONFIG1); /* Disable IRQ of LAN Card */
266 outb(0x00, ioaddr + FJ_BUFCNTL); /* Reset ? I'm not sure (TAMIYA) */
268 /* wait for a while */
271 /* Set the station address in bank zero. */
272 outb(0x00, ioaddr + CONFIG_1);
273 for (i = 0; i < 6; i++)
274 outb(dev->dev_addr[i], ioaddr + 8 + i);
276 /* Switch to bank 1 and set the multicast table to accept none. */
277 outb(0x04, ioaddr + CONFIG_1);
278 for (i = 0; i < 8; i++)
279 outb(0x00, ioaddr + 8 + i);
281 /* Switch to bank 2 and lock our I/O address. */
282 outb(0x08, ioaddr + CONFIG_1);
283 outb(dev->if_port, ioaddr + MODE13);
284 outb(0x00, ioaddr + COL16CNTL);
289 /* Initialize the device structure. */
290 dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
295 memset(dev->priv, 0, sizeof(struct net_local));
297 spin_lock_init(&lp->lock);
299 dev->open = net_open;
300 dev->stop = net_close;
301 dev->hard_start_xmit = net_send_packet;
302 dev->tx_timeout = net_timeout;
303 dev->watchdog_timeo = HZ/10;
304 dev->get_stats = net_get_stats;
305 dev->set_multicast_list = set_multicast_list;
309 free_irq(dev->irq, dev);
311 release_region(ioaddr, FMV18X_IO_EXTENT);
316 static int net_open(struct net_device *dev)
318 struct net_local *lp = dev->priv;
319 int ioaddr = dev->base_addr;
321 /* Set the configuration register 0 to 32K 100ns. byte-wide memory,
322 16 bit bus access, and two 4K Tx, enable the Rx and Tx. */
323 outb(0x5a, ioaddr + CONFIG_0);
325 /* Powerup and switch to register bank 2 for the run-time registers. */
326 outb(0xe8, ioaddr + CONFIG_1);
329 lp->tx_queue_ready = 1;
332 lp->tx_queue_len = 0;
334 /* Clear Tx and Rx Status */
335 outb(0xff, ioaddr + TX_STATUS);
336 outb(0xff, ioaddr + RX_STATUS);
337 lp->open_time = jiffies;
339 netif_start_queue(dev);
341 /* Enable the IRQ of the LAN Card */
342 outb(0x80, ioaddr + FJ_CONFIG1);
344 /* Enable both Tx and Rx interrupts */
345 outw(0x8182, ioaddr+TX_INTR);
350 static void net_timeout(struct net_device *dev)
352 struct net_local *lp = dev->priv;
353 int ioaddr = dev->base_addr;
357 printk(KERN_WARNING "%s: transmit timed out with status %04x, %s?\n", dev->name,
358 htons(inw(ioaddr + TX_STATUS)),
359 inb(ioaddr + TX_STATUS) & 0x80
360 ? "IRQ conflict" : "network cable problem");
361 printk(KERN_WARNING "%s: timeout registers: %04x %04x %04x %04x %04x %04x %04x %04x.\n",
362 dev->name, htons(inw(ioaddr + 0)),
363 htons(inw(ioaddr + 2)), htons(inw(ioaddr + 4)),
364 htons(inw(ioaddr + 6)), htons(inw(ioaddr + 8)),
365 htons(inw(ioaddr +10)), htons(inw(ioaddr +12)),
366 htons(inw(ioaddr +14)));
367 printk(KERN_WARNING "eth card: %04x %04x\n",
368 htons(inw(ioaddr+FJ_STATUS0)),
369 htons(inw(ioaddr+FJ_CONFIG0)));
370 lp->stats.tx_errors++;
371 /* ToDo: We should try to restart the adaptor... */
372 spin_lock_irqsave(&lp->lock, flags);
374 /* Initialize LAN Controller and LAN Card */
375 outb(0xda, ioaddr + CONFIG_0); /* Initialize LAN Controller */
376 outb(0x00, ioaddr + CONFIG_1); /* Stand by mode */
377 outb(0x00, ioaddr + FJ_CONFIG1); /* Disable IRQ of LAN Card */
378 outb(0x00, ioaddr + FJ_BUFCNTL); /* Reset ? I'm not sure */
380 spin_unlock_irqrestore(&lp->lock, flags);
382 netif_wake_queue(dev);
385 static int net_send_packet(struct sk_buff *skb, struct net_device *dev)
387 struct net_local *lp = dev->priv;
388 int ioaddr = dev->base_addr;
389 short length = skb->len;
393 /* Block a transmit from overlapping. */
395 if (length > ETH_FRAME_LEN) {
397 printk("%s: Attempting to send a large packet (%d bytes).\n",
402 if (length < ETH_ZLEN) {
403 skb = skb_padto(skb, ETH_ZLEN);
411 printk("%s: Transmitting a packet of length %lu.\n", dev->name,
412 (unsigned long)skb->len);
413 /* We may not start transmitting unless we finish transferring
414 a packet into the Tx queue. During executing the following
415 codes we possibly catch a Tx interrupt. Thus we flag off
416 tx_queue_ready, so that we prevent the interrupt routine
417 (net_interrupt) to start transmitting. */
418 spin_lock_irqsave(&lp->lock, flags);
419 lp->tx_queue_ready = 0;
421 outw(length, ioaddr + DATAPORT);
422 outsw(ioaddr + DATAPORT, buf, (length + 1) >> 1);
424 lp->tx_queue_len += length + 2;
426 lp->tx_queue_ready = 1;
427 spin_unlock_irqrestore(&lp->lock, flags);
429 if (lp->tx_started == 0) {
430 /* If the Tx is idle, always trigger a transmit. */
431 outb(0x80 | lp->tx_queue, ioaddr + TX_START);
433 lp->tx_queue_len = 0;
434 dev->trans_start = jiffies;
436 } else if (lp->tx_queue_len >= 4096 - 1502) /* No room for a packet */
437 netif_stop_queue(dev);
443 /* The typical workload of the driver:
444 Handle the network interface interrupts. */
446 net_interrupt(int irq, void *dev_id, struct pt_regs *regs)
448 struct net_device *dev = dev_id;
449 struct net_local *lp;
452 ioaddr = dev->base_addr;
454 status = inw(ioaddr + TX_STATUS);
455 outw(status, ioaddr + TX_STATUS);
458 printk("%s: Interrupt with status %04x.\n", dev->name, status);
459 if (lp->rx_started == 0 &&
460 (status & 0xff00 || (inb(ioaddr + RX_MODE) & 0x40) == 0)) {
462 We cannot execute net_rx more than once at the same time for
463 the same device. During executing net_rx, we possibly catch a
464 Tx interrupt. Thus we flag on rx_started, so that we prevent
465 the interrupt routine (net_interrupt) to dive into net_rx
468 outb(0x00, ioaddr + RX_INTR); /* Disable RX intr. */
470 outb(0x81, ioaddr + RX_INTR); /* Enable RX intr. */
473 if (status & 0x00ff) {
475 /* More than 16 collisions occurred */
477 printk("%s: 16 Collision occur during Txing.\n", dev->name);
478 /* Cancel sending a packet. */
479 outb(0x03, ioaddr + COL16CNTL);
480 lp->stats.collisions++;
483 spin_lock(&lp->lock);
484 lp->stats.tx_packets++;
485 if (lp->tx_queue && lp->tx_queue_ready) {
486 outb(0x80 | lp->tx_queue, ioaddr + TX_START);
488 lp->tx_queue_len = 0;
489 dev->trans_start = jiffies;
490 netif_wake_queue(dev); /* Inform upper layers. */
493 netif_wake_queue(dev); /* Inform upper layers. */
495 spin_unlock(&lp->lock);
498 return IRQ_RETVAL(status);
501 /* We have a good packet(s), get it/them out of the buffers. */
502 static void net_rx(struct net_device *dev)
504 struct net_local *lp = dev->priv;
505 int ioaddr = dev->base_addr;
508 while ((inb(ioaddr + RX_MODE) & 0x40) == 0) {
509 /* Clear PKT_RDY bit: by agy 19940922 */
510 /* outb(0x80, ioaddr + RX_STATUS); */
511 ushort status = inw(ioaddr + DATAPORT);
514 printk("%s: Rxing packet mode %02x status %04x.\n",
515 dev->name, inb(ioaddr + RX_MODE), status);
516 #ifndef final_version
518 outb(0x05, ioaddr + 14);
523 if ((status & 0xF0) != 0x20) { /* There was an error. */
524 lp->stats.rx_errors++;
525 if (status & 0x08) lp->stats.rx_length_errors++;
526 if (status & 0x04) lp->stats.rx_frame_errors++;
527 if (status & 0x02) lp->stats.rx_crc_errors++;
528 if (status & 0x01) lp->stats.rx_over_errors++;
530 ushort pkt_len = inw(ioaddr + DATAPORT);
531 /* Malloc up new buffer. */
534 if (pkt_len > 1550) {
535 printk("%s: The FMV-18x claimed a very large packet, size %d.\n",
537 outb(0x05, ioaddr + 14);
538 lp->stats.rx_errors++;
541 skb = dev_alloc_skb(pkt_len+3);
543 printk("%s: Memory squeeze, dropping packet (len %d).\n",
545 outb(0x05, ioaddr + 14);
546 lp->stats.rx_dropped++;
552 insw(ioaddr + DATAPORT, skb_put(skb,pkt_len), (pkt_len + 1) >> 1);
556 printk("%s: Rxed packet of length %d: ", dev->name, pkt_len);
557 for (i = 0; i < 14; i++)
558 printk(" %02x", skb->data[i]);
562 skb->protocol=eth_type_trans(skb, dev);
564 dev->last_rx = jiffies;
565 lp->stats.rx_packets++;
566 lp->stats.rx_bytes += pkt_len;
568 if (--boguscount <= 0)
572 /* If any worth-while packets have been received, dev_rint()
573 has done a mark_bh(NET_BH) for us and will work on them
574 when we get to the bottom-half routine. */
577 for (i = 0; i < 20; i++) {
578 if ((inb(ioaddr + RX_MODE) & 0x40) == 0x40)
580 (void)inw(ioaddr + DATAPORT); /* dummy status read */
581 outb(0x05, ioaddr + 14);
584 if (net_debug > 5 && i > 0)
585 printk("%s: Exint Rx packet with mode %02x after %d ticks.\n",
586 dev->name, inb(ioaddr + RX_MODE), i);
592 /* The inverse routine to net_open(). */
593 static int net_close(struct net_device *dev)
595 int ioaddr = dev->base_addr;
597 ((struct net_local *)dev->priv)->open_time = 0;
599 netif_stop_queue(dev);
601 /* Set configuration register 0 to disable Tx and Rx. */
602 outb(0xda, ioaddr + CONFIG_0);
604 /* Update the statistics -- ToDo. */
606 /* Power-down the chip. Green, green, green! */
607 outb(0x00, ioaddr + CONFIG_1);
609 /* Set the ethernet adaptor disable IRQ */
610 outb(0x00, ioaddr + FJ_CONFIG1);
615 /* Get the current statistics. This may be called with the card open or
617 static struct net_device_stats *net_get_stats(struct net_device *dev)
619 struct net_local *lp = dev->priv;
623 /* Set or clear the multicast filter for this adaptor.
624 num_addrs == -1 Promiscuous mode, receive all packets
625 num_addrs == 0 Normal mode, clear multicast list
626 num_addrs > 0 Multicast mode, receive normal and MC packets, and do
627 best-effort filtering.
630 static void set_multicast_list(struct net_device *dev)
632 short ioaddr = dev->base_addr;
633 if (dev->mc_count || dev->flags&(IFF_PROMISC|IFF_ALLMULTI))
636 * We must make the kernel realise we had to move
637 * into promisc mode or we start all out war on
640 dev->flags|=IFF_PROMISC;
642 outb(3, ioaddr + RX_MODE); /* Enable promiscuous mode */
645 outb(2, ioaddr + RX_MODE); /* Disable promiscuous, use normal mode */
649 static struct net_device *dev_fmv18x;
651 MODULE_PARM(io, "i");
652 MODULE_PARM(irq, "i");
653 MODULE_PARM(net_debug, "i");
654 MODULE_PARM_DESC(io, "FMV-18X I/O address");
655 MODULE_PARM_DESC(irq, "FMV-18X IRQ number");
656 MODULE_PARM_DESC(net_debug, "FMV-18X debug level (0-1,5-6)");
657 MODULE_LICENSE("GPL");
659 int init_module(void)
662 printk("fmv18x: You should not use auto-probing with insmod!\n");
663 dev_fmv18x = fmv18x_probe(-1);
664 if (IS_ERR(dev_fmv18x))
665 return PTR_ERR(dev_fmv18x);
672 unregister_netdev(dev_fmv18x);
673 free_irq(dev_fmv18x->irq, dev_fmv18x);
674 release_region(dev_fmv18x->base_addr, FMV18X_IO_EXTENT);
675 free_netdev(dev_fmv18x);
681 * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c fmv18x.c"
683 * kept-new-versions: 5