ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / zorro8390.c
1 /*
2  *  Amiga Linux/m68k and Linux/PPC Zorro NS8390 Ethernet Driver
3  *
4  *  (C) Copyright 1998-2000 by some Elitist 680x0 Users(TM)
5  *
6  *  ---------------------------------------------------------------------------
7  *
8  *  This program is based on all the other NE2000 drivers for Linux
9  *
10  *  ---------------------------------------------------------------------------
11  *
12  *  This file is subject to the terms and conditions of the GNU General Public
13  *  License.  See the file COPYING in the main directory of the Linux
14  *  distribution for more details.
15  *
16  *  ---------------------------------------------------------------------------
17  *
18  *  The Ariadne II and X-Surf are Zorro-II boards containing Realtek RTL8019AS
19  *  Ethernet Controllers.
20  */
21
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/errno.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/zorro.h>
30
31 #include <asm/system.h>
32 #include <asm/irq.h>
33 #include <asm/amigaints.h>
34 #include <asm/amigahw.h>
35
36 #include "8390.h"
37
38
39 #define NE_BASE         (dev->base_addr)
40 #define NE_CMD          (0x00*2)
41 #define NE_DATAPORT     (0x10*2)        /* NatSemi-defined port window offset. */
42 #define NE_RESET        (0x1f*2)        /* Issue a read to reset, a write to clear. */
43 #define NE_IO_EXTENT    (0x20*2)
44
45 #define NE_EN0_ISR      (0x07*2)
46 #define NE_EN0_DCFG     (0x0e*2)
47
48 #define NE_EN0_RSARLO   (0x08*2)
49 #define NE_EN0_RSARHI   (0x09*2)
50 #define NE_EN0_RCNTLO   (0x0a*2)
51 #define NE_EN0_RXCR     (0x0c*2)
52 #define NE_EN0_TXCR     (0x0d*2)
53 #define NE_EN0_RCNTHI   (0x0b*2)
54 #define NE_EN0_IMR      (0x0f*2)
55
56 #define NESM_START_PG   0x40    /* First page of TX buffer */
57 #define NESM_STOP_PG    0x80    /* Last page +1 of RX ring */
58
59
60 #define WORDSWAP(a)     ((((a)>>8)&0xff) | ((a)<<8))
61
62
63 static struct card_info {
64     zorro_id id;
65     const char *name;
66     unsigned int offset;
67 } cards[] __devinitdata = {
68     { ZORRO_PROD_VILLAGE_TRONIC_ARIADNE2, "Ariadne II", 0x0600 },
69     { ZORRO_PROD_INDIVIDUAL_COMPUTERS_X_SURF, "X-Surf", 0x8600 },
70 };
71
72 static int __devinit zorro8390_init_one(struct zorro_dev *z,
73                                         const struct zorro_device_id *ent);
74 static int __devinit zorro8390_init(struct net_device *dev,
75                                     unsigned long board, const char *name,
76                                     unsigned long ioaddr);
77 static int zorro8390_open(struct net_device *dev);
78 static int zorro8390_close(struct net_device *dev);
79 static void zorro8390_reset_8390(struct net_device *dev);
80 static void zorro8390_get_8390_hdr(struct net_device *dev,
81                                    struct e8390_pkt_hdr *hdr, int ring_page);
82 static void zorro8390_block_input(struct net_device *dev, int count,
83                                   struct sk_buff *skb, int ring_offset);
84 static void zorro8390_block_output(struct net_device *dev, const int count,
85                                    const unsigned char *buf,
86                                    const int start_page);
87 static void __devexit zorro8390_remove_one(struct zorro_dev *z);
88
89 static struct zorro_device_id zorro8390_zorro_tbl[] __devinitdata = {
90     { ZORRO_PROD_VILLAGE_TRONIC_ARIADNE2, },
91     { ZORRO_PROD_INDIVIDUAL_COMPUTERS_X_SURF, },
92     { 0 }
93 };
94
95 static struct zorro_driver zorro8390_driver = {
96     .name       = "zorro8390",
97     .id_table   = zorro8390_zorro_tbl,
98     .probe      = zorro8390_init_one,
99     .remove     = __devexit_p(zorro8390_remove_one),
100 };
101
102 static int __devinit zorro8390_init_one(struct zorro_dev *z,
103                                         const struct zorro_device_id *ent)
104 {
105     struct net_device *dev;
106     unsigned long board, ioaddr;
107     int err, i;
108
109     for (i = ARRAY_SIZE(cards)-1; i >= 0; i--)
110         if (z->id == cards[i].id)
111             break;
112     board = z->resource.start;
113     ioaddr = board+cards[i].offset;
114     dev = alloc_ei_netdev();
115     if (!dev)
116         return -ENOMEM;
117     SET_MODULE_OWNER(dev);
118     if (!request_mem_region(ioaddr, NE_IO_EXTENT*2, dev->name)) {
119         free_netdev(dev);
120         return -EBUSY;
121     }
122     if ((err = zorro8390_init(dev, board, cards[i].name,
123                               ZTWO_VADDR(ioaddr)))) {
124         release_mem_region(ioaddr, NE_IO_EXTENT*2);
125         free_netdev(dev);
126         return err;
127     }
128     zorro_set_drvdata(z, dev);
129     return 0;
130 }
131
132 static int __devinit zorro8390_init(struct net_device *dev,
133                                     unsigned long board, const char *name,
134                                     unsigned long ioaddr)
135 {
136     int i;
137     int err;
138     unsigned char SA_prom[32];
139     int start_page, stop_page;
140     static u32 zorro8390_offsets[16] = {
141         0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e,
142         0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e,
143     };
144
145     /* Reset card. Who knows what dain-bramaged state it was left in. */
146     {
147         unsigned long reset_start_time = jiffies;
148
149         z_writeb(z_readb(ioaddr + NE_RESET), ioaddr + NE_RESET);
150
151         while ((z_readb(ioaddr + NE_EN0_ISR) & ENISR_RESET) == 0)
152             if (jiffies - reset_start_time > 2*HZ/100) {
153                 printk(KERN_WARNING " not found (no reset ack).\n");
154                 return -ENODEV;
155             }
156
157         z_writeb(0xff, ioaddr + NE_EN0_ISR);            /* Ack all intr. */
158     }
159
160     /* Read the 16 bytes of station address PROM.
161        We must first initialize registers, similar to NS8390_init(eifdev, 0).
162        We can't reliably read the SAPROM address without this.
163        (I learned the hard way!). */
164     {
165         struct {
166             u32 value;
167             u32 offset;
168         } program_seq[] = {
169             {E8390_NODMA+E8390_PAGE0+E8390_STOP, NE_CMD}, /* Select page 0*/
170             {0x48,      NE_EN0_DCFG},   /* Set byte-wide (0x48) access. */
171             {0x00,      NE_EN0_RCNTLO}, /* Clear the count regs. */
172             {0x00,      NE_EN0_RCNTHI},
173             {0x00,      NE_EN0_IMR},    /* Mask completion irq. */
174             {0xFF,      NE_EN0_ISR},
175             {E8390_RXOFF, NE_EN0_RXCR}, /* 0x20  Set to monitor */
176             {E8390_TXOFF, NE_EN0_TXCR}, /* 0x02  and loopback mode. */
177             {32,        NE_EN0_RCNTLO},
178             {0x00,      NE_EN0_RCNTHI},
179             {0x00,      NE_EN0_RSARLO}, /* DMA starting at 0x0000. */
180             {0x00,      NE_EN0_RSARHI},
181             {E8390_RREAD+E8390_START, NE_CMD},
182         };
183         for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++) {
184             z_writeb(program_seq[i].value, ioaddr + program_seq[i].offset);
185         }
186     }
187     for (i = 0; i < 16; i++) {
188         SA_prom[i] = z_readb(ioaddr + NE_DATAPORT);
189         (void)z_readb(ioaddr + NE_DATAPORT);
190     }
191
192     /* We must set the 8390 for word mode. */
193     z_writeb(0x49, ioaddr + NE_EN0_DCFG);
194     start_page = NESM_START_PG;
195     stop_page = NESM_STOP_PG;
196
197     dev->base_addr = ioaddr;
198     dev->irq = IRQ_AMIGA_PORTS;
199
200     /* Install the Interrupt handler */
201     i = request_irq(IRQ_AMIGA_PORTS, ei_interrupt, SA_SHIRQ, dev->name, dev);
202     if (i) return i;
203
204     for(i = 0; i < ETHER_ADDR_LEN; i++) {
205 #ifdef DEBUG
206         printk(" %2.2x", SA_prom[i]);
207 #endif
208         dev->dev_addr[i] = SA_prom[i];
209     }
210
211     ei_status.name = name;
212     ei_status.tx_start_page = start_page;
213     ei_status.stop_page = stop_page;
214     ei_status.word16 = 1;
215
216     ei_status.rx_start_page = start_page + TX_PAGES;
217
218     ei_status.reset_8390 = &zorro8390_reset_8390;
219     ei_status.block_input = &zorro8390_block_input;
220     ei_status.block_output = &zorro8390_block_output;
221     ei_status.get_8390_hdr = &zorro8390_get_8390_hdr;
222     ei_status.reg_offset = zorro8390_offsets;
223     dev->open = &zorro8390_open;
224     dev->stop = &zorro8390_close;
225 #ifdef CONFIG_NET_POLL_CONTROLLER
226     dev->poll_controller = ei_poll;
227 #endif
228
229     NS8390_init(dev, 0);
230     err = register_netdev(dev);
231     if (err) {
232         free_irq(IRQ_AMIGA_PORTS, dev);
233         return err;
234     }
235
236     printk(KERN_INFO "%s: %s at 0x%08lx, Ethernet Address "
237            "%02x:%02x:%02x:%02x:%02x:%02x\n", dev->name, name, board,
238            dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
239            dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
240
241     return 0;
242 }
243
244 static int zorro8390_open(struct net_device *dev)
245 {
246     ei_open(dev);
247     return 0;
248 }
249
250 static int zorro8390_close(struct net_device *dev)
251 {
252     if (ei_debug > 1)
253         printk(KERN_DEBUG "%s: Shutting down ethercard.\n", dev->name);
254     ei_close(dev);
255     return 0;
256 }
257
258 /* Hard reset the card.  This used to pause for the same period that a
259    8390 reset command required, but that shouldn't be necessary. */
260 static void zorro8390_reset_8390(struct net_device *dev)
261 {
262     unsigned long reset_start_time = jiffies;
263
264     if (ei_debug > 1)
265         printk(KERN_DEBUG "resetting the 8390 t=%ld...\n", jiffies);
266
267     z_writeb(z_readb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
268
269     ei_status.txing = 0;
270     ei_status.dmaing = 0;
271
272     /* This check _should_not_ be necessary, omit eventually. */
273     while ((z_readb(NE_BASE+NE_EN0_ISR) & ENISR_RESET) == 0)
274         if (jiffies - reset_start_time > 2*HZ/100) {
275             printk(KERN_WARNING "%s: ne_reset_8390() did not complete.\n",
276                    dev->name);
277             break;
278         }
279     z_writeb(ENISR_RESET, NE_BASE + NE_EN0_ISR);        /* Ack intr. */
280 }
281
282 /* Grab the 8390 specific header. Similar to the block_input routine, but
283    we don't need to be concerned with ring wrap as the header will be at
284    the start of a page, so we optimize accordingly. */
285
286 static void zorro8390_get_8390_hdr(struct net_device *dev,
287                                    struct e8390_pkt_hdr *hdr, int ring_page)
288 {
289     int nic_base = dev->base_addr;
290     int cnt;
291     short *ptrs;
292
293     /* This *shouldn't* happen. If it does, it's the last thing you'll see */
294     if (ei_status.dmaing) {
295         printk(KERN_ERR "%s: DMAing conflict in ne_get_8390_hdr "
296            "[DMAstat:%d][irqlock:%d].\n", dev->name, ei_status.dmaing,
297            ei_status.irqlock);
298         return;
299     }
300
301     ei_status.dmaing |= 0x01;
302     z_writeb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
303     z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR);
304     z_writeb(sizeof(struct e8390_pkt_hdr), nic_base + NE_EN0_RCNTLO);
305     z_writeb(0, nic_base + NE_EN0_RCNTHI);
306     z_writeb(0, nic_base + NE_EN0_RSARLO);              /* On page boundary */
307     z_writeb(ring_page, nic_base + NE_EN0_RSARHI);
308     z_writeb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
309
310     ptrs = (short*)hdr;
311     for (cnt = 0; cnt < (sizeof(struct e8390_pkt_hdr)>>1); cnt++)
312         *ptrs++ = z_readw(NE_BASE + NE_DATAPORT);
313
314     z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr. */
315
316     hdr->count = WORDSWAP(hdr->count);
317
318     ei_status.dmaing &= ~0x01;
319 }
320
321 /* Block input and output, similar to the Crynwr packet driver.  If you
322    are porting to a new ethercard, look at the packet driver source for hints.
323    The NEx000 doesn't share the on-board packet memory -- you have to put
324    the packet out through the "remote DMA" dataport using z_writeb. */
325
326 static void zorro8390_block_input(struct net_device *dev, int count,
327                                  struct sk_buff *skb, int ring_offset)
328 {
329     int nic_base = dev->base_addr;
330     char *buf = skb->data;
331     short *ptrs;
332     int cnt;
333
334     /* This *shouldn't* happen. If it does, it's the last thing you'll see */
335     if (ei_status.dmaing) {
336         printk(KERN_ERR "%s: DMAing conflict in ne_block_input "
337            "[DMAstat:%d][irqlock:%d].\n",
338            dev->name, ei_status.dmaing, ei_status.irqlock);
339         return;
340     }
341     ei_status.dmaing |= 0x01;
342     z_writeb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
343     z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR);
344     z_writeb(count & 0xff, nic_base + NE_EN0_RCNTLO);
345     z_writeb(count >> 8, nic_base + NE_EN0_RCNTHI);
346     z_writeb(ring_offset & 0xff, nic_base + NE_EN0_RSARLO);
347     z_writeb(ring_offset >> 8, nic_base + NE_EN0_RSARHI);
348     z_writeb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
349     ptrs = (short*)buf;
350     for (cnt = 0; cnt < (count>>1); cnt++)
351         *ptrs++ = z_readw(NE_BASE + NE_DATAPORT);
352     if (count & 0x01)
353         buf[count-1] = z_readb(NE_BASE + NE_DATAPORT);
354
355     z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr. */
356     ei_status.dmaing &= ~0x01;
357 }
358
359 static void zorro8390_block_output(struct net_device *dev, int count,
360                                    const unsigned char *buf,
361                                    const int start_page)
362 {
363     int nic_base = NE_BASE;
364     unsigned long dma_start;
365     short *ptrs;
366     int cnt;
367
368     /* Round the count up for word writes.  Do we need to do this?
369        What effect will an odd byte count have on the 8390?
370        I should check someday. */
371     if (count & 0x01)
372         count++;
373
374     /* This *shouldn't* happen. If it does, it's the last thing you'll see */
375     if (ei_status.dmaing) {
376         printk(KERN_ERR "%s: DMAing conflict in ne_block_output."
377            "[DMAstat:%d][irqlock:%d]\n", dev->name, ei_status.dmaing,
378            ei_status.irqlock);
379         return;
380     }
381     ei_status.dmaing |= 0x01;
382     /* We should already be in page 0, but to be safe... */
383     z_writeb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
384
385     z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR);
386
387    /* Now the normal output. */
388     z_writeb(count & 0xff, nic_base + NE_EN0_RCNTLO);
389     z_writeb(count >> 8,   nic_base + NE_EN0_RCNTHI);
390     z_writeb(0x00, nic_base + NE_EN0_RSARLO);
391     z_writeb(start_page, nic_base + NE_EN0_RSARHI);
392
393     z_writeb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
394     ptrs = (short*)buf;
395     for (cnt = 0; cnt < count>>1; cnt++)
396         z_writew(*ptrs++, NE_BASE+NE_DATAPORT);
397
398     dma_start = jiffies;
399
400     while ((z_readb(NE_BASE + NE_EN0_ISR) & ENISR_RDC) == 0)
401         if (jiffies - dma_start > 2*HZ/100) {           /* 20ms */
402                 printk(KERN_ERR "%s: timeout waiting for Tx RDC.\n",
403                        dev->name);
404                 zorro8390_reset_8390(dev);
405                 NS8390_init(dev,1);
406                 break;
407         }
408
409     z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr. */
410     ei_status.dmaing &= ~0x01;
411     return;
412 }
413
414 static void __devexit zorro8390_remove_one(struct zorro_dev *z)
415 {
416     struct net_device *dev = zorro_get_drvdata(z);
417
418     unregister_netdev(dev);
419     free_irq(IRQ_AMIGA_PORTS, dev);
420     release_mem_region(ZTWO_PADDR(dev->base_addr), NE_IO_EXTENT*2);
421     free_netdev(dev);
422 }
423
424 static int __init zorro8390_init_module(void)
425 {
426     return zorro_module_init(&zorro8390_driver);
427 }
428
429 static void __exit zorro8390_cleanup_module(void)
430 {
431     zorro_unregister_driver(&zorro8390_driver);
432 }
433
434 module_init(zorro8390_init_module);
435 module_exit(zorro8390_cleanup_module);
436
437 MODULE_LICENSE("GPL");