2 * Linux ARCnet driver - "RIM I" (entirely mem-mapped) cards
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.
8 * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
9 * for sponsoring the further development of this driver.
11 * **********************
13 * The original copyright of skeleton.c was as follows:
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.
21 * **********************
23 * For more details, see drivers/net/arcnet.c
25 * **********************
28 #define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt
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/memblock.h>
37 #include <linux/init.h>
38 #include <linux/interrupt.h>
41 #include "arcdevice.h"
44 /* Internal function declarations */
46 static int arcrimi_probe(struct net_device
*dev
);
47 static int arcrimi_found(struct net_device
*dev
);
48 static void arcrimi_command(struct net_device
*dev
, int command
);
49 static int arcrimi_status(struct net_device
*dev
);
50 static void arcrimi_setmask(struct net_device
*dev
, int mask
);
51 static int arcrimi_reset(struct net_device
*dev
, int really_reset
);
52 static void arcrimi_copy_to_card(struct net_device
*dev
, int bufnum
, int offset
,
53 void *buf
, int count
);
54 static void arcrimi_copy_from_card(struct net_device
*dev
, int bufnum
,
55 int offset
, void *buf
, int count
);
57 /* Handy defines for ARCnet specific stuff */
59 /* Amount of I/O memory used by the card */
60 #define BUFFER_SIZE (512)
61 #define MIRROR_SIZE (BUFFER_SIZE * 4)
63 /* We cannot probe for a RIM I card; one reason is I don't know how to reset
64 * them. In fact, we can't even get their node ID automatically. So, we
65 * need to be passed a specific shmem address, IRQ, and node ID.
67 static int __init
arcrimi_probe(struct net_device
*dev
)
69 if (BUGLVL(D_NORMAL
)) {
70 pr_info("%s\n", "RIM I (entirely mem-mapped) support");
71 pr_info("E-mail me if you actually test the RIM I driver, please!\n");
72 pr_info("Given: node %02Xh, shmem %lXh, irq %d\n",
73 dev
->dev_addr
[0], dev
->mem_start
, dev
->irq
);
76 if (dev
->mem_start
<= 0 || dev
->irq
<= 0) {
78 pr_err("No autoprobe for RIM I; you must specify the shmem and irq!\n");
81 if (dev
->dev_addr
[0] == 0) {
83 pr_err("You need to specify your card's station ID!\n");
86 /* Grab the memory region at mem_start for MIRROR_SIZE bytes.
87 * Later in arcrimi_found() the real size will be determined
88 * and this reserve will be released and the correct size
91 if (!request_mem_region(dev
->mem_start
, MIRROR_SIZE
, "arcnet (90xx)")) {
93 pr_notice("Card memory already allocated\n");
96 return arcrimi_found(dev
);
99 static int check_mirror(unsigned long addr
, size_t size
)
104 if (!request_mem_region(addr
, size
, "arcnet (90xx)"))
107 p
= ioremap(addr
, size
);
109 if (arcnet_readb(p
, COM9026_REG_R_STATUS
) == TESTvalue
)
116 release_mem_region(addr
, size
);
120 /* Set up the struct net_device associated with this card.
121 * Called after probing succeeds.
123 static int __init
arcrimi_found(struct net_device
*dev
)
125 struct arcnet_local
*lp
;
126 unsigned long first_mirror
, last_mirror
, shmem
;
131 p
= ioremap(dev
->mem_start
, MIRROR_SIZE
);
133 release_mem_region(dev
->mem_start
, MIRROR_SIZE
);
134 arc_printk(D_NORMAL
, dev
, "Can't ioremap\n");
138 /* reserve the irq */
139 if (request_irq(dev
->irq
, arcnet_interrupt
, 0, "arcnet (RIM I)", dev
)) {
141 release_mem_region(dev
->mem_start
, MIRROR_SIZE
);
142 arc_printk(D_NORMAL
, dev
, "Can't get IRQ %d!\n", dev
->irq
);
146 shmem
= dev
->mem_start
;
147 arcnet_writeb(TESTvalue
, p
, COM9026_REG_W_INTMASK
);
148 arcnet_writeb(TESTvalue
, p
, COM9026_REG_W_COMMAND
);
149 /* actually the station/node ID */
151 /* find the real shared memory start/end points, including mirrors */
153 /* guess the actual size of one "memory mirror" - the number of
154 * bytes between copies of the shared memory. On most cards, it's
155 * 2k (or there are no mirrors at all) but on some, it's 4k.
157 mirror_size
= MIRROR_SIZE
;
158 if (arcnet_readb(p
, COM9026_REG_R_STATUS
) == TESTvalue
&&
159 check_mirror(shmem
- MIRROR_SIZE
, MIRROR_SIZE
) == 0 &&
160 check_mirror(shmem
- 2 * MIRROR_SIZE
, MIRROR_SIZE
) == 1)
161 mirror_size
= 2 * MIRROR_SIZE
;
163 first_mirror
= shmem
- mirror_size
;
164 while (check_mirror(first_mirror
, mirror_size
) == 1)
165 first_mirror
-= mirror_size
;
166 first_mirror
+= mirror_size
;
168 last_mirror
= shmem
+ mirror_size
;
169 while (check_mirror(last_mirror
, mirror_size
) == 1)
170 last_mirror
+= mirror_size
;
171 last_mirror
-= mirror_size
;
173 dev
->mem_start
= first_mirror
;
174 dev
->mem_end
= last_mirror
+ MIRROR_SIZE
- 1;
176 /* initialize the rest of the device structure. */
178 lp
= netdev_priv(dev
);
179 lp
->card_name
= "RIM I";
180 lp
->hw
.command
= arcrimi_command
;
181 lp
->hw
.status
= arcrimi_status
;
182 lp
->hw
.intmask
= arcrimi_setmask
;
183 lp
->hw
.reset
= arcrimi_reset
;
184 lp
->hw
.owner
= THIS_MODULE
;
185 lp
->hw
.copy_to_card
= arcrimi_copy_to_card
;
186 lp
->hw
.copy_from_card
= arcrimi_copy_from_card
;
188 /* re-reserve the memory region - arcrimi_probe() alloced this reqion
189 * but didn't know the real size. Free that region and then re-get
190 * with the correct size. There is a VERY slim chance this could
194 release_mem_region(shmem
, MIRROR_SIZE
);
195 if (!request_mem_region(dev
->mem_start
,
196 dev
->mem_end
- dev
->mem_start
+ 1,
198 arc_printk(D_NORMAL
, dev
, "Card memory already allocated\n");
202 lp
->mem_start
= ioremap(dev
->mem_start
,
203 dev
->mem_end
- dev
->mem_start
+ 1);
204 if (!lp
->mem_start
) {
205 arc_printk(D_NORMAL
, dev
, "Can't remap device memory!\n");
206 goto err_release_mem
;
209 /* get and check the station ID from offset 1 in shmem */
210 arcnet_set_addr(dev
, arcnet_readb(lp
->mem_start
,
211 COM9026_REG_R_STATION
));
213 arc_printk(D_NORMAL
, dev
, "ARCnet RIM I: station %02Xh found at IRQ %d, ShMem %lXh (%ld*%d bytes)\n",
215 dev
->irq
, dev
->mem_start
,
216 (dev
->mem_end
- dev
->mem_start
+ 1) / mirror_size
,
219 err
= register_netdev(dev
);
226 iounmap(lp
->mem_start
);
228 release_mem_region(dev
->mem_start
, dev
->mem_end
- dev
->mem_start
+ 1);
230 free_irq(dev
->irq
, dev
);
234 /* Do a hardware reset on the card, and set up necessary registers.
236 * This should be called as little as possible, because it disrupts the
237 * token on the network (causes a RECON) and requires a significant delay.
239 * However, it does make sure the card is in a defined state.
241 static int arcrimi_reset(struct net_device
*dev
, int really_reset
)
243 struct arcnet_local
*lp
= netdev_priv(dev
);
244 void __iomem
*ioaddr
= lp
->mem_start
+ 0x800;
246 arc_printk(D_INIT
, dev
, "Resetting %s (status=%02Xh)\n",
247 dev
->name
, arcnet_readb(ioaddr
, COM9026_REG_R_STATUS
));
250 arcnet_writeb(TESTvalue
, ioaddr
, -0x800); /* fake reset */
253 /* clear flags & end reset */
254 arcnet_writeb(CFLAGScmd
| RESETclear
, ioaddr
, COM9026_REG_W_COMMAND
);
255 arcnet_writeb(CFLAGScmd
| CONFIGclear
, ioaddr
, COM9026_REG_W_COMMAND
);
257 /* enable extended (512-byte) packets */
258 arcnet_writeb(CONFIGcmd
| EXTconf
, ioaddr
, COM9026_REG_W_COMMAND
);
260 /* done! return success. */
264 static void arcrimi_setmask(struct net_device
*dev
, int mask
)
266 struct arcnet_local
*lp
= netdev_priv(dev
);
267 void __iomem
*ioaddr
= lp
->mem_start
+ 0x800;
269 arcnet_writeb(mask
, ioaddr
, COM9026_REG_W_INTMASK
);
272 static int arcrimi_status(struct net_device
*dev
)
274 struct arcnet_local
*lp
= netdev_priv(dev
);
275 void __iomem
*ioaddr
= lp
->mem_start
+ 0x800;
277 return arcnet_readb(ioaddr
, COM9026_REG_R_STATUS
);
280 static void arcrimi_command(struct net_device
*dev
, int cmd
)
282 struct arcnet_local
*lp
= netdev_priv(dev
);
283 void __iomem
*ioaddr
= lp
->mem_start
+ 0x800;
285 arcnet_writeb(cmd
, ioaddr
, COM9026_REG_W_COMMAND
);
288 static void arcrimi_copy_to_card(struct net_device
*dev
, int bufnum
, int offset
,
289 void *buf
, int count
)
291 struct arcnet_local
*lp
= netdev_priv(dev
);
292 void __iomem
*memaddr
= lp
->mem_start
+ 0x800 + bufnum
* 512 + offset
;
294 TIME(dev
, "memcpy_toio", count
, memcpy_toio(memaddr
, buf
, count
));
297 static void arcrimi_copy_from_card(struct net_device
*dev
, int bufnum
,
298 int offset
, void *buf
, int count
)
300 struct arcnet_local
*lp
= netdev_priv(dev
);
301 void __iomem
*memaddr
= lp
->mem_start
+ 0x800 + bufnum
* 512 + offset
;
303 TIME(dev
, "memcpy_fromio", count
, memcpy_fromio(buf
, memaddr
, count
));
307 static int io
; /* use the insmod io= irq= node= options */
309 static char device
[9]; /* use eg. device=arc1 to change name */
311 module_param(node
, int, 0);
312 module_param(io
, int, 0);
313 module_param(irq
, int, 0);
314 module_param_string(device
, device
, sizeof(device
), 0);
315 MODULE_DESCRIPTION("ARCnet COM90xx RIM I chipset driver");
316 MODULE_LICENSE("GPL");
318 static struct net_device
*my_dev
;
320 static int __init
arc_rimi_init(void)
322 struct net_device
*dev
;
324 dev
= alloc_arcdev(device
);
328 if (node
&& node
!= 0xff)
329 arcnet_set_addr(dev
, node
);
336 if (arcrimi_probe(dev
)) {
345 static void __exit
arc_rimi_exit(void)
347 struct net_device
*dev
= my_dev
;
348 struct arcnet_local
*lp
= netdev_priv(dev
);
350 unregister_netdev(dev
);
351 iounmap(lp
->mem_start
);
352 release_mem_region(dev
->mem_start
, dev
->mem_end
- dev
->mem_start
+ 1);
353 free_irq(dev
->irq
, dev
);
358 static int __init
arcrimi_setup(char *s
)
362 s
= get_options(s
, 8, ints
);
367 pr_err("Too many arguments\n");
369 case 3: /* Node ID */
375 case 1: /* IO address */
379 snprintf(device
, sizeof(device
), "%s", s
);
382 __setup("arcrimi=", arcrimi_setup
);
385 module_init(arc_rimi_init
)
386 module_exit(arc_rimi_exit
)