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