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