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