Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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