25f84b7437f3a6f846c43decdceccfca9e6dd36e
[cascardo/linux.git] / drivers / net / arcnet / arc-rimi.c
1 /*
2  * Linux ARCnet driver - "RIM I" (entirely mem-mapped) cards
3  *
4  * Written 1994-1999 by Avery Pennarun.
5  * Written 1999-2000 by Martin Mares <mj@ucw.cz>.
6  * Derived from skeleton.c by Donald Becker.
7  *
8  * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
9  *  for sponsoring the further development of this driver.
10  *
11  * **********************
12  *
13  * The original copyright of skeleton.c was as follows:
14  *
15  * skeleton.c Written 1993 by Donald Becker.
16  * Copyright 1993 United States Government as represented by the
17  * Director, National Security Agency.  This software may only be used
18  * and distributed according to the terms of the GNU General Public License as
19  * modified by SRC, incorporated herein by reference.
20  *
21  * **********************
22  *
23  * For more details, see drivers/net/arcnet.c
24  *
25  * **********************
26  */
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/ioport.h>
31 #include <linux/delay.h>
32 #include <linux/netdevice.h>
33 #include <linux/bootmem.h>
34 #include <linux/init.h>
35 #include <linux/interrupt.h>
36 #include <linux/io.h>
37 #include <linux/arcdevice.h>
38
39 #define VERSION "arcnet: RIM I (entirely mem-mapped) support\n"
40
41 /* Internal function declarations */
42
43 static int arcrimi_probe(struct net_device *dev);
44 static int arcrimi_found(struct net_device *dev);
45 static void arcrimi_command(struct net_device *dev, int command);
46 static int arcrimi_status(struct net_device *dev);
47 static void arcrimi_setmask(struct net_device *dev, int mask);
48 static int arcrimi_reset(struct net_device *dev, int really_reset);
49 static void arcrimi_copy_to_card(struct net_device *dev, int bufnum, int offset,
50                                  void *buf, int count);
51 static void arcrimi_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 /* Amount of I/O memory used by the card */
57 #define BUFFER_SIZE     (512)
58 #define MIRROR_SIZE     (BUFFER_SIZE * 4)
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()       readb(_STATUS)
75 #define ACOMMAND(cmd)   writeb((cmd), _COMMAND)
76 #define AINTMASK(msk)   writeb((msk), _INTMASK)
77 #define SETCONF()       writeb(lp->config, _CONFIG)
78
79 /* We cannot probe for a RIM I card; one reason is I don't know how to reset
80  * them.  In fact, we can't even get their node ID automatically.  So, we
81  * need to be passed a specific shmem address, IRQ, and node ID.
82  */
83 static int __init arcrimi_probe(struct net_device *dev)
84 {
85         if (BUGLVL(D_NORMAL)) {
86                 printk(VERSION);
87                 printk("E-mail me if you actually test the RIM I driver, please!\n");
88                 printk("Given: node %02Xh, shmem %lXh, irq %d\n",
89                        dev->dev_addr[0], dev->mem_start, dev->irq);
90         }
91
92         if (dev->mem_start <= 0 || dev->irq <= 0) {
93                 if (BUGLVL(D_NORMAL))
94                         printk("No autoprobe for RIM I; you must specify the shmem and irq!\n");
95                 return -ENODEV;
96         }
97         if (dev->dev_addr[0] == 0) {
98                 if (BUGLVL(D_NORMAL))
99                         printk("You need to specify your card's station ID!\n");
100                 return -ENODEV;
101         }
102         /* Grab the memory region at mem_start for MIRROR_SIZE bytes.
103          * Later in arcrimi_found() the real size will be determined
104          * and this reserve will be released and the correct size
105          * will be taken.
106          */
107         if (!request_mem_region(dev->mem_start, MIRROR_SIZE, "arcnet (90xx)")) {
108                 if (BUGLVL(D_NORMAL))
109                         printk("Card memory already allocated\n");
110                 return -ENODEV;
111         }
112         return arcrimi_found(dev);
113 }
114
115 static int check_mirror(unsigned long addr, size_t size)
116 {
117         void __iomem *p;
118         int res = -1;
119
120         if (!request_mem_region(addr, size, "arcnet (90xx)"))
121                 return -1;
122
123         p = ioremap(addr, size);
124         if (p) {
125                 if (readb(p) == TESTvalue)
126                         res = 1;
127                 else
128                         res = 0;
129                 iounmap(p);
130         }
131
132         release_mem_region(addr, size);
133         return res;
134 }
135
136 /* Set up the struct net_device associated with this card.
137  * Called after probing succeeds.
138  */
139 static int __init arcrimi_found(struct net_device *dev)
140 {
141         struct arcnet_local *lp;
142         unsigned long first_mirror, last_mirror, shmem;
143         void __iomem *p;
144         int mirror_size;
145         int err;
146
147         p = ioremap(dev->mem_start, MIRROR_SIZE);
148         if (!p) {
149                 release_mem_region(dev->mem_start, MIRROR_SIZE);
150                 arc_printk(D_NORMAL, dev, "Can't ioremap\n");
151                 return -ENODEV;
152         }
153
154         /* reserve the irq */
155         if (request_irq(dev->irq, arcnet_interrupt, 0, "arcnet (RIM I)", dev)) {
156                 iounmap(p);
157                 release_mem_region(dev->mem_start, MIRROR_SIZE);
158                 arc_printk(D_NORMAL, dev, "Can't get IRQ %d!\n", dev->irq);
159                 return -ENODEV;
160         }
161
162         shmem = dev->mem_start;
163         writeb(TESTvalue, p);
164         writeb(dev->dev_addr[0], p + 1);        /* actually the node ID */
165
166         /* find the real shared memory start/end points, including mirrors */
167
168         /* guess the actual size of one "memory mirror" - the number of
169          * bytes between copies of the shared memory.  On most cards, it's
170          * 2k (or there are no mirrors at all) but on some, it's 4k.
171          */
172         mirror_size = MIRROR_SIZE;
173         if (readb(p) == TESTvalue &&
174             check_mirror(shmem - MIRROR_SIZE, MIRROR_SIZE) == 0 &&
175             check_mirror(shmem - 2 * MIRROR_SIZE, MIRROR_SIZE) == 1)
176                 mirror_size = 2 * MIRROR_SIZE;
177
178         first_mirror = shmem - mirror_size;
179         while (check_mirror(first_mirror, mirror_size) == 1)
180                 first_mirror -= mirror_size;
181         first_mirror += mirror_size;
182
183         last_mirror = shmem + mirror_size;
184         while (check_mirror(last_mirror, mirror_size) == 1)
185                 last_mirror += mirror_size;
186         last_mirror -= mirror_size;
187
188         dev->mem_start = first_mirror;
189         dev->mem_end = last_mirror + MIRROR_SIZE - 1;
190
191         /* initialize the rest of the device structure. */
192
193         lp = netdev_priv(dev);
194         lp->card_name = "RIM I";
195         lp->hw.command = arcrimi_command;
196         lp->hw.status = arcrimi_status;
197         lp->hw.intmask = arcrimi_setmask;
198         lp->hw.reset = arcrimi_reset;
199         lp->hw.owner = THIS_MODULE;
200         lp->hw.copy_to_card = arcrimi_copy_to_card;
201         lp->hw.copy_from_card = arcrimi_copy_from_card;
202
203         /* re-reserve the memory region - arcrimi_probe() alloced this reqion
204          * but didn't know the real size.  Free that region and then re-get
205          * with the correct size.  There is a VERY slim chance this could
206          * fail.
207          */
208         iounmap(p);
209         release_mem_region(shmem, MIRROR_SIZE);
210         if (!request_mem_region(dev->mem_start,
211                                 dev->mem_end - dev->mem_start + 1,
212                                 "arcnet (90xx)")) {
213                 arc_printk(D_NORMAL, dev, "Card memory already allocated\n");
214                 goto err_free_irq;
215         }
216
217         lp->mem_start = ioremap(dev->mem_start, dev->mem_end - dev->mem_start + 1);
218         if (!lp->mem_start) {
219                 arc_printk(D_NORMAL, dev, "Can't remap device memory!\n");
220                 goto err_release_mem;
221         }
222
223         /* get and check the station ID from offset 1 in shmem */
224         dev->dev_addr[0] = readb(lp->mem_start + 1);
225
226         arc_printk(D_NORMAL, dev, "ARCnet RIM I: station %02Xh found at IRQ %d, ShMem %lXh (%ld*%d bytes)\n",
227                    dev->dev_addr[0],
228                    dev->irq, dev->mem_start,
229                    (dev->mem_end - dev->mem_start + 1) / mirror_size,
230                    mirror_size);
231
232         err = register_netdev(dev);
233         if (err)
234                 goto err_unmap;
235
236         return 0;
237
238 err_unmap:
239         iounmap(lp->mem_start);
240 err_release_mem:
241         release_mem_region(dev->mem_start, dev->mem_end - dev->mem_start + 1);
242 err_free_irq:
243         free_irq(dev->irq, dev);
244         return -EIO;
245 }
246
247 /* Do a hardware reset on the card, and set up necessary registers.
248  *
249  * This should be called as little as possible, because it disrupts the
250  * token on the network (causes a RECON) and requires a significant delay.
251  *
252  * However, it does make sure the card is in a defined state.
253  */
254 static int arcrimi_reset(struct net_device *dev, int really_reset)
255 {
256         struct arcnet_local *lp = netdev_priv(dev);
257         void __iomem *ioaddr = lp->mem_start + 0x800;
258
259         arc_printk(D_INIT, dev, "Resetting %s (status=%02Xh)\n",
260                    dev->name, ASTATUS());
261
262         if (really_reset) {
263                 writeb(TESTvalue, ioaddr - 0x800);      /* fake reset */
264                 return 0;
265         }
266         ACOMMAND(CFLAGScmd | RESETclear);       /* clear flags & end reset */
267         ACOMMAND(CFLAGScmd | CONFIGclear);
268
269         /* enable extended (512-byte) packets */
270         ACOMMAND(CONFIGcmd | EXTconf);
271
272         /* done!  return success. */
273         return 0;
274 }
275
276 static void arcrimi_setmask(struct net_device *dev, int mask)
277 {
278         struct arcnet_local *lp = netdev_priv(dev);
279         void __iomem *ioaddr = lp->mem_start + 0x800;
280
281         AINTMASK(mask);
282 }
283
284 static int arcrimi_status(struct net_device *dev)
285 {
286         struct arcnet_local *lp = netdev_priv(dev);
287         void __iomem *ioaddr = lp->mem_start + 0x800;
288
289         return ASTATUS();
290 }
291
292 static void arcrimi_command(struct net_device *dev, int cmd)
293 {
294         struct arcnet_local *lp = netdev_priv(dev);
295         void __iomem *ioaddr = lp->mem_start + 0x800;
296
297         ACOMMAND(cmd);
298 }
299
300 static void arcrimi_copy_to_card(struct net_device *dev, int bufnum, int offset,
301                                  void *buf, int count)
302 {
303         struct arcnet_local *lp = netdev_priv(dev);
304         void __iomem *memaddr = lp->mem_start + 0x800 + bufnum * 512 + offset;
305
306         TIME(dev, "memcpy_toio", count, memcpy_toio(memaddr, buf, count));
307 }
308
309 static void arcrimi_copy_from_card(struct net_device *dev, int bufnum, int offset,
310                                    void *buf, int count)
311 {
312         struct arcnet_local *lp = netdev_priv(dev);
313         void __iomem *memaddr = lp->mem_start + 0x800 + bufnum * 512 + offset;
314
315         TIME(dev, "memcpy_fromio", count, memcpy_fromio(buf, memaddr, count));
316 }
317
318 static int node;
319 static int io;                  /* use the insmod io= irq= node= options */
320 static int irq;
321 static char device[9];          /* use eg. device=arc1 to change name */
322
323 module_param(node, int, 0);
324 module_param(io, int, 0);
325 module_param(irq, int, 0);
326 module_param_string(device, device, sizeof(device), 0);
327 MODULE_LICENSE("GPL");
328
329 static struct net_device *my_dev;
330
331 static int __init arc_rimi_init(void)
332 {
333         struct net_device *dev;
334
335         dev = alloc_arcdev(device);
336         if (!dev)
337                 return -ENOMEM;
338
339         if (node && node != 0xff)
340                 dev->dev_addr[0] = node;
341
342         dev->mem_start = io;
343         dev->irq = irq;
344         if (dev->irq == 2)
345                 dev->irq = 9;
346
347         if (arcrimi_probe(dev)) {
348                 free_netdev(dev);
349                 return -EIO;
350         }
351
352         my_dev = dev;
353         return 0;
354 }
355
356 static void __exit arc_rimi_exit(void)
357 {
358         struct net_device *dev = my_dev;
359         struct arcnet_local *lp = netdev_priv(dev);
360
361         unregister_netdev(dev);
362         iounmap(lp->mem_start);
363         release_mem_region(dev->mem_start, dev->mem_end - dev->mem_start + 1);
364         free_irq(dev->irq, dev);
365         free_netdev(dev);
366 }
367
368 #ifndef MODULE
369 static int __init arcrimi_setup(char *s)
370 {
371         int ints[8];
372
373         s = get_options(s, 8, ints);
374         if (!ints[0])
375                 return 1;
376         switch (ints[0]) {
377         default:                /* ERROR */
378                 printk("arcrimi: Too many arguments.\n");
379         case 3:         /* Node ID */
380                 node = ints[3];
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("arcrimi=", arcrimi_setup);
391 #endif                          /* MODULE */
392
393 module_init(arc_rimi_init)
394 module_exit(arc_rimi_exit)