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