* better
[mascara-docs.git] / i386 / linux-2.3.21 / drivers / net / apne.c
blob33da607fe7220cae9d07ca6d45a9be4901471290
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/sched.h>
34 #include <linux/errno.h>
35 #include <linux/pci.h>
36 #include <linux/init.h>
37 #include <linux/delay.h>
38 #include <asm/system.h>
39 #include <asm/io.h>
41 #include <linux/netdevice.h>
42 #include <linux/etherdevice.h>
44 #include <asm/setup.h>
45 #include <asm/amigaints.h>
46 #include <asm/amigahw.h>
47 #include <asm/amigayle.h>
48 #include <asm/amipcmcia.h>
50 #include "8390.h"
52 /* ---- No user-serviceable parts below ---- */
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+GAYLE_ODD /* Issue a read to reset, a write to clear. */
58 #define NE_IO_EXTENT 0x20
60 #define NE_EN0_ISR 0x07+GAYLE_ODD
61 #define NE_EN0_DCFG 0x0e
63 #define NE_EN0_RSARLO 0x08
64 #define NE_EN0_RSARHI 0x09+GAYLE_ODD
65 #define NE_EN0_RCNTLO 0x0a
66 #define NE_EN0_RXCR 0x0c
67 #define NE_EN0_TXCR 0x0d+GAYLE_ODD
68 #define NE_EN0_RCNTHI 0x0b+GAYLE_ODD
69 #define NE_EN0_IMR 0x0f+GAYLE_ODD
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 int apne_probe(struct net_device *dev);
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 void 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 #define WORDSWAP(a) ( (((a)>>8)&0xff) | ((a)<<8) )
119 static const char *version =
120 "apne.c:v1.1 7/10/98 Alain Malek (Alain.Malek@cryogen.ch)\n";
122 static int apne_owned = 0; /* signal if card already owned */
124 int __init apne_probe(struct net_device *dev)
126 #ifndef MANUAL_CONFIG
127 char tuple[8];
128 #endif
130 if (apne_owned)
131 return -ENODEV;
133 if ( !(AMIGAHW_PRESENT(PCMCIA)) )
134 return (-ENODEV);
136 printk("Looking for PCMCIA ethernet card : ");
138 /* check if a card is inserted */
139 if (!(PCMCIA_INSERTED)) {
140 printk("NO PCMCIA card inserted\n");
141 return (-ENODEV);
144 /* disable pcmcia irq for readtuple */
145 pcmcia_disable_irq();
147 #ifndef MANUAL_CONFIG
148 if ((pcmcia_copy_tuple(CISTPL_FUNCID, tuple, 8) < 3) ||
149 (tuple[2] != CISTPL_FUNCID_NETWORK)) {
150 printk("not an ethernet card\n");
151 return (-ENODEV);
153 #endif
155 printk("ethernet PCMCIA card inserted\n");
157 if (init_pcmcia())
158 return apne_probe1(dev, IOBASE+GAYLE_IO);
159 else
160 return (-ENODEV);
164 static int __init apne_probe1(struct net_device *dev, int ioaddr)
166 int i;
167 unsigned char SA_prom[32];
168 int wordlength = 2;
169 const char *name = NULL;
170 int start_page, stop_page;
171 #ifndef MANUAL_HWADDR0
172 int neX000, ctron;
173 #endif
174 static unsigned version_printed = 0;
175 static u32 pcmcia_offsets[16]={
176 0, 1+GAYLE_ODD, 2, 3+GAYLE_ODD,
177 4, 5+GAYLE_ODD, 6, 7+GAYLE_ODD,
178 8, 9+GAYLE_ODD, 0xa, 0xb+GAYLE_ODD,
179 0xc, 0xd+GAYLE_ODD, 0xe, 0xf+GAYLE_ODD };
181 if (load_8390_module("apne.c"))
182 return -ENOSYS;
184 /* We should have a "dev" from Space.c or the static module table. */
185 if (dev == NULL) {
186 printk(KERN_ERR "apne.c: Passed a NULL device.\n");
187 dev = init_etherdev(0, 0);
190 if (ei_debug && version_printed++ == 0)
191 printk(version);
193 printk("PCMCIA NE*000 ethercard probe");
195 /* Reset card. Who knows what dain-bramaged state it was left in. */
196 { unsigned long reset_start_time = jiffies;
198 writeb(readb(ioaddr + NE_RESET), ioaddr + NE_RESET);
200 while ((readb(ioaddr + NE_EN0_ISR) & ENISR_RESET) == 0)
201 if (jiffies - reset_start_time > 2*HZ/100) {
202 printk(" not found (no reset ack).\n");
203 return -ENODEV;
206 writeb(0xff, ioaddr + NE_EN0_ISR); /* Ack all intr. */
209 #ifndef MANUAL_HWADDR0
211 /* Read the 16 bytes of station address PROM.
212 We must first initialize registers, similar to NS8390_init(eifdev, 0).
213 We can't reliably read the SAPROM address without this.
214 (I learned the hard way!). */
216 struct {unsigned long value, offset; } program_seq[] = {
217 {E8390_NODMA+E8390_PAGE0+E8390_STOP, NE_CMD}, /* Select page 0*/
218 {0x48, NE_EN0_DCFG}, /* Set byte-wide (0x48) access. */
219 {0x00, NE_EN0_RCNTLO}, /* Clear the count regs. */
220 {0x00, NE_EN0_RCNTHI},
221 {0x00, NE_EN0_IMR}, /* Mask completion irq. */
222 {0xFF, NE_EN0_ISR},
223 {E8390_RXOFF, NE_EN0_RXCR}, /* 0x20 Set to monitor */
224 {E8390_TXOFF, NE_EN0_TXCR}, /* 0x02 and loopback mode. */
225 {32, NE_EN0_RCNTLO},
226 {0x00, NE_EN0_RCNTHI},
227 {0x00, NE_EN0_RSARLO}, /* DMA starting at 0x0000. */
228 {0x00, NE_EN0_RSARHI},
229 {E8390_RREAD+E8390_START, NE_CMD},
231 for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++) {
232 writeb(program_seq[i].value, ioaddr + program_seq[i].offset);
236 for(i = 0; i < 32 /*sizeof(SA_prom)*/; i+=2) {
237 SA_prom[i] = readb(ioaddr + NE_DATAPORT);
238 SA_prom[i+1] = readb(ioaddr + NE_DATAPORT);
239 if (SA_prom[i] != SA_prom[i+1])
240 wordlength = 1;
243 /* At this point, wordlength *only* tells us if the SA_prom is doubled
244 up or not because some broken PCI cards don't respect the byte-wide
245 request in program_seq above, and hence don't have doubled up values.
246 These broken cards would otherwise be detected as an ne1000. */
248 if (wordlength == 2)
249 for (i = 0; i < 16; i++)
250 SA_prom[i] = SA_prom[i+i];
252 if (wordlength == 2) {
253 /* We must set the 8390 for word mode. */
254 writeb(0x49, ioaddr + NE_EN0_DCFG);
255 start_page = NESM_START_PG;
256 stop_page = NESM_STOP_PG;
257 } else {
258 start_page = NE1SM_START_PG;
259 stop_page = NE1SM_STOP_PG;
262 neX000 = (SA_prom[14] == 0x57 && SA_prom[15] == 0x57);
263 ctron = (SA_prom[0] == 0x00 && SA_prom[1] == 0x00 && SA_prom[2] == 0x1d);
265 /* Set up the rest of the parameters. */
266 if (neX000) {
267 name = (wordlength == 2) ? "NE2000" : "NE1000";
268 } else if (ctron) {
269 name = (wordlength == 2) ? "Ctron-8" : "Ctron-16";
270 start_page = 0x01;
271 stop_page = (wordlength == 2) ? 0x40 : 0x20;
272 } else {
273 printk(" not found.\n");
274 return ENXIO;
278 #else
279 wordlength = 2;
280 /* We must set the 8390 for word mode. */
281 writeb(0x49, ioaddr + NE_EN0_DCFG);
282 start_page = NESM_START_PG;
283 stop_page = NESM_STOP_PG;
285 SA_prom[0] = MANUAL_HWADDR0;
286 SA_prom[1] = MANUAL_HWADDR1;
287 SA_prom[2] = MANUAL_HWADDR2;
288 SA_prom[3] = MANUAL_HWADDR3;
289 SA_prom[4] = MANUAL_HWADDR4;
290 SA_prom[5] = MANUAL_HWADDR5;
291 name = "NE2000";
292 #endif
294 dev->base_addr = ioaddr;
296 /* Install the Interrupt handler */
297 if (request_irq(IRQ_AMIGA_PORTS, apne_interrupt, SA_SHIRQ,
298 "apne Ethernet", dev))
299 return -EAGAIN;
302 /* Allocate dev->priv and fill in 8390 specific dev fields. */
303 if (ethdev_init(dev)) {
304 printk (" unable to get memory for dev->priv.\n");
305 return -ENOMEM;
308 for(i = 0; i < ETHER_ADDR_LEN; i++) {
309 printk(" %2.2x", SA_prom[i]);
310 dev->dev_addr[i] = SA_prom[i];
313 printk("\n%s: %s found.\n",
314 dev->name, name);
316 ei_status.name = name;
317 ei_status.tx_start_page = start_page;
318 ei_status.stop_page = stop_page;
319 ei_status.word16 = (wordlength == 2);
321 ei_status.rx_start_page = start_page + TX_PAGES;
323 ei_status.reset_8390 = &apne_reset_8390;
324 ei_status.block_input = &apne_block_input;
325 ei_status.block_output = &apne_block_output;
326 ei_status.get_8390_hdr = &apne_get_8390_hdr;
327 ei_status.reg_offset = pcmcia_offsets;
328 dev->open = &apne_open;
329 dev->stop = &apne_close;
330 NS8390_init(dev, 0);
332 pcmcia_ack_int(pcmcia_get_intreq()); /* ack PCMCIA int req */
333 pcmcia_enable_irq();
335 apne_owned = 1;
337 return 0;
340 static int
341 apne_open(struct net_device *dev)
343 ei_open(dev);
344 MOD_INC_USE_COUNT;
345 return 0;
348 static int
349 apne_close(struct net_device *dev)
351 if (ei_debug > 1)
352 printk("%s: Shutting down ethercard.\n", dev->name);
353 ei_close(dev);
354 MOD_DEC_USE_COUNT;
355 return 0;
358 /* Hard reset the card. This used to pause for the same period that a
359 8390 reset command required, but that shouldn't be necessary. */
360 static void
361 apne_reset_8390(struct net_device *dev)
363 unsigned long reset_start_time = jiffies;
365 init_pcmcia();
367 if (ei_debug > 1) printk("resetting the 8390 t=%ld...", jiffies);
369 writeb(readb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
371 ei_status.txing = 0;
372 ei_status.dmaing = 0;
374 /* This check _should_not_ be necessary, omit eventually. */
375 while ((readb(NE_BASE+NE_EN0_ISR) & ENISR_RESET) == 0)
376 if (jiffies - reset_start_time > 2*HZ/100) {
377 printk("%s: ne_reset_8390() did not complete.\n", dev->name);
378 break;
380 writeb(ENISR_RESET, NE_BASE + NE_EN0_ISR); /* Ack intr. */
383 /* Grab the 8390 specific header. Similar to the block_input routine, but
384 we don't need to be concerned with ring wrap as the header will be at
385 the start of a page, so we optimize accordingly. */
387 static void
388 apne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
391 int nic_base = dev->base_addr;
392 int cnt;
393 char *ptrc;
394 short *ptrs;
396 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
397 if (ei_status.dmaing) {
398 printk("%s: DMAing conflict in ne_get_8390_hdr "
399 "[DMAstat:%d][irqlock:%d][intr:%ld].\n",
400 dev->name, ei_status.dmaing, ei_status.irqlock,
401 dev->interrupt);
402 return;
405 ei_status.dmaing |= 0x01;
406 writeb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
407 writeb(ENISR_RDC, nic_base + NE_EN0_ISR);
408 writeb(sizeof(struct e8390_pkt_hdr), nic_base + NE_EN0_RCNTLO);
409 writeb(0, nic_base + NE_EN0_RCNTHI);
410 writeb(0, nic_base + NE_EN0_RSARLO); /* On page boundary */
411 writeb(ring_page, nic_base + NE_EN0_RSARHI);
412 writeb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
414 if (ei_status.word16) {
415 ptrs = (short*)hdr;
416 for(cnt = 0; cnt < (sizeof(struct e8390_pkt_hdr)>>1); cnt++)
417 *ptrs++ = readw(NE_BASE + NE_DATAPORT);
418 } else {
419 ptrc = (char*)hdr;
420 for(cnt = 0; cnt < sizeof(struct e8390_pkt_hdr); cnt++)
421 *ptrc++ = readb(NE_BASE + NE_DATAPORT);
424 writeb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr. */
426 hdr->count = WORDSWAP(hdr->count);
428 ei_status.dmaing &= ~0x01;
431 /* Block input and output, similar to the Crynwr packet driver. If you
432 are porting to a new ethercard, look at the packet driver source for hints.
433 The NEx000 doesn't share the on-board packet memory -- you have to put
434 the packet out through the "remote DMA" dataport using writeb. */
436 static void
437 apne_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
439 int nic_base = dev->base_addr;
440 char *buf = skb->data;
441 char *ptrc;
442 short *ptrs;
443 int cnt;
445 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
446 if (ei_status.dmaing) {
447 printk("%s: DMAing conflict in ne_block_input "
448 "[DMAstat:%d][irqlock:%d][intr:%ld].\n",
449 dev->name, ei_status.dmaing, ei_status.irqlock,
450 dev->interrupt);
451 return;
453 ei_status.dmaing |= 0x01;
454 writeb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
455 writeb(ENISR_RDC, nic_base + NE_EN0_ISR);
456 writeb(count & 0xff, nic_base + NE_EN0_RCNTLO);
457 writeb(count >> 8, nic_base + NE_EN0_RCNTHI);
458 writeb(ring_offset & 0xff, nic_base + NE_EN0_RSARLO);
459 writeb(ring_offset >> 8, nic_base + NE_EN0_RSARHI);
460 writeb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
461 if (ei_status.word16) {
462 ptrs = (short*)buf;
463 for (cnt = 0; cnt < (count>>1); cnt++)
464 *ptrs++ = readw(NE_BASE + NE_DATAPORT);
465 if (count & 0x01) {
466 buf[count-1] = readb(NE_BASE + NE_DATAPORT);
468 } else {
469 ptrc = (char*)buf;
470 for (cnt = 0; cnt < count; cnt++)
471 *ptrc++ = readb(NE_BASE + NE_DATAPORT);
474 writeb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr. */
475 ei_status.dmaing &= ~0x01;
478 static void
479 apne_block_output(struct net_device *dev, int count,
480 const unsigned char *buf, const int start_page)
482 int nic_base = NE_BASE;
483 unsigned long dma_start;
484 char *ptrc;
485 short *ptrs;
486 int cnt;
488 /* Round the count up for word writes. Do we need to do this?
489 What effect will an odd byte count have on the 8390?
490 I should check someday. */
491 if (ei_status.word16 && (count & 0x01))
492 count++;
494 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
495 if (ei_status.dmaing) {
496 printk("%s: DMAing conflict in ne_block_output."
497 "[DMAstat:%d][irqlock:%d][intr:%ld]\n",
498 dev->name, ei_status.dmaing, ei_status.irqlock,
499 dev->interrupt);
500 return;
502 ei_status.dmaing |= 0x01;
503 /* We should already be in page 0, but to be safe... */
504 writeb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
506 writeb(ENISR_RDC, nic_base + NE_EN0_ISR);
508 /* Now the normal output. */
509 writeb(count & 0xff, nic_base + NE_EN0_RCNTLO);
510 writeb(count >> 8, nic_base + NE_EN0_RCNTHI);
511 writeb(0x00, nic_base + NE_EN0_RSARLO);
512 writeb(start_page, nic_base + NE_EN0_RSARHI);
514 writeb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
515 if (ei_status.word16) {
516 ptrs = (short*)buf;
517 for (cnt = 0; cnt < count>>1; cnt++)
518 writew(*ptrs++, NE_BASE+NE_DATAPORT);
519 } else {
520 ptrc = (char*)buf;
521 for (cnt = 0; cnt < count; cnt++)
522 writeb(*ptrc++, NE_BASE + NE_DATAPORT);
525 dma_start = jiffies;
527 while ((readb(NE_BASE + NE_EN0_ISR) & ENISR_RDC) == 0)
528 if (jiffies - dma_start > 2*HZ/100) { /* 20ms */
529 printk("%s: timeout waiting for Tx RDC.\n", dev->name);
530 apne_reset_8390(dev);
531 NS8390_init(dev,1);
532 break;
535 writeb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr. */
536 ei_status.dmaing &= ~0x01;
537 return;
540 static void apne_interrupt(int irq, void *dev_id, struct pt_regs *regs)
542 unsigned char pcmcia_intreq;
544 if (!(gayle.inten & GAYLE_IRQ_IRQ))
545 return;
547 pcmcia_intreq = pcmcia_get_intreq();
549 if (!(pcmcia_intreq & GAYLE_IRQ_IRQ)) {
550 pcmcia_ack_int(pcmcia_intreq);
551 return;
553 if (ei_debug > 3)
554 printk("pcmcia intreq = %x\n", pcmcia_intreq);
555 pcmcia_disable_irq(); /* to get rid of the sti() within ei_interrupt */
556 ei_interrupt(irq, dev_id, regs);
557 pcmcia_ack_int(pcmcia_get_intreq());
558 pcmcia_enable_irq();
561 #ifdef MODULE
562 static char devicename[9] = {0, };
564 static struct net_device apne_dev =
566 devicename,
567 0, 0, 0, 0,
568 0, 0,
569 0, 0, 0, NULL, apne_probe,
572 int init_module(void)
574 int err;
575 if ((err = register_netdev(&apne_dev))) {
576 if (err == -EIO)
577 printk("No PCMCIA NEx000 ethernet card found.\n");
578 return (err);
580 lock_8390_module();
581 return (0);
584 void cleanup_module(void)
586 unregister_netdev(&apne_dev);
588 pcmcia_disable_irq();
590 free_irq(IRQ_AMIGA_PORTS, &apne_dev);
592 pcmcia_reset();
594 unlock_8390_module();
596 apne_owned = 0;
599 #endif
601 static int init_pcmcia(void)
603 u_char config;
604 #ifndef MANUAL_CONFIG
605 u_char tuple[32];
606 int offset_len;
607 #endif
608 u_long offset;
610 pcmcia_reset();
611 pcmcia_program_voltage(PCMCIA_0V);
612 pcmcia_access_speed(PCMCIA_SPEED_250NS);
613 pcmcia_write_enable();
615 #ifdef MANUAL_CONFIG
616 config = MANUAL_CONFIG;
617 #else
618 /* get and write config byte to enable IO port */
620 if (pcmcia_copy_tuple(CISTPL_CFTABLE_ENTRY, tuple, 32) < 3)
621 return 0;
623 config = tuple[2] & 0x3f;
624 #endif
625 #ifdef MANUAL_OFFSET
626 offset = MANUAL_OFFSET;
627 #else
628 if (pcmcia_copy_tuple(CISTPL_CONFIG, tuple, 32) < 6)
629 return 0;
631 offset_len = (tuple[2] & 0x3) + 1;
632 offset = 0;
633 while(offset_len--) {
634 offset = (offset << 8) | tuple[4+offset_len];
636 #endif
638 writeb(config, GAYLE_ATTRIBUTE+offset);
640 return 1;