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