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