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>
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>
52 /* ---- No user-serviceable parts below ---- */
54 #define NE_BASE (dev->base_addr)
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 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,
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 */
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; /* signal if card already owned */
124 int __init apne_probe(struct net_device *dev)
126 #ifndef MANUAL_CONFIG
133 SET_MODULE_OWNER(dev);
135 if ( !(AMIGAHW_PRESENT(PCMCIA)) )
138 printk("Looking for PCMCIA ethernet card : ");
140 /* check if a card is inserted */
141 if (!(PCMCIA_INSERTED)) {
142 printk("NO PCMCIA card inserted\n");
146 /* disable pcmcia irq for readtuple */
147 pcmcia_disable_irq();
149 #ifndef MANUAL_CONFIG
150 if ((pcmcia_copy_tuple(CISTPL_FUNCID, tuple, 8) < 3) ||
151 (tuple[2] != CISTPL_FUNCID_NETWORK)) {
152 printk("not an ethernet card\n");
157 printk("ethernet PCMCIA card inserted\n");
160 return apne_probe1(dev, IOBASE);
166 static int __init apne_probe1(struct net_device *dev, int ioaddr)
169 unsigned char SA_prom[32];
171 const char *name = NULL;
172 int start_page, stop_page;
173 #ifndef MANUAL_HWADDR0
176 static unsigned version_printed;
178 if (ei_debug && version_printed++ == 0)
181 printk("PCMCIA NE*000 ethercard probe");
183 /* Reset card. Who knows what dain-bramaged state it was left in. */
184 { unsigned long reset_start_time = jiffies;
186 outb(inb(ioaddr + NE_RESET), ioaddr + NE_RESET);
188 while ((inb(ioaddr + NE_EN0_ISR) & ENISR_RESET) == 0)
189 if (jiffies - reset_start_time > 2*HZ/100) {
190 printk(" not found (no reset ack).\n");
194 outb(0xff, ioaddr + NE_EN0_ISR); /* Ack all intr. */
197 #ifndef MANUAL_HWADDR0
199 /* Read the 16 bytes of station address PROM.
200 We must first initialize registers, similar to NS8390_init(eifdev, 0).
201 We can't reliably read the SAPROM address without this.
202 (I learned the hard way!). */
204 struct {unsigned long value, offset; } program_seq[] = {
205 {E8390_NODMA+E8390_PAGE0+E8390_STOP, NE_CMD}, /* Select page 0*/
206 {0x48, NE_EN0_DCFG}, /* Set byte-wide (0x48) access. */
207 {0x00, NE_EN0_RCNTLO}, /* Clear the count regs. */
208 {0x00, NE_EN0_RCNTHI},
209 {0x00, NE_EN0_IMR}, /* Mask completion irq. */
211 {E8390_RXOFF, NE_EN0_RXCR}, /* 0x20 Set to monitor */
212 {E8390_TXOFF, NE_EN0_TXCR}, /* 0x02 and loopback mode. */
214 {0x00, NE_EN0_RCNTHI},
215 {0x00, NE_EN0_RSARLO}, /* DMA starting at 0x0000. */
216 {0x00, NE_EN0_RSARHI},
217 {E8390_RREAD+E8390_START, NE_CMD},
219 for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++) {
220 outb(program_seq[i].value, ioaddr + program_seq[i].offset);
224 for(i = 0; i < 32 /*sizeof(SA_prom)*/; i+=2) {
225 SA_prom[i] = inb(ioaddr + NE_DATAPORT);
226 SA_prom[i+1] = inb(ioaddr + NE_DATAPORT);
227 if (SA_prom[i] != SA_prom[i+1])
231 /* At this point, wordlength *only* tells us if the SA_prom is doubled
232 up or not because some broken PCI cards don't respect the byte-wide
233 request in program_seq above, and hence don't have doubled up values.
234 These broken cards would otherwise be detected as an ne1000. */
237 for (i = 0; i < 16; i++)
238 SA_prom[i] = SA_prom[i+i];
240 if (wordlength == 2) {
241 /* We must set the 8390 for word mode. */
242 outb(0x49, ioaddr + NE_EN0_DCFG);
243 start_page = NESM_START_PG;
244 stop_page = NESM_STOP_PG;
246 start_page = NE1SM_START_PG;
247 stop_page = NE1SM_STOP_PG;
250 neX000 = (SA_prom[14] == 0x57 && SA_prom[15] == 0x57);
251 ctron = (SA_prom[0] == 0x00 && SA_prom[1] == 0x00 && SA_prom[2] == 0x1d);
253 /* Set up the rest of the parameters. */
255 name = (wordlength == 2) ? "NE2000" : "NE1000";
257 name = (wordlength == 2) ? "Ctron-8" : "Ctron-16";
259 stop_page = (wordlength == 2) ? 0x40 : 0x20;
261 printk(" not found.\n");
268 /* We must set the 8390 for word mode. */
269 outb(0x49, ioaddr + NE_EN0_DCFG);
270 start_page = NESM_START_PG;
271 stop_page = NESM_STOP_PG;
273 SA_prom[0] = MANUAL_HWADDR0;
274 SA_prom[1] = MANUAL_HWADDR1;
275 SA_prom[2] = MANUAL_HWADDR2;
276 SA_prom[3] = MANUAL_HWADDR3;
277 SA_prom[4] = MANUAL_HWADDR4;
278 SA_prom[5] = MANUAL_HWADDR5;
282 dev->base_addr = ioaddr;
284 /* Install the Interrupt handler */
285 i = request_irq(IRQ_AMIGA_PORTS, apne_interrupt, SA_SHIRQ, dev->name, dev);
288 /* Allocate dev->priv and fill in 8390 specific dev fields. */
289 if (ethdev_init(dev)) {
290 printk (" unable to get memory for dev->priv.\n");
294 for(i = 0; i < ETHER_ADDR_LEN; i++) {
295 printk(" %2.2x", SA_prom[i]);
296 dev->dev_addr[i] = SA_prom[i];
299 printk("\n%s: %s found.\n", dev->name, name);
301 ei_status.name = name;
302 ei_status.tx_start_page = start_page;
303 ei_status.stop_page = stop_page;
304 ei_status.word16 = (wordlength == 2);
306 ei_status.rx_start_page = start_page + TX_PAGES;
308 ei_status.reset_8390 = &apne_reset_8390;
309 ei_status.block_input = &apne_block_input;
310 ei_status.block_output = &apne_block_output;
311 ei_status.get_8390_hdr = &apne_get_8390_hdr;
312 dev->open = &apne_open;
313 dev->stop = &apne_close;
316 pcmcia_ack_int(pcmcia_get_intreq()); /* ack PCMCIA int req */
325 apne_open(struct net_device *dev)
332 apne_close(struct net_device *dev)
335 printk("%s: Shutting down ethercard.\n", dev->name);
340 /* Hard reset the card. This used to pause for the same period that a
341 8390 reset command required, but that shouldn't be necessary. */
343 apne_reset_8390(struct net_device *dev)
345 unsigned long reset_start_time = jiffies;
349 if (ei_debug > 1) printk("resetting the 8390 t=%ld...", jiffies);
351 outb(inb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
354 ei_status.dmaing = 0;
356 /* This check _should_not_ be necessary, omit eventually. */
357 while ((inb(NE_BASE+NE_EN0_ISR) & ENISR_RESET) == 0)
358 if (jiffies - reset_start_time > 2*HZ/100) {
359 printk("%s: ne_reset_8390() did not complete.\n", dev->name);
362 outb(ENISR_RESET, NE_BASE + NE_EN0_ISR); /* Ack intr. */
365 /* Grab the 8390 specific header. Similar to the block_input routine, but
366 we don't need to be concerned with ring wrap as the header will be at
367 the start of a page, so we optimize accordingly. */
370 apne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
373 int nic_base = dev->base_addr;
378 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
379 if (ei_status.dmaing) {
380 printk("%s: DMAing conflict in ne_get_8390_hdr "
381 "[DMAstat:%d][irqlock:%d][intr:%d].\n",
382 dev->name, ei_status.dmaing, ei_status.irqlock, dev->irq);
386 ei_status.dmaing |= 0x01;
387 outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
388 outb(ENISR_RDC, nic_base + NE_EN0_ISR);
389 outb(sizeof(struct e8390_pkt_hdr), nic_base + NE_EN0_RCNTLO);
390 outb(0, nic_base + NE_EN0_RCNTHI);
391 outb(0, nic_base + NE_EN0_RSARLO); /* On page boundary */
392 outb(ring_page, nic_base + NE_EN0_RSARHI);
393 outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
395 if (ei_status.word16) {
397 for(cnt = 0; cnt < (sizeof(struct e8390_pkt_hdr)>>1); cnt++)
398 *ptrs++ = inw(NE_BASE + NE_DATAPORT);
401 for(cnt = 0; cnt < sizeof(struct e8390_pkt_hdr); cnt++)
402 *ptrc++ = inb(NE_BASE + NE_DATAPORT);
405 outb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr. */
407 hdr->count = WORDSWAP(hdr->count);
409 ei_status.dmaing &= ~0x01;
412 /* Block input and output, similar to the Crynwr packet driver. If you
413 are porting to a new ethercard, look at the packet driver source for hints.
414 The NEx000 doesn't share the on-board packet memory -- you have to put
415 the packet out through the "remote DMA" dataport using outb. */
418 apne_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
420 int nic_base = dev->base_addr;
421 char *buf = skb->data;
426 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
427 if (ei_status.dmaing) {
428 printk("%s: DMAing conflict in ne_block_input "
429 "[DMAstat:%d][irqlock:%d][intr:%d].\n",
430 dev->name, ei_status.dmaing, ei_status.irqlock, dev->irq);
433 ei_status.dmaing |= 0x01;
434 outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
435 outb(ENISR_RDC, nic_base + NE_EN0_ISR);
436 outb(count & 0xff, nic_base + NE_EN0_RCNTLO);
437 outb(count >> 8, nic_base + NE_EN0_RCNTHI);
438 outb(ring_offset & 0xff, nic_base + NE_EN0_RSARLO);
439 outb(ring_offset >> 8, nic_base + NE_EN0_RSARHI);
440 outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
441 if (ei_status.word16) {
443 for (cnt = 0; cnt < (count>>1); cnt++)
444 *ptrs++ = inw(NE_BASE + NE_DATAPORT);
446 buf[count-1] = inb(NE_BASE + NE_DATAPORT);
450 for (cnt = 0; cnt < count; cnt++)
451 *ptrc++ = inb(NE_BASE + NE_DATAPORT);
454 outb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr. */
455 ei_status.dmaing &= ~0x01;
459 apne_block_output(struct net_device *dev, int count,
460 const unsigned char *buf, const int start_page)
462 int nic_base = NE_BASE;
463 unsigned long dma_start;
468 /* Round the count up for word writes. Do we need to do this?
469 What effect will an odd byte count have on the 8390?
470 I should check someday. */
471 if (ei_status.word16 && (count & 0x01))
474 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
475 if (ei_status.dmaing) {
476 printk("%s: DMAing conflict in ne_block_output."
477 "[DMAstat:%d][irqlock:%d][intr:%d]\n",
478 dev->name, ei_status.dmaing, ei_status.irqlock, dev->irq);
481 ei_status.dmaing |= 0x01;
482 /* We should already be in page 0, but to be safe... */
483 outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
485 outb(ENISR_RDC, nic_base + NE_EN0_ISR);
487 /* Now the normal output. */
488 outb(count & 0xff, nic_base + NE_EN0_RCNTLO);
489 outb(count >> 8, nic_base + NE_EN0_RCNTHI);
490 outb(0x00, nic_base + NE_EN0_RSARLO);
491 outb(start_page, nic_base + NE_EN0_RSARHI);
493 outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
494 if (ei_status.word16) {
496 for (cnt = 0; cnt < count>>1; cnt++)
497 outw(*ptrs++, NE_BASE+NE_DATAPORT);
500 for (cnt = 0; cnt < count; cnt++)
501 outb(*ptrc++, NE_BASE + NE_DATAPORT);
506 while ((inb(NE_BASE + NE_EN0_ISR) & ENISR_RDC) == 0)
507 if (jiffies - dma_start > 2*HZ/100) { /* 20ms */
508 printk("%s: timeout waiting for Tx RDC.\n", dev->name);
509 apne_reset_8390(dev);
514 outb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr. */
515 ei_status.dmaing &= ~0x01;
519 static void apne_interrupt(int irq, void *dev_id, struct pt_regs *regs)
521 unsigned char pcmcia_intreq;
523 if (!(gayle.inten & GAYLE_IRQ_IRQ))
526 pcmcia_intreq = pcmcia_get_intreq();
528 if (!(pcmcia_intreq & GAYLE_IRQ_IRQ)) {
529 pcmcia_ack_int(pcmcia_intreq);
533 printk("pcmcia intreq = %x\n", pcmcia_intreq);
534 pcmcia_disable_irq(); /* to get rid of the sti() within ei_interrupt */
535 ei_interrupt(irq, dev_id, regs);
536 pcmcia_ack_int(pcmcia_get_intreq());
541 static struct net_device apne_dev;
543 int init_module(void)
547 apne_dev.init = apne_probe;
548 if ((err = register_netdev(&apne_dev))) {
550 printk("No PCMCIA NEx000 ethernet card found.\n");
556 void cleanup_module(void)
558 unregister_netdev(&apne_dev);
560 pcmcia_disable_irq();
562 free_irq(IRQ_AMIGA_PORTS, &apne_dev);
571 static int init_pcmcia(void)
574 #ifndef MANUAL_CONFIG
581 pcmcia_program_voltage(PCMCIA_0V);
582 pcmcia_access_speed(PCMCIA_SPEED_250NS);
583 pcmcia_write_enable();
586 config = MANUAL_CONFIG;
588 /* get and write config byte to enable IO port */
590 if (pcmcia_copy_tuple(CISTPL_CFTABLE_ENTRY, tuple, 32) < 3)
593 config = tuple[2] & 0x3f;
596 offset = MANUAL_OFFSET;
598 if (pcmcia_copy_tuple(CISTPL_CONFIG, tuple, 32) < 6)
601 offset_len = (tuple[2] & 0x3) + 1;
603 while(offset_len--) {
604 offset = (offset << 8) | tuple[4+offset_len];
608 out_8(GAYLE_ATTRIBUTE+offset, config);
613 MODULE_LICENSE("GPL");