MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / net / apne.c
bloba94216b871849852339b4271a7fd667cfc0d156e
1 /*
2 * Amiga Linux/68k 8390 based PCMCIA Ethernet Driver for the Amiga 1200
4 * (C) Copyright 1997 Alain Malek
5 * (Alain.Malek@cryogen.com)
7 * ----------------------------------------------------------------------------
9 * This program is based on
11 * ne.c: A general non-shared-memory NS8390 ethernet driver for linux
12 * Written 1992-94 by Donald Becker.
14 * 8390.c: A general NS8390 ethernet driver core for linux.
15 * Written 1992-94 by Donald Becker.
17 * cnetdevice: A Sana-II ethernet driver for AmigaOS
18 * Written by Bruce Abbott (bhabbott@inhb.co.nz)
20 * ----------------------------------------------------------------------------
22 * This file is subject to the terms and conditions of the GNU General Public
23 * License. See the file COPYING in the main directory of the Linux
24 * distribution for more details.
26 * ----------------------------------------------------------------------------
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/errno.h>
34 #include <linux/pci.h>
35 #include <linux/init.h>
36 #include <linux/delay.h>
37 #include <linux/netdevice.h>
38 #include <linux/etherdevice.h>
40 #include <asm/system.h>
41 #include <asm/io.h>
42 #include <asm/setup.h>
43 #include <asm/amigaints.h>
44 #include <asm/amigahw.h>
45 #include <asm/amigayle.h>
46 #include <asm/amipcmcia.h>
48 #include "8390.h"
50 /* ---- No user-serviceable parts below ---- */
52 #define DRV_NAME "apne"
54 #define NE_BASE (dev->base_addr)
55 #define NE_CMD 0x00
56 #define NE_DATAPORT 0x10 /* NatSemi-defined port window offset. */
57 #define NE_RESET 0x1f /* Issue a read to reset, a write to clear. */
58 #define NE_IO_EXTENT 0x20
60 #define NE_EN0_ISR 0x07
61 #define NE_EN0_DCFG 0x0e
63 #define NE_EN0_RSARLO 0x08
64 #define NE_EN0_RSARHI 0x09
65 #define NE_EN0_RCNTLO 0x0a
66 #define NE_EN0_RXCR 0x0c
67 #define NE_EN0_TXCR 0x0d
68 #define NE_EN0_RCNTHI 0x0b
69 #define NE_EN0_IMR 0x0f
71 #define NE1SM_START_PG 0x20 /* First page of TX buffer */
72 #define NE1SM_STOP_PG 0x40 /* Last page +1 of RX ring */
73 #define NESM_START_PG 0x40 /* First page of TX buffer */
74 #define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
77 struct net_device * __init apne_probe(int unit);
78 static int apne_probe1(struct net_device *dev, int ioaddr);
80 static int apne_open(struct net_device *dev);
81 static int apne_close(struct net_device *dev);
83 static void apne_reset_8390(struct net_device *dev);
84 static void apne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
85 int ring_page);
86 static void apne_block_input(struct net_device *dev, int count,
87 struct sk_buff *skb, int ring_offset);
88 static void apne_block_output(struct net_device *dev, const int count,
89 const unsigned char *buf, const int start_page);
90 static irqreturn_t apne_interrupt(int irq, void *dev_id, struct pt_regs *regs);
92 static int init_pcmcia(void);
94 /* IO base address used for nic */
96 #define IOBASE 0x300
99 use MANUAL_CONFIG and MANUAL_OFFSET for enabling IO by hand
100 you can find the values to use by looking at the cnet.device
101 config file example (the default values are for the CNET40BC card)
105 #define MANUAL_CONFIG 0x20
106 #define MANUAL_OFFSET 0x3f8
108 #define MANUAL_HWADDR0 0x00
109 #define MANUAL_HWADDR1 0x12
110 #define MANUAL_HWADDR2 0x34
111 #define MANUAL_HWADDR3 0x56
112 #define MANUAL_HWADDR4 0x78
113 #define MANUAL_HWADDR5 0x9a
116 static const char version[] =
117 "apne.c:v1.1 7/10/98 Alain Malek (Alain.Malek@cryogen.ch)\n";
119 static int apne_owned; /* signal if card already owned */
121 struct net_device * __init apne_probe(int unit)
123 struct net_device *dev;
124 #ifndef MANUAL_CONFIG
125 char tuple[8];
126 #endif
127 int err;
129 if (apne_owned)
130 return ERR_PTR(-ENODEV);
132 if ( !(AMIGAHW_PRESENT(PCMCIA)) )
133 return ERR_PTR(-ENODEV);
135 printk("Looking for PCMCIA ethernet card : ");
137 /* check if a card is inserted */
138 if (!(PCMCIA_INSERTED)) {
139 printk("NO PCMCIA card inserted\n");
140 return ERR_PTR(-ENODEV);
143 dev = alloc_ei_netdev();
144 if (!dev)
145 return ERR_PTR(-ENOMEM);
146 if (unit >= 0) {
147 sprintf(dev->name, "eth%d", unit);
148 netdev_boot_setup_check(dev);
150 SET_MODULE_OWNER(dev);
152 /* disable pcmcia irq for readtuple */
153 pcmcia_disable_irq();
155 #ifndef MANUAL_CONFIG
156 if ((pcmcia_copy_tuple(CISTPL_FUNCID, tuple, 8) < 3) ||
157 (tuple[2] != CISTPL_FUNCID_NETWORK)) {
158 printk("not an ethernet card\n");
159 /* XXX: shouldn't we re-enable irq here? */
160 free_netdev(dev);
161 return ERR_PTR(-ENODEV);
163 #endif
165 printk("ethernet PCMCIA card inserted\n");
167 if (!init_pcmcia()) {
168 /* XXX: shouldn't we re-enable irq here? */
169 free_netdev(dev);
170 return ERR_PTR(-ENODEV);
173 if (!request_region(IOBASE, 0x20, DRV_NAME)) {
174 free_netdev(dev);
175 return ERR_PTR(-EBUSY);
178 err = apne_probe1(dev, IOBASE);
179 if (err) {
180 release_region(IOBASE, 0x20);
181 free_netdev(dev);
182 return ERR_PTR(err);
184 err = register_netdev(dev);
185 if (!err)
186 return dev;
188 pcmcia_disable_irq();
189 free_irq(IRQ_AMIGA_PORTS, dev);
190 pcmcia_reset();
191 release_region(IOBASE, 0x20);
192 free_netdev(dev);
193 return ERR_PTR(err);
196 static int __init apne_probe1(struct net_device *dev, int ioaddr)
198 int i;
199 unsigned char SA_prom[32];
200 int wordlength = 2;
201 const char *name = NULL;
202 int start_page, stop_page;
203 #ifndef MANUAL_HWADDR0
204 int neX000, ctron;
205 #endif
206 static unsigned version_printed;
208 if (ei_debug && version_printed++ == 0)
209 printk(version);
211 printk("PCMCIA NE*000 ethercard probe");
213 /* Reset card. Who knows what dain-bramaged state it was left in. */
214 { unsigned long reset_start_time = jiffies;
216 outb(inb(ioaddr + NE_RESET), ioaddr + NE_RESET);
218 while ((inb(ioaddr + NE_EN0_ISR) & ENISR_RESET) == 0)
219 if (jiffies - reset_start_time > 2*HZ/100) {
220 printk(" not found (no reset ack).\n");
221 return -ENODEV;
224 outb(0xff, ioaddr + NE_EN0_ISR); /* Ack all intr. */
227 #ifndef MANUAL_HWADDR0
229 /* Read the 16 bytes of station address PROM.
230 We must first initialize registers, similar to NS8390_init(eifdev, 0).
231 We can't reliably read the SAPROM address without this.
232 (I learned the hard way!). */
234 struct {unsigned long value, offset; } program_seq[] = {
235 {E8390_NODMA+E8390_PAGE0+E8390_STOP, NE_CMD}, /* Select page 0*/
236 {0x48, NE_EN0_DCFG}, /* Set byte-wide (0x48) access. */
237 {0x00, NE_EN0_RCNTLO}, /* Clear the count regs. */
238 {0x00, NE_EN0_RCNTHI},
239 {0x00, NE_EN0_IMR}, /* Mask completion irq. */
240 {0xFF, NE_EN0_ISR},
241 {E8390_RXOFF, NE_EN0_RXCR}, /* 0x20 Set to monitor */
242 {E8390_TXOFF, NE_EN0_TXCR}, /* 0x02 and loopback mode. */
243 {32, NE_EN0_RCNTLO},
244 {0x00, NE_EN0_RCNTHI},
245 {0x00, NE_EN0_RSARLO}, /* DMA starting at 0x0000. */
246 {0x00, NE_EN0_RSARHI},
247 {E8390_RREAD+E8390_START, NE_CMD},
249 for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++) {
250 outb(program_seq[i].value, ioaddr + program_seq[i].offset);
254 for(i = 0; i < 32 /*sizeof(SA_prom)*/; i+=2) {
255 SA_prom[i] = inb(ioaddr + NE_DATAPORT);
256 SA_prom[i+1] = inb(ioaddr + NE_DATAPORT);
257 if (SA_prom[i] != SA_prom[i+1])
258 wordlength = 1;
261 /* At this point, wordlength *only* tells us if the SA_prom is doubled
262 up or not because some broken PCI cards don't respect the byte-wide
263 request in program_seq above, and hence don't have doubled up values.
264 These broken cards would otherwise be detected as an ne1000. */
266 if (wordlength == 2)
267 for (i = 0; i < 16; i++)
268 SA_prom[i] = SA_prom[i+i];
270 if (wordlength == 2) {
271 /* We must set the 8390 for word mode. */
272 outb(0x49, ioaddr + NE_EN0_DCFG);
273 start_page = NESM_START_PG;
274 stop_page = NESM_STOP_PG;
275 } else {
276 start_page = NE1SM_START_PG;
277 stop_page = NE1SM_STOP_PG;
280 neX000 = (SA_prom[14] == 0x57 && SA_prom[15] == 0x57);
281 ctron = (SA_prom[0] == 0x00 && SA_prom[1] == 0x00 && SA_prom[2] == 0x1d);
283 /* Set up the rest of the parameters. */
284 if (neX000) {
285 name = (wordlength == 2) ? "NE2000" : "NE1000";
286 } else if (ctron) {
287 name = (wordlength == 2) ? "Ctron-8" : "Ctron-16";
288 start_page = 0x01;
289 stop_page = (wordlength == 2) ? 0x40 : 0x20;
290 } else {
291 printk(" not found.\n");
292 return -ENXIO;
296 #else
297 wordlength = 2;
298 /* We must set the 8390 for word mode. */
299 outb(0x49, ioaddr + NE_EN0_DCFG);
300 start_page = NESM_START_PG;
301 stop_page = NESM_STOP_PG;
303 SA_prom[0] = MANUAL_HWADDR0;
304 SA_prom[1] = MANUAL_HWADDR1;
305 SA_prom[2] = MANUAL_HWADDR2;
306 SA_prom[3] = MANUAL_HWADDR3;
307 SA_prom[4] = MANUAL_HWADDR4;
308 SA_prom[5] = MANUAL_HWADDR5;
309 name = "NE2000";
310 #endif
312 dev->base_addr = ioaddr;
314 /* Install the Interrupt handler */
315 i = request_irq(IRQ_AMIGA_PORTS, apne_interrupt, SA_SHIRQ, DRV_NAME, dev);
316 if (i) return i;
318 for(i = 0; i < ETHER_ADDR_LEN; i++) {
319 printk(" %2.2x", SA_prom[i]);
320 dev->dev_addr[i] = SA_prom[i];
323 printk("\n%s: %s found.\n", dev->name, name);
325 ei_status.name = name;
326 ei_status.tx_start_page = start_page;
327 ei_status.stop_page = stop_page;
328 ei_status.word16 = (wordlength == 2);
330 ei_status.rx_start_page = start_page + TX_PAGES;
332 ei_status.reset_8390 = &apne_reset_8390;
333 ei_status.block_input = &apne_block_input;
334 ei_status.block_output = &apne_block_output;
335 ei_status.get_8390_hdr = &apne_get_8390_hdr;
336 dev->open = &apne_open;
337 dev->stop = &apne_close;
338 #ifdef CONFIG_NET_POLL_CONTROLLER
339 dev->poll_controller = ei_poll;
340 #endif
341 NS8390_init(dev, 0);
343 pcmcia_ack_int(pcmcia_get_intreq()); /* ack PCMCIA int req */
344 pcmcia_enable_irq();
346 apne_owned = 1;
348 return 0;
351 static int
352 apne_open(struct net_device *dev)
354 ei_open(dev);
355 return 0;
358 static int
359 apne_close(struct net_device *dev)
361 if (ei_debug > 1)
362 printk("%s: Shutting down ethercard.\n", dev->name);
363 ei_close(dev);
364 return 0;
367 /* Hard reset the card. This used to pause for the same period that a
368 8390 reset command required, but that shouldn't be necessary. */
369 static void
370 apne_reset_8390(struct net_device *dev)
372 unsigned long reset_start_time = jiffies;
374 init_pcmcia();
376 if (ei_debug > 1) printk("resetting the 8390 t=%ld...", jiffies);
378 outb(inb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
380 ei_status.txing = 0;
381 ei_status.dmaing = 0;
383 /* This check _should_not_ be necessary, omit eventually. */
384 while ((inb(NE_BASE+NE_EN0_ISR) & ENISR_RESET) == 0)
385 if (jiffies - reset_start_time > 2*HZ/100) {
386 printk("%s: ne_reset_8390() did not complete.\n", dev->name);
387 break;
389 outb(ENISR_RESET, NE_BASE + NE_EN0_ISR); /* Ack intr. */
392 /* Grab the 8390 specific header. Similar to the block_input routine, but
393 we don't need to be concerned with ring wrap as the header will be at
394 the start of a page, so we optimize accordingly. */
396 static void
397 apne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
400 int nic_base = dev->base_addr;
401 int cnt;
402 char *ptrc;
403 short *ptrs;
405 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
406 if (ei_status.dmaing) {
407 printk("%s: DMAing conflict in ne_get_8390_hdr "
408 "[DMAstat:%d][irqlock:%d][intr:%d].\n",
409 dev->name, ei_status.dmaing, ei_status.irqlock, dev->irq);
410 return;
413 ei_status.dmaing |= 0x01;
414 outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
415 outb(ENISR_RDC, nic_base + NE_EN0_ISR);
416 outb(sizeof(struct e8390_pkt_hdr), nic_base + NE_EN0_RCNTLO);
417 outb(0, nic_base + NE_EN0_RCNTHI);
418 outb(0, nic_base + NE_EN0_RSARLO); /* On page boundary */
419 outb(ring_page, nic_base + NE_EN0_RSARHI);
420 outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
422 if (ei_status.word16) {
423 ptrs = (short*)hdr;
424 for(cnt = 0; cnt < (sizeof(struct e8390_pkt_hdr)>>1); cnt++)
425 *ptrs++ = inw(NE_BASE + NE_DATAPORT);
426 } else {
427 ptrc = (char*)hdr;
428 for(cnt = 0; cnt < sizeof(struct e8390_pkt_hdr); cnt++)
429 *ptrc++ = inb(NE_BASE + NE_DATAPORT);
432 outb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr. */
433 ei_status.dmaing &= ~0x01;
435 le16_to_cpus(&hdr->count);
438 /* Block input and output, similar to the Crynwr packet driver. If you
439 are porting to a new ethercard, look at the packet driver source for hints.
440 The NEx000 doesn't share the on-board packet memory -- you have to put
441 the packet out through the "remote DMA" dataport using outb. */
443 static void
444 apne_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
446 int nic_base = dev->base_addr;
447 char *buf = skb->data;
448 char *ptrc;
449 short *ptrs;
450 int cnt;
452 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
453 if (ei_status.dmaing) {
454 printk("%s: DMAing conflict in ne_block_input "
455 "[DMAstat:%d][irqlock:%d][intr:%d].\n",
456 dev->name, ei_status.dmaing, ei_status.irqlock, dev->irq);
457 return;
459 ei_status.dmaing |= 0x01;
460 outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
461 outb(ENISR_RDC, nic_base + NE_EN0_ISR);
462 outb(count & 0xff, nic_base + NE_EN0_RCNTLO);
463 outb(count >> 8, nic_base + NE_EN0_RCNTHI);
464 outb(ring_offset & 0xff, nic_base + NE_EN0_RSARLO);
465 outb(ring_offset >> 8, nic_base + NE_EN0_RSARHI);
466 outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
467 if (ei_status.word16) {
468 ptrs = (short*)buf;
469 for (cnt = 0; cnt < (count>>1); cnt++)
470 *ptrs++ = inw(NE_BASE + NE_DATAPORT);
471 if (count & 0x01) {
472 buf[count-1] = inb(NE_BASE + NE_DATAPORT);
474 } else {
475 ptrc = (char*)buf;
476 for (cnt = 0; cnt < count; cnt++)
477 *ptrc++ = inb(NE_BASE + NE_DATAPORT);
480 outb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr. */
481 ei_status.dmaing &= ~0x01;
484 static void
485 apne_block_output(struct net_device *dev, int count,
486 const unsigned char *buf, const int start_page)
488 int nic_base = NE_BASE;
489 unsigned long dma_start;
490 char *ptrc;
491 short *ptrs;
492 int cnt;
494 /* Round the count up for word writes. Do we need to do this?
495 What effect will an odd byte count have on the 8390?
496 I should check someday. */
497 if (ei_status.word16 && (count & 0x01))
498 count++;
500 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
501 if (ei_status.dmaing) {
502 printk("%s: DMAing conflict in ne_block_output."
503 "[DMAstat:%d][irqlock:%d][intr:%d]\n",
504 dev->name, ei_status.dmaing, ei_status.irqlock, dev->irq);
505 return;
507 ei_status.dmaing |= 0x01;
508 /* We should already be in page 0, but to be safe... */
509 outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
511 outb(ENISR_RDC, nic_base + NE_EN0_ISR);
513 /* Now the normal output. */
514 outb(count & 0xff, nic_base + NE_EN0_RCNTLO);
515 outb(count >> 8, nic_base + NE_EN0_RCNTHI);
516 outb(0x00, nic_base + NE_EN0_RSARLO);
517 outb(start_page, nic_base + NE_EN0_RSARHI);
519 outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
520 if (ei_status.word16) {
521 ptrs = (short*)buf;
522 for (cnt = 0; cnt < count>>1; cnt++)
523 outw(*ptrs++, NE_BASE+NE_DATAPORT);
524 } else {
525 ptrc = (char*)buf;
526 for (cnt = 0; cnt < count; cnt++)
527 outb(*ptrc++, NE_BASE + NE_DATAPORT);
530 dma_start = jiffies;
532 while ((inb(NE_BASE + NE_EN0_ISR) & ENISR_RDC) == 0)
533 if (jiffies - dma_start > 2*HZ/100) { /* 20ms */
534 printk("%s: timeout waiting for Tx RDC.\n", dev->name);
535 apne_reset_8390(dev);
536 NS8390_init(dev,1);
537 break;
540 outb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr. */
541 ei_status.dmaing &= ~0x01;
542 return;
545 static irqreturn_t apne_interrupt(int irq, void *dev_id, struct pt_regs *regs)
547 unsigned char pcmcia_intreq;
549 if (!(gayle.inten & GAYLE_IRQ_IRQ))
550 return IRQ_NONE;
552 pcmcia_intreq = pcmcia_get_intreq();
554 if (!(pcmcia_intreq & GAYLE_IRQ_IRQ)) {
555 pcmcia_ack_int(pcmcia_intreq);
556 return IRQ_NONE;
558 if (ei_debug > 3)
559 printk("pcmcia intreq = %x\n", pcmcia_intreq);
560 pcmcia_disable_irq(); /* to get rid of the sti() within ei_interrupt */
561 ei_interrupt(irq, dev_id, regs);
562 pcmcia_ack_int(pcmcia_get_intreq());
563 pcmcia_enable_irq();
564 return IRQ_HANDLED;
567 #ifdef MODULE
568 static struct net_device *apne_dev;
570 int init_module(void)
572 apne_dev = apne_probe(-1);
573 if (IS_ERR(apne_dev))
574 return PTR_ERR(apne_dev);
575 return 0;
578 void cleanup_module(void)
580 unregister_netdev(apne_dev);
582 pcmcia_disable_irq();
584 free_irq(IRQ_AMIGA_PORTS, apne_dev);
586 pcmcia_reset();
588 release_region(IOBASE, 0x20);
590 free_netdev(apne_dev);
593 #endif
595 static int init_pcmcia(void)
597 u_char config;
598 #ifndef MANUAL_CONFIG
599 u_char tuple[32];
600 int offset_len;
601 #endif
602 u_long offset;
604 pcmcia_reset();
605 pcmcia_program_voltage(PCMCIA_0V);
606 pcmcia_access_speed(PCMCIA_SPEED_250NS);
607 pcmcia_write_enable();
609 #ifdef MANUAL_CONFIG
610 config = MANUAL_CONFIG;
611 #else
612 /* get and write config byte to enable IO port */
614 if (pcmcia_copy_tuple(CISTPL_CFTABLE_ENTRY, tuple, 32) < 3)
615 return 0;
617 config = tuple[2] & 0x3f;
618 #endif
619 #ifdef MANUAL_OFFSET
620 offset = MANUAL_OFFSET;
621 #else
622 if (pcmcia_copy_tuple(CISTPL_CONFIG, tuple, 32) < 6)
623 return 0;
625 offset_len = (tuple[2] & 0x3) + 1;
626 offset = 0;
627 while(offset_len--) {
628 offset = (offset << 8) | tuple[4+offset_len];
630 #endif
632 out_8(GAYLE_ATTRIBUTE+offset, config);
634 return 1;
637 MODULE_LICENSE("GPL");