vserver 2.0 rc7
[linux-2.6.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/slab.h>
32 #include <linux/delay.h>
33 #include <linux/netdevice.h>
34 #include <linux/bootmem.h>
35 #include <linux/init.h>
36 #include <asm/io.h>
37 #include <linux/arcdevice.h>
38
39
40 #define VERSION "arcnet: RIM I (entirely mem-mapped) support\n"
41
42
43 /* Internal function declarations */
44
45 static int arcrimi_probe(struct net_device *dev);
46 static int arcrimi_found(struct net_device *dev);
47 static void arcrimi_command(struct net_device *dev, int command);
48 static int arcrimi_status(struct net_device *dev);
49 static void arcrimi_setmask(struct net_device *dev, int mask);
50 static int arcrimi_reset(struct net_device *dev, int really_reset);
51 static void arcrimi_copy_to_card(struct net_device *dev, int bufnum, int offset,
52                                  void *buf, int count);
53 static void arcrimi_copy_from_card(struct net_device *dev, int bufnum, int offset,
54                                    void *buf, int count);
55
56 /* Handy defines for ARCnet specific stuff */
57
58 /* Amount of I/O memory used by the card */
59 #define BUFFER_SIZE (512)
60 #define MIRROR_SIZE (BUFFER_SIZE*4)
61
62 /* COM 9026 controller chip --> ARCnet register addresses */
63 #define _INTMASK (ioaddr+0)     /* writable */
64 #define _STATUS  (ioaddr+0)     /* readable */
65 #define _COMMAND (ioaddr+1)     /* writable, returns random vals on read (?) */
66 #define _RESET  (ioaddr+8)      /* software reset (on read) */
67 #define _MEMDATA  (ioaddr+12)   /* Data port for IO-mapped memory */
68 #define _ADDR_HI  (ioaddr+15)   /* Control registers for said */
69 #define _ADDR_LO  (ioaddr+14)
70 #define _CONFIG  (ioaddr+2)     /* Configuration register */
71
72 #undef ASTATUS
73 #undef ACOMMAND
74 #undef AINTMASK
75
76 #define ASTATUS()       readb(_STATUS)
77 #define ACOMMAND(cmd)   writeb((cmd),_COMMAND)
78 #define AINTMASK(msk)   writeb((msk),_INTMASK)
79 #define SETCONF()       writeb(lp->config,_CONFIG)
80
81
82 /*
83  * We cannot probe for a RIM I card; one reason is I don't know how to reset
84  * them.  In fact, we can't even get their node ID automatically.  So, we
85  * need to be passed a specific shmem address, IRQ, and node ID.
86  */
87 static int __init arcrimi_probe(struct net_device *dev)
88 {
89         BUGLVL(D_NORMAL) printk(VERSION);
90         BUGLVL(D_NORMAL) printk("E-mail me if you actually test the RIM I driver, please!\n");
91
92         BUGMSG(D_NORMAL, "Given: node %02Xh, shmem %lXh, irq %d\n",
93                dev->dev_addr[0], dev->mem_start, dev->irq);
94
95         if (dev->mem_start <= 0 || dev->irq <= 0) {
96                 BUGMSG(D_NORMAL, "No autoprobe for RIM I; you "
97                        "must specify the shmem and irq!\n");
98                 return -ENODEV;
99         }
100         /*
101          * Grab the memory region at mem_start for BUFFER_SIZE bytes.
102          * Later in arcrimi_found() the real size will be determined
103          * and this reserve will be released and the correct size
104          * will be taken.
105          */
106         if (!request_mem_region(dev->mem_start, BUFFER_SIZE, "arcnet (90xx)")) {
107                 BUGMSG(D_NORMAL, "Card memory already allocated\n");
108                 return -ENODEV;
109         }
110         if (dev->dev_addr[0] == 0) {
111                 release_mem_region(dev->mem_start, BUFFER_SIZE);
112                 BUGMSG(D_NORMAL, "You need to specify your card's station "
113                        "ID!\n");
114                 return -ENODEV;
115         }
116         return arcrimi_found(dev);
117 }
118
119
120 /*
121  * Set up the struct net_device associated with this card.  Called after
122  * probing succeeds.
123  */
124 static int __init arcrimi_found(struct net_device *dev)
125 {
126         struct arcnet_local *lp;
127         unsigned long first_mirror, last_mirror, shmem;
128         int mirror_size;
129         int err;
130
131         /* reserve the irq */
132         if (request_irq(dev->irq, &arcnet_interrupt, 0, "arcnet (RIM I)", dev)) {
133                 release_mem_region(dev->mem_start, BUFFER_SIZE);
134                 BUGMSG(D_NORMAL, "Can't get IRQ %d!\n", dev->irq);
135                 return -ENODEV;
136         }
137
138         shmem = dev->mem_start;
139         isa_writeb(TESTvalue, shmem);
140         isa_writeb(dev->dev_addr[0], shmem + 1);        /* actually the node ID */
141
142         /* find the real shared memory start/end points, including mirrors */
143
144         /* guess the actual size of one "memory mirror" - the number of
145          * bytes between copies of the shared memory.  On most cards, it's
146          * 2k (or there are no mirrors at all) but on some, it's 4k.
147          */
148         mirror_size = MIRROR_SIZE;
149         if (isa_readb(shmem) == TESTvalue
150             && isa_readb(shmem - mirror_size) != TESTvalue
151             && isa_readb(shmem - 2 * mirror_size) == TESTvalue)
152                 mirror_size *= 2;
153
154         first_mirror = last_mirror = shmem;
155         while (isa_readb(first_mirror) == TESTvalue)
156                 first_mirror -= mirror_size;
157         first_mirror += mirror_size;
158
159         while (isa_readb(last_mirror) == TESTvalue)
160                 last_mirror += mirror_size;
161         last_mirror -= mirror_size;
162
163         dev->mem_start = first_mirror;
164         dev->mem_end = last_mirror + MIRROR_SIZE - 1;
165
166         /* initialize the rest of the device structure. */
167
168         lp = dev->priv;
169         lp->card_name = "RIM I";
170         lp->hw.command = arcrimi_command;
171         lp->hw.status = arcrimi_status;
172         lp->hw.intmask = arcrimi_setmask;
173         lp->hw.reset = arcrimi_reset;
174         lp->hw.owner = THIS_MODULE;
175         lp->hw.copy_to_card = arcrimi_copy_to_card;
176         lp->hw.copy_from_card = arcrimi_copy_from_card;
177
178         /*
179          * re-reserve the memory region - arcrimi_probe() alloced this reqion
180          * but didn't know the real size.  Free that region and then re-get
181          * with the correct size.  There is a VERY slim chance this could
182          * fail.
183          */
184         release_mem_region(shmem, BUFFER_SIZE);
185         if (!request_mem_region(dev->mem_start,
186                                 dev->mem_end - dev->mem_start + 1,
187                                 "arcnet (90xx)")) {
188                 BUGMSG(D_NORMAL, "Card memory already allocated\n");
189                 goto err_free_irq;
190         }
191
192         lp->mem_start = ioremap(dev->mem_start, dev->mem_end - dev->mem_start + 1);
193         if (!lp->mem_start) {
194                 BUGMSG(D_NORMAL, "Can't remap device memory!\n");
195                 goto err_release_mem;
196         }
197
198         /* get and check the station ID from offset 1 in shmem */
199         dev->dev_addr[0] = readb(lp->mem_start + 1);
200
201         BUGMSG(D_NORMAL, "ARCnet RIM I: station %02Xh found at IRQ %d, "
202                "ShMem %lXh (%ld*%d bytes).\n",
203                dev->dev_addr[0],
204                dev->irq, dev->mem_start,
205          (dev->mem_end - dev->mem_start + 1) / mirror_size, mirror_size);
206
207         err = register_netdev(dev);
208         if (err)
209                 goto err_unmap;
210
211         return 0;
212
213 err_unmap:
214         iounmap(lp->mem_start);
215 err_release_mem:
216         release_mem_region(dev->mem_start, dev->mem_end - dev->mem_start + 1);
217 err_free_irq:
218         free_irq(dev->irq, dev);
219         return -EIO;
220 }
221
222
223 /*
224  * Do a hardware reset on the card, and set up necessary registers.
225  *
226  * This should be called as little as possible, because it disrupts the
227  * token on the network (causes a RECON) and requires a significant delay.
228  *
229  * However, it does make sure the card is in a defined state.
230  */
231 static int arcrimi_reset(struct net_device *dev, int really_reset)
232 {
233         struct arcnet_local *lp = dev->priv;
234         void __iomem *ioaddr = lp->mem_start + 0x800;
235
236         BUGMSG(D_INIT, "Resetting %s (status=%02Xh)\n", dev->name, ASTATUS());
237
238         if (really_reset) {
239                 writeb(TESTvalue, ioaddr - 0x800);      /* fake reset */
240                 return 0;
241         }
242         ACOMMAND(CFLAGScmd | RESETclear);       /* clear flags & end reset */
243         ACOMMAND(CFLAGScmd | CONFIGclear);
244
245         /* enable extended (512-byte) packets */
246         ACOMMAND(CONFIGcmd | EXTconf);
247
248         /* done!  return success. */
249         return 0;
250 }
251
252 static void arcrimi_setmask(struct net_device *dev, int mask)
253 {
254         struct arcnet_local *lp = dev->priv;
255         void __iomem *ioaddr = lp->mem_start + 0x800;
256
257         AINTMASK(mask);
258 }
259
260 static int arcrimi_status(struct net_device *dev)
261 {
262         struct arcnet_local *lp = dev->priv;
263         void __iomem *ioaddr = lp->mem_start + 0x800;
264
265         return ASTATUS();
266 }
267
268 static void arcrimi_command(struct net_device *dev, int cmd)
269 {
270         struct arcnet_local *lp = dev->priv;
271         void __iomem *ioaddr = lp->mem_start + 0x800;
272
273         ACOMMAND(cmd);
274 }
275
276 static void arcrimi_copy_to_card(struct net_device *dev, int bufnum, int offset,
277                                  void *buf, int count)
278 {
279         struct arcnet_local *lp = dev->priv;
280         void __iomem *memaddr = lp->mem_start + 0x800 + bufnum * 512 + offset;
281         TIME("memcpy_toio", count, memcpy_toio(memaddr, buf, count));
282 }
283
284
285 static void arcrimi_copy_from_card(struct net_device *dev, int bufnum, int offset,
286                                    void *buf, int count)
287 {
288         struct arcnet_local *lp = dev->priv;
289         void __iomem *memaddr = lp->mem_start + 0x800 + bufnum * 512 + offset;
290         TIME("memcpy_fromio", count, memcpy_fromio(buf, memaddr, count));
291 }
292
293 static int node;
294 static int io;                  /* use the insmod io= irq= node= options */
295 static int irq;
296 static char device[9];          /* use eg. device=arc1 to change name */
297
298 module_param(node, int, 0);
299 module_param(io, int, 0);
300 module_param(irq, int, 0);
301 module_param_string(device, device, sizeof(device), 0);
302 MODULE_LICENSE("GPL");
303
304 static struct net_device *my_dev;
305
306 static int __init arc_rimi_init(void)
307 {
308         struct net_device *dev;
309
310         dev = alloc_arcdev(device);
311         if (!dev)
312                 return -ENOMEM;
313
314         if (node && node != 0xff)
315                 dev->dev_addr[0] = node;
316
317         dev->mem_start = io;
318         dev->irq = irq;
319         if (dev->irq == 2)
320                 dev->irq = 9;
321
322         if (arcrimi_probe(dev)) {
323                 free_netdev(dev);
324                 return -EIO;
325         }
326
327         my_dev = dev;
328         return 0;
329 }
330
331 static void __exit arc_rimi_exit(void)
332 {
333         struct net_device *dev = my_dev;
334         struct arcnet_local *lp = dev->priv;
335
336         unregister_netdev(dev);
337         iounmap(lp->mem_start);
338         release_mem_region(dev->mem_start, dev->mem_end - dev->mem_start + 1);
339         free_irq(dev->irq, dev);
340         free_netdev(dev);
341 }
342
343 #ifndef MODULE
344 static int __init arcrimi_setup(char *s)
345 {
346         int ints[8];
347         s = get_options(s, 8, ints);
348         if (!ints[0])
349                 return 1;
350         switch (ints[0]) {
351         default:                /* ERROR */
352                 printk("arcrimi: Too many arguments.\n");
353         case 3:         /* Node ID */
354                 node = ints[3];
355         case 2:         /* IRQ */
356                 irq = ints[2];
357         case 1:         /* IO address */
358                 io = ints[1];
359         }
360         if (*s)
361                 snprintf(device, sizeof(device), "%s", s);
362         return 1;
363 }
364 __setup("arcrimi=", arcrimi_setup);
365 #endif                          /* MODULE */
366
367 module_init(arc_rimi_init)
368 module_exit(arc_rimi_exit)