ath9k: Move TX completion routine to xmit.c
[linux-2.6/verdex.git] / drivers / net / apne.c
blob7426f69406305821a931a1cd1566673d1f7ddcb3
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>
39 #include <linux/jiffies.h>
41 #include <asm/system.h>
42 #include <asm/io.h>
43 #include <asm/setup.h>
44 #include <asm/amigaints.h>
45 #include <asm/amigahw.h>
46 #include <asm/amigayle.h>
47 #include <asm/amipcmcia.h>
49 #include "8390.h"
51 /* ---- No user-serviceable parts below ---- */
53 #define DRV_NAME "apne"
55 #define NE_BASE (dev->base_addr)
56 #define NE_CMD 0x00
57 #define NE_DATAPORT 0x10 /* NatSemi-defined port window offset. */
58 #define NE_RESET 0x1f /* Issue a read to reset, a write to clear. */
59 #define NE_IO_EXTENT 0x20
61 #define NE_EN0_ISR 0x07
62 #define NE_EN0_DCFG 0x0e
64 #define NE_EN0_RSARLO 0x08
65 #define NE_EN0_RSARHI 0x09
66 #define NE_EN0_RCNTLO 0x0a
67 #define NE_EN0_RXCR 0x0c
68 #define NE_EN0_TXCR 0x0d
69 #define NE_EN0_RCNTHI 0x0b
70 #define NE_EN0_IMR 0x0f
72 #define NE1SM_START_PG 0x20 /* First page of TX buffer */
73 #define NE1SM_STOP_PG 0x40 /* Last page +1 of RX ring */
74 #define NESM_START_PG 0x40 /* First page of TX buffer */
75 #define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
78 struct net_device * __init apne_probe(int unit);
79 static int apne_probe1(struct net_device *dev, int ioaddr);
81 static int apne_open(struct net_device *dev);
82 static int apne_close(struct net_device *dev);
84 static void apne_reset_8390(struct net_device *dev);
85 static void apne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
86 int ring_page);
87 static void apne_block_input(struct net_device *dev, int count,
88 struct sk_buff *skb, int ring_offset);
89 static void apne_block_output(struct net_device *dev, const int count,
90 const unsigned char *buf, const int start_page);
91 static irqreturn_t apne_interrupt(int irq, void *dev_id);
93 static int init_pcmcia(void);
95 /* IO base address used for nic */
97 #define IOBASE 0x300
100 use MANUAL_CONFIG and MANUAL_OFFSET for enabling IO by hand
101 you can find the values to use by looking at the cnet.device
102 config file example (the default values are for the CNET40BC card)
106 #define MANUAL_CONFIG 0x20
107 #define MANUAL_OFFSET 0x3f8
109 #define MANUAL_HWADDR0 0x00
110 #define MANUAL_HWADDR1 0x12
111 #define MANUAL_HWADDR2 0x34
112 #define MANUAL_HWADDR3 0x56
113 #define MANUAL_HWADDR4 0x78
114 #define MANUAL_HWADDR5 0x9a
117 static const char version[] =
118 "apne.c:v1.1 7/10/98 Alain Malek (Alain.Malek@cryogen.ch)\n";
120 static int apne_owned; /* signal if card already owned */
122 struct net_device * __init apne_probe(int unit)
124 struct net_device *dev;
125 #ifndef MANUAL_CONFIG
126 char tuple[8];
127 #endif
128 int err;
130 if (!MACH_IS_AMIGA)
131 return ERR_PTR(-ENODEV);
133 if (apne_owned)
134 return ERR_PTR(-ENODEV);
136 if ( !(AMIGAHW_PRESENT(PCMCIA)) )
137 return ERR_PTR(-ENODEV);
139 printk("Looking for PCMCIA ethernet card : ");
141 /* check if a card is inserted */
142 if (!(PCMCIA_INSERTED)) {
143 printk("NO PCMCIA card inserted\n");
144 return ERR_PTR(-ENODEV);
147 dev = alloc_ei_netdev();
148 if (!dev)
149 return ERR_PTR(-ENOMEM);
150 if (unit >= 0) {
151 sprintf(dev->name, "eth%d", unit);
152 netdev_boot_setup_check(dev);
155 /* disable pcmcia irq for readtuple */
156 pcmcia_disable_irq();
158 #ifndef MANUAL_CONFIG
159 if ((pcmcia_copy_tuple(CISTPL_FUNCID, tuple, 8) < 3) ||
160 (tuple[2] != CISTPL_FUNCID_NETWORK)) {
161 printk("not an ethernet card\n");
162 /* XXX: shouldn't we re-enable irq here? */
163 free_netdev(dev);
164 return ERR_PTR(-ENODEV);
166 #endif
168 printk("ethernet PCMCIA card inserted\n");
170 if (!init_pcmcia()) {
171 /* XXX: shouldn't we re-enable irq here? */
172 free_netdev(dev);
173 return ERR_PTR(-ENODEV);
176 if (!request_region(IOBASE, 0x20, DRV_NAME)) {
177 free_netdev(dev);
178 return ERR_PTR(-EBUSY);
181 err = apne_probe1(dev, IOBASE);
182 if (err) {
183 release_region(IOBASE, 0x20);
184 free_netdev(dev);
185 return ERR_PTR(err);
187 err = register_netdev(dev);
188 if (!err)
189 return dev;
191 pcmcia_disable_irq();
192 free_irq(IRQ_AMIGA_PORTS, dev);
193 pcmcia_reset();
194 release_region(IOBASE, 0x20);
195 free_netdev(dev);
196 return ERR_PTR(err);
199 static int __init apne_probe1(struct net_device *dev, int ioaddr)
201 int i;
202 unsigned char SA_prom[32];
203 int wordlength = 2;
204 const char *name = NULL;
205 int start_page, stop_page;
206 #ifndef MANUAL_HWADDR0
207 int neX000, ctron;
208 #endif
209 static unsigned version_printed;
211 if (ei_debug && version_printed++ == 0)
212 printk(version);
214 printk("PCMCIA NE*000 ethercard probe");
216 /* Reset card. Who knows what dain-bramaged state it was left in. */
217 { unsigned long reset_start_time = jiffies;
219 outb(inb(ioaddr + NE_RESET), ioaddr + NE_RESET);
221 while ((inb(ioaddr + NE_EN0_ISR) & ENISR_RESET) == 0)
222 if (time_after(jiffies, reset_start_time + 2*HZ/100)) {
223 printk(" not found (no reset ack).\n");
224 return -ENODEV;
227 outb(0xff, ioaddr + NE_EN0_ISR); /* Ack all intr. */
230 #ifndef MANUAL_HWADDR0
232 /* Read the 16 bytes of station address PROM.
233 We must first initialize registers, similar to NS8390_init(eifdev, 0).
234 We can't reliably read the SAPROM address without this.
235 (I learned the hard way!). */
237 struct {unsigned long value, offset; } program_seq[] = {
238 {E8390_NODMA+E8390_PAGE0+E8390_STOP, NE_CMD}, /* Select page 0*/
239 {0x48, NE_EN0_DCFG}, /* Set byte-wide (0x48) access. */
240 {0x00, NE_EN0_RCNTLO}, /* Clear the count regs. */
241 {0x00, NE_EN0_RCNTHI},
242 {0x00, NE_EN0_IMR}, /* Mask completion irq. */
243 {0xFF, NE_EN0_ISR},
244 {E8390_RXOFF, NE_EN0_RXCR}, /* 0x20 Set to monitor */
245 {E8390_TXOFF, NE_EN0_TXCR}, /* 0x02 and loopback mode. */
246 {32, NE_EN0_RCNTLO},
247 {0x00, NE_EN0_RCNTHI},
248 {0x00, NE_EN0_RSARLO}, /* DMA starting at 0x0000. */
249 {0x00, NE_EN0_RSARHI},
250 {E8390_RREAD+E8390_START, NE_CMD},
252 for (i = 0; i < ARRAY_SIZE(program_seq); i++) {
253 outb(program_seq[i].value, ioaddr + program_seq[i].offset);
257 for(i = 0; i < 32 /*sizeof(SA_prom)*/; i+=2) {
258 SA_prom[i] = inb(ioaddr + NE_DATAPORT);
259 SA_prom[i+1] = inb(ioaddr + NE_DATAPORT);
260 if (SA_prom[i] != SA_prom[i+1])
261 wordlength = 1;
264 /* At this point, wordlength *only* tells us if the SA_prom is doubled
265 up or not because some broken PCI cards don't respect the byte-wide
266 request in program_seq above, and hence don't have doubled up values.
267 These broken cards would otherwise be detected as an ne1000. */
269 if (wordlength == 2)
270 for (i = 0; i < 16; i++)
271 SA_prom[i] = SA_prom[i+i];
273 if (wordlength == 2) {
274 /* We must set the 8390 for word mode. */
275 outb(0x49, ioaddr + NE_EN0_DCFG);
276 start_page = NESM_START_PG;
277 stop_page = NESM_STOP_PG;
278 } else {
279 start_page = NE1SM_START_PG;
280 stop_page = NE1SM_STOP_PG;
283 neX000 = (SA_prom[14] == 0x57 && SA_prom[15] == 0x57);
284 ctron = (SA_prom[0] == 0x00 && SA_prom[1] == 0x00 && SA_prom[2] == 0x1d);
286 /* Set up the rest of the parameters. */
287 if (neX000) {
288 name = (wordlength == 2) ? "NE2000" : "NE1000";
289 } else if (ctron) {
290 name = (wordlength == 2) ? "Ctron-8" : "Ctron-16";
291 start_page = 0x01;
292 stop_page = (wordlength == 2) ? 0x40 : 0x20;
293 } else {
294 printk(" not found.\n");
295 return -ENXIO;
299 #else
300 wordlength = 2;
301 /* We must set the 8390 for word mode. */
302 outb(0x49, ioaddr + NE_EN0_DCFG);
303 start_page = NESM_START_PG;
304 stop_page = NESM_STOP_PG;
306 SA_prom[0] = MANUAL_HWADDR0;
307 SA_prom[1] = MANUAL_HWADDR1;
308 SA_prom[2] = MANUAL_HWADDR2;
309 SA_prom[3] = MANUAL_HWADDR3;
310 SA_prom[4] = MANUAL_HWADDR4;
311 SA_prom[5] = MANUAL_HWADDR5;
312 name = "NE2000";
313 #endif
315 dev->base_addr = ioaddr;
316 dev->irq = IRQ_AMIGA_PORTS;
318 /* Install the Interrupt handler */
319 i = request_irq(dev->irq, apne_interrupt, IRQF_SHARED, DRV_NAME, dev);
320 if (i) return i;
322 for(i = 0; i < ETHER_ADDR_LEN; i++)
323 dev->dev_addr[i] = SA_prom[i];
325 printk(" %pM\n", dev->dev_addr);
327 printk("%s: %s found.\n", dev->name, name);
329 ei_status.name = name;
330 ei_status.tx_start_page = start_page;
331 ei_status.stop_page = stop_page;
332 ei_status.word16 = (wordlength == 2);
334 ei_status.rx_start_page = start_page + TX_PAGES;
336 ei_status.reset_8390 = &apne_reset_8390;
337 ei_status.block_input = &apne_block_input;
338 ei_status.block_output = &apne_block_output;
339 ei_status.get_8390_hdr = &apne_get_8390_hdr;
340 dev->open = &apne_open;
341 dev->stop = &apne_close;
342 #ifdef CONFIG_NET_POLL_CONTROLLER
343 dev->poll_controller = ei_poll;
344 #endif
345 NS8390_init(dev, 0);
347 pcmcia_ack_int(pcmcia_get_intreq()); /* ack PCMCIA int req */
348 pcmcia_enable_irq();
350 apne_owned = 1;
352 return 0;
355 static int
356 apne_open(struct net_device *dev)
358 ei_open(dev);
359 return 0;
362 static int
363 apne_close(struct net_device *dev)
365 if (ei_debug > 1)
366 printk("%s: Shutting down ethercard.\n", dev->name);
367 ei_close(dev);
368 return 0;
371 /* Hard reset the card. This used to pause for the same period that a
372 8390 reset command required, but that shouldn't be necessary. */
373 static void
374 apne_reset_8390(struct net_device *dev)
376 unsigned long reset_start_time = jiffies;
378 init_pcmcia();
380 if (ei_debug > 1) printk("resetting the 8390 t=%ld...", jiffies);
382 outb(inb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
384 ei_status.txing = 0;
385 ei_status.dmaing = 0;
387 /* This check _should_not_ be necessary, omit eventually. */
388 while ((inb(NE_BASE+NE_EN0_ISR) & ENISR_RESET) == 0)
389 if (time_after(jiffies, reset_start_time + 2*HZ/100)) {
390 printk("%s: ne_reset_8390() did not complete.\n", dev->name);
391 break;
393 outb(ENISR_RESET, NE_BASE + NE_EN0_ISR); /* Ack intr. */
396 /* Grab the 8390 specific header. Similar to the block_input routine, but
397 we don't need to be concerned with ring wrap as the header will be at
398 the start of a page, so we optimize accordingly. */
400 static void
401 apne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
404 int nic_base = dev->base_addr;
405 int cnt;
406 char *ptrc;
407 short *ptrs;
409 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
410 if (ei_status.dmaing) {
411 printk("%s: DMAing conflict in ne_get_8390_hdr "
412 "[DMAstat:%d][irqlock:%d][intr:%d].\n",
413 dev->name, ei_status.dmaing, ei_status.irqlock, dev->irq);
414 return;
417 ei_status.dmaing |= 0x01;
418 outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
419 outb(ENISR_RDC, nic_base + NE_EN0_ISR);
420 outb(sizeof(struct e8390_pkt_hdr), nic_base + NE_EN0_RCNTLO);
421 outb(0, nic_base + NE_EN0_RCNTHI);
422 outb(0, nic_base + NE_EN0_RSARLO); /* On page boundary */
423 outb(ring_page, nic_base + NE_EN0_RSARHI);
424 outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
426 if (ei_status.word16) {
427 ptrs = (short*)hdr;
428 for(cnt = 0; cnt < (sizeof(struct e8390_pkt_hdr)>>1); cnt++)
429 *ptrs++ = inw(NE_BASE + NE_DATAPORT);
430 } else {
431 ptrc = (char*)hdr;
432 for(cnt = 0; cnt < sizeof(struct e8390_pkt_hdr); cnt++)
433 *ptrc++ = inb(NE_BASE + NE_DATAPORT);
436 outb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr. */
437 ei_status.dmaing &= ~0x01;
439 le16_to_cpus(&hdr->count);
442 /* Block input and output, similar to the Crynwr packet driver. If you
443 are porting to a new ethercard, look at the packet driver source for hints.
444 The NEx000 doesn't share the on-board packet memory -- you have to put
445 the packet out through the "remote DMA" dataport using outb. */
447 static void
448 apne_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
450 int nic_base = dev->base_addr;
451 char *buf = skb->data;
452 char *ptrc;
453 short *ptrs;
454 int cnt;
456 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
457 if (ei_status.dmaing) {
458 printk("%s: DMAing conflict in ne_block_input "
459 "[DMAstat:%d][irqlock:%d][intr:%d].\n",
460 dev->name, ei_status.dmaing, ei_status.irqlock, dev->irq);
461 return;
463 ei_status.dmaing |= 0x01;
464 outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
465 outb(ENISR_RDC, nic_base + NE_EN0_ISR);
466 outb(count & 0xff, nic_base + NE_EN0_RCNTLO);
467 outb(count >> 8, nic_base + NE_EN0_RCNTHI);
468 outb(ring_offset & 0xff, nic_base + NE_EN0_RSARLO);
469 outb(ring_offset >> 8, nic_base + NE_EN0_RSARHI);
470 outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
471 if (ei_status.word16) {
472 ptrs = (short*)buf;
473 for (cnt = 0; cnt < (count>>1); cnt++)
474 *ptrs++ = inw(NE_BASE + NE_DATAPORT);
475 if (count & 0x01) {
476 buf[count-1] = inb(NE_BASE + NE_DATAPORT);
478 } else {
479 ptrc = (char*)buf;
480 for (cnt = 0; cnt < count; cnt++)
481 *ptrc++ = inb(NE_BASE + NE_DATAPORT);
484 outb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr. */
485 ei_status.dmaing &= ~0x01;
488 static void
489 apne_block_output(struct net_device *dev, int count,
490 const unsigned char *buf, const int start_page)
492 int nic_base = NE_BASE;
493 unsigned long dma_start;
494 char *ptrc;
495 short *ptrs;
496 int cnt;
498 /* Round the count up for word writes. Do we need to do this?
499 What effect will an odd byte count have on the 8390?
500 I should check someday. */
501 if (ei_status.word16 && (count & 0x01))
502 count++;
504 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
505 if (ei_status.dmaing) {
506 printk("%s: DMAing conflict in ne_block_output."
507 "[DMAstat:%d][irqlock:%d][intr:%d]\n",
508 dev->name, ei_status.dmaing, ei_status.irqlock, dev->irq);
509 return;
511 ei_status.dmaing |= 0x01;
512 /* We should already be in page 0, but to be safe... */
513 outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
515 outb(ENISR_RDC, nic_base + NE_EN0_ISR);
517 /* Now the normal output. */
518 outb(count & 0xff, nic_base + NE_EN0_RCNTLO);
519 outb(count >> 8, nic_base + NE_EN0_RCNTHI);
520 outb(0x00, nic_base + NE_EN0_RSARLO);
521 outb(start_page, nic_base + NE_EN0_RSARHI);
523 outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
524 if (ei_status.word16) {
525 ptrs = (short*)buf;
526 for (cnt = 0; cnt < count>>1; cnt++)
527 outw(*ptrs++, NE_BASE+NE_DATAPORT);
528 } else {
529 ptrc = (char*)buf;
530 for (cnt = 0; cnt < count; cnt++)
531 outb(*ptrc++, NE_BASE + NE_DATAPORT);
534 dma_start = jiffies;
536 while ((inb(NE_BASE + NE_EN0_ISR) & ENISR_RDC) == 0)
537 if (time_after(jiffies, dma_start + 2*HZ/100)) { /* 20ms */
538 printk("%s: timeout waiting for Tx RDC.\n", dev->name);
539 apne_reset_8390(dev);
540 NS8390_init(dev,1);
541 break;
544 outb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr. */
545 ei_status.dmaing &= ~0x01;
546 return;
549 static irqreturn_t apne_interrupt(int irq, void *dev_id)
551 unsigned char pcmcia_intreq;
553 if (!(gayle.inten & GAYLE_IRQ_IRQ))
554 return IRQ_NONE;
556 pcmcia_intreq = pcmcia_get_intreq();
558 if (!(pcmcia_intreq & GAYLE_IRQ_IRQ)) {
559 pcmcia_ack_int(pcmcia_intreq);
560 return IRQ_NONE;
562 if (ei_debug > 3)
563 printk("pcmcia intreq = %x\n", pcmcia_intreq);
564 pcmcia_disable_irq(); /* to get rid of the sti() within ei_interrupt */
565 ei_interrupt(irq, dev_id);
566 pcmcia_ack_int(pcmcia_get_intreq());
567 pcmcia_enable_irq();
568 return IRQ_HANDLED;
571 #ifdef MODULE
572 static struct net_device *apne_dev;
574 static int __init apne_module_init(void)
576 apne_dev = apne_probe(-1);
577 if (IS_ERR(apne_dev))
578 return PTR_ERR(apne_dev);
579 return 0;
582 static void __exit apne_module_exit(void)
584 unregister_netdev(apne_dev);
586 pcmcia_disable_irq();
588 free_irq(IRQ_AMIGA_PORTS, apne_dev);
590 pcmcia_reset();
592 release_region(IOBASE, 0x20);
594 free_netdev(apne_dev);
596 module_init(apne_module_init);
597 module_exit(apne_module_exit);
598 #endif
600 static int init_pcmcia(void)
602 u_char config;
603 #ifndef MANUAL_CONFIG
604 u_char tuple[32];
605 int offset_len;
606 #endif
607 u_long offset;
609 pcmcia_reset();
610 pcmcia_program_voltage(PCMCIA_0V);
611 pcmcia_access_speed(PCMCIA_SPEED_250NS);
612 pcmcia_write_enable();
614 #ifdef MANUAL_CONFIG
615 config = MANUAL_CONFIG;
616 #else
617 /* get and write config byte to enable IO port */
619 if (pcmcia_copy_tuple(CISTPL_CFTABLE_ENTRY, tuple, 32) < 3)
620 return 0;
622 config = tuple[2] & 0x3f;
623 #endif
624 #ifdef MANUAL_OFFSET
625 offset = MANUAL_OFFSET;
626 #else
627 if (pcmcia_copy_tuple(CISTPL_CONFIG, tuple, 32) < 6)
628 return 0;
630 offset_len = (tuple[2] & 0x3) + 1;
631 offset = 0;
632 while(offset_len--) {
633 offset = (offset << 8) | tuple[4+offset_len];
635 #endif
637 out_8(GAYLE_ATTRIBUTE+offset, config);
639 return 1;
642 MODULE_LICENSE("GPL");