File: /usr/src/linux/drivers/net/apne.c

1     /*
2      * Amiga Linux/68k 8390 based PCMCIA Ethernet Driver for the Amiga 1200
3      *
4      * (C) Copyright 1997 Alain Malek
5      *                    (Alain.Malek@cryogen.com)
6      *
7      * ----------------------------------------------------------------------------
8      *
9      * This program is based on
10      *
11      * ne.c:       A general non-shared-memory NS8390 ethernet driver for linux
12      *             Written 1992-94 by Donald Becker.
13      *
14      * 8390.c:     A general NS8390 ethernet driver core for linux.
15      *             Written 1992-94 by Donald Becker.
16      *
17      * cnetdevice: A Sana-II ethernet driver for AmigaOS
18      *             Written by Bruce Abbott (bhabbott@inhb.co.nz)
19      *
20      * ----------------------------------------------------------------------------
21      *
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.
25      *
26      * ----------------------------------------------------------------------------
27      *
28      */
29     
30     
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>
40     
41     #include <linux/netdevice.h>
42     #include <linux/etherdevice.h>
43     
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>
49     
50     #include "8390.h"
51     
52     /* ---- No user-serviceable parts below ---- */
53     
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
59     
60     #define NE_EN0_ISR		0x07+GAYLE_ODD
61     #define NE_EN0_DCFG		0x0e
62     
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
70     
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 */
75     
76     
77     int apne_probe(struct net_device *dev);
78     static int apne_probe1(struct net_device *dev, int ioaddr);
79     
80     static int apne_open(struct net_device *dev);
81     static int apne_close(struct net_device *dev);
82     
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);
91     
92     static int init_pcmcia(void);
93     
94     /* IO base address used for nic */
95     
96     #define IOBASE 0x300
97     
98     /*
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)
102     */
103     
104     /*
105     #define MANUAL_CONFIG 0x20
106     #define MANUAL_OFFSET 0x3f8
107     
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
114     */
115     
116     #define WORDSWAP(a) ( (((a)>>8)&0xff) | ((a)<<8) )
117     
118     
119     static const char version[] =
120         "apne.c:v1.1 7/10/98 Alain Malek (Alain.Malek@cryogen.ch)\n";
121     
122     static int apne_owned;	/* signal if card already owned */
123     
124     int __init apne_probe(struct net_device *dev)
125     {
126     #ifndef MANUAL_CONFIG
127     	char tuple[8];
128     #endif
129     
130     	if (apne_owned)
131     		return -ENODEV;
132     
133     	SET_MODULE_OWNER(dev);
134     
135     	if ( !(AMIGAHW_PRESENT(PCMCIA)) )
136     		return (-ENODEV);
137                                     
138     	printk("Looking for PCMCIA ethernet card : ");
139                                             
140     	/* check if a card is inserted */
141     	if (!(PCMCIA_INSERTED)) {
142     		printk("NO PCMCIA card inserted\n");
143     		return (-ENODEV);
144     	}
145                                                                                                     
146     	/* disable pcmcia irq for readtuple */
147     	pcmcia_disable_irq();
148     
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");
153     		return (-ENODEV);
154     	}
155     #endif
156     
157     	printk("ethernet PCMCIA card inserted\n");
158     
159     	if (init_pcmcia())
160     		return apne_probe1(dev, IOBASE+GAYLE_IO);
161     	else
162     		return (-ENODEV);
163     
164     }
165     
166     static int __init apne_probe1(struct net_device *dev, int ioaddr)
167     {
168         int i;
169         unsigned char SA_prom[32];
170         int wordlength = 2;
171         const char *name = NULL;
172         int start_page, stop_page;
173     #ifndef MANUAL_HWADDR0
174         int neX000, ctron;
175     #endif
176         static unsigned version_printed;
177         static u32 pcmcia_offsets[16]={
178                     0,   1+GAYLE_ODD,   2,   3+GAYLE_ODD,
179                     4,   5+GAYLE_ODD,   6,   7+GAYLE_ODD,
180                     8,   9+GAYLE_ODD, 0xa, 0xb+GAYLE_ODD,
181                   0xc, 0xd+GAYLE_ODD, 0xe, 0xf+GAYLE_ODD };
182     
183         if (ei_debug  &&  version_printed++ == 0)
184     	printk(version);
185     
186         printk("PCMCIA NE*000 ethercard probe");
187     
188         /* Reset card. Who knows what dain-bramaged state it was left in. */
189         {	unsigned long reset_start_time = jiffies;
190     
191     	writeb(readb(ioaddr + NE_RESET), ioaddr + NE_RESET);
192     
193     	while ((readb(ioaddr + NE_EN0_ISR) & ENISR_RESET) == 0)
194     		if (jiffies - reset_start_time > 2*HZ/100) {
195     			printk(" not found (no reset ack).\n");
196     			return -ENODEV;
197     		}
198     
199     	writeb(0xff, ioaddr + NE_EN0_ISR);		/* Ack all intr. */
200         }
201     
202     #ifndef MANUAL_HWADDR0
203     
204         /* Read the 16 bytes of station address PROM.
205            We must first initialize registers, similar to NS8390_init(eifdev, 0).
206            We can't reliably read the SAPROM address without this.
207            (I learned the hard way!). */
208         {
209     	struct {unsigned long value, offset; } program_seq[] = {
210     	    {E8390_NODMA+E8390_PAGE0+E8390_STOP, NE_CMD}, /* Select page 0*/
211     	    {0x48,	NE_EN0_DCFG},	/* Set byte-wide (0x48) access. */
212     	    {0x00,	NE_EN0_RCNTLO},	/* Clear the count regs. */
213     	    {0x00,	NE_EN0_RCNTHI},
214     	    {0x00,	NE_EN0_IMR},	/* Mask completion irq. */
215     	    {0xFF,	NE_EN0_ISR},
216     	    {E8390_RXOFF, NE_EN0_RXCR},	/* 0x20  Set to monitor */
217     	    {E8390_TXOFF, NE_EN0_TXCR},	/* 0x02  and loopback mode. */
218     	    {32,	NE_EN0_RCNTLO},
219     	    {0x00,	NE_EN0_RCNTHI},
220     	    {0x00,	NE_EN0_RSARLO},	/* DMA starting at 0x0000. */
221     	    {0x00,	NE_EN0_RSARHI},
222     	    {E8390_RREAD+E8390_START, NE_CMD},
223     	};
224     	for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++) {
225     	    writeb(program_seq[i].value, ioaddr + program_seq[i].offset);
226     	}
227     
228         }
229         for(i = 0; i < 32 /*sizeof(SA_prom)*/; i+=2) {
230     	SA_prom[i] = readb(ioaddr + NE_DATAPORT);
231     	SA_prom[i+1] = readb(ioaddr + NE_DATAPORT);
232     	if (SA_prom[i] != SA_prom[i+1])
233     	    wordlength = 1;
234         }
235     
236         /*	At this point, wordlength *only* tells us if the SA_prom is doubled
237     	up or not because some broken PCI cards don't respect the byte-wide
238     	request in program_seq above, and hence don't have doubled up values. 
239     	These broken cards would otherwise be detected as an ne1000.  */
240     
241         if (wordlength == 2)
242     	for (i = 0; i < 16; i++)
243     		SA_prom[i] = SA_prom[i+i];
244         
245         if (wordlength == 2) {
246     	/* We must set the 8390 for word mode. */
247     	writeb(0x49, ioaddr + NE_EN0_DCFG);
248     	start_page = NESM_START_PG;
249     	stop_page = NESM_STOP_PG;
250         } else {
251     	start_page = NE1SM_START_PG;
252     	stop_page = NE1SM_STOP_PG;
253         }
254     
255         neX000 = (SA_prom[14] == 0x57  &&  SA_prom[15] == 0x57);
256         ctron =  (SA_prom[0] == 0x00 && SA_prom[1] == 0x00 && SA_prom[2] == 0x1d);
257     
258         /* Set up the rest of the parameters. */
259         if (neX000) {
260     	name = (wordlength == 2) ? "NE2000" : "NE1000";
261         } else if (ctron) {
262     	name = (wordlength == 2) ? "Ctron-8" : "Ctron-16";
263     	start_page = 0x01;
264     	stop_page = (wordlength == 2) ? 0x40 : 0x20;
265         } else {
266     	printk(" not found.\n");
267     	return -ENXIO;
268     
269         }
270     
271     #else
272         wordlength = 2;
273         /* We must set the 8390 for word mode. */
274         writeb(0x49, ioaddr + NE_EN0_DCFG);
275         start_page = NESM_START_PG;
276         stop_page = NESM_STOP_PG;
277     
278         SA_prom[0] = MANUAL_HWADDR0;
279         SA_prom[1] = MANUAL_HWADDR1;
280         SA_prom[2] = MANUAL_HWADDR2;
281         SA_prom[3] = MANUAL_HWADDR3;
282         SA_prom[4] = MANUAL_HWADDR4;
283         SA_prom[5] = MANUAL_HWADDR5;
284         name = "NE2000";
285     #endif
286     
287         dev->base_addr = ioaddr;
288     
289         /* Install the Interrupt handler */
290         i = request_irq(IRQ_AMIGA_PORTS, apne_interrupt, SA_SHIRQ, dev->name, dev);
291         if (i) return i;
292     
293         /* Allocate dev->priv and fill in 8390 specific dev fields. */
294         if (ethdev_init(dev)) {
295     	printk (" unable to get memory for dev->priv.\n");
296     	return -ENOMEM;
297         }
298     
299         for(i = 0; i < ETHER_ADDR_LEN; i++) {
300     	printk(" %2.2x", SA_prom[i]);
301     	dev->dev_addr[i] = SA_prom[i];
302         }
303     
304         printk("\n%s: %s found.\n", dev->name, name);
305     
306         ei_status.name = name;
307         ei_status.tx_start_page = start_page;
308         ei_status.stop_page = stop_page;
309         ei_status.word16 = (wordlength == 2);
310     
311         ei_status.rx_start_page = start_page + TX_PAGES;
312     
313         ei_status.reset_8390 = &apne_reset_8390;
314         ei_status.block_input = &apne_block_input;
315         ei_status.block_output = &apne_block_output;
316         ei_status.get_8390_hdr = &apne_get_8390_hdr;
317         ei_status.reg_offset = pcmcia_offsets;
318         dev->open = &apne_open;
319         dev->stop = &apne_close;
320         NS8390_init(dev, 0);
321     
322         pcmcia_ack_int(pcmcia_get_intreq());		/* ack PCMCIA int req */
323         pcmcia_enable_irq();
324     
325         apne_owned = 1;
326     
327         return 0;
328     }
329     
330     static int
331     apne_open(struct net_device *dev)
332     {
333         ei_open(dev);
334         return 0;
335     }
336     
337     static int
338     apne_close(struct net_device *dev)
339     {
340         if (ei_debug > 1)
341     	printk("%s: Shutting down ethercard.\n", dev->name);
342         ei_close(dev);
343         return 0;
344     }
345     
346     /* Hard reset the card.  This used to pause for the same period that a
347        8390 reset command required, but that shouldn't be necessary. */
348     static void
349     apne_reset_8390(struct net_device *dev)
350     {
351         unsigned long reset_start_time = jiffies;
352     
353         init_pcmcia();
354     
355         if (ei_debug > 1) printk("resetting the 8390 t=%ld...", jiffies);
356     
357         writeb(readb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
358     
359         ei_status.txing = 0;
360         ei_status.dmaing = 0;
361     
362         /* This check _should_not_ be necessary, omit eventually. */
363         while ((readb(NE_BASE+NE_EN0_ISR) & ENISR_RESET) == 0)
364     	if (jiffies - reset_start_time > 2*HZ/100) {
365     	    printk("%s: ne_reset_8390() did not complete.\n", dev->name);
366     	    break;
367     	}
368         writeb(ENISR_RESET, NE_BASE + NE_EN0_ISR);	/* Ack intr. */
369     }
370     
371     /* Grab the 8390 specific header. Similar to the block_input routine, but
372        we don't need to be concerned with ring wrap as the header will be at
373        the start of a page, so we optimize accordingly. */
374     
375     static void
376     apne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
377     {
378     
379         int nic_base = dev->base_addr;
380         int cnt;
381         char *ptrc;
382         short *ptrs;
383     
384         /* This *shouldn't* happen. If it does, it's the last thing you'll see */
385         if (ei_status.dmaing) {
386     	printk("%s: DMAing conflict in ne_get_8390_hdr "
387     	   "[DMAstat:%d][irqlock:%d][intr:%d].\n",
388     	   dev->name, ei_status.dmaing, ei_status.irqlock, dev->irq);
389     	return;
390         }
391     
392         ei_status.dmaing |= 0x01;
393         writeb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
394         writeb(ENISR_RDC, nic_base + NE_EN0_ISR);
395         writeb(sizeof(struct e8390_pkt_hdr), nic_base + NE_EN0_RCNTLO);
396         writeb(0, nic_base + NE_EN0_RCNTHI);
397         writeb(0, nic_base + NE_EN0_RSARLO);		/* On page boundary */
398         writeb(ring_page, nic_base + NE_EN0_RSARHI);
399         writeb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
400     
401         if (ei_status.word16) {
402             ptrs = (short*)hdr;
403             for(cnt = 0; cnt < (sizeof(struct e8390_pkt_hdr)>>1); cnt++)
404                 *ptrs++ = readw(NE_BASE + NE_DATAPORT);
405         } else {
406             ptrc = (char*)hdr;
407             for(cnt = 0; cnt < sizeof(struct e8390_pkt_hdr); cnt++)
408                 *ptrc++ = readb(NE_BASE + NE_DATAPORT);
409         }
410     
411         writeb(ENISR_RDC, nic_base + NE_EN0_ISR);	/* Ack intr. */
412     
413         hdr->count = WORDSWAP(hdr->count);
414     
415         ei_status.dmaing &= ~0x01;
416     }
417     
418     /* Block input and output, similar to the Crynwr packet driver.  If you
419        are porting to a new ethercard, look at the packet driver source for hints.
420        The NEx000 doesn't share the on-board packet memory -- you have to put
421        the packet out through the "remote DMA" dataport using writeb. */
422     
423     static void
424     apne_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
425     {
426         int nic_base = dev->base_addr;
427         char *buf = skb->data;
428         char *ptrc;
429         short *ptrs;
430         int cnt;
431     
432         /* This *shouldn't* happen. If it does, it's the last thing you'll see */
433         if (ei_status.dmaing) {
434     	printk("%s: DMAing conflict in ne_block_input "
435     	   "[DMAstat:%d][irqlock:%d][intr:%d].\n",
436     	   dev->name, ei_status.dmaing, ei_status.irqlock, dev->irq);
437     	return;
438         }
439         ei_status.dmaing |= 0x01;
440         writeb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
441         writeb(ENISR_RDC, nic_base + NE_EN0_ISR);
442         writeb(count & 0xff, nic_base + NE_EN0_RCNTLO);
443         writeb(count >> 8, nic_base + NE_EN0_RCNTHI);
444         writeb(ring_offset & 0xff, nic_base + NE_EN0_RSARLO);
445         writeb(ring_offset >> 8, nic_base + NE_EN0_RSARHI);
446         writeb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
447         if (ei_status.word16) {
448           ptrs = (short*)buf;
449           for (cnt = 0; cnt < (count>>1); cnt++)
450             *ptrs++ = readw(NE_BASE + NE_DATAPORT);
451           if (count & 0x01) {
452     	buf[count-1] = readb(NE_BASE + NE_DATAPORT);
453           }
454         } else {
455           ptrc = (char*)buf;
456           for (cnt = 0; cnt < count; cnt++)
457             *ptrc++ = readb(NE_BASE + NE_DATAPORT);
458         }
459     
460         writeb(ENISR_RDC, nic_base + NE_EN0_ISR);	/* Ack intr. */
461         ei_status.dmaing &= ~0x01;
462     }
463     
464     static void
465     apne_block_output(struct net_device *dev, int count,
466     		const unsigned char *buf, const int start_page)
467     {
468         int nic_base = NE_BASE;
469         unsigned long dma_start;
470         char *ptrc;
471         short *ptrs;
472         int cnt;
473     
474         /* Round the count up for word writes.  Do we need to do this?
475            What effect will an odd byte count have on the 8390?
476            I should check someday. */
477         if (ei_status.word16 && (count & 0x01))
478           count++;
479     
480         /* This *shouldn't* happen. If it does, it's the last thing you'll see */
481         if (ei_status.dmaing) {
482     	printk("%s: DMAing conflict in ne_block_output."
483     	   "[DMAstat:%d][irqlock:%d][intr:%d]\n",
484     	   dev->name, ei_status.dmaing, ei_status.irqlock, dev->irq);
485     	return;
486         }
487         ei_status.dmaing |= 0x01;
488         /* We should already be in page 0, but to be safe... */
489         writeb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
490     
491         writeb(ENISR_RDC, nic_base + NE_EN0_ISR);
492     
493        /* Now the normal output. */
494         writeb(count & 0xff, nic_base + NE_EN0_RCNTLO);
495         writeb(count >> 8,   nic_base + NE_EN0_RCNTHI);
496         writeb(0x00, nic_base + NE_EN0_RSARLO);
497         writeb(start_page, nic_base + NE_EN0_RSARHI);
498     
499         writeb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
500         if (ei_status.word16) {
501             ptrs = (short*)buf;
502             for (cnt = 0; cnt < count>>1; cnt++)
503                 writew(*ptrs++, NE_BASE+NE_DATAPORT);
504         } else {
505             ptrc = (char*)buf;
506             for (cnt = 0; cnt < count; cnt++)
507     	    writeb(*ptrc++, NE_BASE + NE_DATAPORT);
508         }
509     
510         dma_start = jiffies;
511     
512         while ((readb(NE_BASE + NE_EN0_ISR) & ENISR_RDC) == 0)
513     	if (jiffies - dma_start > 2*HZ/100) {		/* 20ms */
514     		printk("%s: timeout waiting for Tx RDC.\n", dev->name);
515     		apne_reset_8390(dev);
516     		NS8390_init(dev,1);
517     		break;
518     	}
519     
520         writeb(ENISR_RDC, nic_base + NE_EN0_ISR);	/* Ack intr. */
521         ei_status.dmaing &= ~0x01;
522         return;
523     }
524     
525     static void apne_interrupt(int irq, void *dev_id, struct pt_regs *regs)
526     {
527         unsigned char pcmcia_intreq;
528     
529         if (!(gayle.inten & GAYLE_IRQ_IRQ))
530             return;
531     
532         pcmcia_intreq = pcmcia_get_intreq();
533     
534         if (!(pcmcia_intreq & GAYLE_IRQ_IRQ)) {
535             pcmcia_ack_int(pcmcia_intreq);
536             return;
537         }
538         if (ei_debug > 3)
539             printk("pcmcia intreq = %x\n", pcmcia_intreq);
540         pcmcia_disable_irq();			/* to get rid of the sti() within ei_interrupt */
541         ei_interrupt(irq, dev_id, regs);
542         pcmcia_ack_int(pcmcia_get_intreq());
543         pcmcia_enable_irq();
544     }
545     
546     #ifdef MODULE
547     static struct net_device apne_dev;
548     
549     int init_module(void)
550     {
551     	int err;
552     
553     	apne_dev.init = apne_probe;
554     	if ((err = register_netdev(&apne_dev))) {
555     		if (err == -EIO)
556     			printk("No PCMCIA NEx000 ethernet card found.\n");
557     		return (err);
558     	}
559     	return (0);
560     }
561     
562     void cleanup_module(void)
563     {
564     	unregister_netdev(&apne_dev);
565     
566     	pcmcia_disable_irq();
567     
568     	free_irq(IRQ_AMIGA_PORTS, &apne_dev);
569     
570     	pcmcia_reset();
571     
572     	apne_owned = 0;
573     }
574     
575     #endif
576     
577     static int init_pcmcia(void)
578     {
579     	u_char config;
580     #ifndef MANUAL_CONFIG
581     	u_char tuple[32];
582     	int offset_len;
583     #endif
584     	u_long offset;
585     
586     	pcmcia_reset();
587     	pcmcia_program_voltage(PCMCIA_0V);
588     	pcmcia_access_speed(PCMCIA_SPEED_250NS);
589     	pcmcia_write_enable();
590     
591     #ifdef MANUAL_CONFIG
592     	config = MANUAL_CONFIG;
593     #else
594     	/* get and write config byte to enable IO port */
595     
596     	if (pcmcia_copy_tuple(CISTPL_CFTABLE_ENTRY, tuple, 32) < 3)
597     		return 0;
598     
599     	config = tuple[2] & 0x3f;
600     #endif
601     #ifdef MANUAL_OFFSET
602     	offset = MANUAL_OFFSET;
603     #else
604     	if (pcmcia_copy_tuple(CISTPL_CONFIG, tuple, 32) < 6)
605     		return 0;
606     
607     	offset_len = (tuple[2] & 0x3) + 1;
608     	offset = 0;
609     	while(offset_len--) {
610     		offset = (offset << 8) | tuple[4+offset_len];
611     	}
612     #endif
613     
614     	writeb(config, GAYLE_ATTRIBUTE+offset);
615     
616     	return 1;
617     }
618