arcnet: Add and remove blank lines
[cascardo/linux.git] / drivers / net / arcnet / com90io.c
1 /*
2  * Linux ARCnet driver - COM90xx chipset (IO-mapped buffers)
3  *
4  * Written 1997 by David Woodhouse.
5  * Written 1994-1999 by Avery Pennarun.
6  * Written 1999-2000 by Martin Mares <mj@ucw.cz>.
7  * Derived from skeleton.c by Donald Becker.
8  *
9  * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
10  *  for sponsoring the further development of this driver.
11  *
12  * **********************
13  *
14  * The original copyright of skeleton.c was as follows:
15  *
16  * skeleton.c Written 1993 by Donald Becker.
17  * Copyright 1993 United States Government as represented by the
18  * Director, National Security Agency.  This software may only be used
19  * and distributed according to the terms of the GNU General Public License as
20  * modified by SRC, incorporated herein by reference.
21  *
22  * **********************
23  *
24  * For more details, see drivers/net/arcnet.c
25  *
26  * **********************
27  */
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
31 #include <linux/ioport.h>
32 #include <linux/delay.h>
33 #include <linux/netdevice.h>
34 #include <linux/bootmem.h>
35 #include <linux/init.h>
36 #include <linux/interrupt.h>
37 #include <asm/io.h>
38 #include <linux/arcdevice.h>
39
40 #define VERSION "arcnet: COM90xx IO-mapped mode support (by David Woodhouse et el.)\n"
41
42 /* Internal function declarations */
43
44 static int com90io_found(struct net_device *dev);
45 static void com90io_command(struct net_device *dev, int command);
46 static int com90io_status(struct net_device *dev);
47 static void com90io_setmask(struct net_device *dev, int mask);
48 static int com90io_reset(struct net_device *dev, int really_reset);
49 static void com90io_copy_to_card(struct net_device *dev, int bufnum, int offset,
50                                  void *buf, int count);
51 static void com90io_copy_from_card(struct net_device *dev, int bufnum, int offset,
52                                    void *buf, int count);
53
54 /* Handy defines for ARCnet specific stuff */
55
56 /* The number of low I/O ports used by the card. */
57 #define ARCNET_TOTAL_SIZE 16
58
59 /* COM 9026 controller chip --> ARCnet register addresses */
60 #define _INTMASK        (ioaddr + 0)    /* writable */
61 #define _STATUS         (ioaddr + 0)    /* readable */
62 #define _COMMAND        (ioaddr + 1)    /* writable, returns random vals on read (?) */
63 #define _RESET          (ioaddr + 8)    /* software reset (on read) */
64 #define _MEMDATA        (ioaddr + 12)   /* Data port for IO-mapped memory */
65 #define _ADDR_HI        (ioaddr + 15)   /* Control registers for said */
66 #define _ADDR_LO        (ioaddr + 14)
67 #define _CONFIG         (ioaddr + 2)    /* Configuration register */
68
69 #undef ASTATUS
70 #undef ACOMMAND
71 #undef AINTMASK
72
73 #define ASTATUS()       inb(_STATUS)
74 #define ACOMMAND(cmd)   outb((cmd), _COMMAND)
75 #define AINTMASK(msk)   outb((msk), _INTMASK)
76 #define SETCONF()       outb((lp->config), _CONFIG)
77
78 /****************************************************************************
79  *                                                                          *
80  * IO-mapped operation routines                                             *
81  *                                                                          *
82  ****************************************************************************/
83
84 #undef ONE_AT_A_TIME_TX
85 #undef ONE_AT_A_TIME_RX
86
87 static u_char get_buffer_byte(struct net_device *dev, unsigned offset)
88 {
89         int ioaddr = dev->base_addr;
90
91         outb(offset >> 8, _ADDR_HI);
92         outb(offset & 0xff, _ADDR_LO);
93
94         return inb(_MEMDATA);
95 }
96
97 #ifdef ONE_AT_A_TIME_TX
98 static void put_buffer_byte(struct net_device *dev, unsigned offset, u_char datum)
99 {
100         int ioaddr = dev->base_addr;
101
102         outb(offset >> 8, _ADDR_HI);
103         outb(offset & 0xff, _ADDR_LO);
104
105         outb(datum, _MEMDATA);
106 }
107
108 #endif
109
110 static void get_whole_buffer(struct net_device *dev, unsigned offset, unsigned length, char *dest)
111 {
112         int ioaddr = dev->base_addr;
113
114         outb((offset >> 8) | AUTOINCflag, _ADDR_HI);
115         outb(offset & 0xff, _ADDR_LO);
116
117         while (length--)
118 #ifdef ONE_AT_A_TIME_RX
119                 *(dest++) = get_buffer_byte(dev, offset++);
120 #else
121                 *(dest++) = inb(_MEMDATA);
122 #endif
123 }
124
125 static void put_whole_buffer(struct net_device *dev, unsigned offset, unsigned length, char *dest)
126 {
127         int ioaddr = dev->base_addr;
128
129         outb((offset >> 8) | AUTOINCflag, _ADDR_HI);
130         outb(offset & 0xff, _ADDR_LO);
131
132         while (length--)
133 #ifdef ONE_AT_A_TIME_TX
134                 put_buffer_byte(dev, offset++, *(dest++));
135 #else
136                 outb(*(dest++), _MEMDATA);
137 #endif
138 }
139
140 /*
141  * We cannot probe for an IO mapped card either, although we can check that
142  * it's where we were told it was, and even autoirq
143  */
144 static int __init com90io_probe(struct net_device *dev)
145 {
146         int ioaddr = dev->base_addr, status;
147         unsigned long airqmask;
148
149         BUGLVL(D_NORMAL) printk(VERSION);
150         BUGLVL(D_NORMAL) printk("E-mail me if you actually test this driver, please!\n");
151
152         if (!ioaddr) {
153                 BUGMSG(D_NORMAL, "No autoprobe for IO mapped cards; you "
154                        "must specify the base address!\n");
155                 return -ENODEV;
156         }
157         if (!request_region(ioaddr, ARCNET_TOTAL_SIZE, "com90io probe")) {
158                 BUGMSG(D_INIT_REASONS, "IO request_region %x-%x failed.\n",
159                        ioaddr, ioaddr + ARCNET_TOTAL_SIZE - 1);
160                 return -ENXIO;
161         }
162         if (ASTATUS() == 0xFF) {
163                 BUGMSG(D_INIT_REASONS, "IO address %x empty\n", ioaddr);
164                 goto err_out;
165         }
166         inb(_RESET);
167         mdelay(RESETtime);
168
169         status = ASTATUS();
170
171         if ((status & 0x9D) != (NORXflag | RECONflag | TXFREEflag | RESETflag)) {
172                 BUGMSG(D_INIT_REASONS, "Status invalid (%Xh).\n", status);
173                 goto err_out;
174         }
175         BUGMSG(D_INIT_REASONS, "Status after reset: %X\n", status);
176
177         ACOMMAND(CFLAGScmd | RESETclear | CONFIGclear);
178
179         BUGMSG(D_INIT_REASONS, "Status after reset acknowledged: %X\n", status);
180
181         status = ASTATUS();
182
183         if (status & RESETflag) {
184                 BUGMSG(D_INIT_REASONS, "Eternal reset (status=%Xh)\n", status);
185                 goto err_out;
186         }
187         outb((0x16 | IOMAPflag) & ~ENABLE16flag, _CONFIG);
188
189         /* Read first loc'n of memory */
190
191         outb(AUTOINCflag, _ADDR_HI);
192         outb(0, _ADDR_LO);
193
194         if ((status = inb(_MEMDATA)) != 0xd1) {
195                 BUGMSG(D_INIT_REASONS, "Signature byte not found"
196                        " (%Xh instead).\n", status);
197                 goto err_out;
198         }
199         if (!dev->irq) {
200                 /*
201                  * if we do this, we're sure to get an IRQ since the
202                  * card has just reset and the NORXflag is on until
203                  * we tell it to start receiving.
204                  */
205
206                 airqmask = probe_irq_on();
207                 outb(NORXflag, _INTMASK);
208                 udelay(1);
209                 outb(0, _INTMASK);
210                 dev->irq = probe_irq_off(airqmask);
211
212                 if ((int)dev->irq <= 0) {
213                         BUGMSG(D_INIT_REASONS, "Autoprobe IRQ failed\n");
214                         goto err_out;
215                 }
216         }
217         release_region(ioaddr, ARCNET_TOTAL_SIZE); /* end of probing */
218         return com90io_found(dev);
219
220 err_out:
221         release_region(ioaddr, ARCNET_TOTAL_SIZE);
222         return -ENODEV;
223 }
224
225 /* Set up the struct net_device associated with this card.  Called after
226  * probing succeeds.
227  */
228 static int __init com90io_found(struct net_device *dev)
229 {
230         struct arcnet_local *lp;
231         int ioaddr = dev->base_addr;
232         int err;
233
234         /* Reserve the irq */
235         if (request_irq(dev->irq, arcnet_interrupt, 0, "arcnet (COM90xx-IO)", dev)) {
236                 BUGMSG(D_NORMAL, "Can't get IRQ %d!\n", dev->irq);
237                 return -ENODEV;
238         }
239         /* Reserve the I/O region */
240         if (!request_region(dev->base_addr, ARCNET_TOTAL_SIZE, "arcnet (COM90xx-IO)")) {
241                 free_irq(dev->irq, dev);
242                 return -EBUSY;
243         }
244
245         lp = netdev_priv(dev);
246         lp->card_name = "COM90xx I/O";
247         lp->hw.command = com90io_command;
248         lp->hw.status = com90io_status;
249         lp->hw.intmask = com90io_setmask;
250         lp->hw.reset = com90io_reset;
251         lp->hw.owner = THIS_MODULE;
252         lp->hw.copy_to_card = com90io_copy_to_card;
253         lp->hw.copy_from_card = com90io_copy_from_card;
254
255         lp->config = (0x16 | IOMAPflag) & ~ENABLE16flag;
256         SETCONF();
257
258         /* get and check the station ID from offset 1 in shmem */
259
260         dev->dev_addr[0] = get_buffer_byte(dev, 1);
261
262         err = register_netdev(dev);
263         if (err) {
264                 outb((inb(_CONFIG) & ~IOMAPflag), _CONFIG);
265                 free_irq(dev->irq, dev);
266                 release_region(dev->base_addr, ARCNET_TOTAL_SIZE);
267                 return err;
268         }
269
270         BUGMSG(D_NORMAL, "COM90IO: station %02Xh found at %03lXh, IRQ %d.\n",
271                dev->dev_addr[0], dev->base_addr, dev->irq);
272
273         return 0;
274 }
275
276 /*
277  * Do a hardware reset on the card, and set up necessary registers.
278  *
279  * This should be called as little as possible, because it disrupts the
280  * token on the network (causes a RECON) and requires a significant delay.
281  *
282  * However, it does make sure the card is in a defined state.
283  */
284 static int com90io_reset(struct net_device *dev, int really_reset)
285 {
286         struct arcnet_local *lp = netdev_priv(dev);
287         short ioaddr = dev->base_addr;
288
289         BUGMSG(D_INIT, "Resetting %s (status=%02Xh)\n", dev->name, ASTATUS());
290
291         if (really_reset) {
292                 /* reset the card */
293                 inb(_RESET);
294                 mdelay(RESETtime);
295         }
296         /* Set the thing to IO-mapped, 8-bit  mode */
297         lp->config = (0x1C | IOMAPflag) & ~ENABLE16flag;
298         SETCONF();
299
300         ACOMMAND(CFLAGScmd | RESETclear);       /* clear flags & end reset */
301         ACOMMAND(CFLAGScmd | CONFIGclear);
302
303         /* verify that the ARCnet signature byte is present */
304         if (get_buffer_byte(dev, 0) != TESTvalue) {
305                 BUGMSG(D_NORMAL, "reset failed: TESTvalue not present.\n");
306                 return 1;
307         }
308         /* enable extended (512-byte) packets */
309         ACOMMAND(CONFIGcmd | EXTconf);
310
311         /* done!  return success. */
312         return 0;
313 }
314
315 static void com90io_command(struct net_device *dev, int cmd)
316 {
317         short ioaddr = dev->base_addr;
318
319         ACOMMAND(cmd);
320 }
321
322 static int com90io_status(struct net_device *dev)
323 {
324         short ioaddr = dev->base_addr;
325
326         return ASTATUS();
327 }
328
329 static void com90io_setmask(struct net_device *dev, int mask)
330 {
331         short ioaddr = dev->base_addr;
332
333         AINTMASK(mask);
334 }
335
336 static void com90io_copy_to_card(struct net_device *dev, int bufnum, int offset,
337                                  void *buf, int count)
338 {
339         TIME("put_whole_buffer", count, put_whole_buffer(dev, bufnum * 512 + offset, count, buf));
340 }
341
342 static void com90io_copy_from_card(struct net_device *dev, int bufnum, int offset,
343                                    void *buf, int count)
344 {
345         TIME("get_whole_buffer", count, get_whole_buffer(dev, bufnum * 512 + offset, count, buf));
346 }
347
348 static int io;                  /* use the insmod io= irq= shmem= options */
349 static int irq;
350 static char device[9];          /* use eg. device=arc1 to change name */
351
352 module_param(io, int, 0);
353 module_param(irq, int, 0);
354 module_param_string(device, device, sizeof(device), 0);
355 MODULE_LICENSE("GPL");
356
357 #ifndef MODULE
358 static int __init com90io_setup(char *s)
359 {
360         int ints[4];
361
362         s = get_options(s, 4, ints);
363         if (!ints[0])
364                 return 0;
365         switch (ints[0]) {
366         default:                /* ERROR */
367                 printk("com90io: Too many arguments.\n");
368         case 2:         /* IRQ */
369                 irq = ints[2];
370         case 1:         /* IO address */
371                 io = ints[1];
372         }
373         if (*s)
374                 snprintf(device, sizeof(device), "%s", s);
375         return 1;
376 }
377 __setup("com90io=", com90io_setup);
378 #endif
379
380 static struct net_device *my_dev;
381
382 static int __init com90io_init(void)
383 {
384         struct net_device *dev;
385         int err;
386
387         dev = alloc_arcdev(device);
388         if (!dev)
389                 return -ENOMEM;
390
391         dev->base_addr = io;
392         dev->irq = irq;
393         if (dev->irq == 2)
394                 dev->irq = 9;
395
396         err = com90io_probe(dev);
397
398         if (err) {
399                 free_netdev(dev);
400                 return err;
401         }
402
403         my_dev = dev;
404         return 0;
405 }
406
407 static void __exit com90io_exit(void)
408 {
409         struct net_device *dev = my_dev;
410         int ioaddr = dev->base_addr;
411
412         unregister_netdev(dev);
413
414         /* Set the thing back to MMAP mode, in case the old driver is loaded later */
415         outb((inb(_CONFIG) & ~IOMAPflag), _CONFIG);
416
417         free_irq(dev->irq, dev);
418         release_region(dev->base_addr, ARCNET_TOTAL_SIZE);
419         free_netdev(dev);
420 }
421
422 module_init(com90io_init)
423 module_exit(com90io_exit)