2 * Linux ARCnet driver - COM90xx chipset (memory-mapped buffers)
4 * Written 1994-1999 by Avery Pennarun.
5 * Written 1999 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 * **********************
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/init.h>
30 #include <linux/interrupt.h>
31 #include <linux/ioport.h>
32 #include <linux/delay.h>
33 #include <linux/netdevice.h>
34 #include <linux/slab.h>
36 #include <linux/arcdevice.h>
39 #define VERSION "arcnet: COM90xx chipset support\n"
42 /* Define this to speed up the autoprobe by assuming if only one io port and
43 * shmem are left in the list at Stage 5, they must correspond to each
46 * This is undefined by default because it might not always be true, and the
47 * extra check makes the autoprobe even more careful. Speed demons can turn
48 * it on - I think it should be fine if you only have one ARCnet card
51 * If no ARCnet cards are installed, this delay never happens anyway and thus
52 * the option has no effect.
57 /* Internal function declarations */
58 static int com90xx_found(int ioaddr
, int airq
, u_long shmem
, void __iomem
*);
59 static void com90xx_command(struct net_device
*dev
, int command
);
60 static int com90xx_status(struct net_device
*dev
);
61 static void com90xx_setmask(struct net_device
*dev
, int mask
);
62 static int com90xx_reset(struct net_device
*dev
, int really_reset
);
63 static void com90xx_copy_to_card(struct net_device
*dev
, int bufnum
, int offset
,
64 void *buf
, int count
);
65 static void com90xx_copy_from_card(struct net_device
*dev
, int bufnum
, int offset
,
66 void *buf
, int count
);
68 /* Known ARCnet cards */
70 static struct net_device
*cards
[16];
73 /* Handy defines for ARCnet specific stuff */
75 /* The number of low I/O ports used by the card */
76 #define ARCNET_TOTAL_SIZE 16
78 /* Amount of I/O memory used by the card */
79 #define BUFFER_SIZE (512)
80 #define MIRROR_SIZE (BUFFER_SIZE*4)
82 /* COM 9026 controller chip --> ARCnet register addresses */
83 #define _INTMASK (ioaddr+0) /* writable */
84 #define _STATUS (ioaddr+0) /* readable */
85 #define _COMMAND (ioaddr+1) /* writable, returns random vals on read (?) */
86 #define _CONFIG (ioaddr+2) /* Configuration register */
87 #define _RESET (ioaddr+8) /* software reset (on read) */
88 #define _MEMDATA (ioaddr+12) /* Data port for IO-mapped memory */
89 #define _ADDR_HI (ioaddr+15) /* Control registers for said */
90 #define _ADDR_LO (ioaddr+14)
96 #define ASTATUS() inb(_STATUS)
97 #define ACOMMAND(cmd) outb((cmd),_COMMAND)
98 #define AINTMASK(msk) outb((msk),_INTMASK)
101 static int com90xx_skip_probe __initdata
= 0;
103 /* Module parameters */
105 static int io
; /* use the insmod io= irq= shmem= options */
108 static char device
[9]; /* use eg. device=arc1 to change name */
110 module_param(io
, int, 0);
111 module_param(irq
, int, 0);
112 module_param(shmem
, int, 0);
113 module_param_string(device
, device
, sizeof(device
), 0);
115 static void __init
com90xx_probe(void)
117 int count
, status
, ioaddr
, numprint
, airq
, openparen
= 0;
118 unsigned long airqmask
;
119 int ports
[(0x3f0 - 0x200) / 16 + 1] =
121 unsigned long *shmems
;
122 void __iomem
**iomem
;
123 int numports
, numshmems
, *port
;
127 if (!io
&& !irq
&& !shmem
&& !*device
&& com90xx_skip_probe
)
130 shmems
= kzalloc(((0x100000-0xa0000) / 0x800) * sizeof(unsigned long),
134 iomem
= kzalloc(((0x100000-0xa0000) / 0x800) * sizeof(void __iomem
*),
141 BUGLVL(D_NORMAL
) printk(VERSION
);
143 /* set up the arrays where we'll store the possible probe addresses */
144 numports
= numshmems
= 0;
146 ports
[numports
++] = io
;
148 for (count
= 0x200; count
<= 0x3f0; count
+= 16)
149 ports
[numports
++] = count
;
151 shmems
[numshmems
++] = shmem
;
153 for (count
= 0xA0000; count
<= 0xFF800; count
+= 2048)
154 shmems
[numshmems
++] = count
;
156 /* Stage 1: abandon any reserved ports, or ones with status==0xFF
157 * (empty), and reset any others by reading the reset port.
160 for (port
= &ports
[0]; port
- ports
< numports
; port
++) {
164 BUGMSG2(D_INIT
, "\n");
165 BUGMSG2(D_INIT
, "S1: ");
167 BUGMSG2(D_INIT
, "%Xh ", *port
);
171 if (!request_region(*port
, ARCNET_TOTAL_SIZE
, "arcnet (90xx)")) {
172 BUGMSG2(D_INIT_REASONS
, "(request_region)\n");
173 BUGMSG2(D_INIT_REASONS
, "S1: ");
174 BUGLVL(D_INIT_REASONS
) numprint
= 0;
175 *port
-- = ports
[--numports
];
178 if (ASTATUS() == 0xFF) {
179 BUGMSG2(D_INIT_REASONS
, "(empty)\n");
180 BUGMSG2(D_INIT_REASONS
, "S1: ");
181 BUGLVL(D_INIT_REASONS
) numprint
= 0;
182 release_region(*port
, ARCNET_TOTAL_SIZE
);
183 *port
-- = ports
[--numports
];
186 inb(_RESET
); /* begin resetting card */
188 BUGMSG2(D_INIT_REASONS
, "\n");
189 BUGMSG2(D_INIT_REASONS
, "S1: ");
190 BUGLVL(D_INIT_REASONS
) numprint
= 0;
192 BUGMSG2(D_INIT
, "\n");
195 BUGMSG2(D_NORMAL
, "S1: No ARCnet cards found.\n");
200 /* Stage 2: we have now reset any possible ARCnet cards, so we can't
201 * do anything until they finish. If D_INIT, print the list of
202 * cards that are left.
205 for (port
= &ports
[0]; port
< ports
+ numports
; port
++) {
209 BUGMSG2(D_INIT
, "\n");
210 BUGMSG2(D_INIT
, "S2: ");
212 BUGMSG2(D_INIT
, "%Xh ", *port
);
214 BUGMSG2(D_INIT
, "\n");
217 /* Stage 3: abandon any shmem addresses that don't have the signature
218 * 0xD1 byte in the right place, or are read-only.
221 for (index
= 0, p
= &shmems
[0]; index
< numshmems
; p
++, index
++) {
227 BUGMSG2(D_INIT
, "\n");
228 BUGMSG2(D_INIT
, "S3: ");
230 BUGMSG2(D_INIT
, "%lXh ", *p
);
232 if (!request_mem_region(*p
, MIRROR_SIZE
, "arcnet (90xx)")) {
233 BUGMSG2(D_INIT_REASONS
, "(request_mem_region)\n");
234 BUGMSG2(D_INIT_REASONS
, "Stage 3: ");
235 BUGLVL(D_INIT_REASONS
) numprint
= 0;
238 base
= ioremap(*p
, MIRROR_SIZE
);
240 BUGMSG2(D_INIT_REASONS
, "(ioremap)\n");
241 BUGMSG2(D_INIT_REASONS
, "Stage 3: ");
242 BUGLVL(D_INIT_REASONS
) numprint
= 0;
245 if (readb(base
) != TESTvalue
) {
246 BUGMSG2(D_INIT_REASONS
, "(%02Xh != %02Xh)\n",
247 readb(base
), TESTvalue
);
248 BUGMSG2(D_INIT_REASONS
, "S3: ");
249 BUGLVL(D_INIT_REASONS
) numprint
= 0;
252 /* By writing 0x42 to the TESTvalue location, we also make
253 * sure no "mirror" shmem areas show up - if they occur
254 * in another pass through this loop, they will be discarded
255 * because *cptr != TESTvalue.
258 if (readb(base
) != 0x42) {
259 BUGMSG2(D_INIT_REASONS
, "(read only)\n");
260 BUGMSG2(D_INIT_REASONS
, "S3: ");
263 BUGMSG2(D_INIT_REASONS
, "\n");
264 BUGMSG2(D_INIT_REASONS
, "S3: ");
265 BUGLVL(D_INIT_REASONS
) numprint
= 0;
271 release_mem_region(*p
, MIRROR_SIZE
);
273 *p
-- = shmems
[--numshmems
];
276 BUGMSG2(D_INIT
, "\n");
279 BUGMSG2(D_NORMAL
, "S3: No ARCnet cards found.\n");
280 for (port
= &ports
[0]; port
< ports
+ numports
; port
++)
281 release_region(*port
, ARCNET_TOTAL_SIZE
);
286 /* Stage 4: something of a dummy, to report the shmems that are
287 * still possible after stage 3.
290 for (p
= &shmems
[0]; p
< shmems
+ numshmems
; p
++) {
294 BUGMSG2(D_INIT
, "\n");
295 BUGMSG2(D_INIT
, "S4: ");
297 BUGMSG2(D_INIT
, "%lXh ", *p
);
299 BUGMSG2(D_INIT
, "\n");
301 /* Stage 5: for any ports that have the correct status, can disable
302 * the RESET flag, and (if no irq is given) generate an autoirq,
303 * register an ARCnet device.
305 * Currently, we can only register one device per probe, so quit
306 * after the first one is found.
309 for (port
= &ports
[0]; port
< ports
+ numports
; port
++) {
314 BUGMSG2(D_INIT
, "\n");
315 BUGMSG2(D_INIT
, "S5: ");
317 BUGMSG2(D_INIT
, "%Xh ", *port
);
323 != (NORXflag
| RECONflag
| TXFREEflag
| RESETflag
)) {
324 BUGMSG2(D_INIT_REASONS
, "(status=%Xh)\n", status
);
325 BUGMSG2(D_INIT_REASONS
, "S5: ");
326 BUGLVL(D_INIT_REASONS
) numprint
= 0;
327 release_region(*port
, ARCNET_TOTAL_SIZE
);
328 *port
-- = ports
[--numports
];
331 ACOMMAND(CFLAGScmd
| RESETclear
| CONFIGclear
);
333 if (status
& RESETflag
) {
334 BUGMSG2(D_INIT_REASONS
, " (eternal reset, status=%Xh)\n",
336 BUGMSG2(D_INIT_REASONS
, "S5: ");
337 BUGLVL(D_INIT_REASONS
) numprint
= 0;
338 release_region(*port
, ARCNET_TOTAL_SIZE
);
339 *port
-- = ports
[--numports
];
342 /* skip this completely if an IRQ was given, because maybe
343 * we're on a machine that locks during autoirq!
346 /* if we do this, we're sure to get an IRQ since the
347 * card has just reset and the NORXflag is on until
348 * we tell it to start receiving.
350 airqmask
= probe_irq_on();
354 airq
= probe_irq_off(airqmask
);
357 BUGMSG2(D_INIT_REASONS
, "(airq=%d)\n", airq
);
358 BUGMSG2(D_INIT_REASONS
, "S5: ");
359 BUGLVL(D_INIT_REASONS
) numprint
= 0;
360 release_region(*port
, ARCNET_TOTAL_SIZE
);
361 *port
-- = ports
[--numports
];
368 BUGMSG2(D_INIT
, "(%d,", airq
);
371 /* Everything seems okay. But which shmem, if any, puts
372 * back its signature byte when the card is reset?
374 * If there are multiple cards installed, there might be
375 * multiple shmems still in the list.
378 if (numports
> 1 || numshmems
> 1) {
382 /* just one shmem and port, assume they match */
383 writeb(TESTvalue
, iomem
[0]);
390 for (index
= 0; index
< numshmems
; index
++) {
391 u_long ptr
= shmems
[index
];
392 void __iomem
*base
= iomem
[index
];
394 if (readb(base
) == TESTvalue
) { /* found one */
395 BUGMSG2(D_INIT
, "%lXh)\n", *p
);
398 /* register the card */
399 if (com90xx_found(*port
, airq
, ptr
, base
) == 0)
403 /* remove shmem from the list */
404 shmems
[index
] = shmems
[--numshmems
];
405 iomem
[index
] = iomem
[numshmems
];
406 break; /* go to the next I/O port */
408 BUGMSG2(D_INIT_REASONS
, "%Xh-", readb(base
));
413 BUGLVL(D_INIT
) printk("no matching shmem)\n");
414 BUGLVL(D_INIT_REASONS
) printk("S5: ");
415 BUGLVL(D_INIT_REASONS
) numprint
= 0;
418 release_region(*port
, ARCNET_TOTAL_SIZE
);
419 *port
-- = ports
[--numports
];
422 BUGLVL(D_INIT_REASONS
) printk("\n");
424 /* Now put back TESTvalue on all leftover shmems. */
425 for (index
= 0; index
< numshmems
; index
++) {
426 writeb(TESTvalue
, iomem
[index
]);
427 iounmap(iomem
[index
]);
428 release_mem_region(shmems
[index
], MIRROR_SIZE
);
434 static int check_mirror(unsigned long addr
, size_t size
)
439 if (!request_mem_region(addr
, size
, "arcnet (90xx)"))
442 p
= ioremap(addr
, size
);
444 if (readb(p
) == TESTvalue
)
451 release_mem_region(addr
, size
);
455 /* Set up the struct net_device associated with this card. Called after
458 static int __init
com90xx_found(int ioaddr
, int airq
, u_long shmem
, void __iomem
*p
)
460 struct net_device
*dev
= NULL
;
461 struct arcnet_local
*lp
;
462 u_long first_mirror
, last_mirror
;
465 /* allocate struct net_device */
466 dev
= alloc_arcdev(device
);
468 BUGMSG2(D_NORMAL
, "com90xx: Can't allocate device!\n");
470 release_mem_region(shmem
, MIRROR_SIZE
);
473 lp
= netdev_priv(dev
);
474 /* find the real shared memory start/end points, including mirrors */
476 /* guess the actual size of one "memory mirror" - the number of
477 * bytes between copies of the shared memory. On most cards, it's
478 * 2k (or there are no mirrors at all) but on some, it's 4k.
480 mirror_size
= MIRROR_SIZE
;
481 if (readb(p
) == TESTvalue
&&
482 check_mirror(shmem
- MIRROR_SIZE
, MIRROR_SIZE
) == 0 &&
483 check_mirror(shmem
- 2 * MIRROR_SIZE
, MIRROR_SIZE
) == 1)
484 mirror_size
= 2 * MIRROR_SIZE
;
486 first_mirror
= shmem
- mirror_size
;
487 while (check_mirror(first_mirror
, mirror_size
) == 1)
488 first_mirror
-= mirror_size
;
489 first_mirror
+= mirror_size
;
491 last_mirror
= shmem
+ mirror_size
;
492 while (check_mirror(last_mirror
, mirror_size
) == 1)
493 last_mirror
+= mirror_size
;
494 last_mirror
-= mirror_size
;
496 dev
->mem_start
= first_mirror
;
497 dev
->mem_end
= last_mirror
+ MIRROR_SIZE
- 1;
500 release_mem_region(shmem
, MIRROR_SIZE
);
502 if (!request_mem_region(dev
->mem_start
, dev
->mem_end
- dev
->mem_start
+ 1, "arcnet (90xx)"))
505 /* reserve the irq */
506 if (request_irq(airq
, arcnet_interrupt
, 0, "arcnet (90xx)", dev
)) {
507 BUGMSG(D_NORMAL
, "Can't get IRQ %d!\n", airq
);
508 goto err_release_mem
;
512 /* Initialize the rest of the device structure. */
513 lp
->card_name
= "COM90xx";
514 lp
->hw
.command
= com90xx_command
;
515 lp
->hw
.status
= com90xx_status
;
516 lp
->hw
.intmask
= com90xx_setmask
;
517 lp
->hw
.reset
= com90xx_reset
;
518 lp
->hw
.owner
= THIS_MODULE
;
519 lp
->hw
.copy_to_card
= com90xx_copy_to_card
;
520 lp
->hw
.copy_from_card
= com90xx_copy_from_card
;
521 lp
->mem_start
= ioremap(dev
->mem_start
, dev
->mem_end
- dev
->mem_start
+ 1);
522 if (!lp
->mem_start
) {
523 BUGMSG(D_NORMAL
, "Can't remap device memory!\n");
527 /* get and check the station ID from offset 1 in shmem */
528 dev
->dev_addr
[0] = readb(lp
->mem_start
+ 1);
530 dev
->base_addr
= ioaddr
;
532 BUGMSG(D_NORMAL
, "COM90xx station %02Xh found at %03lXh, IRQ %d, "
533 "ShMem %lXh (%ld*%xh).\n",
535 dev
->base_addr
, dev
->irq
, dev
->mem_start
,
536 (dev
->mem_end
- dev
->mem_start
+ 1) / mirror_size
, mirror_size
);
538 if (register_netdev(dev
))
541 cards
[numcards
++] = dev
;
545 iounmap(lp
->mem_start
);
547 free_irq(dev
->irq
, dev
);
549 release_mem_region(dev
->mem_start
, dev
->mem_end
- dev
->mem_start
+ 1);
556 static void com90xx_command(struct net_device
*dev
, int cmd
)
558 short ioaddr
= dev
->base_addr
;
564 static int com90xx_status(struct net_device
*dev
)
566 short ioaddr
= dev
->base_addr
;
572 static void com90xx_setmask(struct net_device
*dev
, int mask
)
574 short ioaddr
= dev
->base_addr
;
581 * Do a hardware reset on the card, and set up necessary registers.
583 * This should be called as little as possible, because it disrupts the
584 * token on the network (causes a RECON) and requires a significant delay.
586 * However, it does make sure the card is in a defined state.
588 static int com90xx_reset(struct net_device
*dev
, int really_reset
)
590 struct arcnet_local
*lp
= netdev_priv(dev
);
591 short ioaddr
= dev
->base_addr
;
593 BUGMSG(D_INIT
, "Resetting (status=%02Xh)\n", ASTATUS());
600 ACOMMAND(CFLAGScmd
| RESETclear
); /* clear flags & end reset */
601 ACOMMAND(CFLAGScmd
| CONFIGclear
);
603 /* don't do this until we verify that it doesn't hurt older cards! */
604 /* outb(inb(_CONFIG) | ENABLE16flag, _CONFIG); */
606 /* verify that the ARCnet signature byte is present */
607 if (readb(lp
->mem_start
) != TESTvalue
) {
609 BUGMSG(D_NORMAL
, "reset failed: TESTvalue not present.\n");
612 /* enable extended (512-byte) packets */
613 ACOMMAND(CONFIGcmd
| EXTconf
);
615 /* clean out all the memory to make debugging make more sense :) */
617 memset_io(lp
->mem_start
, 0x42, 2048);
619 /* done! return success. */
623 static void com90xx_copy_to_card(struct net_device
*dev
, int bufnum
, int offset
,
624 void *buf
, int count
)
626 struct arcnet_local
*lp
= netdev_priv(dev
);
627 void __iomem
*memaddr
= lp
->mem_start
+ bufnum
* 512 + offset
;
628 TIME("memcpy_toio", count
, memcpy_toio(memaddr
, buf
, count
));
632 static void com90xx_copy_from_card(struct net_device
*dev
, int bufnum
, int offset
,
633 void *buf
, int count
)
635 struct arcnet_local
*lp
= netdev_priv(dev
);
636 void __iomem
*memaddr
= lp
->mem_start
+ bufnum
* 512 + offset
;
637 TIME("memcpy_fromio", count
, memcpy_fromio(buf
, memaddr
, count
));
641 MODULE_LICENSE("GPL");
643 static int __init
com90xx_init(void)
653 static void __exit
com90xx_exit(void)
655 struct net_device
*dev
;
656 struct arcnet_local
*lp
;
659 for (count
= 0; count
< numcards
; count
++) {
661 lp
= netdev_priv(dev
);
663 unregister_netdev(dev
);
664 free_irq(dev
->irq
, dev
);
665 iounmap(lp
->mem_start
);
666 release_region(dev
->base_addr
, ARCNET_TOTAL_SIZE
);
667 release_mem_region(dev
->mem_start
, dev
->mem_end
- dev
->mem_start
+ 1);
672 module_init(com90xx_init
);
673 module_exit(com90xx_exit
);
676 static int __init
com90xx_setup(char *s
)
680 s
= get_options(s
, 8, ints
);
681 if (!ints
[0] && !*s
) {
682 printk("com90xx: Disabled.\n");
688 printk("com90xx: Too many arguments.\n");
689 case 3: /* Mem address */
693 case 1: /* IO address */
698 snprintf(device
, sizeof(device
), "%s", s
);
703 __setup("com90xx=", com90xx_setup
);