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