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/ethtool.h>
44 #include <linux/skbuff.h>
45 #include <linux/delay.h>
46 #include <linux/device.h>
47 #include <linux/init.h>
48 #include <linux/bitops.h>
50 #include <asm/system.h>
51 #include <asm/ecard.h>
60 #define DRV_NAME "etherh"
61 #define DRV_VERSION "1.11"
63 static unsigned int net_debug = NET_DEBUG;
66 void __iomem *ioc_fast;
68 void __iomem *dma_base;
70 void __iomem *ctrl_port;
77 unsigned long ns8390_offset;
78 unsigned long dataport_offset;
79 unsigned long ctrlport_offset;
83 unsigned char tx_start_page;
84 unsigned char stop_page;
87 MODULE_AUTHOR("Russell King");
88 MODULE_DESCRIPTION("EtherH/EtherM driver");
89 MODULE_LICENSE("GPL");
91 static char version[] __initdata =
92 "EtherH/EtherM Driver (c) 2002-2004 Russell King " DRV_VERSION "\n";
94 #define ETHERH500_DATAPORT 0x800 /* MEMC */
95 #define ETHERH500_NS8390 0x000 /* MEMC */
96 #define ETHERH500_CTRLPORT 0x800 /* IOC */
98 #define ETHERH600_DATAPORT 0x040 /* MEMC */
99 #define ETHERH600_NS8390 0x800 /* MEMC */
100 #define ETHERH600_CTRLPORT 0x200 /* MEMC */
102 #define ETHERH_CP_IE 1
103 #define ETHERH_CP_IF 2
104 #define ETHERH_CP_HEARTBEAT 2
106 #define ETHERH_TX_START_PAGE 1
107 #define ETHERH_STOP_PAGE 127
110 * These came from CK/TEW
112 #define ETHERM_DATAPORT 0x200 /* MEMC */
113 #define ETHERM_NS8390 0x800 /* MEMC */
114 #define ETHERM_CTRLPORT 0x23c /* MEMC */
116 #define ETHERM_TX_START_PAGE 64
117 #define ETHERM_STOP_PAGE 127
119 /* ------------------------------------------------------------------------ */
121 #define etherh_priv(dev) \
122 ((struct etherh_priv *)(((char *)netdev_priv(dev)) + sizeof(struct ei_device)))
124 static inline void etherh_set_ctrl(struct etherh_priv *eh, unsigned char mask)
126 unsigned char ctrl = eh->ctrl | mask;
128 writeb(ctrl, eh->ctrl_port);
131 static inline void etherh_clr_ctrl(struct etherh_priv *eh, unsigned char mask)
133 unsigned char ctrl = eh->ctrl & ~mask;
135 writeb(ctrl, eh->ctrl_port);
138 static inline unsigned int etherh_get_stat(struct etherh_priv *eh)
140 return readb(eh->ctrl_port);
146 static void etherh_irq_enable(ecard_t *ec, int irqnr)
148 struct etherh_priv *eh = ec->irq_data;
150 etherh_set_ctrl(eh, ETHERH_CP_IE);
153 static void etherh_irq_disable(ecard_t *ec, int irqnr)
155 struct etherh_priv *eh = ec->irq_data;
157 etherh_clr_ctrl(eh, ETHERH_CP_IE);
160 static expansioncard_ops_t etherh_ops = {
161 .irqenable = etherh_irq_enable,
162 .irqdisable = etherh_irq_disable,
169 etherh_setif(struct net_device *dev)
171 struct ei_device *ei_local = netdev_priv(dev);
175 local_irq_save(flags);
177 /* set the interface type */
178 switch (etherh_priv(dev)->id) {
179 case PROD_I3_ETHERLAN600:
180 case PROD_I3_ETHERLAN600A:
181 addr = etherh_priv(dev)->base + EN0_RCNTHI;
183 switch (dev->if_port) {
184 case IF_PORT_10BASE2:
185 writeb((readb(addr) & 0xf8) | 1, addr);
187 case IF_PORT_10BASET:
188 writeb((readb(addr) & 0xf8), addr);
193 case PROD_I3_ETHERLAN500:
194 switch (dev->if_port) {
195 case IF_PORT_10BASE2:
196 etherh_clr_ctrl(etherh_priv(dev), ETHERH_CP_IF);
199 case IF_PORT_10BASET:
200 etherh_set_ctrl(etherh_priv(dev), ETHERH_CP_IF);
209 local_irq_restore(flags);
213 etherh_getifstat(struct net_device *dev)
215 struct ei_device *ei_local = netdev_priv(dev);
219 switch (etherh_priv(dev)->id) {
220 case PROD_I3_ETHERLAN600:
221 case PROD_I3_ETHERLAN600A:
222 addr = etherh_priv(dev)->base + EN0_RCNTHI;
223 switch (dev->if_port) {
224 case IF_PORT_10BASE2:
227 case IF_PORT_10BASET:
228 stat = readb(addr) & 4;
233 case PROD_I3_ETHERLAN500:
234 switch (dev->if_port) {
235 case IF_PORT_10BASE2:
238 case IF_PORT_10BASET:
239 stat = etherh_get_stat(etherh_priv(dev)) & ETHERH_CP_HEARTBEAT;
253 * Configure the interface. Note that we ignore the other
254 * parts of ifmap, since its mostly meaningless for this driver.
256 static int etherh_set_config(struct net_device *dev, struct ifmap *map)
259 case IF_PORT_10BASE2:
260 case IF_PORT_10BASET:
262 * If the user explicitly sets the interface
263 * media type, turn off automedia detection.
265 dev->flags &= ~IFF_AUTOMEDIA;
266 dev->if_port = map->port;
279 * Reset the 8390 (hard reset). Note that we can't actually do this.
282 etherh_reset(struct net_device *dev)
284 struct ei_device *ei_local = netdev_priv(dev);
285 void __iomem *addr = etherh_priv(dev)->base;
287 writeb(E8390_NODMA+E8390_PAGE0+E8390_STOP, addr);
290 * See if we need to change the interface type.
291 * Note that we use 'interface_num' as a flag
292 * to indicate that we need to change the media.
294 if (dev->flags & IFF_AUTOMEDIA && ei_local->interface_num) {
295 ei_local->interface_num = 0;
297 if (dev->if_port == IF_PORT_10BASET)
298 dev->if_port = IF_PORT_10BASE2;
300 dev->if_port = IF_PORT_10BASET;
307 * Write a block of data out to the 8390
310 etherh_block_output (struct net_device *dev, int count, const unsigned char *buf, int start_page)
312 struct ei_device *ei_local = netdev_priv(dev);
313 unsigned long dma_start;
314 void __iomem *dma_base, *addr;
316 if (ei_local->dmaing) {
317 printk(KERN_ERR "%s: DMAing conflict in etherh_block_input: "
318 " DMAstat %d irqlock %d\n", dev->name,
319 ei_local->dmaing, ei_local->irqlock);
324 * Make sure we have a round number of bytes if we're in word mode.
326 if (count & 1 && ei_local->word16)
329 ei_local->dmaing = 1;
331 addr = etherh_priv(dev)->base;
332 dma_base = etherh_priv(dev)->dma_base;
334 count = (count + 1) & ~1;
335 writeb (E8390_NODMA | E8390_PAGE0 | E8390_START, addr + E8390_CMD);
337 writeb (0x42, addr + EN0_RCNTLO);
338 writeb (0x00, addr + EN0_RCNTHI);
339 writeb (0x42, addr + EN0_RSARLO);
340 writeb (0x00, addr + EN0_RSARHI);
341 writeb (E8390_RREAD | E8390_START, addr + E8390_CMD);
345 writeb (ENISR_RDC, addr + EN0_ISR);
346 writeb (count, addr + EN0_RCNTLO);
347 writeb (count >> 8, addr + EN0_RCNTHI);
348 writeb (0, addr + EN0_RSARLO);
349 writeb (start_page, addr + EN0_RSARHI);
350 writeb (E8390_RWRITE | E8390_START, addr + E8390_CMD);
352 if (ei_local->word16)
353 writesw (dma_base, buf, count >> 1);
355 writesb (dma_base, buf, count);
359 while ((readb (addr + EN0_ISR) & ENISR_RDC) == 0)
360 if (jiffies - dma_start > 2*HZ/100) { /* 20ms */
361 printk(KERN_ERR "%s: timeout waiting for TX RDC\n",
364 NS8390_init (dev, 1);
368 writeb (ENISR_RDC, addr + EN0_ISR);
369 ei_local->dmaing = 0;
373 * Read a block of data from the 8390
376 etherh_block_input (struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
378 struct ei_device *ei_local = netdev_priv(dev);
380 void __iomem *dma_base, *addr;
382 if (ei_local->dmaing) {
383 printk(KERN_ERR "%s: DMAing conflict in etherh_block_input: "
384 " DMAstat %d irqlock %d\n", dev->name,
385 ei_local->dmaing, ei_local->irqlock);
389 ei_local->dmaing = 1;
391 addr = etherh_priv(dev)->base;
392 dma_base = etherh_priv(dev)->dma_base;
395 writeb (E8390_NODMA | E8390_PAGE0 | E8390_START, addr + E8390_CMD);
396 writeb (count, addr + EN0_RCNTLO);
397 writeb (count >> 8, addr + EN0_RCNTHI);
398 writeb (ring_offset, addr + EN0_RSARLO);
399 writeb (ring_offset >> 8, addr + EN0_RSARHI);
400 writeb (E8390_RREAD | E8390_START, addr + E8390_CMD);
402 if (ei_local->word16) {
403 readsw (dma_base, buf, count >> 1);
405 buf[count - 1] = readb (dma_base);
407 readsb (dma_base, buf, count);
409 writeb (ENISR_RDC, addr + EN0_ISR);
410 ei_local->dmaing = 0;
414 * Read a header from the 8390
417 etherh_get_header (struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
419 struct ei_device *ei_local = netdev_priv(dev);
420 void __iomem *dma_base, *addr;
422 if (ei_local->dmaing) {
423 printk(KERN_ERR "%s: DMAing conflict in etherh_get_header: "
424 " DMAstat %d irqlock %d\n", dev->name,
425 ei_local->dmaing, ei_local->irqlock);
429 ei_local->dmaing = 1;
431 addr = etherh_priv(dev)->base;
432 dma_base = etherh_priv(dev)->dma_base;
434 writeb (E8390_NODMA | E8390_PAGE0 | E8390_START, addr + E8390_CMD);
435 writeb (sizeof (*hdr), addr + EN0_RCNTLO);
436 writeb (0, addr + EN0_RCNTHI);
437 writeb (0, addr + EN0_RSARLO);
438 writeb (ring_page, addr + EN0_RSARHI);
439 writeb (E8390_RREAD | E8390_START, addr + E8390_CMD);
441 if (ei_local->word16)
442 readsw (dma_base, hdr, sizeof (*hdr) >> 1);
444 readsb (dma_base, hdr, sizeof (*hdr));
446 writeb (ENISR_RDC, addr + EN0_ISR);
447 ei_local->dmaing = 0;
451 * Open/initialize the board. This is called (in the current kernel)
452 * sometime after booting when the 'ifconfig' program is run.
454 * This routine should set everything up anew at each open, even
455 * registers that "should" only need to be set once at boot, so that
456 * there is non-reboot way to recover if something goes wrong.
459 etherh_open(struct net_device *dev)
461 struct ei_device *ei_local = netdev_priv(dev);
463 if (!is_valid_ether_addr(dev->dev_addr)) {
464 printk(KERN_WARNING "%s: invalid ethernet MAC address\n",
469 if (request_irq(dev->irq, ei_interrupt, 0, dev->name, dev))
473 * Make sure that we aren't going to change the
474 * media type on the next reset - we are about to
475 * do automedia manually now.
477 ei_local->interface_num = 0;
480 * If we are doing automedia detection, do it now.
481 * This is more reliable than the 8390's detection.
483 if (dev->flags & IFF_AUTOMEDIA) {
484 dev->if_port = IF_PORT_10BASET;
487 if (!etherh_getifstat(dev)) {
488 dev->if_port = IF_PORT_10BASE2;
501 * The inverse routine to etherh_open().
504 etherh_close(struct net_device *dev)
507 free_irq (dev->irq, dev);
515 static void __init etherh_banner(void)
517 static int version_printed;
519 if (net_debug && version_printed++ == 0)
520 printk(KERN_INFO "%s", version);
524 * Read the ethernet address string from the on board rom.
525 * This is an ascii string...
527 static int __init etherh_addr(char *addr, struct expansion_card *ec)
529 struct in_chunk_dir cd;
532 if (!ecard_readchunk(&cd, ec, 0xf5, 0)) {
533 printk(KERN_ERR "%s: unable to read podule description string\n",
538 s = strchr(cd.d.string, '(');
542 for (i = 0; i < 6; i++) {
543 addr[i] = simple_strtoul(s + 1, &s, 0x10);
544 if (*s != (i == 5? ')' : ':'))
552 printk(KERN_ERR "%s: unable to parse MAC address: %s\n",
553 ec->dev.bus_id, cd.d.string);
560 * Create an ethernet address from the system serial number.
562 static int __init etherm_addr(char *addr)
566 if (system_serial_low == 0 && system_serial_high == 0)
569 serial = system_serial_low | system_serial_high;
574 addr[3] = 0x10 + (serial >> 24);
575 addr[4] = serial >> 16;
576 addr[5] = serial >> 8;
580 static void etherh_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
582 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
583 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
584 strlcpy(info->bus_info, dev->class_dev.dev->bus_id,
585 sizeof(info->bus_info));
588 static int etherh_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
590 cmd->supported = etherh_priv(dev)->supported;
591 cmd->speed = SPEED_10;
592 cmd->duplex = DUPLEX_HALF;
593 cmd->port = dev->if_port == IF_PORT_10BASET ? PORT_TP : PORT_BNC;
594 cmd->autoneg = dev->flags & IFF_AUTOMEDIA ? AUTONEG_ENABLE : AUTONEG_DISABLE;
598 static int etherh_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
600 switch (cmd->autoneg) {
602 dev->flags |= IFF_AUTOMEDIA;
605 case AUTONEG_DISABLE:
608 dev->if_port = IF_PORT_10BASET;
612 dev->if_port = IF_PORT_10BASE2;
618 dev->flags &= ~IFF_AUTOMEDIA;
630 static struct ethtool_ops etherh_ethtool_ops = {
631 .get_settings = etherh_get_settings,
632 .set_settings = etherh_set_settings,
633 .get_drvinfo = etherh_get_drvinfo,
636 static u32 etherh_regoffsets[16];
637 static u32 etherm_regoffsets[16];
640 etherh_probe(struct expansion_card *ec, const struct ecard_id *id)
642 const struct etherh_data *data = id->data;
643 struct ei_device *ei_local;
644 struct net_device *dev;
645 struct etherh_priv *eh;
650 ret = ecard_request_resources(ec);
654 dev = __alloc_ei_netdev(sizeof(struct etherh_priv));
660 SET_MODULE_OWNER(dev);
661 SET_NETDEV_DEV(dev, &ec->dev);
663 dev->open = etherh_open;
664 dev->stop = etherh_close;
665 dev->set_config = etherh_set_config;
667 dev->ethtool_ops = ðerh_ethtool_ops;
669 if (data->supported & SUPPORTED_Autoneg)
670 dev->flags |= IFF_AUTOMEDIA;
671 if (data->supported & SUPPORTED_TP) {
672 dev->flags |= IFF_PORTSEL;
673 dev->if_port = IF_PORT_10BASET;
674 } else if (data->supported & SUPPORTED_BNC) {
675 dev->flags |= IFF_PORTSEL;
676 dev->if_port = IF_PORT_10BASE2;
678 dev->if_port = IF_PORT_UNKNOWN;
680 eh = etherh_priv(dev);
681 eh->supported = data->supported;
683 eh->id = ec->cid.product;
684 eh->memc = ioremap(ecard_resource_start(ec, ECARD_RES_MEMC), PAGE_SIZE);
690 eh->ctrl_port = eh->memc;
691 if (data->ctrl_ioc) {
692 eh->ioc_fast = ioremap(ecard_resource_start(ec, ECARD_RES_IOCFAST), PAGE_SIZE);
697 eh->ctrl_port = eh->ioc_fast;
700 eh->base = eh->memc + data->ns8390_offset;
701 dev->base_addr = (unsigned long)eh->base;
702 eh->dma_base = eh->memc + data->dataport_offset;
703 eh->ctrl_port += data->ctrlport_offset;
706 * IRQ and control port handling - only for non-NIC slot cards.
708 if (ec->slot_no != 8) {
709 ec->ops = ðerh_ops;
713 * If we're in the NIC slot, make sure the IRQ is enabled
715 etherh_set_ctrl(eh, ETHERH_CP_IE);
718 ei_local = netdev_priv(dev);
719 spin_lock_init(&ei_local->page_lock);
721 if (ec->cid.product == PROD_ANT_ETHERM) {
722 etherm_addr(dev->dev_addr);
723 ei_local->reg_offset = etherm_regoffsets;
725 etherh_addr(dev->dev_addr, ec);
726 ei_local->reg_offset = etherh_regoffsets;
729 ei_local->name = dev->name;
730 ei_local->word16 = 1;
731 ei_local->tx_start_page = data->tx_start_page;
732 ei_local->rx_start_page = ei_local->tx_start_page + TX_PAGES;
733 ei_local->stop_page = data->stop_page;
734 ei_local->reset_8390 = etherh_reset;
735 ei_local->block_input = etherh_block_input;
736 ei_local->block_output = etherh_block_output;
737 ei_local->get_8390_hdr = etherh_get_header;
738 ei_local->interface_num = 0;
743 ret = register_netdev(dev);
747 printk(KERN_INFO "%s: %s in slot %d, ",
748 dev->name, data->name, ec->slot_no);
750 for (i = 0; i < 6; i++)
751 printk("%2.2x%c", dev->dev_addr[i], i == 5 ? '\n' : ':');
753 ecard_set_drvdata(ec, dev);
759 iounmap(eh->ioc_fast);
764 ecard_release_resources(ec);
769 static void __devexit etherh_remove(struct expansion_card *ec)
771 struct net_device *dev = ecard_get_drvdata(ec);
772 struct etherh_priv *eh = etherh_priv(dev);
774 ecard_set_drvdata(ec, NULL);
776 unregister_netdev(dev);
780 iounmap(eh->ioc_fast);
785 ecard_release_resources(ec);
788 static struct etherh_data etherm_data = {
789 .ns8390_offset = ETHERM_NS8390,
790 .dataport_offset = ETHERM_NS8390 + ETHERM_DATAPORT,
791 .ctrlport_offset = ETHERM_NS8390 + ETHERM_CTRLPORT,
792 .name = "ANT EtherM",
793 .supported = SUPPORTED_10baseT_Half,
794 .tx_start_page = ETHERM_TX_START_PAGE,
795 .stop_page = ETHERM_STOP_PAGE,
798 static struct etherh_data etherlan500_data = {
799 .ns8390_offset = ETHERH500_NS8390,
800 .dataport_offset = ETHERH500_NS8390 + ETHERH500_DATAPORT,
801 .ctrlport_offset = ETHERH500_CTRLPORT,
803 .name = "i3 EtherH 500",
804 .supported = SUPPORTED_10baseT_Half,
805 .tx_start_page = ETHERH_TX_START_PAGE,
806 .stop_page = ETHERH_STOP_PAGE,
809 static struct etherh_data etherlan600_data = {
810 .ns8390_offset = ETHERH600_NS8390,
811 .dataport_offset = ETHERH600_NS8390 + ETHERH600_DATAPORT,
812 .ctrlport_offset = ETHERH600_NS8390 + ETHERH600_CTRLPORT,
813 .name = "i3 EtherH 600",
814 .supported = SUPPORTED_10baseT_Half | SUPPORTED_TP | SUPPORTED_BNC | SUPPORTED_Autoneg,
815 .tx_start_page = ETHERH_TX_START_PAGE,
816 .stop_page = ETHERH_STOP_PAGE,
819 static struct etherh_data etherlan600a_data = {
820 .ns8390_offset = ETHERH600_NS8390,
821 .dataport_offset = ETHERH600_NS8390 + ETHERH600_DATAPORT,
822 .ctrlport_offset = ETHERH600_NS8390 + ETHERH600_CTRLPORT,
823 .name = "i3 EtherH 600A",
824 .supported = SUPPORTED_10baseT_Half | SUPPORTED_TP | SUPPORTED_BNC | SUPPORTED_Autoneg,
825 .tx_start_page = ETHERH_TX_START_PAGE,
826 .stop_page = ETHERH_STOP_PAGE,
829 static const struct ecard_id etherh_ids[] = {
830 { MANU_ANT, PROD_ANT_ETHERM, ðerm_data },
831 { MANU_I3, PROD_I3_ETHERLAN500, ðerlan500_data },
832 { MANU_I3, PROD_I3_ETHERLAN600, ðerlan600_data },
833 { MANU_I3, PROD_I3_ETHERLAN600A, ðerlan600a_data },
837 static struct ecard_driver etherh_driver = {
838 .probe = etherh_probe,
839 .remove = __devexit_p(etherh_remove),
840 .id_table = etherh_ids,
846 static int __init etherh_init(void)
850 for (i = 0; i < 16; i++) {
851 etherh_regoffsets[i] = i << 2;
852 etherm_regoffsets[i] = i << 5;
855 return ecard_register_driver(ðerh_driver);
858 static void __exit etherh_exit(void)
860 ecard_remove_driver(ðerh_driver);
863 module_init(etherh_init);
864 module_exit(etherh_exit);