MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / net / arm / etherh.c
blobd861c54a7766aff4ec1f0f5c6863b8b58b695ceb
1 /*
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.
16 * Changelog:
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>
37 #include <linux/in.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/device.h>
46 #include <linux/init.h>
48 #include <asm/system.h>
49 #include <asm/bitops.h>
50 #include <asm/ecard.h>
51 #include <asm/io.h>
52 #include <asm/irq.h>
54 #include "../8390.h"
56 #define NET_DEBUG 0
57 #define DEBUG_INIT 2
59 static unsigned int net_debug = NET_DEBUG;
61 struct etherh_priv {
62 void *ioc_fast;
63 void *memc;
64 unsigned int id;
65 void *ctrl_port;
66 unsigned char ctrl;
69 struct etherh_data {
70 unsigned long ns8390_offset;
71 unsigned long dataport_offset;
72 unsigned long ctrlport_offset;
73 int ctrl_ioc;
74 const char name[16];
76 * netdev flags and port
78 unsigned short flags;
79 unsigned char if_port;
80 unsigned char tx_start_page;
81 unsigned char stop_page;
84 MODULE_AUTHOR("Russell King");
85 MODULE_DESCRIPTION("EtherH/EtherM driver");
86 MODULE_LICENSE("GPL");
88 static char version[] __initdata =
89 "EtherH/EtherM Driver (c) 2002 Russell King v1.09\n";
91 #define ETHERH500_DATAPORT 0x800 /* MEMC */
92 #define ETHERH500_NS8390 0x000 /* MEMC */
93 #define ETHERH500_CTRLPORT 0x800 /* IOC */
95 #define ETHERH600_DATAPORT 0x040 /* MEMC */
96 #define ETHERH600_NS8390 0x800 /* MEMC */
97 #define ETHERH600_CTRLPORT 0x200 /* MEMC */
99 #define ETHERH_CP_IE 1
100 #define ETHERH_CP_IF 2
101 #define ETHERH_CP_HEARTBEAT 2
103 #define ETHERH_TX_START_PAGE 1
104 #define ETHERH_STOP_PAGE 127
107 * These came from CK/TEW
109 #define ETHERM_DATAPORT 0x200 /* MEMC */
110 #define ETHERM_NS8390 0x800 /* MEMC */
111 #define ETHERM_CTRLPORT 0x23c /* MEMC */
113 #define ETHERM_TX_START_PAGE 64
114 #define ETHERM_STOP_PAGE 127
116 /* ------------------------------------------------------------------------ */
118 #define etherh_priv(dev) \
119 ((struct etherh_priv *)(((char *)netdev_priv(dev)) + sizeof(struct ei_device)))
121 static inline void etherh_set_ctrl(struct etherh_priv *eh, unsigned char mask)
123 unsigned char ctrl = eh->ctrl | mask;
124 eh->ctrl = ctrl;
125 writeb(ctrl, eh->ctrl_port);
128 static inline void etherh_clr_ctrl(struct etherh_priv *eh, unsigned char mask)
130 unsigned char ctrl = eh->ctrl & ~mask;
131 eh->ctrl = ctrl;
132 writeb(ctrl, eh->ctrl_port);
135 static inline unsigned int etherh_get_stat(struct etherh_priv *eh)
137 return readb(eh->ctrl_port);
143 static void etherh_irq_enable(ecard_t *ec, int irqnr)
145 struct etherh_priv *eh = ec->irq_data;
147 etherh_set_ctrl(eh, ETHERH_CP_IE);
150 static void etherh_irq_disable(ecard_t *ec, int irqnr)
152 struct etherh_priv *eh = ec->irq_data;
154 etherh_clr_ctrl(eh, ETHERH_CP_IE);
157 static expansioncard_ops_t etherh_ops = {
158 .irqenable = etherh_irq_enable,
159 .irqdisable = etherh_irq_disable,
165 static void
166 etherh_setif(struct net_device *dev)
168 struct ei_device *ei_local = netdev_priv(dev);
169 unsigned long addr, flags;
171 local_irq_save(flags);
173 /* set the interface type */
174 switch (etherh_priv(dev)->id) {
175 case PROD_I3_ETHERLAN600:
176 case PROD_I3_ETHERLAN600A:
177 addr = dev->base_addr + EN0_RCNTHI;
179 switch (dev->if_port) {
180 case IF_PORT_10BASE2:
181 writeb((readb(addr) & 0xf8) | 1, addr);
182 break;
183 case IF_PORT_10BASET:
184 writeb((readb(addr) & 0xf8), addr);
185 break;
187 break;
189 case PROD_I3_ETHERLAN500:
190 switch (dev->if_port) {
191 case IF_PORT_10BASE2:
192 etherh_clr_ctrl(etherh_priv(dev), ETHERH_CP_IF);
193 break;
195 case IF_PORT_10BASET:
196 etherh_set_ctrl(etherh_priv(dev), ETHERH_CP_IF);
197 break;
199 break;
201 default:
202 break;
205 local_irq_restore(flags);
208 static int
209 etherh_getifstat(struct net_device *dev)
211 struct ei_device *ei_local = netdev_priv(dev);
212 int stat = 0;
214 switch (etherh_priv(dev)->id) {
215 case PROD_I3_ETHERLAN600:
216 case PROD_I3_ETHERLAN600A:
217 switch (dev->if_port) {
218 case IF_PORT_10BASE2:
219 stat = 1;
220 break;
221 case IF_PORT_10BASET:
222 stat = readb(dev->base_addr+EN0_RCNTHI) & 4;
223 break;
225 break;
227 case PROD_I3_ETHERLAN500:
228 switch (dev->if_port) {
229 case IF_PORT_10BASE2:
230 stat = 1;
231 break;
232 case IF_PORT_10BASET:
233 stat = etherh_get_stat(etherh_priv(dev)) & ETHERH_CP_HEARTBEAT;
234 break;
236 break;
238 default:
239 stat = 0;
240 break;
243 return stat != 0;
247 * Configure the interface. Note that we ignore the other
248 * parts of ifmap, since its mostly meaningless for this driver.
250 static int etherh_set_config(struct net_device *dev, struct ifmap *map)
252 switch (map->port) {
253 case IF_PORT_10BASE2:
254 case IF_PORT_10BASET:
256 * If the user explicitly sets the interface
257 * media type, turn off automedia detection.
259 dev->flags &= ~IFF_AUTOMEDIA;
260 dev->if_port = map->port;
261 break;
263 default:
264 return -EINVAL;
267 etherh_setif(dev);
269 return 0;
273 * Reset the 8390 (hard reset). Note that we can't actually do this.
275 static void
276 etherh_reset(struct net_device *dev)
278 struct ei_device *ei_local = netdev_priv(dev);
280 writeb(E8390_NODMA+E8390_PAGE0+E8390_STOP, dev->base_addr);
283 * See if we need to change the interface type.
284 * Note that we use 'interface_num' as a flag
285 * to indicate that we need to change the media.
287 if (dev->flags & IFF_AUTOMEDIA && ei_local->interface_num) {
288 ei_local->interface_num = 0;
290 if (dev->if_port == IF_PORT_10BASET)
291 dev->if_port = IF_PORT_10BASE2;
292 else
293 dev->if_port = IF_PORT_10BASET;
295 etherh_setif(dev);
300 * Write a block of data out to the 8390
302 static void
303 etherh_block_output (struct net_device *dev, int count, const unsigned char *buf, int start_page)
305 struct ei_device *ei_local = netdev_priv(dev);
306 unsigned int addr, dma_addr;
307 unsigned long dma_start;
309 if (ei_local->dmaing) {
310 printk(KERN_ERR "%s: DMAing conflict in etherh_block_input: "
311 " DMAstat %d irqlock %d\n", dev->name,
312 ei_local->dmaing, ei_local->irqlock);
313 return;
317 * Make sure we have a round number of bytes if we're in word mode.
319 if (count & 1 && ei_local->word16)
320 count++;
322 ei_local->dmaing = 1;
324 addr = dev->base_addr;
325 dma_addr = dev->mem_start;
327 count = (count + 1) & ~1;
328 writeb (E8390_NODMA | E8390_PAGE0 | E8390_START, addr + E8390_CMD);
330 writeb (0x42, addr + EN0_RCNTLO);
331 writeb (0x00, addr + EN0_RCNTHI);
332 writeb (0x42, addr + EN0_RSARLO);
333 writeb (0x00, addr + EN0_RSARHI);
334 writeb (E8390_RREAD | E8390_START, addr + E8390_CMD);
336 udelay (1);
338 writeb (ENISR_RDC, addr + EN0_ISR);
339 writeb (count, addr + EN0_RCNTLO);
340 writeb (count >> 8, addr + EN0_RCNTHI);
341 writeb (0, addr + EN0_RSARLO);
342 writeb (start_page, addr + EN0_RSARHI);
343 writeb (E8390_RWRITE | E8390_START, addr + E8390_CMD);
345 if (ei_local->word16)
346 writesw (dma_addr, buf, count >> 1);
347 else
348 writesb (dma_addr, buf, count);
350 dma_start = jiffies;
352 while ((readb (addr + EN0_ISR) & ENISR_RDC) == 0)
353 if (jiffies - dma_start > 2*HZ/100) { /* 20ms */
354 printk(KERN_ERR "%s: timeout waiting for TX RDC\n",
355 dev->name);
356 etherh_reset (dev);
357 NS8390_init (dev, 1);
358 break;
361 writeb (ENISR_RDC, addr + EN0_ISR);
362 ei_local->dmaing = 0;
366 * Read a block of data from the 8390
368 static void
369 etherh_block_input (struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
371 struct ei_device *ei_local = netdev_priv(dev);
372 unsigned int addr, dma_addr;
373 unsigned char *buf;
375 if (ei_local->dmaing) {
376 printk(KERN_ERR "%s: DMAing conflict in etherh_block_input: "
377 " DMAstat %d irqlock %d\n", dev->name,
378 ei_local->dmaing, ei_local->irqlock);
379 return;
382 ei_local->dmaing = 1;
384 addr = dev->base_addr;
385 dma_addr = dev->mem_start;
387 buf = skb->data;
388 writeb (E8390_NODMA | E8390_PAGE0 | E8390_START, addr + E8390_CMD);
389 writeb (count, addr + EN0_RCNTLO);
390 writeb (count >> 8, addr + EN0_RCNTHI);
391 writeb (ring_offset, addr + EN0_RSARLO);
392 writeb (ring_offset >> 8, addr + EN0_RSARHI);
393 writeb (E8390_RREAD | E8390_START, addr + E8390_CMD);
395 if (ei_local->word16) {
396 readsw (dma_addr, buf, count >> 1);
397 if (count & 1)
398 buf[count - 1] = readb (dma_addr);
399 } else
400 readsb (dma_addr, buf, count);
402 writeb (ENISR_RDC, addr + EN0_ISR);
403 ei_local->dmaing = 0;
407 * Read a header from the 8390
409 static void
410 etherh_get_header (struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
412 struct ei_device *ei_local = netdev_priv(dev);
413 unsigned int addr, dma_addr;
415 if (ei_local->dmaing) {
416 printk(KERN_ERR "%s: DMAing conflict in etherh_get_header: "
417 " DMAstat %d irqlock %d\n", dev->name,
418 ei_local->dmaing, ei_local->irqlock);
419 return;
422 ei_local->dmaing = 1;
424 addr = dev->base_addr;
425 dma_addr = dev->mem_start;
427 writeb (E8390_NODMA | E8390_PAGE0 | E8390_START, addr + E8390_CMD);
428 writeb (sizeof (*hdr), addr + EN0_RCNTLO);
429 writeb (0, addr + EN0_RCNTHI);
430 writeb (0, addr + EN0_RSARLO);
431 writeb (ring_page, addr + EN0_RSARHI);
432 writeb (E8390_RREAD | E8390_START, addr + E8390_CMD);
434 if (ei_local->word16)
435 readsw (dma_addr, hdr, sizeof (*hdr) >> 1);
436 else
437 readsb (dma_addr, hdr, sizeof (*hdr));
439 writeb (ENISR_RDC, addr + EN0_ISR);
440 ei_local->dmaing = 0;
444 * Open/initialize the board. This is called (in the current kernel)
445 * sometime after booting when the 'ifconfig' program is run.
447 * This routine should set everything up anew at each open, even
448 * registers that "should" only need to be set once at boot, so that
449 * there is non-reboot way to recover if something goes wrong.
451 static int
452 etherh_open(struct net_device *dev)
454 struct ei_device *ei_local = netdev_priv(dev);
456 if (!is_valid_ether_addr(dev->dev_addr)) {
457 printk(KERN_WARNING "%s: invalid ethernet MAC address\n",
458 dev->name);
459 return -EINVAL;
462 if (request_irq(dev->irq, ei_interrupt, 0, dev->name, dev))
463 return -EAGAIN;
466 * Make sure that we aren't going to change the
467 * media type on the next reset - we are about to
468 * do automedia manually now.
470 ei_local->interface_num = 0;
473 * If we are doing automedia detection, do it now.
474 * This is more reliable than the 8390's detection.
476 if (dev->flags & IFF_AUTOMEDIA) {
477 dev->if_port = IF_PORT_10BASET;
478 etherh_setif(dev);
479 mdelay(1);
480 if (!etherh_getifstat(dev)) {
481 dev->if_port = IF_PORT_10BASE2;
482 etherh_setif(dev);
484 } else
485 etherh_setif(dev);
487 etherh_reset(dev);
488 ei_open(dev);
490 return 0;
494 * The inverse routine to etherh_open().
496 static int
497 etherh_close(struct net_device *dev)
499 ei_close (dev);
500 free_irq (dev->irq, dev);
501 return 0;
505 * Initialisation
508 static void __init etherh_banner(void)
510 static int version_printed;
512 if (net_debug && version_printed++ == 0)
513 printk(KERN_INFO "%s", version);
517 * Read the ethernet address string from the on board rom.
518 * This is an ascii string...
520 static int __init etherh_addr(char *addr, struct expansion_card *ec)
522 struct in_chunk_dir cd;
523 char *s;
525 if (ecard_readchunk(&cd, ec, 0xf5, 0) && (s = strchr(cd.d.string, '('))) {
526 int i;
527 for (i = 0; i < 6; i++) {
528 addr[i] = simple_strtoul(s + 1, &s, 0x10);
529 if (*s != (i == 5? ')' : ':'))
530 break;
532 if (i == 6)
533 return 0;
535 return -ENODEV;
539 * Create an ethernet address from the system serial number.
541 static int __init etherm_addr(char *addr)
543 unsigned int serial;
545 if (system_serial_low == 0 && system_serial_high == 0)
546 return -ENODEV;
548 serial = system_serial_low | system_serial_high;
550 addr[0] = 0;
551 addr[1] = 0;
552 addr[2] = 0xa4;
553 addr[3] = 0x10 + (serial >> 24);
554 addr[4] = serial >> 16;
555 addr[5] = serial >> 8;
556 return 0;
559 static u32 etherh_regoffsets[16];
560 static u32 etherm_regoffsets[16];
562 static int __init
563 etherh_probe(struct expansion_card *ec, const struct ecard_id *id)
565 const struct etherh_data *data = id->data;
566 struct ei_device *ei_local;
567 struct net_device *dev;
568 struct etherh_priv *eh;
569 int i, ret;
571 etherh_banner();
573 ret = ecard_request_resources(ec);
574 if (ret)
575 goto out;
577 dev = __alloc_ei_netdev(sizeof(struct etherh_priv));
578 if (!dev) {
579 ret = -ENOMEM;
580 goto release;
583 SET_MODULE_OWNER(dev);
584 SET_NETDEV_DEV(dev, &ec->dev);
586 dev->open = etherh_open;
587 dev->stop = etherh_close;
588 dev->set_config = etherh_set_config;
589 dev->irq = ec->irq;
590 dev->if_port = data->if_port;
591 dev->flags |= data->flags;
593 eh = etherh_priv(dev);
594 eh->ctrl = 0;
595 eh->id = ec->cid.product;
596 eh->memc = ioremap(ecard_resource_start(ec, ECARD_RES_MEMC), PAGE_SIZE);
597 if (!eh->memc) {
598 ret = -ENOMEM;
599 goto free;
602 eh->ctrl_port = eh->memc;
603 if (data->ctrl_ioc) {
604 eh->ioc_fast = ioremap(ecard_resource_start(ec, ECARD_RES_IOCFAST), PAGE_SIZE);
605 if (!eh->ioc_fast) {
606 ret = -ENOMEM;
607 goto free;
609 eh->ctrl_port = eh->ioc_fast;
612 dev->base_addr = (unsigned long)eh->memc + data->ns8390_offset;
613 dev->mem_start = (unsigned long)eh->memc + data->dataport_offset;
614 eh->ctrl_port += data->ctrlport_offset;
617 * IRQ and control port handling - only for non-NIC slot cards.
619 if (ec->slot_no != 8) {
620 ec->ops = &etherh_ops;
621 ec->irq_data = eh;
622 } else {
624 * If we're in the NIC slot, make sure the IRQ is enabled
626 etherh_set_ctrl(eh, ETHERH_CP_IE);
629 ei_local = netdev_priv(dev);
630 spin_lock_init(&ei_local->page_lock);
632 if (ec->cid.product == PROD_ANT_ETHERM) {
633 etherm_addr(dev->dev_addr);
634 ei_local->reg_offset = etherm_regoffsets;
635 } else {
636 etherh_addr(dev->dev_addr, ec);
637 ei_local->reg_offset = etherh_regoffsets;
640 ei_local->name = dev->name;
641 ei_local->word16 = 1;
642 ei_local->tx_start_page = data->tx_start_page;
643 ei_local->rx_start_page = ei_local->tx_start_page + TX_PAGES;
644 ei_local->stop_page = data->stop_page;
645 ei_local->reset_8390 = etherh_reset;
646 ei_local->block_input = etherh_block_input;
647 ei_local->block_output = etherh_block_output;
648 ei_local->get_8390_hdr = etherh_get_header;
649 ei_local->interface_num = 0;
651 etherh_reset(dev);
652 NS8390_init(dev, 0);
654 ret = register_netdev(dev);
655 if (ret)
656 goto free;
658 printk(KERN_INFO "%s: %s in slot %d, ",
659 dev->name, data->name, ec->slot_no);
661 for (i = 0; i < 6; i++)
662 printk("%2.2x%c", dev->dev_addr[i], i == 5 ? '\n' : ':');
664 ecard_set_drvdata(ec, dev);
666 return 0;
668 free:
669 if (eh->ioc_fast)
670 iounmap(eh->ioc_fast);
671 if (eh->memc)
672 iounmap(eh->memc);
673 free_netdev(dev);
674 release:
675 ecard_release_resources(ec);
676 out:
677 return ret;
680 static void __devexit etherh_remove(struct expansion_card *ec)
682 struct net_device *dev = ecard_get_drvdata(ec);
683 struct etherh_priv *eh = etherh_priv(dev);
685 ecard_set_drvdata(ec, NULL);
687 unregister_netdev(dev);
688 ec->ops = NULL;
690 if (eh->ioc_fast)
691 iounmap(eh->ioc_fast);
692 iounmap(eh->memc);
694 free_netdev(dev);
696 ecard_release_resources(ec);
699 static struct etherh_data etherm_data = {
700 .ns8390_offset = ETHERM_NS8390,
701 .dataport_offset = ETHERM_NS8390 + ETHERM_DATAPORT,
702 .ctrlport_offset = ETHERM_NS8390 + ETHERM_CTRLPORT,
703 .name = "ANT EtherM",
704 .if_port = IF_PORT_UNKNOWN,
705 .tx_start_page = ETHERM_TX_START_PAGE,
706 .stop_page = ETHERM_STOP_PAGE,
709 static struct etherh_data etherlan500_data = {
710 .ns8390_offset = ETHERH500_NS8390,
711 .dataport_offset = ETHERH500_NS8390 + ETHERH500_DATAPORT,
712 .ctrlport_offset = ETHERH500_CTRLPORT,
713 .ctrl_ioc = 1,
714 .name = "i3 EtherH 500",
715 .if_port = IF_PORT_UNKNOWN,
716 .tx_start_page = ETHERH_TX_START_PAGE,
717 .stop_page = ETHERH_STOP_PAGE,
720 static struct etherh_data etherlan600_data = {
721 .ns8390_offset = ETHERH600_NS8390,
722 .dataport_offset = ETHERH600_NS8390 + ETHERH600_DATAPORT,
723 .ctrlport_offset = ETHERH600_NS8390 + ETHERH600_CTRLPORT,
724 .name = "i3 EtherH 600",
725 .flags = IFF_PORTSEL | IFF_AUTOMEDIA,
726 .if_port = IF_PORT_10BASET,
727 .tx_start_page = ETHERH_TX_START_PAGE,
728 .stop_page = ETHERH_STOP_PAGE,
731 static struct etherh_data etherlan600a_data = {
732 .ns8390_offset = ETHERH600_NS8390,
733 .dataport_offset = ETHERH600_NS8390 + ETHERH600_DATAPORT,
734 .ctrlport_offset = ETHERH600_NS8390 + ETHERH600_CTRLPORT,
735 .name = "i3 EtherH 600A",
736 .flags = IFF_PORTSEL | IFF_AUTOMEDIA,
737 .if_port = IF_PORT_10BASET,
738 .tx_start_page = ETHERH_TX_START_PAGE,
739 .stop_page = ETHERH_STOP_PAGE,
742 static const struct ecard_id etherh_ids[] = {
743 { MANU_ANT, PROD_ANT_ETHERM, &etherm_data },
744 { MANU_I3, PROD_I3_ETHERLAN500, &etherlan500_data },
745 { MANU_I3, PROD_I3_ETHERLAN600, &etherlan600_data },
746 { MANU_I3, PROD_I3_ETHERLAN600A, &etherlan600a_data },
747 { 0xffff, 0xffff }
750 static struct ecard_driver etherh_driver = {
751 .probe = etherh_probe,
752 .remove = __devexit_p(etherh_remove),
753 .id_table = etherh_ids,
754 .drv = {
755 .name = "etherh",
759 static int __init etherh_init(void)
761 int i;
763 for (i = 0; i < 16; i++) {
764 etherh_regoffsets[i] = i << 2;
765 etherm_regoffsets[i] = i << 5;
768 return ecard_register_driver(&etherh_driver);
771 static void __exit etherh_exit(void)
773 ecard_remove_driver(&etherh_driver);
776 module_init(etherh_init);
777 module_exit(etherh_exit);