2 * linux/drivers/acorn/net/etherh.c
4 * Copyright (C) 2000-2002 Russell King
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * NS8390 I-cubed EtherH and ANT EtherM specific driver
11 * Thanks to I-Cubed for information on their cards.
12 * EtherM conversion (C) 1999 Chris Kemp and Tim Watterton
13 * EtherM integration (C) 2000 Aleph One Ltd (Tak-Shing Chan)
14 * EtherM integration re-engineered by Russell King.
17 * 08-12-1996 RMK 1.00 Created
18 * RMK 1.03 Added support for EtherLan500 cards
19 * 23-11-1997 RMK 1.04 Added media autodetection
20 * 16-04-1998 RMK 1.05 Improved media autodetection
21 * 10-02-2000 RMK 1.06 Updated for 2.3.43
22 * 13-05-2000 RMK 1.07 Updated for 2.3.99-pre8
23 * 12-10-1999 CK/TEW EtherM driver first release
24 * 21-12-2000 TTC EtherH/EtherM integration
25 * 25-12-2000 RMK 1.08 Clean integration of EtherM into this driver.
26 * 03-01-2002 RMK 1.09 Always enable IRQs if we're in the nic slot.
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/types.h>
33 #include <linux/fcntl.h>
34 #include <linux/interrupt.h>
35 #include <linux/ptrace.h>
36 #include <linux/ioport.h>
38 #include <linux/slab.h>
39 #include <linux/string.h>
40 #include <linux/errno.h>
41 #include <linux/netdevice.h>
42 #include <linux/etherdevice.h>
43 #include <linux/skbuff.h>
44 #include <linux/delay.h>
45 #include <linux/init.h>
47 #include <asm/system.h>
48 #include <asm/bitops.h>
49 #include <asm/ecard.h>
53 #include "../../net/8390.h"
58 static unsigned int net_debug = NET_DEBUG;
60 static const card_ids __init etherh_cids[] = {
61 { MANU_ANT, PROD_ANT_ETHERM },
62 { MANU_I3, PROD_I3_ETHERLAN500 },
63 { MANU_I3, PROD_I3_ETHERLAN600 },
64 { MANU_I3, PROD_I3_ETHERLAN600A },
70 unsigned int ctrl_port;
74 MODULE_AUTHOR("Russell King");
75 MODULE_DESCRIPTION("EtherH/EtherM driver");
76 MODULE_LICENSE("GPL");
78 static char version[] __initdata =
79 "EtherH/EtherM Driver (c) 2002 Russell King v1.09\n";
81 #define ETHERH500_DATAPORT 0x200 /* MEMC */
82 #define ETHERH500_NS8390 0x000 /* MEMC */
83 #define ETHERH500_CTRLPORT 0x200 /* IOC */
85 #define ETHERH600_DATAPORT 16 /* MEMC */
86 #define ETHERH600_NS8390 0x200 /* MEMC */
87 #define ETHERH600_CTRLPORT 0x080 /* MEMC */
89 #define ETHERH_CP_IE 1
90 #define ETHERH_CP_IF 2
91 #define ETHERH_CP_HEARTBEAT 2
93 #define ETHERH_TX_START_PAGE 1
94 #define ETHERH_STOP_PAGE 127
97 * These came from CK/TEW
99 #define ETHERM_DATAPORT 0x080 /* MEMC */
100 #define ETHERM_NS8390 0x200 /* MEMC */
101 #define ETHERM_CTRLPORT 0x08f /* MEMC */
103 #define ETHERM_TX_START_PAGE 64
104 #define ETHERM_STOP_PAGE 127
106 /* ------------------------------------------------------------------------ */
108 static inline void etherh_set_ctrl(struct etherh_priv *eh, unsigned int mask)
111 outb(eh->ctrl, eh->ctrl_port);
114 static inline void etherh_clr_ctrl(struct etherh_priv *eh, unsigned int mask)
117 outb(eh->ctrl, eh->ctrl_port);
120 static inline unsigned int etherh_get_stat(struct etherh_priv *eh)
122 return inb(eh->ctrl_port);
128 static void etherh_irq_enable(ecard_t *ec, int irqnr)
130 struct etherh_priv *eh = ec->irq_data;
132 etherh_set_ctrl(eh, ETHERH_CP_IE);
135 static void etherh_irq_disable(ecard_t *ec, int irqnr)
137 struct etherh_priv *eh = ec->irq_data;
139 etherh_clr_ctrl(eh, ETHERH_CP_IE);
142 static expansioncard_ops_t etherh_ops = {
143 irqenable: etherh_irq_enable,
144 irqdisable: etherh_irq_disable,
151 etherh_setif(struct net_device *dev)
153 struct ei_device *ei_local = (struct ei_device *) dev->priv;
154 struct etherh_priv *eh = (struct etherh_priv *)dev->rmem_start;
155 unsigned long addr, flags;
157 save_flags_cli(flags);
159 /* set the interface type */
161 case PROD_I3_ETHERLAN600:
162 case PROD_I3_ETHERLAN600A:
163 addr = dev->base_addr + EN0_RCNTHI;
165 switch (dev->if_port) {
166 case IF_PORT_10BASE2:
167 outb((inb(addr) & 0xf8) | 1, addr);
169 case IF_PORT_10BASET:
170 outb((inb(addr) & 0xf8), addr);
175 case PROD_I3_ETHERLAN500:
176 switch (dev->if_port) {
177 case IF_PORT_10BASE2:
178 etherh_clr_ctrl(eh, ETHERH_CP_IF);
181 case IF_PORT_10BASET:
182 etherh_set_ctrl(eh, ETHERH_CP_IF);
191 restore_flags(flags);
195 etherh_getifstat(struct net_device *dev)
197 struct ei_device *ei_local = (struct ei_device *) dev->priv;
198 struct etherh_priv *eh = (struct etherh_priv *)dev->rmem_start;
202 case PROD_I3_ETHERLAN600:
203 case PROD_I3_ETHERLAN600A:
204 switch (dev->if_port) {
205 case IF_PORT_10BASE2:
208 case IF_PORT_10BASET:
209 stat = inb(dev->base_addr+EN0_RCNTHI) & 4;
214 case PROD_I3_ETHERLAN500:
215 switch (dev->if_port) {
216 case IF_PORT_10BASE2:
219 case IF_PORT_10BASET:
220 stat = etherh_get_stat(eh) & ETHERH_CP_HEARTBEAT;
234 * Configure the interface. Note that we ignore the other
235 * parts of ifmap, since its mostly meaningless for this driver.
237 static int etherh_set_config(struct net_device *dev, struct ifmap *map)
240 case IF_PORT_10BASE2:
241 case IF_PORT_10BASET:
243 * If the user explicitly sets the interface
244 * media type, turn off automedia detection.
246 dev->flags &= ~IFF_AUTOMEDIA;
247 dev->if_port = map->port;
260 * Reset the 8390 (hard reset). Note that we can't actually do this.
263 etherh_reset(struct net_device *dev)
265 struct ei_device *ei_local = (struct ei_device *) dev->priv;
267 outb_p(E8390_NODMA+E8390_PAGE0+E8390_STOP, dev->base_addr);
270 * See if we need to change the interface type.
271 * Note that we use 'interface_num' as a flag
272 * to indicate that we need to change the media.
274 if (dev->flags & IFF_AUTOMEDIA && ei_local->interface_num) {
275 ei_local->interface_num = 0;
277 if (dev->if_port == IF_PORT_10BASET)
278 dev->if_port = IF_PORT_10BASE2;
280 dev->if_port = IF_PORT_10BASET;
287 * Write a block of data out to the 8390
290 etherh_block_output (struct net_device *dev, int count, const unsigned char *buf, int start_page)
292 struct ei_device *ei_local = (struct ei_device *) dev->priv;
293 unsigned int addr, dma_addr;
294 unsigned long dma_start;
296 if (ei_local->dmaing) {
297 printk(KERN_ERR "%s: DMAing conflict in etherh_block_input: "
298 " DMAstat %d irqlock %d\n", dev->name,
299 ei_local->dmaing, ei_local->irqlock);
304 * Make sure we have a round number of bytes if we're in word mode.
306 if (count & 1 && ei_local->word16)
309 ei_local->dmaing = 1;
311 addr = dev->base_addr;
312 dma_addr = dev->mem_start;
314 count = (count + 1) & ~1;
315 outb (E8390_NODMA | E8390_PAGE0 | E8390_START, addr + E8390_CMD);
317 outb (0x42, addr + EN0_RCNTLO);
318 outb (0x00, addr + EN0_RCNTHI);
319 outb (0x42, addr + EN0_RSARLO);
320 outb (0x00, addr + EN0_RSARHI);
321 outb (E8390_RREAD | E8390_START, addr + E8390_CMD);
325 outb (ENISR_RDC, addr + EN0_ISR);
326 outb (count, addr + EN0_RCNTLO);
327 outb (count >> 8, addr + EN0_RCNTHI);
328 outb (0, addr + EN0_RSARLO);
329 outb (start_page, addr + EN0_RSARHI);
330 outb (E8390_RWRITE | E8390_START, addr + E8390_CMD);
332 if (ei_local->word16)
333 outsw (dma_addr, buf, count >> 1);
335 outsb (dma_addr, buf, count);
339 while ((inb (addr + EN0_ISR) & ENISR_RDC) == 0)
340 if (jiffies - dma_start > 2*HZ/100) { /* 20ms */
341 printk(KERN_ERR "%s: timeout waiting for TX RDC\n",
344 NS8390_init (dev, 1);
348 outb (ENISR_RDC, addr + EN0_ISR);
349 ei_local->dmaing = 0;
353 * Read a block of data from the 8390
356 etherh_block_input (struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
358 struct ei_device *ei_local = (struct ei_device *) dev->priv;
359 unsigned int addr, dma_addr;
362 if (ei_local->dmaing) {
363 printk(KERN_ERR "%s: DMAing conflict in etherh_block_input: "
364 " DMAstat %d irqlock %d\n", dev->name,
365 ei_local->dmaing, ei_local->irqlock);
369 ei_local->dmaing = 1;
371 addr = dev->base_addr;
372 dma_addr = dev->mem_start;
375 outb (E8390_NODMA | E8390_PAGE0 | E8390_START, addr + E8390_CMD);
376 outb (count, addr + EN0_RCNTLO);
377 outb (count >> 8, addr + EN0_RCNTHI);
378 outb (ring_offset, addr + EN0_RSARLO);
379 outb (ring_offset >> 8, addr + EN0_RSARHI);
380 outb (E8390_RREAD | E8390_START, addr + E8390_CMD);
382 if (ei_local->word16) {
383 insw (dma_addr, buf, count >> 1);
385 buf[count - 1] = inb (dma_addr);
387 insb (dma_addr, buf, count);
389 outb (ENISR_RDC, addr + EN0_ISR);
390 ei_local->dmaing = 0;
394 * Read a header from the 8390
397 etherh_get_header (struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
399 struct ei_device *ei_local = (struct ei_device *) dev->priv;
400 unsigned int addr, dma_addr;
402 if (ei_local->dmaing) {
403 printk(KERN_ERR "%s: DMAing conflict in etherh_get_header: "
404 " DMAstat %d irqlock %d\n", dev->name,
405 ei_local->dmaing, ei_local->irqlock);
409 ei_local->dmaing = 1;
411 addr = dev->base_addr;
412 dma_addr = dev->mem_start;
414 outb (E8390_NODMA | E8390_PAGE0 | E8390_START, addr + E8390_CMD);
415 outb (sizeof (*hdr), addr + EN0_RCNTLO);
416 outb (0, addr + EN0_RCNTHI);
417 outb (0, addr + EN0_RSARLO);
418 outb (ring_page, addr + EN0_RSARHI);
419 outb (E8390_RREAD | E8390_START, addr + E8390_CMD);
421 if (ei_local->word16)
422 insw (dma_addr, hdr, sizeof (*hdr) >> 1);
424 insb (dma_addr, hdr, sizeof (*hdr));
426 outb (ENISR_RDC, addr + EN0_ISR);
427 ei_local->dmaing = 0;
431 * Open/initialize the board. This is called (in the current kernel)
432 * sometime after booting when the 'ifconfig' program is run.
434 * This routine should set everything up anew at each open, even
435 * registers that "should" only need to be set once at boot, so that
436 * there is non-reboot way to recover if something goes wrong.
439 etherh_open(struct net_device *dev)
441 struct ei_device *ei_local = (struct ei_device *) dev->priv;
443 if (!is_valid_ether_addr(dev->dev_addr)) {
444 printk(KERN_WARNING "%s: invalid ethernet address\n",
449 if (request_irq(dev->irq, ei_interrupt, 0, dev->name, dev))
453 * Make sure that we aren't going to change the
454 * media type on the next reset - we are about to
455 * do automedia manually now.
457 ei_local->interface_num = 0;
460 * If we are doing automedia detection, do it now.
461 * This is more reliable than the 8390's detection.
463 if (dev->flags & IFF_AUTOMEDIA) {
464 dev->if_port = IF_PORT_10BASET;
467 if (!etherh_getifstat(dev)) {
468 dev->if_port = IF_PORT_10BASE2;
481 * The inverse routine to etherh_open().
484 etherh_close(struct net_device *dev)
487 free_irq (dev->irq, dev);
492 etherh_set_mac_address(struct net_device *dev, void *p)
494 struct sockaddr *addr = p;
496 if (netif_running(dev))
499 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
502 * We'll set the MAC address on the chip when we open it.
512 static void __init etherh_banner(void)
514 static int version_printed;
516 if (net_debug && version_printed++ == 0)
517 printk(KERN_INFO "%s", version);
521 * Read the ethernet address string from the on board rom.
522 * This is an ascii string...
524 static void __init etherh_addr(char *addr, struct expansion_card *ec)
526 struct in_chunk_dir cd;
531 if (ecard_readchunk(&cd, ec, 0xf5, 0) && (s = strchr(cd.d.string, '('))) {
533 for (i = 0; i < 6; i++) {
534 addr[i] = simple_strtoul(s + 1, &s, 0x10);
535 if (*s != (i == 5? ')' : ':'))
542 * Create an ethernet address from the system serial number.
544 static void __init etherm_addr(char *addr)
548 serial = system_serial_low | system_serial_high;
553 addr[3] = 0x10 + (serial >> 24);
554 addr[4] = serial >> 16;
555 addr[5] = serial >> 8;
559 static u32 etherh_regoffsets[16];
560 static u32 etherm_regoffsets[16];
562 static struct net_device * __init etherh_init_one(struct expansion_card *ec)
564 struct ei_device *ei_local;
565 struct net_device *dev;
566 struct etherh_priv *eh;
567 const char *dev_type;
574 dev = init_etherdev(NULL, 0);
578 eh = kmalloc(sizeof(struct etherh_priv), GFP_KERNEL);
582 SET_MODULE_OWNER(dev);
584 dev->open = etherh_open;
585 dev->stop = etherh_close;
586 dev->set_mac_address = etherh_set_mac_address;
587 dev->set_config = etherh_set_config;
589 dev->base_addr = ecard_address(ec, ECARD_MEMC, 0);
590 dev->rmem_start = (unsigned long)eh;
593 * IRQ and control port handling
595 ec->ops = ðerh_ops;
598 eh->id = ec->cid.product;
600 switch (ec->cid.product) {
601 case PROD_ANT_ETHERM:
602 etherm_addr(dev->dev_addr);
603 dev->base_addr += ETHERM_NS8390;
604 dev->mem_start = dev->base_addr + ETHERM_DATAPORT;
605 eh->ctrl_port = dev->base_addr + ETHERM_CTRLPORT;
608 case PROD_I3_ETHERLAN500:
609 etherh_addr(dev->dev_addr, ec);
610 dev->base_addr += ETHERH500_NS8390;
611 dev->mem_start = dev->base_addr + ETHERH500_DATAPORT;
612 eh->ctrl_port = ecard_address (ec, ECARD_IOC, ECARD_FAST)
613 + ETHERH500_CTRLPORT;
616 case PROD_I3_ETHERLAN600:
617 case PROD_I3_ETHERLAN600A:
618 etherh_addr(dev->dev_addr, ec);
619 dev->base_addr += ETHERH600_NS8390;
620 dev->mem_start = dev->base_addr + ETHERH600_DATAPORT;
621 eh->ctrl_port = dev->base_addr + ETHERH600_CTRLPORT;
625 printk(KERN_ERR "%s: unknown card type %x\n",
626 dev->name, ec->cid.product);
631 if (ec->cid.product == PROD_ANT_ETHERM)
634 if (!request_region(dev->base_addr, size, dev->name))
637 if (ethdev_init(dev))
641 * If we're in the NIC slot, make sure the IRQ is enabled
644 etherh_set_ctrl(eh, ETHERH_CP_IE);
647 * Unfortunately, ethdev_init eventually calls
648 * ether_setup, which re-writes dev->flags.
650 switch (ec->cid.product) {
651 case PROD_ANT_ETHERM:
652 dev_type = "ANT EtherM";
653 dev->if_port = IF_PORT_UNKNOWN;
656 case PROD_I3_ETHERLAN500:
657 dev_type = "i3 EtherH 500";
658 dev->if_port = IF_PORT_UNKNOWN;
661 case PROD_I3_ETHERLAN600:
662 dev_type = "i3 EtherH 600";
663 dev->flags |= IFF_PORTSEL | IFF_AUTOMEDIA;
664 dev->if_port = IF_PORT_10BASET;
667 case PROD_I3_ETHERLAN600A:
668 dev_type = "i3 EtherH 600A";
669 dev->flags |= IFF_PORTSEL | IFF_AUTOMEDIA;
670 dev->if_port = IF_PORT_10BASET;
674 dev_type = "unknown";
678 printk(KERN_INFO "%s: %s in slot %d, ",
679 dev->name, dev_type, ec->slot_no);
681 for (i = 0; i < 6; i++)
682 printk("%2.2x%c", dev->dev_addr[i], i == 5 ? '\n' : ':');
684 ei_local = (struct ei_device *) dev->priv;
685 if (ec->cid.product == PROD_ANT_ETHERM) {
686 ei_local->tx_start_page = ETHERM_TX_START_PAGE;
687 ei_local->stop_page = ETHERM_STOP_PAGE;
688 ei_local->reg_offset = etherm_regoffsets;
690 ei_local->tx_start_page = ETHERH_TX_START_PAGE;
691 ei_local->stop_page = ETHERH_STOP_PAGE;
692 ei_local->reg_offset = etherh_regoffsets;
695 ei_local->name = dev->name;
696 ei_local->word16 = 1;
697 ei_local->rx_start_page = ei_local->tx_start_page + TX_PAGES;
698 ei_local->reset_8390 = etherh_reset;
699 ei_local->block_input = etherh_block_input;
700 ei_local->block_output = etherh_block_output;
701 ei_local->get_8390_hdr = etherh_get_header;
702 ei_local->interface_num = 0;
709 release_region(dev->base_addr, 16);
713 unregister_netdev(dev);
720 #define MAX_ETHERH_CARDS 2
722 static struct net_device *e_dev[MAX_ETHERH_CARDS];
723 static struct expansion_card *e_card[MAX_ETHERH_CARDS];
725 static int __init etherh_init(void)
727 int i, ret = -ENODEV;
729 for (i = 0; i < 16; i++) {
730 etherh_regoffsets[i] = i;
731 etherm_regoffsets[i] = i << 3;
736 for (i = 0; i < MAX_ECARDS; i++) {
737 struct expansion_card *ec;
738 struct net_device *dev;
740 ec = ecard_find(0, etherh_cids);
744 dev = etherh_init_one(ec);
756 static void __exit etherh_exit(void)
760 for (i = 0; i < MAX_ETHERH_CARDS; i++) {
763 unregister_netdev(e_dev[i]);
765 if (e_card[i]->cid.product == PROD_ANT_ETHERM)
767 release_region(e_dev[i]->base_addr, size);
772 e_card[i]->ops = NULL;
773 kfree(e_card[i]->irq_data);
774 ecard_release(e_card[i]);
780 module_init(etherh_init);
781 module_exit(etherh_exit);