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