c7ab23e8ec6a6096599a705fe89feeab46f66581
[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         BUGLVL(D_NORMAL) printk(VERSION);
86         BUGLVL(D_NORMAL) printk("E-mail me if you actually test the RIM I driver, please!\n");
87
88         BUGLVL(D_NORMAL) printk("Given: node %02Xh, shmem %lXh, irq %d\n",
89                                 dev->dev_addr[0], dev->mem_start, dev->irq);
90
91         if (dev->mem_start <= 0 || dev->irq <= 0) {
92                 BUGLVL(D_NORMAL) printk("No autoprobe for RIM I; you must specify the shmem and irq!\n");
93                 return -ENODEV;
94         }
95         if (dev->dev_addr[0] == 0) {
96                 BUGLVL(D_NORMAL) printk("You need to specify your card's station ID!\n");
97                 return -ENODEV;
98         }
99         /* Grab the memory region at mem_start for MIRROR_SIZE bytes.
100          * Later in arcrimi_found() the real size will be determined
101          * and this reserve will be released and the correct size
102          * will be taken.
103          */
104         if (!request_mem_region(dev->mem_start, MIRROR_SIZE, "arcnet (90xx)")) {
105                 BUGLVL(D_NORMAL) printk("Card memory already allocated\n");
106                 return -ENODEV;
107         }
108         return arcrimi_found(dev);
109 }
110
111 static int check_mirror(unsigned long addr, size_t size)
112 {
113         void __iomem *p;
114         int res = -1;
115
116         if (!request_mem_region(addr, size, "arcnet (90xx)"))
117                 return -1;
118
119         p = ioremap(addr, size);
120         if (p) {
121                 if (readb(p) == TESTvalue)
122                         res = 1;
123                 else
124                         res = 0;
125                 iounmap(p);
126         }
127
128         release_mem_region(addr, size);
129         return res;
130 }
131
132 /* Set up the struct net_device associated with this card.
133  * Called after probing succeeds.
134  */
135 static int __init arcrimi_found(struct net_device *dev)
136 {
137         struct arcnet_local *lp;
138         unsigned long first_mirror, last_mirror, shmem;
139         void __iomem *p;
140         int mirror_size;
141         int err;
142
143         p = ioremap(dev->mem_start, MIRROR_SIZE);
144         if (!p) {
145                 release_mem_region(dev->mem_start, MIRROR_SIZE);
146                 BUGMSG(D_NORMAL, "Can't ioremap\n");
147                 return -ENODEV;
148         }
149
150         /* reserve the irq */
151         if (request_irq(dev->irq, arcnet_interrupt, 0, "arcnet (RIM I)", dev)) {
152                 iounmap(p);
153                 release_mem_region(dev->mem_start, MIRROR_SIZE);
154                 BUGMSG(D_NORMAL, "Can't get IRQ %d!\n", dev->irq);
155                 return -ENODEV;
156         }
157
158         shmem = dev->mem_start;
159         writeb(TESTvalue, p);
160         writeb(dev->dev_addr[0], p + 1);        /* actually the node ID */
161
162         /* find the real shared memory start/end points, including mirrors */
163
164         /* guess the actual size of one "memory mirror" - the number of
165          * bytes between copies of the shared memory.  On most cards, it's
166          * 2k (or there are no mirrors at all) but on some, it's 4k.
167          */
168         mirror_size = MIRROR_SIZE;
169         if (readb(p) == TESTvalue &&
170             check_mirror(shmem - MIRROR_SIZE, MIRROR_SIZE) == 0 &&
171             check_mirror(shmem - 2 * MIRROR_SIZE, MIRROR_SIZE) == 1)
172                 mirror_size = 2 * MIRROR_SIZE;
173
174         first_mirror = shmem - mirror_size;
175         while (check_mirror(first_mirror, mirror_size) == 1)
176                 first_mirror -= mirror_size;
177         first_mirror += mirror_size;
178
179         last_mirror = shmem + mirror_size;
180         while (check_mirror(last_mirror, mirror_size) == 1)
181                 last_mirror += mirror_size;
182         last_mirror -= mirror_size;
183
184         dev->mem_start = first_mirror;
185         dev->mem_end = last_mirror + MIRROR_SIZE - 1;
186
187         /* initialize the rest of the device structure. */
188
189         lp = netdev_priv(dev);
190         lp->card_name = "RIM I";
191         lp->hw.command = arcrimi_command;
192         lp->hw.status = arcrimi_status;
193         lp->hw.intmask = arcrimi_setmask;
194         lp->hw.reset = arcrimi_reset;
195         lp->hw.owner = THIS_MODULE;
196         lp->hw.copy_to_card = arcrimi_copy_to_card;
197         lp->hw.copy_from_card = arcrimi_copy_from_card;
198
199         /* re-reserve the memory region - arcrimi_probe() alloced this reqion
200          * but didn't know the real size.  Free that region and then re-get
201          * with the correct size.  There is a VERY slim chance this could
202          * fail.
203          */
204         iounmap(p);
205         release_mem_region(shmem, MIRROR_SIZE);
206         if (!request_mem_region(dev->mem_start,
207                                 dev->mem_end - dev->mem_start + 1,
208                                 "arcnet (90xx)")) {
209                 BUGMSG(D_NORMAL, "Card memory already allocated\n");
210                 goto err_free_irq;
211         }
212
213         lp->mem_start = ioremap(dev->mem_start, dev->mem_end - dev->mem_start + 1);
214         if (!lp->mem_start) {
215                 BUGMSG(D_NORMAL, "Can't remap device memory!\n");
216                 goto err_release_mem;
217         }
218
219         /* get and check the station ID from offset 1 in shmem */
220         dev->dev_addr[0] = readb(lp->mem_start + 1);
221
222         BUGMSG(D_NORMAL, "ARCnet RIM I: station %02Xh found at IRQ %d, ShMem %lXh (%ld*%d bytes)\n",
223                dev->dev_addr[0],
224                dev->irq, dev->mem_start,
225          (dev->mem_end - dev->mem_start + 1) / mirror_size, mirror_size);
226
227         err = register_netdev(dev);
228         if (err)
229                 goto err_unmap;
230
231         return 0;
232
233 err_unmap:
234         iounmap(lp->mem_start);
235 err_release_mem:
236         release_mem_region(dev->mem_start, dev->mem_end - dev->mem_start + 1);
237 err_free_irq:
238         free_irq(dev->irq, dev);
239         return -EIO;
240 }
241
242 /* Do a hardware reset on the card, and set up necessary registers.
243  *
244  * This should be called as little as possible, because it disrupts the
245  * token on the network (causes a RECON) and requires a significant delay.
246  *
247  * However, it does make sure the card is in a defined state.
248  */
249 static int arcrimi_reset(struct net_device *dev, int really_reset)
250 {
251         struct arcnet_local *lp = netdev_priv(dev);
252         void __iomem *ioaddr = lp->mem_start + 0x800;
253
254         BUGMSG(D_INIT, "Resetting %s (status=%02Xh)\n", dev->name, ASTATUS());
255
256         if (really_reset) {
257                 writeb(TESTvalue, ioaddr - 0x800);      /* fake reset */
258                 return 0;
259         }
260         ACOMMAND(CFLAGScmd | RESETclear);       /* clear flags & end reset */
261         ACOMMAND(CFLAGScmd | CONFIGclear);
262
263         /* enable extended (512-byte) packets */
264         ACOMMAND(CONFIGcmd | EXTconf);
265
266         /* done!  return success. */
267         return 0;
268 }
269
270 static void arcrimi_setmask(struct net_device *dev, int mask)
271 {
272         struct arcnet_local *lp = netdev_priv(dev);
273         void __iomem *ioaddr = lp->mem_start + 0x800;
274
275         AINTMASK(mask);
276 }
277
278 static int arcrimi_status(struct net_device *dev)
279 {
280         struct arcnet_local *lp = netdev_priv(dev);
281         void __iomem *ioaddr = lp->mem_start + 0x800;
282
283         return ASTATUS();
284 }
285
286 static void arcrimi_command(struct net_device *dev, int cmd)
287 {
288         struct arcnet_local *lp = netdev_priv(dev);
289         void __iomem *ioaddr = lp->mem_start + 0x800;
290
291         ACOMMAND(cmd);
292 }
293
294 static void arcrimi_copy_to_card(struct net_device *dev, int bufnum, int offset,
295                                  void *buf, int count)
296 {
297         struct arcnet_local *lp = netdev_priv(dev);
298         void __iomem *memaddr = lp->mem_start + 0x800 + bufnum * 512 + offset;
299
300         TIME("memcpy_toio", count, memcpy_toio(memaddr, buf, count));
301 }
302
303 static void arcrimi_copy_from_card(struct net_device *dev, int bufnum, int offset,
304                                    void *buf, int count)
305 {
306         struct arcnet_local *lp = netdev_priv(dev);
307         void __iomem *memaddr = lp->mem_start + 0x800 + bufnum * 512 + offset;
308
309         TIME("memcpy_fromio", count, memcpy_fromio(buf, memaddr, count));
310 }
311
312 static int node;
313 static int io;                  /* use the insmod io= irq= node= options */
314 static int irq;
315 static char device[9];          /* use eg. device=arc1 to change name */
316
317 module_param(node, int, 0);
318 module_param(io, int, 0);
319 module_param(irq, int, 0);
320 module_param_string(device, device, sizeof(device), 0);
321 MODULE_LICENSE("GPL");
322
323 static struct net_device *my_dev;
324
325 static int __init arc_rimi_init(void)
326 {
327         struct net_device *dev;
328
329         dev = alloc_arcdev(device);
330         if (!dev)
331                 return -ENOMEM;
332
333         if (node && node != 0xff)
334                 dev->dev_addr[0] = node;
335
336         dev->mem_start = io;
337         dev->irq = irq;
338         if (dev->irq == 2)
339                 dev->irq = 9;
340
341         if (arcrimi_probe(dev)) {
342                 free_netdev(dev);
343                 return -EIO;
344         }
345
346         my_dev = dev;
347         return 0;
348 }
349
350 static void __exit arc_rimi_exit(void)
351 {
352         struct net_device *dev = my_dev;
353         struct arcnet_local *lp = netdev_priv(dev);
354
355         unregister_netdev(dev);
356         iounmap(lp->mem_start);
357         release_mem_region(dev->mem_start, dev->mem_end - dev->mem_start + 1);
358         free_irq(dev->irq, dev);
359         free_netdev(dev);
360 }
361
362 #ifndef MODULE
363 static int __init arcrimi_setup(char *s)
364 {
365         int ints[8];
366
367         s = get_options(s, 8, ints);
368         if (!ints[0])
369                 return 1;
370         switch (ints[0]) {
371         default:                /* ERROR */
372                 printk("arcrimi: Too many arguments.\n");
373         case 3:         /* Node ID */
374                 node = ints[3];
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("arcrimi=", arcrimi_setup);
385 #endif                          /* MODULE */
386
387 module_init(arc_rimi_init)
388 module_exit(arc_rimi_exit)